32#include <pybind11/cast.h>
33#include <pybind11/functional.h>
34#include <pybind11/numpy.h>
39 py::module math_module = m.def_submodule(
"math",
"Shamrock math lib");
41 shampylib::init_shamrock_math_AABB<f64_3>(math_module,
"AABB_f64_3");
42 shampylib::init_shamrock_math_Ray<f64_3>(math_module,
"Ray_f64_3");
43 shampylib::init_shamrock_math_RingRay<f64_3>(math_module,
"RingRay_f64_3");
44 shampylib::init_shamrock_math_sfc(math_module);
45 shampylib::init_shamrock_math_sphkernels(math_module);
47 math_module.def(
"derivative_upwind", [](
f64 x,
f64 dx, std::function<
f64(
f64)> fct) {
48 return shammath::derivative_upwind<f64>(x, dx, [&](
f64 x) {
52 math_module.def(
"derivative_centered", [](
f64 x,
f64 dx, std::function<
f64(
f64)> fct) {
53 return shammath::derivative_centered<f64>(x, dx, [&](
f64 x) {
57 math_module.def(
"derivative_3point_forward", [](
f64 x,
f64 dx, std::function<
f64(
f64)> fct) {
58 return shammath::derivative_3point_forward<f64>(x, dx, [&](
f64 x) {
62 math_module.def(
"derivative_3point_backward", [](
f64 x,
f64 dx, std::function<
f64(
f64)> fct) {
63 return shammath::derivative_3point_backward<f64>(x, dx, [&](
f64 x) {
67 math_module.def(
"derivative_5point_midpoint", [](
f64 x,
f64 dx, std::function<
f64(
f64)> fct) {
68 return shammath::derivative_5point_midpoint<f64>(x, dx, [&](
f64 x) {
75 return shammath::estim_deriv_step<f64>(order);
78 Estim the correct step to use for a given order when using derivatives
81 py::class_<shammath::paving_function_periodic_3d<f64_3>>(
82 math_module, "paving_function_periodic_3d")
83 .def(py::init([](f64_3 box_size) {
84 return std::make_unique<shammath::paving_function_periodic_3d<f64_3>>(
92 "get_paving_index_intersecting",
95 py::class_<shammath::paving_function_general_3d<f64_3>>(
96 math_module,
"paving_function_general_3d")
98 py::init([](f64_3 box_size,
102 bool is_z_periodic) {
103 return std::make_unique<shammath::paving_function_general_3d<f64_3>>(
105 box_size, box_center, is_x_periodic, is_y_periodic, is_z_periodic});
112 "get_paving_index_intersecting",
115 py::class_<shammath::paving_function_general_3d_shear_x<f64_3>>(
116 math_module,
"paving_function_general_3d_shear_x")
118 py::init([](f64_3 box_size,
124 return std::make_unique<shammath::paving_function_general_3d_shear_x<f64_3>>(
138 "get_paving_index_intersecting",
141 py::class_<f64_4x4>(math_module,
"f64_4x4")
143 return std::make_unique<f64_4x4>();
147 [](
const f64_4x4 &m, std::pair<int, int> idx) ->
double {
148 return m(idx.first, idx.second);
152 [](
f64_4x4 &m, std::pair<int, int> idx,
double value) {
153 m(idx.first, idx.second) = value;
158 std::ostringstream oss;
160 for (
size_t i = 0; i < 4; ++i) {
162 for (
size_t j = 0; j < 4; ++j) {
182 .def(
"to_pyarray", [](
const f64_4x4 &self) {
183 py::array_t<f64> ret({4, 4});
184 for (
u32 i = 0; i < 4; i++) {
185 for (
u32 j = 0; j < 4; j++) {
186 ret.mutable_at(i, j) = self(i, j);
193 math_module.def(
"get_identity_f64_4x4", []() ->
f64_4x4 {
194 return shammath::mat_identity<f64, 4>();
203 math_module.def(
"mat_set_identity", [](
f64_4x4 &a) {
208 py::class_<shammath::SymTensor3d_1<f64>>(math_module,
"SymTensor3d_1_f64")
209 .def(py::init<f64, f64, f64>(), py::arg(
"v_0"), py::arg(
"v_1"), py::arg(
"v_2"))
218 "Inner product with another SymTensor3d_1")
222 "Scalar multiplication")
229 return fmt::format(
"SymTensor3d_1(v_0={}, v_1={}, v_2={})", t.v_0, t.v_1, t.v_2);
233 py::class_<shammath::SymTensor3d_2<f64>>(math_module,
"SymTensor3d_2_f64")
235 py::init<f64, f64, f64, f64, f64, f64>(),
253 "Inner product with another SymTensor3d_2")
258 "Inner product with SymTensor3d_1")
262 "Scalar multiplication")
270 "SymTensor3d_2(v_00={}, v_01={}, v_02={}, v_11={}, v_12={}, v_22={})",
280 py::class_<shammath::SymTensor3d_3<f64>>(math_module,
"SymTensor3d_3_f64")
282 py::init<f64, f64, f64, f64, f64, f64, f64, f64, f64, f64>(),
308 "Inner product with another SymTensor3d_3")
313 "Inner product with SymTensor3d_2")
318 "Inner product with SymTensor3d_1")
322 "Scalar multiplication")
330 "SymTensor3d_3(v_000={}, v_001={}, v_002={}, v_011={}, v_012={}, v_022={}, "
331 "v_111={}, v_112={}, v_122={}, v_222={})",
345 py::class_<shammath::SymTensor3d_4<f64>>(math_module,
"SymTensor3d_4_f64")
347 py::init<f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64>(),
383 "Inner product with another SymTensor3d_4")
388 "Inner product with SymTensor3d_3")
393 "Inner product with SymTensor3d_2")
398 "Inner product with SymTensor3d_1")
402 "Scalar multiplication")
410 "SymTensor3d_4(v_0000={}, v_0001={}, v_0002={}, v_0011={}, v_0012={}, v_0022={}, "
411 "v_0111={}, v_0112={}, v_0122={}, v_0222={}, v_1111={}, v_1112={}, v_1122={}, "
412 "v_1222={}, v_2222={})",
431 py::class_<shammath::SymTensor3d_5<f64>>(math_module,
"SymTensor3d_5_f64")
502 "Inner product with another SymTensor3d_5")
507 "Inner product with SymTensor3d_4")
512 "Inner product with SymTensor3d_3")
517 "Inner product with SymTensor3d_2")
522 "Inner product with SymTensor3d_1")
526 "Scalar multiplication")
534 "SymTensor3d_5(v_00000={}, v_00001={}, v_00002={}, v_00011={}, v_00012={}, "
535 "v_00022={}, v_00111={}, v_00112={}, v_00122={}, v_00222={}, v_01111={}, "
536 "v_01112={}, v_01122={}, v_01222={}, v_02222={}, v_11111={}, v_11112={}, "
537 "v_11122={}, v_11222={}, v_12222={}, v_22222={})",
563 py::class_<shammath::SymTensorCollection<f64, 0, 5>>(math_module,
"SymTensorCollection_f64_0_5")
592 "SymTensorCollection_f64_0_5(\n t0={},\n t1={},\n t2={},\n t3={},\n t4={},\n "
595 py::str(py::cast(c.t1)).cast<std::string>(),
596 py::str(py::cast(c.t2)).cast<std::string>(),
597 py::str(py::cast(c.t3)).cast<std::string>(),
598 py::str(py::cast(c.t4)).cast<std::string>(),
599 py::str(py::cast(c.t5)).cast<std::string>());
603 py::class_<shammath::SymTensorCollection<f64, 0, 4>>(math_module,
"SymTensorCollection_f64_0_4")
629 "SymTensorCollection_f64_0_4(\n t0={},\n t1={},\n t2={},\n t3={},\n t4={}\n)",
631 py::str(py::cast(c.t1)).cast<std::string>(),
632 py::str(py::cast(c.t2)).cast<std::string>(),
633 py::str(py::cast(c.t3)).cast<std::string>(),
634 py::str(py::cast(c.t4)).cast<std::string>());
638 py::class_<shammath::SymTensorCollection<f64, 0, 3>>(math_module,
"SymTensorCollection_f64_0_3")
661 "SymTensorCollection_f64_0_3(\n t0={},\n t1={},\n t2={},\n t3={}\n)",
663 py::str(py::cast(c.t1)).cast<std::string>(),
664 py::str(py::cast(c.t2)).cast<std::string>(),
665 py::str(py::cast(c.t3)).cast<std::string>());
669 py::class_<shammath::SymTensorCollection<f64, 0, 2>>(math_module,
"SymTensorCollection_f64_0_2")
686 "SymTensorCollection_f64_0_2(\n t0={},\n t1={},\n t2={}\n)",
688 py::str(py::cast(c.t1)).cast<std::string>(),
689 py::str(py::cast(c.t2)).cast<std::string>());
693 py::class_<shammath::SymTensorCollection<f64, 0, 1>>(math_module,
"SymTensorCollection_f64_0_1")
705 "SymTensorCollection_f64_0_1(\n t0={},\n t1={}\n)",
707 py::str(py::cast(c.t1)).cast<std::string>());
711 py::class_<shammath::SymTensorCollection<f64, 0, 0>>(math_module,
"SymTensorCollection_f64_0_0")
713 .def(py::init<f64>(), py::arg(
"t0"))
721 return fmt::format(
"SymTensorCollection_f64_0_0(t0={})", c.t0);
725 py::class_<shammath::SymTensorCollection<f64, 1, 5>>(math_module,
"SymTensorCollection_f64_1_5")
751 "SymTensorCollection_f64_1_5(\n t1={},\n t2={},\n t3={},\n t4={},\n t5={}\n)",
752 py::str(py::cast(c.t1)).cast<std::string>(),
753 py::str(py::cast(c.t2)).cast<std::string>(),
754 py::str(py::cast(c.t3)).cast<std::string>(),
755 py::str(py::cast(c.t4)).cast<std::string>(),
756 py::str(py::cast(c.t5)).cast<std::string>());
760 py::class_<shammath::SymTensorCollection<f64, 1, 4>>(math_module,
"SymTensorCollection_f64_1_4")
783 "SymTensorCollection_f64_1_4(\n t1={},\n t2={},\n t3={},\n t4={}\n)",
784 py::str(py::cast(c.t1)).cast<std::string>(),
785 py::str(py::cast(c.t2)).cast<std::string>(),
786 py::str(py::cast(c.t3)).cast<std::string>(),
787 py::str(py::cast(c.t4)).cast<std::string>());
791 py::class_<shammath::SymTensorCollection<f64, 1, 3>>(math_module,
"SymTensorCollection_f64_1_3")
811 "SymTensorCollection_f64_1_3(\n t1={},\n t2={},\n t3={}\n)",
812 py::str(py::cast(c.t1)).cast<std::string>(),
813 py::str(py::cast(c.t2)).cast<std::string>(),
814 py::str(py::cast(c.t3)).cast<std::string>());
818 py::class_<shammath::SymTensorCollection<f64, 1, 2>>(math_module,
"SymTensorCollection_f64_1_2")
833 "SymTensorCollection_f64_1_2(\n t1={},\n t2={}\n)",
834 py::str(py::cast(c.t1)).cast<std::string>(),
835 py::str(py::cast(c.t2)).cast<std::string>());
839 py::class_<shammath::SymTensorCollection<f64, 1, 1>>(math_module,
"SymTensorCollection_f64_1_1")
850 "SymTensorCollection_f64_1_1(\n t1={}\n)",
851 py::str(py::cast(c.t1)).cast<std::string>());
double f64
Alias for double.
std::uint32_t u32
32 bit unsigned integer
Matrix class based on std::array storage and mdspan.
constexpr auto get_mdspan()
Get the matrix data as a mdspan.
void mat_set_identity(const std::mdspan< T, Extents, Layout, Accessor > &input1)
Set the content of a matrix to the identity matrix.
void mat_prod(const std::mdspan< Ta, Extents1, Layout1, Accessor1 > &input1, const std::mdspan< Ta, Extents2, Layout2, Accessor2 > &input2, const std::mdspan< Tb, Extents3, Layout3, Accessor3 > &output)
Compute the product of two matrices.
Pybind11 include and definitions.
#define Register_pymod(placeholdername)
Register a python module init function using static initialisation.
A structure for 3D paving functions with shearing along the x-axis and general boundary conditions.
A structure for 3D paving functions with general boundary conditions (periodic or reflective per dire...
A structure for 3D paving functions with periodic boundary conditions.