/* pybind11/numpy.h: Basic NumPy support, vectorize() wrapper Copyright (c) 2016 Wenzel Jakob All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. */ #pragma once #include "pybind11.h" #include "complex.h" #include #include #include #include #include #include #include #include #include #include #include #if defined(_MSC_VER) # pragma warning(push) # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant #endif /* This will be true on all flat address space platforms and allows us to reduce the whole npy_intp / size_t / Py_intptr_t business down to just size_t for all size and dimension types (e.g. shape, strides, indexing), instead of inflicting this upon the library user. */ static_assert(sizeof(size_t) == sizeof(Py_intptr_t), "size_t != Py_intptr_t"); NAMESPACE_BEGIN(pybind11) NAMESPACE_BEGIN(detail) template struct npy_format_descriptor; struct PyArrayDescr_Proxy { PyObject_HEAD PyObject *typeobj; char kind; char type; char byteorder; char flags; int type_num; int elsize; int alignment; char *subarray; PyObject *fields; PyObject *names; }; struct PyArray_Proxy { PyObject_HEAD char *data; int nd; ssize_t *dimensions; ssize_t *strides; PyObject *base; PyObject *descr; int flags; }; struct PyVoidScalarObject_Proxy { PyObject_VAR_HEAD char *obval; PyArrayDescr_Proxy *descr; int flags; PyObject *base; }; struct numpy_type_info { PyObject* dtype_ptr; std::string format_str; }; struct numpy_internals { std::unordered_map registered_dtypes; numpy_type_info *get_type_info(const std::type_info& tinfo, bool throw_if_missing = true) { auto it = registered_dtypes.find(std::type_index(tinfo)); if (it != registered_dtypes.end()) return &(it->second); if (throw_if_missing) pybind11_fail(std::string("NumPy type info missing for ") + tinfo.name()); return nullptr; } template numpy_type_info *get_type_info(bool throw_if_missing = true) { return get_type_info(typeid(typename std::remove_cv::type), throw_if_missing); } }; inline PYBIND11_NOINLINE void load_numpy_internals(numpy_internals* &ptr) { ptr = &get_or_create_shared_data("_numpy_internals"); } inline numpy_internals& get_numpy_internals() { static numpy_internals* ptr = nullptr; if (!ptr) load_numpy_internals(ptr); return *ptr; } struct npy_api { enum constants { NPY_ARRAY_C_CONTIGUOUS_ = 0x0001, NPY_ARRAY_F_CONTIGUOUS_ = 0x0002, NPY_ARRAY_OWNDATA_ = 0x0004, NPY_ARRAY_FORCECAST_ = 0x0010, NPY_ARRAY_ENSUREARRAY_ = 0x0040, NPY_ARRAY_ALIGNED_ = 0x0100, NPY_ARRAY_WRITEABLE_ = 0x0400, NPY_BOOL_ = 0, NPY_BYTE_, NPY_UBYTE_, NPY_SHORT_, NPY_USHORT_, NPY_INT_, NPY_UINT_, NPY_LONG_, NPY_ULONG_, NPY_LONGLONG_, NPY_ULONGLONG_, NPY_FLOAT_, NPY_DOUBLE_, NPY_LONGDOUBLE_, NPY_CFLOAT_, NPY_CDOUBLE_, NPY_CLONGDOUBLE_, NPY_OBJECT_ = 17, NPY_STRING_, NPY_UNICODE_, NPY_VOID_ }; static npy_api& get() { static npy_api api = lookup(); return api; } bool PyArray_Check_(PyObject *obj) const { return (bool) PyObject_TypeCheck(obj, PyArray_Type_); } bool PyArrayDescr_Check_(PyObject *obj) const { return (bool) PyObject_TypeCheck(obj, PyArrayDescr_Type_); } PyObject *(*PyArray_DescrFromType_)(int); PyObject *(*PyArray_NewFromDescr_) (PyTypeObject *, PyObject *, int, Py_intptr_t *, Py_intptr_t *, void *, int, PyObject *); PyObject *(*PyArray_DescrNewFromType_)(int); PyObject *(*PyArray_NewCopy_)(PyObject *, int); PyTypeObject *PyArray_Type_; PyTypeObject *PyVoidArrType_Type_; PyTypeObject *PyArrayDescr_Type_; PyObject *(*PyArray_DescrFromScalar_)(PyObject *); PyObject *(*PyArray_FromAny_) (PyObject *, PyObject *, int, int, int, PyObject *); int (*PyArray_DescrConverter_) (PyObject *, PyObject **); bool (*PyArray_EquivTypes_) (PyObject *, PyObject *); int (*PyArray_GetArrayParamsFromObject_)(PyObject *, PyObject *, char, PyObject **, int *, Py_ssize_t *, PyObject **, PyObject *); PyObject *(*PyArray_Squeeze_)(PyObject *); int (*PyArray_SetBaseObject_)(PyObject *, PyObject *); private: enum functions { API_PyArray_Type = 2, API_PyArrayDescr_Type = 3, API_PyVoidArrType_Type = 39, API_PyArray_DescrFromType = 45, API_PyArray_DescrFromScalar = 57, API_PyArray_FromAny = 69, API_PyArray_NewCopy = 85, API_PyArray_NewFromDescr = 94, API_PyArray_DescrNewFromType = 9, API_PyArray_DescrConverter = 174, API_PyArray_EquivTypes = 182, API_PyArray_GetArrayParamsFromObject = 278, API_PyArray_Squeeze = 136, API_PyArray_SetBaseObject = 282 }; static npy_api lookup() { module m = module::import("numpy.core.multiarray"); auto c = m.attr("_ARRAY_API"); #if PY_MAJOR_VERSION >= 3 void **api_ptr = (void **) PyCapsule_GetPointer(c.ptr(), NULL); #else void **api_ptr = (void **) PyCObject_AsVoidPtr(c.ptr()); #endif npy_api api; #define DECL_NPY_API(Func) api.Func##_ = (decltype(api.Func##_)) api_ptr[API_##Func]; DECL_NPY_API(PyArray_Type); DECL_NPY_API(PyVoidArrType_Type); DECL_NPY_API(PyArrayDescr_Type); DECL_NPY_API(PyArray_DescrFromType); DECL_NPY_API(PyArray_DescrFromScalar); DECL_NPY_API(PyArray_FromAny); DECL_NPY_API(PyArray_NewCopy); DECL_NPY_API(PyArray_NewFromDescr); DECL_NPY_API(PyArray_DescrNewFromType); DECL_NPY_API(PyArray_DescrConverter); DECL_NPY_API(PyArray_EquivTypes); DECL_NPY_API(PyArray_GetArrayParamsFromObject); DECL_NPY_API(PyArray_Squeeze); DECL_NPY_API(PyArray_SetBaseObject); #undef DECL_NPY_API return api; } }; inline PyArray_Proxy* array_proxy(void* ptr) { return reinterpret_cast(ptr); } inline const PyArray_Proxy* array_proxy(const void* ptr) { return reinterpret_cast(ptr); } inline PyArrayDescr_Proxy* array_descriptor_proxy(PyObject* ptr) { return reinterpret_cast(ptr); } inline const PyArrayDescr_Proxy* array_descriptor_proxy(const PyObject* ptr) { return reinterpret_cast(ptr); } inline bool check_flags(const void* ptr, int flag) { return (flag == (array_proxy(ptr)->flags & flag)); } template struct is_std_array : std::false_type { }; template struct is_std_array> : std::true_type { }; template struct is_complex : std::false_type { }; template struct is_complex> : std::true_type { }; template using is_pod_struct = all_of< std::is_pod, // since we're accessing directly in memory we need a POD type satisfies_none_of >; NAMESPACE_END(detail) class dtype : public object { public: PYBIND11_OBJECT_DEFAULT(dtype, object, detail::npy_api::get().PyArrayDescr_Check_); explicit dtype(const buffer_info &info) { dtype descr(_dtype_from_pep3118()(PYBIND11_STR_TYPE(info.format))); // If info.itemsize == 0, use the value calculated from the format string m_ptr = descr.strip_padding(info.itemsize ? info.itemsize : descr.itemsize()).release().ptr(); } explicit dtype(const std::string &format) { m_ptr = from_args(pybind11::str(format)).release().ptr(); } dtype(const char *format) : dtype(std::string(format)) { } dtype(list names, list formats, list offsets, size_t itemsize) { dict args; args["names"] = names; args["formats"] = formats; args["offsets"] = offsets; args["itemsize"] = pybind11::int_(itemsize); m_ptr = from_args(args).release().ptr(); } /// This is essentially the same as calling numpy.dtype(args) in Python. static dtype from_args(object args) { PyObject *ptr = nullptr; if (!detail::npy_api::get().PyArray_DescrConverter_(args.release().ptr(), &ptr) || !ptr) throw error_already_set(); return reinterpret_steal(ptr); } /// Return dtype associated with a C++ type. template static dtype of() { return detail::npy_format_descriptor::type>::dtype(); } /// Size of the data type in bytes. size_t itemsize() const { return (size_t) detail::array_descriptor_proxy(m_ptr)->elsize; } /// Returns true for structured data types. bool has_fields() const { return detail::array_descriptor_proxy(m_ptr)->names != nullptr; } /// Single-character type code. char kind() const { return detail::array_descriptor_proxy(m_ptr)->kind; } private: static object _dtype_from_pep3118() { static PyObject *obj = module::import("numpy.core._internal") .attr("_dtype_from_pep3118").cast().release().ptr(); return reinterpret_borrow(obj); } dtype strip_padding(size_t itemsize) { // Recursively strip all void fields with empty names that are generated for // padding fields (as of NumPy v1.11). if (!has_fields()) return *this; struct field_descr { PYBIND11_STR_TYPE name; object format; pybind11::int_ offset; }; std::vector field_descriptors; for (auto field : attr("fields").attr("items")()) { auto spec = field.cast(); auto name = spec[0].cast(); auto format = spec[1].cast()[0].cast(); auto offset = spec[1].cast()[1].cast(); if (!len(name) && format.kind() == 'V') continue; field_descriptors.push_back({(PYBIND11_STR_TYPE) name, format.strip_padding(format.itemsize()), offset}); } std::sort(field_descriptors.begin(), field_descriptors.end(), [](const field_descr& a, const field_descr& b) { return a.offset.cast() < b.offset.cast(); }); list names, formats, offsets; for (auto& descr : field_descriptors) { names.append(descr.name); formats.append(descr.format); offsets.append(descr.offset); } return dtype(names, formats, offsets, itemsize); } }; class array : public buffer { public: PYBIND11_OBJECT_CVT(array, buffer, detail::npy_api::get().PyArray_Check_, raw_array) enum { c_style = detail::npy_api::NPY_ARRAY_C_CONTIGUOUS_, f_style = detail::npy_api::NPY_ARRAY_F_CONTIGUOUS_, forcecast = detail::npy_api::NPY_ARRAY_FORCECAST_ }; array() : array(0, static_cast(nullptr)) {} array(const pybind11::dtype &dt, const std::vector &shape, const std::vector &strides, const void *ptr = nullptr, handle base = handle()) { auto& api = detail::npy_api::get(); auto ndim = shape.size(); if (shape.size() != strides.size()) pybind11_fail("NumPy: shape ndim doesn't match strides ndim"); auto descr = dt; int flags = 0; if (base && ptr) { if (isinstance(base)) /* Copy flags from base (except ownership bit) */ flags = reinterpret_borrow(base).flags() & ~detail::npy_api::NPY_ARRAY_OWNDATA_; else /* Writable by default, easy to downgrade later on if needed */ flags = detail::npy_api::NPY_ARRAY_WRITEABLE_; } auto tmp = reinterpret_steal(api.PyArray_NewFromDescr_( api.PyArray_Type_, descr.release().ptr(), (int) ndim, reinterpret_cast(const_cast(shape.data())), reinterpret_cast(const_cast(strides.data())), const_cast(ptr), flags, nullptr)); if (!tmp) pybind11_fail("NumPy: unable to create array!"); if (ptr) { if (base) { api.PyArray_SetBaseObject_(tmp.ptr(), base.inc_ref().ptr()); } else { tmp = reinterpret_steal(api.PyArray_NewCopy_(tmp.ptr(), -1 /* any order */)); } } m_ptr = tmp.release().ptr(); } array(const pybind11::dtype &dt, const std::vector &shape, const void *ptr = nullptr, handle base = handle()) : array(dt, shape, default_strides(shape, dt.itemsize()), ptr, base) { } array(const pybind11::dtype &dt, size_t count, const void *ptr = nullptr, handle base = handle()) : array(dt, std::vector{ count }, ptr, base) { } template array(const std::vector& shape, const std::vector& strides, const T* ptr, handle base = handle()) : array(pybind11::dtype::of(), shape, strides, (const void *) ptr, base) { } template array(const std::vector &shape, const T *ptr, handle base = handle()) : array(shape, default_strides(shape, sizeof(T)), ptr, base) { } template array(size_t count, const T *ptr, handle base = handle()) : array(std::vector{ count }, ptr, base) { } explicit array(const buffer_info &info) : array(pybind11::dtype(info), info.shape, info.strides, info.ptr) { } /// Array descriptor (dtype) pybind11::dtype dtype() const { return reinterpret_borrow(detail::array_proxy(m_ptr)->descr); } /// Total number of elements size_t size() const { return std::accumulate(shape(), shape() + ndim(), (size_t) 1, std::multiplies()); } /// Byte size of a single element size_t itemsize() const { return (size_t) detail::array_descriptor_proxy(detail::array_proxy(m_ptr)->descr)->elsize; } /// Total number of bytes size_t nbytes() const { return size() * itemsize(); } /// Number of dimensions size_t ndim() const { return (size_t) detail::array_proxy(m_ptr)->nd; } /// Base object object base() const { return reinterpret_borrow(detail::array_proxy(m_ptr)->base); } /// Dimensions of the array const size_t* shape() const { return reinterpret_cast(detail::array_proxy(m_ptr)->dimensions); } /// Dimension along a given axis size_t shape(size_t dim) const { if (dim >= ndim()) fail_dim_check(dim, "invalid axis"); return shape()[dim]; } /// Strides of the array const size_t* strides() const { return reinterpret_cast(detail::array_proxy(m_ptr)->strides); } /// Stride along a given axis size_t strides(size_t dim) const { if (dim >= ndim()) fail_dim_check(dim, "invalid axis"); return strides()[dim]; } /// Return the NumPy array flags int flags() const { return detail::array_proxy(m_ptr)->flags; } /// If set, the array is writeable (otherwise the buffer is read-only) bool writeable() const { return detail::check_flags(m_ptr, detail::npy_api::NPY_ARRAY_WRITEABLE_); } /// If set, the array owns the data (will be freed when the array is deleted) bool owndata() const { return detail::check_flags(m_ptr, detail::npy_api::NPY_ARRAY_OWNDATA_); } /// Pointer to the contained data. If index is not provided, points to the /// beginning of the buffer. May throw if the index would lead to out of bounds access. template const void* data(Ix... index) const { return static_cast(detail::array_proxy(m_ptr)->data + offset_at(index...)); } /// Mutable pointer to the contained data. If index is not provided, points to the /// beginning of the buffer. May throw if the index would lead to out of bounds access. /// May throw if the array is not writeable. template void* mutable_data(Ix... index) { check_writeable(); return static_cast(detail::array_proxy(m_ptr)->data + offset_at(index...)); } /// Byte offset from beginning of the array to a given index (full or partial). /// May throw if the index would lead to out of bounds access. template size_t offset_at(Ix... index) const { if (sizeof...(index) > ndim()) fail_dim_check(sizeof...(index), "too many indices for an array"); return byte_offset(size_t(index)...); } size_t offset_at() const { return 0; } /// Item count from beginning of the array to a given index (full or partial). /// May throw if the index would lead to out of bounds access. template size_t index_at(Ix... index) const { return offset_at(index...) / itemsize(); } /// Return a new view with all of the dimensions of length 1 removed array squeeze() { auto& api = detail::npy_api::get(); return reinterpret_steal(api.PyArray_Squeeze_(m_ptr)); } /// Ensure that the argument is a NumPy array /// In case of an error, nullptr is returned and the Python error is cleared. static array ensure(handle h, int ExtraFlags = 0) { auto result = reinterpret_steal(raw_array(h.ptr(), ExtraFlags)); if (!result) PyErr_Clear(); return result; } protected: template friend struct detail::npy_format_descriptor; void fail_dim_check(size_t dim, const std::string& msg) const { throw index_error(msg + ": " + std::to_string(dim) + " (ndim = " + std::to_string(ndim()) + ")"); } template size_t byte_offset(Ix... index) const { check_dimensions(index...); return byte_offset_unsafe(index...); } template size_t byte_offset_unsafe(size_t i, Ix... index) const { return i * strides()[dim] + byte_offset_unsafe(index...); } template size_t byte_offset_unsafe() const { return 0; } void check_writeable() const { if (!writeable()) throw std::runtime_error("array is not writeable"); } static std::vector default_strides(const std::vector& shape, size_t itemsize) { auto ndim = shape.size(); std::vector strides(ndim); if (ndim) { std::fill(strides.begin(), strides.end(), itemsize); for (size_t i = 0; i < ndim - 1; i++) for (size_t j = 0; j < ndim - 1 - i; j++) strides[j] *= shape[ndim - 1 - i]; } return strides; } template void check_dimensions(Ix... index) const { check_dimensions_impl(size_t(0), shape(), size_t(index)...); } void check_dimensions_impl(size_t, const size_t*) const { } template void check_dimensions_impl(size_t axis, const size_t* shape, size_t i, Ix... index) const { if (i >= *shape) { throw index_error(std::string("index ") + std::to_string(i) + " is out of bounds for axis " + std::to_string(axis) + " with size " + std::to_string(*shape)); } check_dimensions_impl(axis + 1, shape + 1, index...); } /// Create array from any object -- always returns a new reference static PyObject *raw_array(PyObject *ptr, int ExtraFlags = 0) { if (ptr == nullptr) return nullptr; return detail::npy_api::get().PyArray_FromAny_( ptr, nullptr, 0, 0, detail::npy_api::NPY_ARRAY_ENSUREARRAY_ | ExtraFlags, nullptr); } }; template class array_t : public array { public: array_t() : array(0, static_cast(nullptr)) {} array_t(handle h, borrowed_t) : array(h, borrowed) { } array_t(handle h, stolen_t) : array(h, stolen) { } PYBIND11_DEPRECATED("Use array_t::ensure() instead") array_t(handle h, bool is_borrowed) : array(raw_array_t(h.ptr()), stolen) { if (!m_ptr) PyErr_Clear(); if (!is_borrowed) Py_XDECREF(h.ptr()); } array_t(const object &o) : array(raw_array_t(o.ptr()), stolen) { if (!m_ptr) throw error_already_set(); } explicit array_t(const buffer_info& info) : array(info) { } array_t(const std::vector &shape, const std::vector &strides, const T *ptr = nullptr, handle base = handle()) : array(shape, strides, ptr, base) { } explicit array_t(const std::vector &shape, const T *ptr = nullptr, handle base = handle()) : array(shape, ptr, base) { } explicit array_t(size_t count, const T *ptr = nullptr, handle base = handle()) : array(count, ptr, base) { } constexpr size_t itemsize() const { return sizeof(T); } template size_t index_at(Ix... index) const { return offset_at(index...) / itemsize(); } template const T* data(Ix... index) const { return static_cast(array::data(index...)); } template T* mutable_data(Ix... index) { return static_cast(array::mutable_data(index...)); } // Reference to element at a given index template const T& at(Ix... index) const { if (sizeof...(index) != ndim()) fail_dim_check(sizeof...(index), "index dimension mismatch"); return *(static_cast(array::data()) + byte_offset(size_t(index)...) / itemsize()); } // Mutable reference to element at a given index template T& mutable_at(Ix... index) { if (sizeof...(index) != ndim()) fail_dim_check(sizeof...(index), "index dimension mismatch"); return *(static_cast(array::mutable_data()) + byte_offset(size_t(index)...) / itemsize()); } /// Ensure that the argument is a NumPy array of the correct dtype (and if not, try to convert /// it). In case of an error, nullptr is returned and the Python error is cleared. static array_t ensure(handle h) { auto result = reinterpret_steal(raw_array_t(h.ptr())); if (!result) PyErr_Clear(); return result; } static bool check_(handle h) { const auto &api = detail::npy_api::get(); return api.PyArray_Check_(h.ptr()) && api.PyArray_EquivTypes_(detail::array_proxy(h.ptr())->descr, dtype::of().ptr()); } protected: /// Create array from any object -- always returns a new reference static PyObject *raw_array_t(PyObject *ptr) { if (ptr == nullptr) return nullptr; return detail::npy_api::get().PyArray_FromAny_( ptr, dtype::of().release().ptr(), 0, 0, detail::npy_api::NPY_ARRAY_ENSUREARRAY_ | ExtraFlags, nullptr); } }; template struct format_descriptor::value>> { static std::string format() { return detail::npy_format_descriptor::type>::format(); } }; template struct format_descriptor { static std::string format() { return std::to_string(N) + "s"; } }; template struct format_descriptor> { static std::string format() { return std::to_string(N) + "s"; } }; template struct format_descriptor::value>> { static std::string format() { return format_descriptor< typename std::remove_cv::type>::type>::format(); } }; NAMESPACE_BEGIN(detail) template struct pyobject_caster> { using type = array_t; bool load(handle src, bool /* convert */) { value = type::ensure(src); return static_cast(value); } static handle cast(const handle &src, return_value_policy /* policy */, handle /* parent */) { return src.inc_ref(); } PYBIND11_TYPE_CASTER(type, handle_type_name::name()); }; template struct npy_format_descriptor::value>> { private: // NB: the order here must match the one in common.h constexpr static const int values[15] = { npy_api::NPY_BOOL_, npy_api::NPY_BYTE_, npy_api::NPY_UBYTE_, npy_api::NPY_SHORT_, npy_api::NPY_USHORT_, npy_api::NPY_INT_, npy_api::NPY_UINT_, npy_api::NPY_LONGLONG_, npy_api::NPY_ULONGLONG_, npy_api::NPY_FLOAT_, npy_api::NPY_DOUBLE_, npy_api::NPY_LONGDOUBLE_, npy_api::NPY_CFLOAT_, npy_api::NPY_CDOUBLE_, npy_api::NPY_CLONGDOUBLE_ }; public: static constexpr int value = values[detail::is_fmt_numeric::index]; static pybind11::dtype dtype() { if (auto ptr = npy_api::get().PyArray_DescrFromType_(value)) return reinterpret_borrow(ptr); pybind11_fail("Unsupported buffer format!"); } template ::value, int> = 0> static PYBIND11_DESCR name() { return _::value>(_("bool"), _::value>("int", "uint") + _()); } template ::value, int> = 0> static PYBIND11_DESCR name() { return _::value || std::is_same::value>( _("float") + _(), _("longdouble")); } template ::value, int> = 0> static PYBIND11_DESCR name() { return _::value || std::is_same::value>( _("complex") + _(), _("longcomplex")); } }; #define PYBIND11_DECL_CHAR_FMT \ static PYBIND11_DESCR name() { return _("S") + _(); } \ static pybind11::dtype dtype() { return pybind11::dtype(std::string("S") + std::to_string(N)); } template struct npy_format_descriptor { PYBIND11_DECL_CHAR_FMT }; template struct npy_format_descriptor> { PYBIND11_DECL_CHAR_FMT }; #undef PYBIND11_DECL_CHAR_FMT template struct npy_format_descriptor::value>> { private: using base_descr = npy_format_descriptor::type>; public: static PYBIND11_DESCR name() { return base_descr::name(); } static pybind11::dtype dtype() { return base_descr::dtype(); } }; struct field_descriptor { const char *name; size_t offset; size_t size; size_t alignment; std::string format; dtype descr; }; inline PYBIND11_NOINLINE void register_structured_dtype( const std::initializer_list& fields, const std::type_info& tinfo, size_t itemsize, bool (*direct_converter)(PyObject *, void *&)) { auto& numpy_internals = get_numpy_internals(); if (numpy_internals.get_type_info(tinfo, false)) pybind11_fail("NumPy: dtype is already registered"); list names, formats, offsets; for (auto field : fields) { if (!field.descr) pybind11_fail(std::string("NumPy: unsupported field dtype: `") + field.name + "` @ " + tinfo.name()); names.append(PYBIND11_STR_TYPE(field.name)); formats.append(field.descr); offsets.append(pybind11::int_(field.offset)); } auto dtype_ptr = pybind11::dtype(names, formats, offsets, itemsize).release().ptr(); // There is an existing bug in NumPy (as of v1.11): trailing bytes are // not encoded explicitly into the format string. This will supposedly // get fixed in v1.12; for further details, see these: // - https://github.com/numpy/numpy/issues/7797 // - https://github.com/numpy/numpy/pull/7798 // Because of this, we won't use numpy's logic to generate buffer format // strings and will just do it ourselves. std::vector ordered_fields(fields); std::sort(ordered_fields.begin(), ordered_fields.end(), [](const field_descriptor &a, const field_descriptor &b) { return a.offset < b.offset; }); size_t offset = 0; std::ostringstream oss; oss << "T{"; for (auto& field : ordered_fields) { if (field.offset > offset) oss << (field.offset - offset) << 'x'; // mark unaligned fields with '^' (unaligned native type) if (field.offset % field.alignment) oss << '^'; oss << field.format << ':' << field.name << ':'; offset = field.offset + field.size; } if (itemsize > offset) oss << (itemsize - offset) << 'x'; oss << '}'; auto format_str = oss.str(); // Sanity check: verify that NumPy properly parses our buffer format string auto& api = npy_api::get(); auto arr = array(buffer_info(nullptr, itemsize, format_str, 1)); if (!api.PyArray_EquivTypes_(dtype_ptr, arr.dtype().ptr())) pybind11_fail("NumPy: invalid buffer descriptor!"); auto tindex = std::type_index(tinfo); numpy_internals.registered_dtypes[tindex] = { dtype_ptr, format_str }; get_internals().direct_conversions[tindex].push_back(direct_converter); } template struct npy_format_descriptor { static_assert(is_pod_struct::value, "Attempt to use a non-POD or unimplemented POD type as a numpy dtype"); static PYBIND11_DESCR name() { return _("struct"); } static pybind11::dtype dtype() { return reinterpret_borrow(dtype_ptr()); } static std::string format() { static auto format_str = get_numpy_internals().get_type_info(true)->format_str; return format_str; } static void register_dtype(const std::initializer_list& fields) { register_structured_dtype(fields, typeid(typename std::remove_cv::type), sizeof(T), &direct_converter); } private: static PyObject* dtype_ptr() { static PyObject* ptr = get_numpy_internals().get_type_info(true)->dtype_ptr; return ptr; } static bool direct_converter(PyObject *obj, void*& value) { auto& api = npy_api::get(); if (!PyObject_TypeCheck(obj, api.PyVoidArrType_Type_)) return false; if (auto descr = reinterpret_steal(api.PyArray_DescrFromScalar_(obj))) { if (api.PyArray_EquivTypes_(dtype_ptr(), descr.ptr())) { value = ((PyVoidScalarObject_Proxy *) obj)->obval; return true; } } return false; } }; #define PYBIND11_FIELD_DESCRIPTOR_EX(T, Field, Name) \ ::pybind11::detail::field_descriptor { \ Name, offsetof(T, Field), sizeof(decltype(std::declval().Field)), \ alignof(decltype(std::declval().Field)), \ ::pybind11::format_descriptor().Field)>::format(), \ ::pybind11::detail::npy_format_descriptor().Field)>::dtype() \ } // Extract name, offset and format descriptor for a struct field #define PYBIND11_FIELD_DESCRIPTOR(T, Field) PYBIND11_FIELD_DESCRIPTOR_EX(T, Field, #Field) // The main idea of this macro is borrowed from https://github.com/swansontec/map-macro // (C) William Swanson, Paul Fultz #define PYBIND11_EVAL0(...) __VA_ARGS__ #define PYBIND11_EVAL1(...) PYBIND11_EVAL0 (PYBIND11_EVAL0 (PYBIND11_EVAL0 (__VA_ARGS__))) #define PYBIND11_EVAL2(...) PYBIND11_EVAL1 (PYBIND11_EVAL1 (PYBIND11_EVAL1 (__VA_ARGS__))) #define PYBIND11_EVAL3(...) PYBIND11_EVAL2 (PYBIND11_EVAL2 (PYBIND11_EVAL2 (__VA_ARGS__))) #define PYBIND11_EVAL4(...) PYBIND11_EVAL3 (PYBIND11_EVAL3 (PYBIND11_EVAL3 (__VA_ARGS__))) #define PYBIND11_EVAL(...) PYBIND11_EVAL4 (PYBIND11_EVAL4 (PYBIND11_EVAL4 (__VA_ARGS__))) #define PYBIND11_MAP_END(...) #define PYBIND11_MAP_OUT #define PYBIND11_MAP_COMMA , #define PYBIND11_MAP_GET_END() 0, PYBIND11_MAP_END #define PYBIND11_MAP_NEXT0(test, next, ...) next PYBIND11_MAP_OUT #define PYBIND11_MAP_NEXT1(test, next) PYBIND11_MAP_NEXT0 (test, next, 0) #define PYBIND11_MAP_NEXT(test, next) PYBIND11_MAP_NEXT1 (PYBIND11_MAP_GET_END test, next) #ifdef _MSC_VER // MSVC is not as eager to expand macros, hence this workaround #define PYBIND11_MAP_LIST_NEXT1(test, next) \ PYBIND11_EVAL0 (PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0)) #else #define PYBIND11_MAP_LIST_NEXT1(test, next) \ PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0) #endif #define PYBIND11_MAP_LIST_NEXT(test, next) \ PYBIND11_MAP_LIST_NEXT1 (PYBIND11_MAP_GET_END test, next) #define PYBIND11_MAP_LIST0(f, t, x, peek, ...) \ f(t, x) PYBIND11_MAP_LIST_NEXT (peek, PYBIND11_MAP_LIST1) (f, t, peek, __VA_ARGS__) #define PYBIND11_MAP_LIST1(f, t, x, peek, ...) \ f(t, x) PYBIND11_MAP_LIST_NEXT (peek, PYBIND11_MAP_LIST0) (f, t, peek, __VA_ARGS__) // PYBIND11_MAP_LIST(f, t, a1, a2, ...) expands to f(t, a1), f(t, a2), ... #define PYBIND11_MAP_LIST(f, t, ...) \ PYBIND11_EVAL (PYBIND11_MAP_LIST1 (f, t, __VA_ARGS__, (), 0)) #define PYBIND11_NUMPY_DTYPE(Type, ...) \ ::pybind11::detail::npy_format_descriptor::register_dtype \ ({PYBIND11_MAP_LIST (PYBIND11_FIELD_DESCRIPTOR, Type, __VA_ARGS__)}) #ifdef _MSC_VER #define PYBIND11_MAP2_LIST_NEXT1(test, next) \ PYBIND11_EVAL0 (PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0)) #else #define PYBIND11_MAP2_LIST_NEXT1(test, next) \ PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0) #endif #define PYBIND11_MAP2_LIST_NEXT(test, next) \ PYBIND11_MAP2_LIST_NEXT1 (PYBIND11_MAP_GET_END test, next) #define PYBIND11_MAP2_LIST0(f, t, x1, x2, peek, ...) \ f(t, x1, x2) PYBIND11_MAP2_LIST_NEXT (peek, PYBIND11_MAP2_LIST1) (f, t, peek, __VA_ARGS__) #define PYBIND11_MAP2_LIST1(f, t, x1, x2, peek, ...) \ f(t, x1, x2) PYBIND11_MAP2_LIST_NEXT (peek, PYBIND11_MAP2_LIST0) (f, t, peek, __VA_ARGS__) // PYBIND11_MAP2_LIST(f, t, a1, a2, ...) expands to f(t, a1, a2), f(t, a3, a4), ... #define PYBIND11_MAP2_LIST(f, t, ...) \ PYBIND11_EVAL (PYBIND11_MAP2_LIST1 (f, t, __VA_ARGS__, (), 0)) #define PYBIND11_NUMPY_DTYPE_EX(Type, ...) \ ::pybind11::detail::npy_format_descriptor::register_dtype \ ({PYBIND11_MAP2_LIST (PYBIND11_FIELD_DESCRIPTOR_EX, Type, __VA_ARGS__)}) template using array_iterator = typename std::add_pointer::type; template array_iterator array_begin(const buffer_info& buffer) { return array_iterator(reinterpret_cast(buffer.ptr)); } template array_iterator array_end(const buffer_info& buffer) { return array_iterator(reinterpret_cast(buffer.ptr) + buffer.size); } class common_iterator { public: using container_type = std::vector; using value_type = container_type::value_type; using size_type = container_type::size_type; common_iterator() : p_ptr(0), m_strides() {} common_iterator(void* ptr, const container_type& strides, const std::vector& shape) : p_ptr(reinterpret_cast(ptr)), m_strides(strides.size()) { m_strides.back() = static_cast(strides.back()); for (size_type i = m_strides.size() - 1; i != 0; --i) { size_type j = i - 1; value_type s = static_cast(shape[i]); m_strides[j] = strides[j] + m_strides[i] - strides[i] * s; } } void increment(size_type dim) { p_ptr += m_strides[dim]; } void* data() const { return p_ptr; } private: char* p_ptr; container_type m_strides; }; template class multi_array_iterator { public: using container_type = std::vector; multi_array_iterator(const std::array &buffers, const std::vector &shape) : m_shape(shape.size()), m_index(shape.size(), 0), m_common_iterator() { // Manual copy to avoid conversion warning if using std::copy for (size_t i = 0; i < shape.size(); ++i) m_shape[i] = static_cast(shape[i]); container_type strides(shape.size()); for (size_t i = 0; i < N; ++i) init_common_iterator(buffers[i], shape, m_common_iterator[i], strides); } multi_array_iterator& operator++() { for (size_t j = m_index.size(); j != 0; --j) { size_t i = j - 1; if (++m_index[i] != m_shape[i]) { increment_common_iterator(i); break; } else { m_index[i] = 0; } } return *this; } template const T& data() const { return *reinterpret_cast(m_common_iterator[K].data()); } private: using common_iter = common_iterator; void init_common_iterator(const buffer_info &buffer, const std::vector &shape, common_iter &iterator, container_type &strides) { auto buffer_shape_iter = buffer.shape.rbegin(); auto buffer_strides_iter = buffer.strides.rbegin(); auto shape_iter = shape.rbegin(); auto strides_iter = strides.rbegin(); while (buffer_shape_iter != buffer.shape.rend()) { if (*shape_iter == *buffer_shape_iter) *strides_iter = static_cast(*buffer_strides_iter); else *strides_iter = 0; ++buffer_shape_iter; ++buffer_strides_iter; ++shape_iter; ++strides_iter; } std::fill(strides_iter, strides.rend(), 0); iterator = common_iter(buffer.ptr, strides, shape); } void increment_common_iterator(size_t dim) { for (auto &iter : m_common_iterator) iter.increment(dim); } container_type m_shape; container_type m_index; std::array m_common_iterator; }; template bool broadcast(const std::array& buffers, size_t& ndim, std::vector& shape) { ndim = std::accumulate(buffers.begin(), buffers.end(), size_t(0), [](size_t res, const buffer_info& buf) { return std::max(res, buf.ndim); }); shape = std::vector(ndim, 1); bool trivial_broadcast = true; for (size_t i = 0; i < N; ++i) { auto res_iter = shape.rbegin(); bool i_trivial_broadcast = (buffers[i].size == 1) || (buffers[i].ndim == ndim); for (auto shape_iter = buffers[i].shape.rbegin(); shape_iter != buffers[i].shape.rend(); ++shape_iter, ++res_iter) { if (*res_iter == 1) *res_iter = *shape_iter; else if ((*shape_iter != 1) && (*res_iter != *shape_iter)) pybind11_fail("pybind11::vectorize: incompatible size/dimension of inputs!"); i_trivial_broadcast = i_trivial_broadcast && (*res_iter == *shape_iter); } trivial_broadcast = trivial_broadcast && i_trivial_broadcast; } return trivial_broadcast; } template struct vectorize_helper { typename std::remove_reference::type f; template explicit vectorize_helper(T&&f) : f(std::forward(f)) { } object operator()(array_t... args) { return run(args..., make_index_sequence()); } template object run(array_t&... args, index_sequence index) { /* Request buffers from all parameters */ const size_t N = sizeof...(Args); std::array buffers {{ args.request()... }}; /* Determine dimensions parameters of output array */ size_t ndim = 0; std::vector shape(0); bool trivial_broadcast = broadcast(buffers, ndim, shape); size_t size = 1; std::vector strides(ndim); if (ndim > 0) { strides[ndim-1] = sizeof(Return); for (size_t i = ndim - 1; i > 0; --i) { strides[i - 1] = strides[i] * shape[i]; size *= shape[i]; } size *= shape[0]; } if (size == 1) return cast(f(*((Args *) buffers[Index].ptr)...)); array_t result(shape, strides); auto buf = result.request(); auto output = (Return *) buf.ptr; if (trivial_broadcast) { /* Call the function */ for (size_t i = 0; i < size; ++i) { output[i] = f((buffers[Index].size == 1 ? *((Args *) buffers[Index].ptr) : ((Args *) buffers[Index].ptr)[i])...); } } else { apply_broadcast(buffers, buf, index); } return result; } template void apply_broadcast(const std::array &buffers, buffer_info &output, index_sequence) { using input_iterator = multi_array_iterator; using output_iterator = array_iterator; input_iterator input_iter(buffers, output.shape); output_iterator output_end = array_end(output); for (output_iterator iter = array_begin(output); iter != output_end; ++iter, ++input_iter) { *iter = f((input_iter.template data())...); } } }; template struct handle_type_name> { static PYBIND11_DESCR name() { return _("numpy.ndarray[") + make_caster::name() + _("]"); } }; NAMESPACE_END(detail) template detail::vectorize_helper vectorize(const Func &f, Return (*) (Args ...)) { return detail::vectorize_helper(f); } template detail::vectorize_helper vectorize(Return (*f) (Args ...)) { return vectorize(f, f); } template ::type::operator())>::type> auto vectorize(Func &&f) -> decltype( vectorize(std::forward(f), (FuncType *) nullptr)) { return vectorize(std::forward(f), (FuncType *) nullptr); } NAMESPACE_END(pybind11) #if defined(_MSC_VER) #pragma warning(pop) #endif