23#ifndef NDARRAY_pybind11_h_INCLUDED
24#define NDARRAY_pybind11_h_INCLUDED
45#include "pybind11/numpy.h"
54inline void destroyCapsule(PyObject * p) {
55 void * m = PyCapsule_GetPointer(p,
"ndarray.Manager");
56 Manager::Ptr * b =
reinterpret_cast<Manager::Ptr*
>(m);
62inline PyObject* makePyManager(Manager::Ptr
const & m) {
66 detail::destroyCapsule
70#if PYBIND11_VERSION_MAJOR == 2 && PYBIND11_VERSION_MINOR <= 1
71using pybind11_np_size_t = size_t;
73using pybind11_np_size_t = ssize_t;
76template <
typename T,
int N,
int C>
81__attribute__((visibility(
"hidden")))
85 using Wrapper = pybind11::array_t<typename std::remove_const<Element>::type, 0>;
86 static constexpr bool isConst = std::is_const<Element>::value;
90 bool init(pybind11::handle src) {
91 isNone = src.is_none();
95 if (!Wrapper::check_(src)) {
99 wrapper = pybind11::reinterpret_borrow<Wrapper>(src);
100 }
catch (pybind11::error_already_set & err) {
113 if (wrapper.ndim() != N) {
116 if (!isConst && !wrapper.writeable()) {
119 pybind11_np_size_t
const * shape = wrapper.shape();
120 pybind11_np_size_t
const * strides = wrapper.strides();
121 pybind11_np_size_t
const itemsize = wrapper.itemsize();
125 for (
int i = 0; i < C; ++i) {
126 if (shape[N-i-1] == 0) {
130 pybind11_np_size_t requiredStride = itemsize;
131 for (
int i = 0; i < C; ++i) {
132 if (strides[N-i-1] != requiredStride) {
135 requiredStride *= shape[N-i-1];
140 for (
int i = 0; i < -C; ++i) {
145 pybind11_np_size_t requiredStride = itemsize;
146 for (
int i = 0; i < -C; ++i) {
147 if (strides[i] != requiredStride) {
150 requiredStride *= shape[i];
160 if (!pybind11::reinterpret_borrow<pybind11::bool_>(wrapper.dtype().attr(
"isnative"))) {
161 PyErr_SetString(PyExc_TypeError,
"Only arrays with native byteorder can be converted to C++.");
162 throw pybind11::error_already_set();
166 pybind11_np_size_t
const * pShape = wrapper.shape();
167 pybind11_np_size_t
const * pStrides = wrapper.strides();
168 pybind11_np_size_t
const itemsize = wrapper.itemsize();
169 for (
int i = 0; i < N; ++i) {
170 if (pStrides[i] % itemsize != 0) {
173 "Cannot convert array to C++: strides must be an integer multiple of the element size"
175 throw pybind11::error_already_set();
177 nShape[i] = pShape[i];
178 nStrides[i] = pStrides[i]/itemsize;
182 nShape, nStrides, pybind11::object(wrapper))
189 std::vector<pybind11_np_size_t> pShape(N);
190 std::vector<pybind11_np_size_t> pStrides(N);
191 pybind11_np_size_t
const itemsize =
sizeof(Element);
192 for (
int i = 0; i < N; ++i) {
193 pShape[i] = nShape[i];
194 pStrides[i] = nStrides[i]*itemsize;
196 pybind11::object base;
198 base = pybind11::reinterpret_steal<pybind11::object>(ndarray::makePyManager(src.
getManager()));
200 Wrapper result(pShape, pStrides, src.
getData(), base);
201 if (std::is_const<Element>::value) {
202 result.attr(
"flags")[
"WRITEABLE"] =
false;
204 return result.release();
218template <
typename T,
int N,
int C>
219class type_caster< ndarray::Array<T,N,C> > {
223 bool load(handle src,
bool) {
224 return _helper.init(src) && _helper.check();
228 _value = _helper.convert();
232 return Helper::toPython(src);
235 static constexpr auto name = _(
"numpy.ndarray");
238 return cast(*src, policy, parent);
242 if (_helper.isNone) {
252 template <
typename _T>
using cast_op_type = pybind11::detail::cast_op_type<_T>;
Manager::Ptr getManager() const
Return the opaque object responsible for memory management.
Definition ArrayBase.h:136
Traits::Element Element
Data type of array elements.
Definition ArrayBase.h:50
Element * getData() const
Return a raw pointer to the first element of the array.
Definition ArrayBase.h:130
Strides getStrides() const
Return a Vector of the strides of all dimensions.
Definition ArrayBase.h:152
Index getShape() const
Return a Vector of the sizes of all dimensions.
Definition ArrayBase.h:149
A multidimensional strided array.
Definition Array.h:35
Functions that return an Eigen Map non-reference-counted view into an ndarray::Array.
detail::ExternalInitializer< T, N, Owner > external(T *data, Vector< U, N > const &shape, Vector< V, N > const &strides, Owner const &owner)
Create an expression that initializes an Array with externally allocated memory.
Definition initialization.h:176
Main public header file for ndarray.
Public header file for pybind11-based Python support.
A fixed-size 1D array class.
Definition Vector.h:82