30#if defined(NUMCPP_INCLUDE_BOOST_PYTHON_INTERFACE) && !defined(NUMCPP_NO_USE_BOOST)
38#include "boost/python.hpp"
39#include "boost/python/numpy.hpp"
47 namespace boostPythonInterface
51 template<
typename dtype>
52 class BoostNdarrayHelper
68 explicit BoostNdarrayHelper(
const boost::python::numpy::ndarray& inArray) :
69 theArray_(inArray.
astype(boost::python::numpy::dtype::get_builtin<dtype>())),
70 numDimensions_(static_cast<
uint8>(inArray.get_nd())),
71 shape_(numDimensions_),
72 strides_(numDimensions_),
76 Py_intptr_t
const* shapePtr = inArray.get_shape();
77 for (
uint8 i = 0; i < numDimensions_; ++i)
79 strides_[i] =
static_cast<uint32>(theArray_.strides(i));
80 shape_[i] = shapePtr[i];
83 if (numDimensions_ > 1 && inArray.strides(0) < inArray.strides(1))
94 explicit BoostNdarrayHelper(boost::python::tuple inShape) :
95 theArray_(boost::python::numpy::
zeros(inShape, boost::python::numpy::dtype::get_builtin<dtype>())),
96 numDimensions_(static_cast<
uint8>(theArray_.get_nd())),
97 shape_(numDimensions_),
98 strides_(numDimensions_),
101 Py_intptr_t
const* shapePtr = theArray_.get_shape();
102 for (
uint8 i = 0; i < numDimensions_; ++i)
104 strides_[i] =
static_cast<uint32>(theArray_.strides(i));
105 shape_[i] = shapePtr[i];
108 if (numDimensions_ > 1 && theArray_.strides(0) < theArray_.strides(1))
119 const boost::python::numpy::ndarray& getArray() noexcept
129 boost::python::numpy::matrix getArrayAsMatrix()
131 return boost::python::numpy::matrix(theArray_);
139 uint8 numDimensions() noexcept
141 return numDimensions_;
149 const std::vector<Py_intptr_t>&
shape() noexcept
162 for (
auto dimSize : shape_)
164 theSize *=
static_cast<uint32>(dimSize);
174 const std::vector<uint32>& strides()
196 bool shapeEqual(BoostNdarrayHelper& otherNdarrayHelper)
198 if (shape_.size() != otherNdarrayHelper.shape_.size())
213 dtype& operator()(
uint32 index)
215 checkIndices1D(index);
217 return *
reinterpret_cast<dtype*
>(theArray_.get_data() + strides_.front() * index);
230 checkIndices2D(index1, index2);
232 return *
reinterpret_cast<dtype*
>(theArray_.get_data() + strides_.front() * index1 +
233 strides_[1] * index2);
241 printf(
"array = \n");
242 if (numDimensions_ != 1)
244 std::cout <<
"printArray1D can only be used on a 1D array." << std::endl;
248 for (
int32 i = 0; i < shape_.front(); ++i)
250 printf(
"\t%f\n",
operator()(i));
259 printf(
"array = \n");
260 if (numDimensions_ != 2)
262 std::cout <<
"printArray2D can only be used on a 2D array." << std::endl;
266 for (
int32 index1 = 0; index1 < shape_.front(); ++index1)
268 for (
int32 index2 = 0; index2 < shape_.back(); ++index2)
270 printf(
"\t%f",
operator()(index1, index2));
278 boost::python::numpy::ndarray theArray_;
279 uint8 numDimensions_;
280 std::vector<Py_intptr_t> shape_;
281 std::vector<uint32> strides_;
289 void checkIndicesGeneric(boost::python::tuple indices)
291 if (boost::python::len(indices) != numDimensions_)
294 "Error: BoostNdarrayHelper::checkIndicesGeneric: Array has " +
utils::num2str(numDimensions_);
295 errStr +=
" dimensions, you asked for " +
296 utils::num2str(
static_cast<int>(boost::python::len(indices))) +
"!";
297 PyErr_SetString(PyExc_RuntimeError, errStr.c_str());
300 for (
int i = 0; i < numDimensions_; ++i)
302 int index = boost::python::extract<int>(indices[i]);
303 if (index > shape_[i])
306 "Error: BoostNdarrayHelper::checkIndicesGeneric: Input index [" +
utils::num2str(index);
307 errStr +=
"] is larger than the size of the array [" +
utils::num2str(shape_[i]) +
"].";
308 PyErr_SetString(PyExc_RuntimeError, errStr.c_str());
318 void checkIndices1D(
uint32 index)
320 boost::python::tuple indices = boost::python::make_tuple(index);
321 checkIndicesGeneric(indices);
332 boost::python::tuple indices = boost::python::make_tuple(index1, index2);
333 checkIndicesGeneric(indices);
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) noexcept
Definition: StlAlgorithms.hpp:140
std::string num2str(dtype inNumber)
Definition: num2str.hpp:44
Definition: Cartesian.hpp:40
uint32 size(const NdArray< dtype > &inArray) noexcept
Definition: size.hpp:43
std::int32_t int32
Definition: Types.hpp:36
std::uint8_t uint8
Definition: Types.hpp:42
NdArray< dtype > zeros(uint32 inSquareSize)
Definition: zeros.hpp:48
Shape shape(const NdArray< dtype > &inArray) noexcept
Definition: Functions/Shape.hpp:42
NdArray< dtypeOut > astype(const NdArray< dtype > inArray)
Definition: astype.hpp:45
std::uint32_t uint32
Definition: Types.hpp:40