2015-07-26 14:33:49 +00:00
|
|
|
/*
|
2016-05-05 18:33:54 +00:00
|
|
|
pybind11/numpy.h: Basic NumPy support, vectorize() wrapper
|
2015-07-26 14:33:49 +00:00
|
|
|
|
2016-04-17 18:21:41 +00:00
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
2015-07-26 14:33:49 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
#include "pybind11.h"
|
|
|
|
#include "complex.h"
|
2016-02-11 09:47:11 +00:00
|
|
|
#include <numeric>
|
|
|
|
#include <algorithm>
|
2016-07-19 23:19:24 +00:00
|
|
|
#include <array>
|
2016-06-19 13:50:06 +00:00
|
|
|
#include <cstdlib>
|
2016-06-19 14:48:55 +00:00
|
|
|
#include <cstring>
|
2016-07-05 23:28:12 +00:00
|
|
|
#include <sstream>
|
2016-08-14 12:45:49 +00:00
|
|
|
#include <string>
|
2016-06-19 14:48:55 +00:00
|
|
|
#include <initializer_list>
|
2016-08-29 01:41:05 +00:00
|
|
|
#include <functional>
|
2015-07-29 15:51:54 +00:00
|
|
|
|
2015-07-26 14:33:49 +00:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
|
|
|
|
#endif
|
|
|
|
|
2016-09-08 20:48:14 +00:00
|
|
|
/* 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");
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
NAMESPACE_BEGIN(pybind11)
|
2016-09-08 20:48:14 +00:00
|
|
|
NAMESPACE_BEGIN(detail)
|
2016-06-26 15:46:40 +00:00
|
|
|
template <typename type, typename SFINAE = void> struct npy_format_descriptor { };
|
2016-07-19 23:26:18 +00:00
|
|
|
template <typename type> struct is_pod_struct;
|
2015-07-28 14:12:20 +00:00
|
|
|
|
2016-08-29 01:41:05 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-07-19 23:54:57 +00:00
|
|
|
struct npy_api {
|
|
|
|
enum constants {
|
|
|
|
NPY_C_CONTIGUOUS_ = 0x0001,
|
|
|
|
NPY_F_CONTIGUOUS_ = 0x0002,
|
2016-08-29 01:41:05 +00:00
|
|
|
NPY_ARRAY_OWNDATA_ = 0x0004,
|
2016-07-19 23:54:57 +00:00
|
|
|
NPY_ARRAY_FORCECAST_ = 0x0010,
|
|
|
|
NPY_ENSURE_ARRAY_ = 0x0040,
|
2016-08-29 01:41:05 +00:00
|
|
|
NPY_ARRAY_ALIGNED_ = 0x0100,
|
|
|
|
NPY_ARRAY_WRITEABLE_ = 0x0400,
|
2016-07-19 23:54:57 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-07-23 20:55:37 +00:00
|
|
|
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_);
|
|
|
|
}
|
2016-07-19 23:54:57 +00:00
|
|
|
|
|
|
|
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_;
|
2016-07-23 20:55:37 +00:00
|
|
|
PyTypeObject *PyArrayDescr_Type_;
|
2016-07-19 23:54:57 +00:00
|
|
|
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 *);
|
|
|
|
private:
|
|
|
|
enum functions {
|
|
|
|
API_PyArray_Type = 2,
|
2016-07-23 20:55:37 +00:00
|
|
|
API_PyArrayDescr_Type = 3,
|
2016-07-19 23:54:57 +00:00
|
|
|
API_PyArray_DescrFromType = 45,
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
static npy_api lookup() {
|
|
|
|
module m = module::import("numpy.core.multiarray");
|
|
|
|
object c = (object) m.attr("_ARRAY_API");
|
2015-09-04 21:42:12 +00:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
2016-07-19 23:54:57 +00:00
|
|
|
void **api_ptr = (void **) (c ? PyCapsule_GetPointer(c.ptr(), NULL) : nullptr);
|
2015-09-04 21:42:12 +00:00
|
|
|
#else
|
2016-07-19 23:54:57 +00:00
|
|
|
void **api_ptr = (void **) (c ? PyCObject_AsVoidPtr(c.ptr()) : nullptr);
|
2015-09-04 21:42:12 +00:00
|
|
|
#endif
|
2016-07-19 23:54:57 +00:00
|
|
|
npy_api api;
|
2016-06-19 13:44:20 +00:00
|
|
|
#define DECL_NPY_API(Func) api.Func##_ = (decltype(api.Func##_)) api_ptr[API_##Func];
|
2016-07-19 23:54:57 +00:00
|
|
|
DECL_NPY_API(PyArray_Type);
|
2016-07-23 20:55:37 +00:00
|
|
|
DECL_NPY_API(PyArrayDescr_Type);
|
2016-07-19 23:54:57 +00:00
|
|
|
DECL_NPY_API(PyArray_DescrFromType);
|
|
|
|
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);
|
2016-06-19 13:44:20 +00:00
|
|
|
#undef DECL_NPY_API
|
2016-07-19 23:54:57 +00:00
|
|
|
return api;
|
|
|
|
}
|
|
|
|
};
|
2016-09-08 20:48:14 +00:00
|
|
|
NAMESPACE_END(detail)
|
2015-07-28 14:12:20 +00:00
|
|
|
|
2016-09-08 20:48:14 +00:00
|
|
|
#define PyArray_GET_(ptr, attr) \
|
|
|
|
(reinterpret_cast<::pybind11::detail::PyArray_Proxy*>(ptr)->attr)
|
|
|
|
#define PyArrayDescr_GET_(ptr, attr) \
|
|
|
|
(reinterpret_cast<::pybind11::detail::PyArrayDescr_Proxy*>(ptr)->attr)
|
2016-08-29 01:41:05 +00:00
|
|
|
#define PyArray_CHKFLAGS_(ptr, flag) \
|
|
|
|
(flag == (reinterpret_cast<::pybind11::detail::PyArray_Proxy*>(ptr)->flags & flag))
|
|
|
|
|
2016-07-23 20:55:37 +00:00
|
|
|
class dtype : public object {
|
2016-07-19 23:54:57 +00:00
|
|
|
public:
|
2016-07-23 20:55:37 +00:00
|
|
|
PYBIND11_OBJECT_DEFAULT(dtype, object, detail::npy_api::get().PyArrayDescr_Check_);
|
2015-07-26 14:33:49 +00:00
|
|
|
|
2016-07-23 20:55:37 +00:00
|
|
|
dtype(const buffer_info &info) {
|
2016-08-13 11:42:02 +00:00
|
|
|
dtype descr(_dtype_from_pep3118()(PYBIND11_STR_TYPE(info.format)));
|
2016-07-23 20:55:37 +00:00
|
|
|
m_ptr = descr.strip_padding().release().ptr();
|
|
|
|
}
|
2016-05-05 08:00:00 +00:00
|
|
|
|
2016-07-23 20:55:37 +00:00
|
|
|
dtype(std::string format) {
|
|
|
|
m_ptr = from_args(pybind11::str(format)).release().ptr();
|
2015-07-26 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 23:58:17 +00:00
|
|
|
dtype(const char *format) : dtype(std::string(format)) { }
|
|
|
|
|
2016-07-24 16:51:35 +00:00
|
|
|
dtype(list names, list formats, list offsets, size_t itemsize) {
|
|
|
|
dict args;
|
|
|
|
args["names"] = names;
|
|
|
|
args["formats"] = formats;
|
|
|
|
args["offsets"] = offsets;
|
2016-08-25 00:16:47 +00:00
|
|
|
args["itemsize"] = pybind11::int_(itemsize);
|
2016-07-24 16:51:35 +00:00
|
|
|
m_ptr = from_args(args).release().ptr();
|
|
|
|
}
|
|
|
|
|
2016-07-23 20:55:37 +00:00
|
|
|
static dtype from_args(object args) {
|
|
|
|
// This is essentially the same as calling np.dtype() constructor in Python
|
|
|
|
PyObject *ptr = nullptr;
|
|
|
|
if (!detail::npy_api::get().PyArray_DescrConverter_(args.release().ptr(), &ptr) || !ptr)
|
|
|
|
pybind11_fail("NumPy: failed to create structured dtype");
|
|
|
|
return object(ptr, false);
|
|
|
|
}
|
2016-07-05 23:28:12 +00:00
|
|
|
|
2016-07-23 20:55:37 +00:00
|
|
|
template <typename T> static dtype of() {
|
2016-08-15 00:24:28 +00:00
|
|
|
return detail::npy_format_descriptor<typename std::remove_cv<T>::type>::dtype();
|
2016-07-23 20:55:37 +00:00
|
|
|
}
|
2016-07-05 23:28:12 +00:00
|
|
|
|
2016-07-23 20:55:37 +00:00
|
|
|
size_t itemsize() const {
|
2016-08-29 01:41:05 +00:00
|
|
|
return (size_t) PyArrayDescr_GET_(m_ptr, elsize);
|
2015-07-26 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 20:55:37 +00:00
|
|
|
bool has_fields() const {
|
2016-08-29 01:41:05 +00:00
|
|
|
return PyArrayDescr_GET_(m_ptr, names) != nullptr;
|
2016-07-23 20:55:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 01:41:05 +00:00
|
|
|
char kind() const {
|
|
|
|
return PyArrayDescr_GET_(m_ptr, kind);
|
2016-07-23 20:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-07-24 16:34:53 +00:00
|
|
|
static object _dtype_from_pep3118() {
|
|
|
|
static PyObject *obj = module::import("numpy.core._internal")
|
|
|
|
.attr("_dtype_from_pep3118").cast<object>().release().ptr();
|
|
|
|
return object(obj, true);
|
2016-07-23 20:55:37 +00:00
|
|
|
}
|
2016-07-05 23:28:12 +00:00
|
|
|
|
2016-07-23 20:55:37 +00:00
|
|
|
dtype strip_padding() {
|
2016-07-05 23:28:12 +00:00
|
|
|
// Recursively strip all void fields with empty names that are generated for
|
|
|
|
// padding fields (as of NumPy v1.11).
|
2016-08-29 01:41:05 +00:00
|
|
|
if (!has_fields())
|
2016-07-23 20:55:37 +00:00
|
|
|
return *this;
|
2016-07-05 23:28:12 +00:00
|
|
|
|
2016-08-25 00:16:47 +00:00
|
|
|
struct field_descr { PYBIND11_STR_TYPE name; object format; pybind11::int_ offset; };
|
2016-07-05 23:28:12 +00:00
|
|
|
std::vector<field_descr> field_descriptors;
|
|
|
|
|
2016-08-29 01:41:05 +00:00
|
|
|
auto fields = attr("fields").cast<object>();
|
2016-07-05 23:28:12 +00:00
|
|
|
auto items = fields.attr("items").cast<object>();
|
|
|
|
for (auto field : items()) {
|
|
|
|
auto spec = object(field, true).cast<tuple>();
|
|
|
|
auto name = spec[0].cast<pybind11::str>();
|
2016-07-23 20:55:37 +00:00
|
|
|
auto format = spec[1].cast<tuple>()[0].cast<dtype>();
|
2016-08-25 00:16:47 +00:00
|
|
|
auto offset = spec[1].cast<tuple>()[1].cast<pybind11::int_>();
|
2016-08-29 01:41:05 +00:00
|
|
|
if (!len(name) && format.kind() == 'V')
|
2016-07-18 21:37:42 +00:00
|
|
|
continue;
|
2016-08-13 11:42:02 +00:00
|
|
|
field_descriptors.push_back({(PYBIND11_STR_TYPE) name, format.strip_padding(), offset});
|
2016-07-05 23:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(field_descriptors.begin(), field_descriptors.end(),
|
|
|
|
[](const field_descr& a, const field_descr& b) {
|
2016-08-25 20:52:52 +00:00
|
|
|
return a.offset.cast<int>() < b.offset.cast<int>();
|
2016-07-05 23:28:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
list names, formats, offsets;
|
|
|
|
for (auto& descr : field_descriptors) {
|
2016-07-24 16:51:35 +00:00
|
|
|
names.append(descr.name);
|
|
|
|
formats.append(descr.format);
|
|
|
|
offsets.append(descr.offset);
|
2016-07-05 23:28:12 +00:00
|
|
|
}
|
2016-07-24 16:51:35 +00:00
|
|
|
return dtype(names, formats, offsets, itemsize());
|
2016-07-23 20:55:37 +00:00
|
|
|
}
|
|
|
|
};
|
2016-07-05 23:28:12 +00:00
|
|
|
|
2016-07-23 20:55:37 +00:00
|
|
|
class array : public buffer {
|
|
|
|
public:
|
|
|
|
PYBIND11_OBJECT_DEFAULT(array, buffer, detail::npy_api::get().PyArray_Check_)
|
|
|
|
|
|
|
|
enum {
|
|
|
|
c_style = detail::npy_api::NPY_C_CONTIGUOUS_,
|
|
|
|
f_style = detail::npy_api::NPY_F_CONTIGUOUS_,
|
|
|
|
forcecast = detail::npy_api::NPY_ARRAY_FORCECAST_
|
|
|
|
};
|
|
|
|
|
2016-07-24 17:35:14 +00:00
|
|
|
array(const pybind11::dtype& dt, const std::vector<size_t>& shape,
|
2016-09-08 20:48:14 +00:00
|
|
|
const std::vector<size_t>& strides, const void *ptr = nullptr) {
|
2016-07-23 20:55:37 +00:00
|
|
|
auto& api = detail::npy_api::get();
|
2016-07-24 17:35:14 +00:00
|
|
|
auto ndim = shape.size();
|
|
|
|
if (shape.size() != strides.size())
|
|
|
|
pybind11_fail("NumPy: shape ndim doesn't match strides ndim");
|
|
|
|
auto descr = dt;
|
|
|
|
object tmp(api.PyArray_NewFromDescr_(
|
|
|
|
api.PyArray_Type_, descr.release().ptr(), (int) ndim, (Py_intptr_t *) shape.data(),
|
2016-09-08 20:48:14 +00:00
|
|
|
(Py_intptr_t *) strides.data(), const_cast<void *>(ptr), 0, nullptr), false);
|
2016-07-23 20:55:37 +00:00
|
|
|
if (!tmp)
|
|
|
|
pybind11_fail("NumPy: unable to create array!");
|
|
|
|
if (ptr)
|
|
|
|
tmp = object(api.PyArray_NewCopy_(tmp.ptr(), -1 /* any order */), false);
|
|
|
|
m_ptr = tmp.release().ptr();
|
|
|
|
}
|
|
|
|
|
2016-09-08 20:48:14 +00:00
|
|
|
array(const pybind11::dtype& dt, const std::vector<size_t>& shape, const void *ptr = nullptr)
|
2016-07-24 23:46:39 +00:00
|
|
|
: array(dt, shape, default_strides(shape, dt.itemsize()), ptr) { }
|
2016-07-24 17:35:14 +00:00
|
|
|
|
2016-09-08 20:48:14 +00:00
|
|
|
array(const pybind11::dtype& dt, size_t count, const void *ptr = nullptr)
|
2016-08-27 11:09:02 +00:00
|
|
|
: array(dt, std::vector<size_t> { count }, ptr) { }
|
2016-07-24 17:35:14 +00:00
|
|
|
|
|
|
|
template<typename T> array(const std::vector<size_t>& shape,
|
2016-09-08 20:48:14 +00:00
|
|
|
const std::vector<size_t>& strides, const T* ptr)
|
2016-07-24 23:46:39 +00:00
|
|
|
: array(pybind11::dtype::of<T>(), shape, strides, (void *) ptr) { }
|
2016-07-24 17:35:14 +00:00
|
|
|
|
2016-09-08 20:48:14 +00:00
|
|
|
template<typename T> array(const std::vector<size_t>& shape, const T* ptr)
|
2016-07-24 23:46:39 +00:00
|
|
|
: array(shape, default_strides(shape, sizeof(T)), ptr) { }
|
2016-07-24 17:35:14 +00:00
|
|
|
|
2016-09-08 20:48:14 +00:00
|
|
|
template<typename T> array(size_t count, const T* ptr)
|
2016-08-29 01:41:05 +00:00
|
|
|
: array(std::vector<size_t> { count }, ptr) { }
|
2016-07-24 17:35:14 +00:00
|
|
|
|
|
|
|
array(const buffer_info &info)
|
2016-07-24 23:46:39 +00:00
|
|
|
: array(pybind11::dtype(info), info.shape, info.strides, info.ptr) { }
|
2016-07-23 20:55:37 +00:00
|
|
|
|
2016-08-29 01:41:05 +00:00
|
|
|
/// Array descriptor (dtype)
|
|
|
|
pybind11::dtype dtype() const {
|
|
|
|
return object(PyArray_GET_(m_ptr, descr), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Total number of elements
|
|
|
|
size_t size() const {
|
|
|
|
return std::accumulate(shape(), shape() + ndim(), (size_t) 1, std::multiplies<size_t>());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Byte size of a single element
|
|
|
|
size_t itemsize() const {
|
|
|
|
return (size_t) PyArrayDescr_GET_(PyArray_GET_(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) PyArray_GET_(m_ptr, nd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Dimensions of the array
|
|
|
|
const size_t* shape() const {
|
|
|
|
return reinterpret_cast<const size_t *>(PyArray_GET_(m_ptr, dimensions));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Dimension along a given axis
|
|
|
|
size_t shape(size_t dim) const {
|
|
|
|
if (dim >= ndim())
|
2016-09-08 20:48:14 +00:00
|
|
|
fail_dim_check(dim, "invalid axis");
|
2016-08-29 01:41:05 +00:00
|
|
|
return shape()[dim];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Strides of the array
|
|
|
|
const size_t* strides() const {
|
|
|
|
return reinterpret_cast<const size_t *>(PyArray_GET_(m_ptr, strides));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stride along a given axis
|
|
|
|
size_t strides(size_t dim) const {
|
|
|
|
if (dim >= ndim())
|
2016-09-08 20:48:14 +00:00
|
|
|
fail_dim_check(dim, "invalid axis");
|
2016-08-29 01:41:05 +00:00
|
|
|
return strides()[dim];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If set, the array is writeable (otherwise the buffer is read-only)
|
|
|
|
bool writeable() const {
|
|
|
|
return PyArray_CHKFLAGS_(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 PyArray_CHKFLAGS_(m_ptr, detail::npy_api::NPY_ARRAY_OWNDATA_);
|
|
|
|
}
|
|
|
|
|
2016-09-08 20:48:14 +00:00
|
|
|
/// 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<typename... Ix> const void* data(Ix&&... index) const {
|
|
|
|
return static_cast<const void *>(PyArray_GET_(m_ptr, data) + offset_at(index...));
|
2016-08-29 01:41:05 +00:00
|
|
|
}
|
|
|
|
|
2016-09-08 20:48:14 +00:00
|
|
|
/// 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<typename... Ix> void* mutable_data(Ix&&... index) {
|
|
|
|
check_writeable();
|
|
|
|
return static_cast<void *>(PyArray_GET_(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<typename... Ix> size_t offset_at(Ix&&... index) const {
|
|
|
|
if (sizeof...(index) > ndim())
|
|
|
|
fail_dim_check(sizeof...(index), "too many indices for an array");
|
|
|
|
return get_byte_offset(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<typename... Ix> size_t index_at(Ix&&... index) const {
|
|
|
|
return offset_at(index...) / itemsize();
|
2016-07-23 20:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2016-09-08 20:48:14 +00:00
|
|
|
template<typename, typename> 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<typename... Ix> size_t get_byte_offset(Ix&&... index) const {
|
|
|
|
const size_t idx[] = { (size_t) index... };
|
|
|
|
if (!std::equal(idx + 0, idx + sizeof...(index), shape(), std::less<size_t>{})) {
|
|
|
|
auto mismatch = std::mismatch(idx + 0, idx + sizeof...(index), shape(), std::less<size_t>{});
|
|
|
|
throw index_error(std::string("index ") + std::to_string(*mismatch.first) +
|
|
|
|
" is out of bounds for axis " + std::to_string(mismatch.first - idx) +
|
|
|
|
" with size " + std::to_string(*mismatch.second));
|
|
|
|
}
|
|
|
|
return std::inner_product(idx + 0, idx + sizeof...(index), strides(), (size_t) 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t get_byte_offset() const { return 0; }
|
|
|
|
|
|
|
|
void check_writeable() const {
|
|
|
|
if (!writeable())
|
|
|
|
throw std::runtime_error("array is not writeable");
|
|
|
|
}
|
2016-07-24 17:35:14 +00:00
|
|
|
|
|
|
|
static std::vector<size_t> default_strides(const std::vector<size_t>& shape, size_t itemsize) {
|
|
|
|
auto ndim = shape.size();
|
|
|
|
std::vector<size_t> 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;
|
|
|
|
}
|
2015-07-26 14:33:49 +00:00
|
|
|
};
|
|
|
|
|
2016-05-19 14:02:09 +00:00
|
|
|
template <typename T, int ExtraFlags = array::forcecast> class array_t : public array {
|
2015-07-26 14:33:49 +00:00
|
|
|
public:
|
2015-10-18 14:48:30 +00:00
|
|
|
PYBIND11_OBJECT_CVT(array_t, array, is_non_null, m_ptr = ensure(m_ptr));
|
2016-07-24 17:54:53 +00:00
|
|
|
|
2015-10-13 15:38:22 +00:00
|
|
|
array_t() : array() { }
|
2016-07-24 17:54:53 +00:00
|
|
|
|
|
|
|
array_t(const buffer_info& info) : array(info) { }
|
|
|
|
|
2016-09-08 20:48:14 +00:00
|
|
|
array_t(const std::vector<size_t>& shape, const std::vector<size_t>& strides, const T* ptr = nullptr)
|
2016-07-24 23:46:39 +00:00
|
|
|
: array(shape, strides, ptr) { }
|
2016-07-24 17:54:53 +00:00
|
|
|
|
2016-09-08 20:48:14 +00:00
|
|
|
array_t(const std::vector<size_t>& shape, const T* ptr = nullptr)
|
2016-07-24 17:54:53 +00:00
|
|
|
: array(shape, ptr) { }
|
|
|
|
|
2016-09-08 20:48:14 +00:00
|
|
|
array_t(size_t count, const T* ptr = nullptr)
|
2016-08-29 01:41:05 +00:00
|
|
|
: array(count, ptr) { }
|
|
|
|
|
2016-09-08 20:48:14 +00:00
|
|
|
constexpr size_t itemsize() const {
|
|
|
|
return sizeof(T);
|
2016-08-29 01:41:05 +00:00
|
|
|
}
|
|
|
|
|
2016-09-08 20:48:14 +00:00
|
|
|
template<typename... Ix> size_t index_at(Ix&... index) const {
|
|
|
|
return offset_at(index...) / itemsize();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Ix> const T* data(Ix&&... index) const {
|
|
|
|
return static_cast<const T*>(array::data(index...));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Ix> T* mutable_data(Ix&&... index) {
|
|
|
|
return static_cast<T*>(array::mutable_data(index...));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reference to element at a given index
|
|
|
|
template<typename... Ix> const T& at(Ix&&... index) const {
|
|
|
|
if (sizeof...(index) != ndim())
|
|
|
|
fail_dim_check(sizeof...(index), "index dimension mismatch");
|
|
|
|
// not using offset_at() / index_at() here so as to avoid another dimension check
|
|
|
|
return *(static_cast<const T*>(array::data()) + get_byte_offset(index...) / itemsize());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutable reference to element at a given index
|
|
|
|
template<typename... Ix> T& mutable_at(Ix&&... index) {
|
|
|
|
if (sizeof...(index) != ndim())
|
|
|
|
fail_dim_check(sizeof...(index), "index dimension mismatch");
|
|
|
|
// not using offset_at() / index_at() here so as to avoid another dimension check
|
|
|
|
return *(static_cast<T*>(array::mutable_data()) + get_byte_offset(index...) / itemsize());
|
2016-08-29 01:41:05 +00:00
|
|
|
}
|
2016-07-24 17:54:53 +00:00
|
|
|
|
2015-07-26 14:33:49 +00:00
|
|
|
static bool is_non_null(PyObject *ptr) { return ptr != nullptr; }
|
2016-07-24 23:46:39 +00:00
|
|
|
|
2015-12-28 07:11:16 +00:00
|
|
|
static PyObject *ensure(PyObject *ptr) {
|
2015-07-30 13:29:00 +00:00
|
|
|
if (ptr == nullptr)
|
|
|
|
return nullptr;
|
2016-07-19 23:54:57 +00:00
|
|
|
auto& api = detail::npy_api::get();
|
2016-07-23 20:55:37 +00:00
|
|
|
PyObject *result = api.PyArray_FromAny_(ptr, pybind11::dtype::of<T>().release().ptr(), 0, 0,
|
2016-07-19 23:54:57 +00:00
|
|
|
detail::npy_api::NPY_ENSURE_ARRAY_ | ExtraFlags, nullptr);
|
2016-05-19 14:02:09 +00:00
|
|
|
if (!result)
|
|
|
|
PyErr_Clear();
|
2016-01-17 21:36:41 +00:00
|
|
|
Py_DECREF(ptr);
|
|
|
|
return result;
|
2015-07-26 14:33:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-26 15:46:40 +00:00
|
|
|
template <typename T>
|
|
|
|
struct format_descriptor<T, typename std::enable_if<detail::is_pod_struct<T>::value>::type> {
|
2016-08-15 00:24:28 +00:00
|
|
|
static std::string format() {
|
|
|
|
return detail::npy_format_descriptor<typename std::remove_cv<T>::type>::format();
|
|
|
|
}
|
2016-07-19 23:19:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <size_t N> struct format_descriptor<char[N]> {
|
2016-08-14 12:45:49 +00:00
|
|
|
static std::string format() { return std::to_string(N) + "s"; }
|
2016-07-19 23:19:24 +00:00
|
|
|
};
|
|
|
|
template <size_t N> struct format_descriptor<std::array<char, N>> {
|
2016-08-14 12:45:49 +00:00
|
|
|
static std::string format() { return std::to_string(N) + "s"; }
|
2016-06-19 14:48:55 +00:00
|
|
|
};
|
|
|
|
|
2016-05-05 18:33:54 +00:00
|
|
|
NAMESPACE_BEGIN(detail)
|
2016-07-19 23:26:18 +00:00
|
|
|
template <typename T> struct is_std_array : std::false_type { };
|
|
|
|
template <typename T, size_t N> struct is_std_array<std::array<T, N>> : std::true_type { };
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct is_pod_struct {
|
|
|
|
enum { value = std::is_pod<T>::value && // offsetof only works correctly for POD types
|
2016-08-15 00:23:59 +00:00
|
|
|
!std::is_reference<T>::value &&
|
2016-07-19 23:26:18 +00:00
|
|
|
!std::is_array<T>::value &&
|
|
|
|
!is_std_array<T>::value &&
|
|
|
|
!std::is_integral<T>::value &&
|
2016-08-15 00:23:59 +00:00
|
|
|
!std::is_same<typename std::remove_cv<T>::type, float>::value &&
|
|
|
|
!std::is_same<typename std::remove_cv<T>::type, double>::value &&
|
|
|
|
!std::is_same<typename std::remove_cv<T>::type, bool>::value &&
|
|
|
|
!std::is_same<typename std::remove_cv<T>::type, std::complex<float>>::value &&
|
|
|
|
!std::is_same<typename std::remove_cv<T>::type, std::complex<double>>::value };
|
2016-07-19 23:26:18 +00:00
|
|
|
};
|
2016-05-05 18:33:54 +00:00
|
|
|
|
2016-05-04 20:22:48 +00:00
|
|
|
template <typename T> struct npy_format_descriptor<T, typename std::enable_if<std::is_integral<T>::value>::type> {
|
|
|
|
private:
|
2016-05-11 12:45:01 +00:00
|
|
|
constexpr static const int values[8] = {
|
2016-07-19 23:54:57 +00:00
|
|
|
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_ };
|
2016-05-04 20:22:48 +00:00
|
|
|
public:
|
2016-06-26 15:19:18 +00:00
|
|
|
enum { value = values[detail::log2(sizeof(T)) * 2 + (std::is_unsigned<T>::value ? 1 : 0)] };
|
2016-07-23 20:55:37 +00:00
|
|
|
static pybind11::dtype dtype() {
|
2016-07-19 23:54:57 +00:00
|
|
|
if (auto ptr = npy_api::get().PyArray_DescrFromType_(value))
|
2016-06-26 15:19:18 +00:00
|
|
|
return object(ptr, true);
|
|
|
|
pybind11_fail("Unsupported buffer format!");
|
2016-06-19 13:53:20 +00:00
|
|
|
}
|
2016-05-05 18:33:54 +00:00
|
|
|
template <typename T2 = T, typename std::enable_if<std::is_signed<T2>::value, int>::type = 0>
|
|
|
|
static PYBIND11_DESCR name() { return _("int") + _<sizeof(T)*8>(); }
|
|
|
|
template <typename T2 = T, typename std::enable_if<!std::is_signed<T2>::value, int>::type = 0>
|
|
|
|
static PYBIND11_DESCR name() { return _("uint") + _<sizeof(T)*8>(); }
|
2016-05-04 20:22:48 +00:00
|
|
|
};
|
|
|
|
template <typename T> constexpr const int npy_format_descriptor<
|
|
|
|
T, typename std::enable_if<std::is_integral<T>::value>::type>::values[8];
|
|
|
|
|
2016-05-05 18:33:54 +00:00
|
|
|
#define DECL_FMT(Type, NumPyName, Name) template<> struct npy_format_descriptor<Type> { \
|
2016-07-19 23:54:57 +00:00
|
|
|
enum { value = npy_api::NumPyName }; \
|
2016-07-23 20:55:37 +00:00
|
|
|
static pybind11::dtype dtype() { \
|
2016-07-19 23:54:57 +00:00
|
|
|
if (auto ptr = npy_api::get().PyArray_DescrFromType_(value)) \
|
2016-06-26 15:19:18 +00:00
|
|
|
return object(ptr, true); \
|
|
|
|
pybind11_fail("Unsupported buffer format!"); \
|
2016-06-19 13:53:20 +00:00
|
|
|
} \
|
2016-05-05 18:33:54 +00:00
|
|
|
static PYBIND11_DESCR name() { return _(Name); } }
|
2016-06-17 23:46:12 +00:00
|
|
|
DECL_FMT(float, NPY_FLOAT_, "float32");
|
|
|
|
DECL_FMT(double, NPY_DOUBLE_, "float64");
|
|
|
|
DECL_FMT(bool, NPY_BOOL_, "bool");
|
|
|
|
DECL_FMT(std::complex<float>, NPY_CFLOAT_, "complex64");
|
|
|
|
DECL_FMT(std::complex<double>, NPY_CDOUBLE_, "complex128");
|
2015-07-29 15:51:54 +00:00
|
|
|
#undef DECL_FMT
|
|
|
|
|
2016-07-19 23:19:24 +00:00
|
|
|
#define DECL_CHAR_FMT \
|
|
|
|
static PYBIND11_DESCR name() { return _("S") + _<N>(); } \
|
2016-08-14 12:45:49 +00:00
|
|
|
static pybind11::dtype dtype() { return std::string("S") + std::to_string(N); }
|
2016-07-19 23:19:24 +00:00
|
|
|
template <size_t N> struct npy_format_descriptor<char[N]> { DECL_CHAR_FMT };
|
|
|
|
template <size_t N> struct npy_format_descriptor<std::array<char, N>> { DECL_CHAR_FMT };
|
|
|
|
#undef DECL_CHAR_FMT
|
|
|
|
|
2016-06-19 14:48:55 +00:00
|
|
|
struct field_descriptor {
|
|
|
|
const char *name;
|
2016-07-03 09:22:10 +00:00
|
|
|
size_t offset;
|
2016-07-05 23:28:12 +00:00
|
|
|
size_t size;
|
2016-08-14 12:45:49 +00:00
|
|
|
std::string format;
|
2016-07-23 20:55:37 +00:00
|
|
|
dtype descr;
|
2016-06-19 14:48:55 +00:00
|
|
|
};
|
|
|
|
|
2016-06-26 15:46:40 +00:00
|
|
|
template <typename T>
|
2016-06-27 14:47:51 +00:00
|
|
|
struct npy_format_descriptor<T, typename std::enable_if<is_pod_struct<T>::value>::type> {
|
2016-07-18 18:58:20 +00:00
|
|
|
static PYBIND11_DESCR name() { return _("struct"); }
|
2016-06-19 14:48:55 +00:00
|
|
|
|
2016-07-23 20:55:37 +00:00
|
|
|
static pybind11::dtype dtype() {
|
2016-08-14 12:45:49 +00:00
|
|
|
if (!dtype_ptr)
|
2016-06-19 14:48:55 +00:00
|
|
|
pybind11_fail("NumPy: unsupported buffer format!");
|
2016-08-14 12:45:49 +00:00
|
|
|
return object(dtype_ptr, true);
|
2016-06-19 14:48:55 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 12:45:49 +00:00
|
|
|
static std::string format() {
|
|
|
|
if (!dtype_ptr)
|
2016-06-26 15:34:39 +00:00
|
|
|
pybind11_fail("NumPy: unsupported buffer format!");
|
2016-08-14 12:45:49 +00:00
|
|
|
return format_str;
|
2016-06-19 14:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void register_dtype(std::initializer_list<field_descriptor> fields) {
|
2016-07-24 16:51:35 +00:00
|
|
|
list names, formats, offsets;
|
2016-06-19 14:48:55 +00:00
|
|
|
for (auto field : fields) {
|
|
|
|
if (!field.descr)
|
|
|
|
pybind11_fail("NumPy: unsupported field dtype");
|
2016-08-13 11:42:02 +00:00
|
|
|
names.append(PYBIND11_STR_TYPE(field.name));
|
2016-06-21 23:42:10 +00:00
|
|
|
formats.append(field.descr);
|
2016-08-24 23:33:02 +00:00
|
|
|
offsets.append(pybind11::int_(field.offset));
|
2016-06-19 14:48:55 +00:00
|
|
|
}
|
2016-08-14 12:45:49 +00:00
|
|
|
dtype_ptr = pybind11::dtype(names, formats, offsets, sizeof(T)).release().ptr();
|
2016-07-05 23:28:12 +00:00
|
|
|
|
|
|
|
// 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<field_descriptor> 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';
|
|
|
|
// note that '=' is required to cover the case of unaligned fields
|
|
|
|
oss << '=' << field.format << ':' << field.name << ':';
|
|
|
|
offset = field.offset + field.size;
|
2016-06-29 14:16:49 +00:00
|
|
|
}
|
2016-07-05 23:28:12 +00:00
|
|
|
if (sizeof(T) > offset)
|
|
|
|
oss << (sizeof(T) - offset) << 'x';
|
|
|
|
oss << '}';
|
2016-08-14 12:45:49 +00:00
|
|
|
format_str = oss.str();
|
2016-07-05 23:28:12 +00:00
|
|
|
|
|
|
|
// Sanity check: verify that NumPy properly parses our buffer format string
|
2016-07-23 20:55:37 +00:00
|
|
|
auto& api = npy_api::get();
|
2016-08-14 12:45:49 +00:00
|
|
|
auto arr = array(buffer_info(nullptr, sizeof(T), format(), 1));
|
|
|
|
if (!api.PyArray_EquivTypes_(dtype_ptr, arr.dtype().ptr()))
|
2016-07-17 10:10:19 +00:00
|
|
|
pybind11_fail("NumPy: invalid buffer descriptor!");
|
2016-06-19 14:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-08-14 12:45:49 +00:00
|
|
|
static std::string format_str;
|
|
|
|
static PyObject* dtype_ptr;
|
2016-06-19 14:48:55 +00:00
|
|
|
};
|
|
|
|
|
2016-08-14 12:45:49 +00:00
|
|
|
template <typename T>
|
|
|
|
std::string npy_format_descriptor<T, typename std::enable_if<is_pod_struct<T>::value>::type>::format_str;
|
|
|
|
template <typename T>
|
|
|
|
PyObject* npy_format_descriptor<T, typename std::enable_if<is_pod_struct<T>::value>::type>::dtype_ptr = nullptr;
|
|
|
|
|
2016-06-29 14:21:51 +00:00
|
|
|
// Extract name, offset and format descriptor for a struct field
|
2016-06-27 22:02:21 +00:00
|
|
|
#define PYBIND11_FIELD_DESCRIPTOR(Type, Field) \
|
2016-06-19 14:48:55 +00:00
|
|
|
::pybind11::detail::field_descriptor { \
|
2016-07-05 23:28:12 +00:00
|
|
|
#Field, offsetof(Type, Field), sizeof(decltype(static_cast<Type*>(0)->Field)), \
|
|
|
|
::pybind11::format_descriptor<decltype(static_cast<Type*>(0)->Field)>::format(), \
|
2016-06-26 15:19:18 +00:00
|
|
|
::pybind11::detail::npy_format_descriptor<decltype(static_cast<Type*>(0)->Field)>::dtype() \
|
2016-06-21 23:42:10 +00:00
|
|
|
}
|
2016-06-19 14:48:55 +00:00
|
|
|
|
|
|
|
// The main idea of this macro is borrowed from https://github.com/swansontec/map-macro
|
|
|
|
// (C) William Swanson, Paul Fultz
|
2016-06-27 22:02:21 +00:00
|
|
|
#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)
|
2016-06-29 14:21:51 +00:00
|
|
|
#ifdef _MSC_VER // MSVC is not as eager to expand macros, hence this workaround
|
2016-06-27 22:02:21 +00:00
|
|
|
#define PYBIND11_MAP_LIST_NEXT1(test, next) \
|
|
|
|
PYBIND11_EVAL0 (PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0))
|
2016-06-27 16:01:22 +00:00
|
|
|
#else
|
2016-06-27 22:02:21 +00:00
|
|
|
#define PYBIND11_MAP_LIST_NEXT1(test, next) \
|
|
|
|
PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0)
|
2016-06-27 16:01:22 +00:00
|
|
|
#endif
|
2016-06-27 22:02:21 +00:00
|
|
|
#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__)
|
2016-06-29 14:21:51 +00:00
|
|
|
// PYBIND11_MAP_LIST(f, t, a1, a2, ...) expands to f(t, a1), f(t, a2), ...
|
2016-06-27 22:02:21 +00:00
|
|
|
#define PYBIND11_MAP_LIST(f, t, ...) \
|
|
|
|
PYBIND11_EVAL (PYBIND11_MAP_LIST1 (f, t, __VA_ARGS__, (), 0))
|
2016-06-19 14:48:55 +00:00
|
|
|
|
2016-07-02 15:18:42 +00:00
|
|
|
#define PYBIND11_NUMPY_DTYPE(Type, ...) \
|
2016-06-21 23:48:36 +00:00
|
|
|
::pybind11::detail::npy_format_descriptor<Type>::register_dtype \
|
2016-06-27 22:02:21 +00:00
|
|
|
({PYBIND11_MAP_LIST (PYBIND11_FIELD_DESCRIPTOR, Type, __VA_ARGS__)})
|
2016-06-19 14:48:55 +00:00
|
|
|
|
2016-02-11 09:47:11 +00:00
|
|
|
template <class T>
|
|
|
|
using array_iterator = typename std::add_pointer<T>::type;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
array_iterator<T> array_begin(const buffer_info& buffer) {
|
|
|
|
return array_iterator<T>(reinterpret_cast<T*>(buffer.ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
array_iterator<T> array_end(const buffer_info& buffer) {
|
|
|
|
return array_iterator<T>(reinterpret_cast<T*>(buffer.ptr) + buffer.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
class common_iterator {
|
|
|
|
public:
|
|
|
|
using container_type = std::vector<size_t>;
|
|
|
|
using value_type = container_type::value_type;
|
|
|
|
using size_type = container_type::size_type;
|
|
|
|
|
|
|
|
common_iterator() : p_ptr(0), m_strides() {}
|
2016-02-20 11:17:17 +00:00
|
|
|
|
2016-02-11 09:47:11 +00:00
|
|
|
common_iterator(void* ptr, const container_type& strides, const std::vector<size_t>& shape)
|
|
|
|
: p_ptr(reinterpret_cast<char*>(ptr)), m_strides(strides.size()) {
|
|
|
|
m_strides.back() = static_cast<value_type>(strides.back());
|
|
|
|
for (size_type i = m_strides.size() - 1; i != 0; --i) {
|
|
|
|
size_type j = i - 1;
|
|
|
|
value_type s = static_cast<value_type>(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;
|
|
|
|
};
|
|
|
|
|
2016-02-20 11:17:17 +00:00
|
|
|
template <size_t N> class multi_array_iterator {
|
2016-02-11 09:47:11 +00:00
|
|
|
public:
|
|
|
|
using container_type = std::vector<size_t>;
|
|
|
|
|
2016-02-20 11:17:17 +00:00
|
|
|
multi_array_iterator(const std::array<buffer_info, N> &buffers,
|
|
|
|
const std::vector<size_t> &shape)
|
|
|
|
: m_shape(shape.size()), m_index(shape.size(), 0),
|
|
|
|
m_common_iterator() {
|
|
|
|
|
2016-02-11 09:47:11 +00:00
|
|
|
// Manual copy to avoid conversion warning if using std::copy
|
2016-02-20 11:17:17 +00:00
|
|
|
for (size_t i = 0; i < shape.size(); ++i)
|
2016-02-11 09:47:11 +00:00
|
|
|
m_shape[i] = static_cast<container_type::value_type>(shape[i]);
|
|
|
|
|
|
|
|
container_type strides(shape.size());
|
2016-02-20 11:17:17 +00:00
|
|
|
for (size_t i = 0; i < N; ++i)
|
2016-02-11 09:47:11 +00:00
|
|
|
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;
|
2016-02-20 11:17:17 +00:00
|
|
|
} else {
|
2016-02-11 09:47:11 +00:00
|
|
|
m_index[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-02-20 11:17:17 +00:00
|
|
|
template <size_t K, class T> const T& data() const {
|
2016-02-11 09:47:11 +00:00
|
|
|
return *reinterpret_cast<T*>(m_common_iterator[K].data());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
using common_iter = common_iterator;
|
|
|
|
|
2016-02-20 11:17:17 +00:00
|
|
|
void init_common_iterator(const buffer_info &buffer,
|
|
|
|
const std::vector<size_t> &shape,
|
|
|
|
common_iter &iterator, container_type &strides) {
|
2016-02-11 09:47:11 +00:00
|
|
|
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)
|
2016-05-29 11:40:40 +00:00
|
|
|
*strides_iter = static_cast<size_t>(*buffer_strides_iter);
|
2016-02-11 09:47:11 +00:00
|
|
|
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) {
|
2016-02-20 11:17:17 +00:00
|
|
|
for (auto &iter : m_common_iterator)
|
2016-02-11 09:47:11 +00:00
|
|
|
iter.increment(dim);
|
|
|
|
}
|
|
|
|
|
|
|
|
container_type m_shape;
|
|
|
|
container_type m_index;
|
|
|
|
std::array<common_iter, N> m_common_iterator;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <size_t N>
|
2016-05-29 11:40:40 +00:00
|
|
|
bool broadcast(const std::array<buffer_info, N>& buffers, size_t& ndim, std::vector<size_t>& shape) {
|
|
|
|
ndim = std::accumulate(buffers.begin(), buffers.end(), size_t(0), [](size_t res, const buffer_info& buf) {
|
2016-02-11 09:47:11 +00:00
|
|
|
return std::max(res, buf.ndim);
|
|
|
|
});
|
|
|
|
|
2016-05-29 11:40:40 +00:00
|
|
|
shape = std::vector<size_t>(ndim, 1);
|
2016-02-11 09:47:11 +00:00
|
|
|
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);
|
2016-02-20 11:17:17 +00:00
|
|
|
for (auto shape_iter = buffers[i].shape.rbegin();
|
|
|
|
shape_iter != buffers[i].shape.rend(); ++shape_iter, ++res_iter) {
|
|
|
|
|
|
|
|
if (*res_iter == 1)
|
2016-02-11 09:47:11 +00:00
|
|
|
*res_iter = *shape_iter;
|
2016-02-20 11:17:17 +00:00
|
|
|
else if ((*shape_iter != 1) && (*res_iter != *shape_iter))
|
2016-02-11 09:47:11 +00:00
|
|
|
pybind11_fail("pybind11::vectorize: incompatible size/dimension of inputs!");
|
2016-02-20 11:17:17 +00:00
|
|
|
|
2016-02-11 09:47:11 +00:00
|
|
|
i_trivial_broadcast = i_trivial_broadcast && (*res_iter == *shape_iter);
|
|
|
|
}
|
|
|
|
trivial_broadcast = trivial_broadcast && i_trivial_broadcast;
|
|
|
|
}
|
|
|
|
return trivial_broadcast;
|
|
|
|
}
|
|
|
|
|
2015-07-29 15:51:54 +00:00
|
|
|
template <typename Func, typename Return, typename... Args>
|
|
|
|
struct vectorize_helper {
|
|
|
|
typename std::remove_reference<Func>::type f;
|
|
|
|
|
2015-07-30 13:29:00 +00:00
|
|
|
template <typename T>
|
|
|
|
vectorize_helper(T&&f) : f(std::forward<T>(f)) { }
|
2015-07-28 14:12:20 +00:00
|
|
|
|
2016-05-19 14:02:09 +00:00
|
|
|
object operator()(array_t<Args, array::c_style | array::forcecast>... args) {
|
2015-07-29 15:51:54 +00:00
|
|
|
return run(args..., typename make_index_sequence<sizeof...(Args)>::type());
|
|
|
|
}
|
2015-07-26 14:33:49 +00:00
|
|
|
|
2016-05-19 14:02:09 +00:00
|
|
|
template <size_t ... Index> object run(array_t<Args, array::c_style | array::forcecast>&... args, index_sequence<Index...> index) {
|
2015-07-26 14:33:49 +00:00
|
|
|
/* Request buffers from all parameters */
|
2015-07-29 15:51:54 +00:00
|
|
|
const size_t N = sizeof...(Args);
|
2016-02-11 09:47:11 +00:00
|
|
|
|
2015-07-26 14:33:49 +00:00
|
|
|
std::array<buffer_info, N> buffers {{ args.request()... }};
|
|
|
|
|
|
|
|
/* Determine dimensions parameters of output array */
|
2016-05-29 11:40:40 +00:00
|
|
|
size_t ndim = 0;
|
2016-02-11 09:47:11 +00:00
|
|
|
std::vector<size_t> shape(0);
|
|
|
|
bool trivial_broadcast = broadcast(buffers, ndim, shape);
|
2016-05-19 14:02:09 +00:00
|
|
|
|
2016-02-11 09:47:11 +00:00
|
|
|
size_t size = 1;
|
2015-07-26 14:33:49 +00:00
|
|
|
std::vector<size_t> strides(ndim);
|
|
|
|
if (ndim > 0) {
|
2015-07-29 15:51:54 +00:00
|
|
|
strides[ndim-1] = sizeof(Return);
|
2016-05-29 11:40:40 +00:00
|
|
|
for (size_t i = ndim - 1; i > 0; --i) {
|
2016-02-11 09:47:11 +00:00
|
|
|
strides[i - 1] = strides[i] * shape[i];
|
|
|
|
size *= shape[i];
|
|
|
|
}
|
|
|
|
size *= shape[0];
|
2015-07-26 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 21:36:40 +00:00
|
|
|
if (size == 1)
|
2016-01-17 21:36:39 +00:00
|
|
|
return cast(f(*((Args *) buffers[Index].ptr)...));
|
2015-07-26 14:33:49 +00:00
|
|
|
|
2016-08-29 01:41:05 +00:00
|
|
|
array_t<Return> result(shape, strides);
|
|
|
|
auto buf = result.request();
|
|
|
|
auto output = (Return *) buf.ptr;
|
2016-01-17 21:36:39 +00:00
|
|
|
|
2016-05-05 18:33:54 +00:00
|
|
|
if (trivial_broadcast) {
|
2016-02-11 09:47:11 +00:00
|
|
|
/* Call the function */
|
2016-08-29 01:41:05 +00:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
2016-02-11 09:47:11 +00:00
|
|
|
output[i] = f((buffers[Index].size == 1
|
2016-01-17 21:36:39 +00:00
|
|
|
? *((Args *) buffers[Index].ptr)
|
|
|
|
: ((Args *) buffers[Index].ptr)[i])...);
|
2016-02-11 09:47:11 +00:00
|
|
|
}
|
2016-02-20 11:17:17 +00:00
|
|
|
} else {
|
2016-02-11 09:47:11 +00:00
|
|
|
apply_broadcast<N, Index...>(buffers, buf, index);
|
|
|
|
}
|
2016-01-17 21:36:39 +00:00
|
|
|
|
|
|
|
return result;
|
2015-07-29 15:51:54 +00:00
|
|
|
}
|
2016-02-11 09:47:11 +00:00
|
|
|
|
|
|
|
template <size_t N, size_t... Index>
|
2016-02-20 11:17:17 +00:00
|
|
|
void apply_broadcast(const std::array<buffer_info, N> &buffers,
|
|
|
|
buffer_info &output, index_sequence<Index...>) {
|
2016-02-11 09:47:11 +00:00
|
|
|
using input_iterator = multi_array_iterator<N>;
|
|
|
|
using output_iterator = array_iterator<Return>;
|
|
|
|
|
|
|
|
input_iterator input_iter(buffers, output.shape);
|
|
|
|
output_iterator output_end = array_end<Return>(output);
|
|
|
|
|
2016-02-20 11:17:17 +00:00
|
|
|
for (output_iterator iter = array_begin<Return>(output);
|
|
|
|
iter != output_end; ++iter, ++input_iter) {
|
2016-02-11 09:47:11 +00:00
|
|
|
*iter = f((input_iter.template data<Index, Args>())...);
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 15:51:54 +00:00
|
|
|
};
|
|
|
|
|
2016-05-19 14:02:09 +00:00
|
|
|
template <typename T, int Flags> struct handle_type_name<array_t<T, Flags>> {
|
2016-08-03 23:40:40 +00:00
|
|
|
static PYBIND11_DESCR name() { return _("numpy.ndarray[") + type_caster<T>::name() + _("]"); }
|
2016-02-20 11:17:17 +00:00
|
|
|
};
|
|
|
|
|
2015-07-29 15:51:54 +00:00
|
|
|
NAMESPACE_END(detail)
|
2015-07-26 14:33:49 +00:00
|
|
|
|
2015-07-29 15:51:54 +00:00
|
|
|
template <typename Func, typename Return, typename... Args>
|
|
|
|
detail::vectorize_helper<Func, Return, Args...> vectorize(const Func &f, Return (*) (Args ...)) {
|
|
|
|
return detail::vectorize_helper<Func, Return, Args...>(f);
|
2015-07-26 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 15:51:54 +00:00
|
|
|
template <typename Return, typename... Args>
|
|
|
|
detail::vectorize_helper<Return (*) (Args ...), Return, Args...> vectorize(Return (*f) (Args ...)) {
|
|
|
|
return vectorize<Return (*) (Args ...), Return, Args...>(f, f);
|
2015-07-26 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename func> auto vectorize(func &&f) -> decltype(
|
|
|
|
vectorize(std::forward<func>(f), (typename detail::remove_class<decltype(&std::remove_reference<func>::type::operator())>::type *) nullptr)) {
|
|
|
|
return vectorize(std::forward<func>(f), (typename detail::remove_class<decltype(
|
|
|
|
&std::remove_reference<func>::type::operator())>::type *) nullptr);
|
|
|
|
}
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
NAMESPACE_END(pybind11)
|
2015-07-26 14:33:49 +00:00
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|