diff --git a/numpy/include/numpy.hpp b/numpy/include/numpy.hpp index e64ca5b..b383cf3 100644 --- a/numpy/include/numpy.hpp +++ b/numpy/include/numpy.hpp @@ -295,6 +295,14 @@ class ndarray { void set_item(int index, const ndarray& value) { xt::view(_array, index, xt::all()) = value.get_array(); } + void set_item(int i1, int i2, const ndarray& value) { xt::view(_array, i1, i2, xt::all()) = value.get_array(); } + + void set_item(int i1, int i2, int i3, const ndarray& value) { xt::view(_array, i1, i2, i3, xt::all()) = value.get_array(); } + + void set_item(int i1, int i2, int i3, int i4, const ndarray& value) { xt::view(_array, i1, i2, i3, i4, xt::all()) = value.get_array(); } + + void set_item(int i1, int i2, int i3, int i4, int i5, const ndarray& value) { xt::view(_array, i1, i2, i3, i4, i5, xt::all()) = value.get_array(); } + void set_item(const std::vector& indices, const ndarray& value) { xt::view(_array, xt::keep(indices)) = value.get_array(); } @@ -303,13 +311,13 @@ class ndarray { xt::view(_array, xt::range(std::get<0>(slice), std::get<1>(slice), std::get<2>(slice))) = value.get_array(); } - void set_item_2d(int i1, int i2, T value) { xt::view(_array, i1, i2) = value; } + void set_item(int i1, int i2, T value) { xt::view(_array, i1, i2) = value; } - void set_item_3d(int i1, int i2, int i3, T value) { xt::view(_array, i1, i2, i3) = value; } + void set_item(int i1, int i2, int i3, T value) { xt::view(_array, i1, i2, i3) = value; } - void set_item_4d(int i1, int i2, int i3, int i4, T value) { xt::view(_array, i1, i2, i3, i4) = value; } + void set_item(int i1, int i2, int i3, int i4, T value) { xt::view(_array, i1, i2, i3, i4) = value; } - void set_item_5d(int i1, int i2, int i3, int i4, int i5, T value) { xt::view(_array, i1, i2, i3, i4, i5) = value; } + void set_item(int i1, int i2, int i3, int i4, int i5, T value) { xt::view(_array, i1, i2, i3, i4, i5) = value; } // Boolean Functions bool all() const { return xt::all(_array); } diff --git a/numpy/src/numpy.cpp b/numpy/src/numpy.cpp index baa50dc..bc15429 100644 --- a/numpy/src/numpy.cpp +++ b/numpy/src/numpy.cpp @@ -232,9 +232,25 @@ class ndarray_base { virtual void set_item_index_float_4d(int index, const std::vector>>>& value) = 0; - virtual void set_item_tuple_int(py::tuple args, int_ value) = 0; + virtual void set_item_tuple_int1(py::tuple args, int_ value) = 0; - virtual void set_item_tuple_float(py::tuple args, float64 value) = 0; + virtual void set_item_tuple_int2(py::tuple args, const std::vector& value) = 0; + + virtual void set_item_tuple_int3(py::tuple args, const std::vector>& value) = 0; + + virtual void set_item_tuple_int4(py::tuple args, const std::vector>>& value) = 0; + + virtual void set_item_tuple_int5(py::tuple args, const std::vector>>>& value) = 0; + + virtual void set_item_tuple_float1(py::tuple args, float64 value) = 0; + + virtual void set_item_tuple_float2(py::tuple args, const std::vector& value) = 0; + + virtual void set_item_tuple_float3(py::tuple args, const std::vector>& value) = 0; + + virtual void set_item_tuple_float4(py::tuple args, const std::vector>>& value) = 0; + + virtual void set_item_tuple_float5(py::tuple args, const std::vector>>>& value) = 0; virtual void set_item_vector_int1(const std::vector& indices, int_ value) = 0; @@ -2001,7 +2017,7 @@ class ndarray : public ndarray_base { } } - void set_item_tuple_int(py::tuple args, int_ value) override { + void set_item_tuple_int1(py::tuple args, int_ value) override { std::vector indices; for(auto item: args) { indices.push_back(py::cast(item)); @@ -2014,16 +2030,172 @@ class ndarray : public ndarray_base { data.set_item(index, (pkpy::numpy::adapt(std::vector{value})).astype()); } } else if(indices.size() == 2 && indices.size() <= data.ndim()) - data.set_item_2d(indices[0], indices[1], static_cast(value)); + data.set_item(indices[0], indices[1], static_cast(value)); else if(indices.size() == 3 && indices.size() <= data.ndim()) - data.set_item_3d(indices[0], indices[1], indices[2], static_cast(value)); + data.set_item(indices[0], indices[1], indices[2], static_cast(value)); else if(indices.size() == 4 && indices.size() <= data.ndim()) - data.set_item_4d(indices[0], indices[1], indices[2], indices[3], static_cast(value)); + data.set_item(indices[0], indices[1], indices[2], indices[3], static_cast(value)); else if(indices.size() == 5 && indices.size() <= data.ndim()) - data.set_item_5d(indices[0], indices[1], indices[2], indices[3], indices[4], static_cast(value)); + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], static_cast(value)); + } + + void set_item_tuple_int2(py::tuple args, const std::vector& value) override { + std::vector indices; + for(auto item: args) { + indices.push_back(py::cast(item)); + } + if(indices.size() == 1) { + int index = indices[0]; + if constexpr(std::is_same_v) { + data.set_item(index, pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(index, (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 2 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 3 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 4 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 5 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], (pkpy::numpy::adapt(value)).astype()); + } + } + } + + void set_item_tuple_int3(py::tuple args, const std::vector>& value) override { + std::vector indices; + for(auto item: args) { + indices.push_back(py::cast(item)); + } + if(indices.size() == 1) { + int index = indices[0]; + if constexpr(std::is_same_v) { + data.set_item(index, pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(index, (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 2 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 3 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 4 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 5 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], (pkpy::numpy::adapt(value)).astype()); + } + } + } + + void set_item_tuple_int4(py::tuple args, const std::vector>>& value) override { + std::vector indices; + for(auto item: args) { + indices.push_back(py::cast(item)); + } + if(indices.size() == 1) { + int index = indices[0]; + if constexpr(std::is_same_v) { + data.set_item(index, pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(index, (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 2 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 3 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 4 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 5 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], (pkpy::numpy::adapt(value)).astype()); + } + } + } + + void set_item_tuple_int5(py::tuple args, const std::vector>>>& value) override { + std::vector indices; + for(auto item: args) { + indices.push_back(py::cast(item)); + } + if(indices.size() == 1) { + int index = indices[0]; + if constexpr(std::is_same_v) { + data.set_item(index, pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(index, (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 2 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 3 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 4 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 5 && indices.size() <= data.ndim()) { + if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], (pkpy::numpy::adapt(value)).astype()); + } + } } - void set_item_tuple_float(py::tuple args, float64 value) override { + void set_item_tuple_float1(py::tuple args, float64 value) override { std::vector indices; for(auto item: args) { indices.push_back(py::cast(item)); @@ -2036,13 +2208,181 @@ class ndarray : public ndarray_base { data.set_item(index, (pkpy::numpy::adapt(std::vector{value})).astype()); } } else if(indices.size() == 2 && indices.size() <= data.ndim()) - data.set_item_2d(indices[0], indices[1], static_cast(value)); + data.set_item(indices[0], indices[1], static_cast(value)); else if(indices.size() == 3 && indices.size() <= data.ndim()) - data.set_item_3d(indices[0], indices[1], indices[2], static_cast(value)); + data.set_item(indices[0], indices[1], indices[2], static_cast(value)); else if(indices.size() == 4 && indices.size() <= data.ndim()) - data.set_item_4d(indices[0], indices[1], indices[2], indices[3], static_cast(value)); + data.set_item(indices[0], indices[1], indices[2], indices[3], static_cast(value)); else if(indices.size() == 5 && indices.size() <= data.ndim()) - data.set_item_5d(indices[0], indices[1], indices[2], indices[3], indices[4], static_cast(value)); + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], static_cast(value)); + } + + void set_item_tuple_float2(py::tuple args, const std::vector& value) override { + std::vector indices; + for(auto item: args) { + indices.push_back(py::cast(item)); + } + if(indices.size() == 1) { + int index = indices[0]; + if constexpr(std::is_same_v) { + data.set_item(index, pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(index, (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 2 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], (pkpy::numpy::adapt(value)).astype()); + } + } + else if(indices.size() == 3 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], (pkpy::numpy::adapt(value)).astype()); + } + } + else if(indices.size() == 4 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], (pkpy::numpy::adapt(value)).astype()); + } + } + else if(indices.size() == 5 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], (pkpy::numpy::adapt(value)).astype()); + } + } + } + + void set_item_tuple_float3(py::tuple args, const std::vector>& value) override { + std::vector indices; + for(auto item: args) { + indices.push_back(py::cast(item)); + } + if(indices.size() == 1) { + int index = indices[0]; + if constexpr(std::is_same_v) { + data.set_item(index, pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(index, (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 2 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], (pkpy::numpy::adapt(value)).astype()); + } + } + else if(indices.size() == 3 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], (pkpy::numpy::adapt(value)).astype()); + } + } + else if(indices.size() == 4 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], (pkpy::numpy::adapt(value)).astype()); + } + } + else if(indices.size() == 5 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], (pkpy::numpy::adapt(value)).astype()); + } + } + } + + void set_item_tuple_float4(py::tuple args, const std::vector>>& value) override { + std::vector indices; + for(auto item: args) { + indices.push_back(py::cast(item)); + } + if(indices.size() == 1) { + int index = indices[0]; + if constexpr(std::is_same_v) { + data.set_item(index, pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(index, (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 2 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], (pkpy::numpy::adapt(value)).astype()); + } + } + else if(indices.size() == 3 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], (pkpy::numpy::adapt(value)).astype()); + } + } + else if(indices.size() == 4 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], (pkpy::numpy::adapt(value)).astype()); + } + } + else if(indices.size() == 5 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], (pkpy::numpy::adapt(value)).astype()); + } + } + } + + void set_item_tuple_float5(py::tuple args, const std::vector>>>& value) override { + std::vector indices; + for(auto item: args) { + indices.push_back(py::cast(item)); + } + if(indices.size() == 1) { + int index = indices[0]; + if constexpr(std::is_same_v) { + data.set_item(index, pkpy::numpy::adapt(value)); + } else if constexpr(std::is_same_v) { + data.set_item(index, (pkpy::numpy::adapt(value)).astype()); + } + } else if(indices.size() == 2 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], (pkpy::numpy::adapt(value)).astype()); + } + } + else if(indices.size() == 3 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], (pkpy::numpy::adapt(value)).astype()); + } + } + else if(indices.size() == 4 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], (pkpy::numpy::adapt(value)).astype()); + } + } + else if(indices.size() == 5 && indices.size() <= data.ndim()) { + if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], pkpy::numpy::adapt(value)); + } else if constexpr (std::is_same_v) { + data.set_item(indices[0], indices[1], indices[2], indices[3], indices[4], (pkpy::numpy::adapt(value)).astype()); + } + } } void set_item_vector_int1(const std::vector& indices, int_ value) override { @@ -2657,8 +2997,16 @@ PYBIND11_EMBEDDED_MODULE(numpy_bindings, m) { .def("__setitem__", &ndarray_base::set_item_index_float_2d) .def("__setitem__", &ndarray_base::set_item_index_float_3d) .def("__setitem__", &ndarray_base::set_item_index_float_4d) - .def("__setitem__", &ndarray_base::set_item_tuple_int) - .def("__setitem__", &ndarray_base::set_item_tuple_float) + .def("__setitem__", &ndarray_base::set_item_tuple_int1) + .def("__setitem__", &ndarray_base::set_item_tuple_int2) + .def("__setitem__", &ndarray_base::set_item_tuple_int3) + .def("__setitem__", &ndarray_base::set_item_tuple_int4) + .def("__setitem__", &ndarray_base::set_item_tuple_int5) + .def("__setitem__", &ndarray_base::set_item_tuple_float1) + .def("__setitem__", &ndarray_base::set_item_tuple_float2) + .def("__setitem__", &ndarray_base::set_item_tuple_float3) + .def("__setitem__", &ndarray_base::set_item_tuple_float4) + .def("__setitem__", &ndarray_base::set_item_tuple_float5) .def("__setitem__", &ndarray_base::set_item_vector_int1) .def("__setitem__", &ndarray_base::set_item_vector_int2) .def("__setitem__", &ndarray_base::set_item_vector_int3)