From 65f4ad72e955923fa1e45c996378247bb36351a3 Mon Sep 17 00:00:00 2001 From: Thomas Oberbichler Date: Fri, 26 Mar 2021 10:26:42 +0100 Subject: [PATCH 1/9] Add const ref --- include/hyperjet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/hyperjet.h b/include/hyperjet.h index fccfd72..14b2fc2 100644 --- a/include/hyperjet.h +++ b/include/hyperjet.h @@ -397,7 +397,7 @@ class DDScalar { } } - static std::vector variables(std::vector values) + static std::vector variables(const std::vector& values) { const index s = length(values); From 2ea2240c0108569132fed4a2fe79a85d03cecdec Mon Sep 17 00:00:00 2001 From: Thomas Oberbichler Date: Fri, 26 Mar 2021 10:27:00 +0100 Subject: [PATCH 2/9] Add variables by array --- include/hyperjet.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/hyperjet.h b/include/hyperjet.h index 14b2fc2..72d41fa 100644 --- a/include/hyperjet.h +++ b/include/hyperjet.h @@ -412,6 +412,30 @@ class DDScalar { return vars; } + template + static std::conditional_t, std::array> variables(const std::array& values) + { + if constexpr (!is_dynamic()) { + static_assert(T == TSize); + } + + const index s = length(values); + + if constexpr (is_dynamic()) { + std::vector vars(s); + for (index i = 0; i < s; i++) { + vars[i] = variable(i, values[i], s); + } + return vars; + } else { + std::array vars; + for (index i = 0; i < s; i++) { + vars[i] = variable(i, values[i], s); + } + return vars; + } + } + Scalar& f() { return m_data[0]; From c5b2b1f140165243479f733b229975249ebe94e8 Mon Sep 17 00:00:00 2001 From: Thomas Oberbichler Date: Fri, 26 Mar 2021 10:27:26 +0100 Subject: [PATCH 3/9] Expose variables by array --- src/python_module.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/python_module.cpp b/src/python_module.cpp index 3436843..658e881 100644 --- a/src/python_module.cpp +++ b/src/python_module.cpp @@ -39,7 +39,6 @@ void register_ddscalar(pybind11::module& m, const std::string& name) .def_static("empty", py::overload_cast<>(&Type::empty)) .def_static("empty", py::overload_cast(&Type::empty), "size"_a) .def_static("variable", py::overload_cast(&Type::variable), "i"_a, "f"_a, "size"_a) - .def_static("variables", &Type::variables, "values"_a) .def_static("zero", py::overload_cast<>(&Type::zero)) .def_static("zero", py::overload_cast(&Type::zero), "size"_a) // methods @@ -159,13 +158,16 @@ void register_ddscalar(pybind11::module& m, const std::string& name) // methods .def("resize", &Type::resize, "size"_a) .def("pad_right", &Type::pad_right, "new_size"_a) - .def("pad_left", &Type::pad_left, "new_size"_a); + .def("pad_left", &Type::pad_left, "new_size"_a) + // static methods + .def_static("variables", [](const std::vector& values) { return Type::variables(values); }, "values"_a); } else { py_class // constructor .def(py::init(py::overload_cast(&Type::constant)), "f"_a=0) // static methods - .def_static("variable", py::overload_cast(&Type::variable), "i"_a, "f"_a); + .def_static("variable", py::overload_cast(&Type::variable), "i"_a, "f"_a) + .def_static("variables", py::overload_cast&>(&Type::variables), "values"_a); } } From 44c315ebae6e0540f1a691ca53231f345fb6ee25 Mon Sep 17 00:00:00 2001 From: Thomas Oberbichler Date: Fri, 26 Mar 2021 10:27:58 +0100 Subject: [PATCH 4/9] Add `hj::variables` --- src/python_module.cpp | 128 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/src/python_module.cpp b/src/python_module.cpp index 658e881..f2b398a 100644 --- a/src/python_module.cpp +++ b/src/python_module.cpp @@ -173,7 +173,10 @@ void register_ddscalar(pybind11::module& m, const std::string& name) PYBIND11_MODULE(hyperjet, m) { + using namespace pybind11::literals; + namespace py = pybind11; + namespace hj = hyperjet; m.doc() = "HyperJet by Thomas Oberbichler"; m.attr("__author__") = "Thomas Oberbichler"; @@ -227,5 +230,130 @@ PYBIND11_MODULE(hyperjet, m) m.attr("f") = py::eval("np.vectorize(lambda v: v.f if hasattr(v, 'f') else v)", global); m.attr("d") = py::eval("np.vectorize(lambda v: v.g if hasattr(v, 'g') else np.zeros((0)), signature='()->(n)')", global); m.attr("dd") = py::eval("np.vectorize(lambda v: v.hm() if hasattr(v, 'hm') else np.zeros((0, 0)), signature='()->(n,m)')", global); + + m.def("variables", [](const std::vector& values, const hj::index order) { + if (order < 0 || 2 < order) { + throw std::runtime_error("Invalid order"); + } + + py::list results; + + const auto extend = results.attr("extend"); + + switch (order) { + case 0: + extend(values); + break; + case 1: + switch (hj::length(values)) { + case 0: + break; + case 1: + extend(hj::DDScalar<1, double, 1>::variables(values)); + break; + case 2: + extend(hj::DDScalar<1, double, 2>::variables(values)); + break; + case 3: + extend(hj::DDScalar<1, double, 3>::variables(values)); + break; + case 4: + extend(hj::DDScalar<1, double, 4>::variables(values)); + break; + case 5: + extend(hj::DDScalar<1, double, 5>::variables(values)); + break; + case 6: + extend(hj::DDScalar<1, double, 6>::variables(values)); + break; + case 7: + extend(hj::DDScalar<1, double, 7>::variables(values)); + break; + case 8: + extend(hj::DDScalar<1, double, 8>::variables(values)); + break; + case 9: + extend(hj::DDScalar<1, double, 9>::variables(values)); + break; + case 10: + extend(hj::DDScalar<1, double, 10>::variables(values)); + break; + case 11: + extend(hj::DDScalar<1, double, 11>::variables(values)); + break; + case 12: + extend(hj::DDScalar<1, double, 12>::variables(values)); + break; + case 13: + extend(hj::DDScalar<1, double, 13>::variables(values)); + break; + case 14: + extend(hj::DDScalar<1, double, 14>::variables(values)); + break; + case 15: + extend(hj::DDScalar<1, double, 15>::variables(values)); + break; + default: + extend(hj::DDScalar<1, double, -1>::variables(values)); + break; + } + break; + case 2: + switch (hj::length(values)) { + case 0: + break; + case 1: + extend(hj::DDScalar<2, double, 1>::variables(values)); + break; + case 2: + extend(hj::DDScalar<2, double, 2>::variables(values)); + break; + case 3: + extend(hj::DDScalar<2, double, 3>::variables(values)); + break; + case 4: + extend(hj::DDScalar<2, double, 4>::variables(values)); + break; + case 5: + extend(hj::DDScalar<2, double, 5>::variables(values)); + break; + case 6: + extend(hj::DDScalar<2, double, 6>::variables(values)); + break; + case 7: + extend(hj::DDScalar<2, double, 7>::variables(values)); + break; + case 8: + extend(hj::DDScalar<2, double, 8>::variables(values)); + break; + case 9: + extend(hj::DDScalar<2, double, 9>::variables(values)); + break; + case 10: + extend(hj::DDScalar<2, double, 10>::variables(values)); + break; + case 11: + extend(hj::DDScalar<2, double, 11>::variables(values)); + break; + case 12: + extend(hj::DDScalar<2, double, 12>::variables(values)); + break; + case 13: + extend(hj::DDScalar<2, double, 13>::variables(values)); + break; + case 14: + extend(hj::DDScalar<2, double, 14>::variables(values)); + break; + case 15: + extend(hj::DDScalar<2, double, 15>::variables(values)); + break; + default: + extend(hj::DDScalar<2, double, -1>::variables(values)); + break; + } + break; + } + return results; + }, "values"_a, "order"_a=2); } } \ No newline at end of file From 430bae16a8c86a905c45bf466cb7e296315d3370 Mon Sep 17 00:00:00 2001 From: Thomas Oberbichler Date: Fri, 26 Mar 2021 10:28:08 +0100 Subject: [PATCH 5/9] Test variables --- tests/test_DDScalar.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_DDScalar.py b/tests/test_DDScalar.py index 3683624..1e04b2e 100644 --- a/tests/test_DDScalar.py +++ b/tests/test_DDScalar.py @@ -794,3 +794,33 @@ def test_dd_of_scalar(): assert_equal(hj.dd(np.array([1, 2, 3, 4])), np.empty((4, 0, 0))) assert_equal(hj.dd(np.array([[1, 2], [3, 4]])), np.empty((2, 2, 0, 0))) + + +def test_generate_variables(): + small = [1, 2, 3] + large = [i + 1 for i in range(20)] + + variables = hj.variables(small, order=0) + + assert_equal(variables, small) + assert_equal(type(variables[0]), float) + + variables = hj.variables(small, order=1) + + assert_equal(hj.f(variables), small) + assert_equal(type(variables[0]), hj.D3Scalar) + + variables = hj.variables(large, order=1) + + assert_equal(hj.f(variables), large) + assert_equal(type(variables[0]), hj.DScalar) + + variables = hj.variables(small, order=2) + + assert_equal(hj.f(variables), small) + assert_equal(type(variables[0]), hj.DD3Scalar) + + variables = hj.variables(large, order=2) + + assert_equal(hj.f(variables), large) + assert_equal(type(variables[0]), hj.DDScalar) From f09481e4b64d0bc22530bff256d8397b686e1c8e Mon Sep 17 00:00:00 2001 From: Thomas Oberbichler Date: Fri, 26 Mar 2021 10:33:03 +0100 Subject: [PATCH 6/9] Drop overload_cast for gcc --- src/python_module.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/python_module.cpp b/src/python_module.cpp index f2b398a..b7b72ac 100644 --- a/src/python_module.cpp +++ b/src/python_module.cpp @@ -167,7 +167,7 @@ void register_ddscalar(pybind11::module& m, const std::string& name) .def(py::init(py::overload_cast(&Type::constant)), "f"_a=0) // static methods .def_static("variable", py::overload_cast(&Type::variable), "i"_a, "f"_a) - .def_static("variables", py::overload_cast&>(&Type::variables), "values"_a); + .def_static("variables", [](const std::array& values) { return Type::variables(values); }, "values"_a); } } From ac9517534ef5315db53df1dbbaf3a5cce96d6552 Mon Sep 17 00:00:00 2001 From: Thomas Oberbichler Date: Fri, 26 Mar 2021 10:39:44 +0100 Subject: [PATCH 7/9] Add template argument --- src/python_module.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/python_module.cpp b/src/python_module.cpp index b7b72ac..303e0a4 100644 --- a/src/python_module.cpp +++ b/src/python_module.cpp @@ -167,7 +167,7 @@ void register_ddscalar(pybind11::module& m, const std::string& name) .def(py::init(py::overload_cast(&Type::constant)), "f"_a=0) // static methods .def_static("variable", py::overload_cast(&Type::variable), "i"_a, "f"_a) - .def_static("variables", [](const std::array& values) { return Type::variables(values); }, "values"_a); + .def_static("variables", [](const std::array& values) { return Type::variables(values); }, "values"_a); } } From 44686f0d8287b3fc0888542c0ce8a27829f446fd Mon Sep 17 00:00:00 2001 From: Thomas Oberbichler Date: Fri, 26 Mar 2021 10:41:43 +0100 Subject: [PATCH 8/9] Add spaces --- src/python_module.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/python_module.cpp b/src/python_module.cpp index 303e0a4..3aba1df 100644 --- a/src/python_module.cpp +++ b/src/python_module.cpp @@ -167,7 +167,7 @@ void register_ddscalar(pybind11::module& m, const std::string& name) .def(py::init(py::overload_cast(&Type::constant)), "f"_a=0) // static methods .def_static("variable", py::overload_cast(&Type::variable), "i"_a, "f"_a) - .def_static("variables", [](const std::array& values) { return Type::variables(values); }, "values"_a); + .def_static("variables", [](const std::array& values) { return Type::variables< TSize >(values); }, "values"_a); } } From 4fa3f89f8d7094b2f25e3d07cfecaed5e15bd88a Mon Sep 17 00:00:00 2001 From: Thomas Oberbichler Date: Fri, 26 Mar 2021 10:42:54 +0100 Subject: [PATCH 9/9] Add template --- src/python_module.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/python_module.cpp b/src/python_module.cpp index 3aba1df..15099fd 100644 --- a/src/python_module.cpp +++ b/src/python_module.cpp @@ -167,7 +167,7 @@ void register_ddscalar(pybind11::module& m, const std::string& name) .def(py::init(py::overload_cast(&Type::constant)), "f"_a=0) // static methods .def_static("variable", py::overload_cast(&Type::variable), "i"_a, "f"_a) - .def_static("variables", [](const std::array& values) { return Type::variables< TSize >(values); }, "values"_a); + .def_static("variables", [](const std::array& values) { return Type::template variables(values); }, "values"_a); } }