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>
|
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
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
NAMESPACE_BEGIN(pybind11)
|
2016-05-05 18:33:54 +00:00
|
|
|
namespace detail { template <typename type, typename SFINAE = void> struct npy_format_descriptor { }; }
|
2015-07-28 14:12:20 +00:00
|
|
|
|
2015-07-26 14:33:49 +00:00
|
|
|
class array : public buffer {
|
2015-07-28 14:12:20 +00:00
|
|
|
public:
|
2015-07-26 14:33:49 +00:00
|
|
|
struct API {
|
|
|
|
enum Entries {
|
|
|
|
API_PyArray_Type = 2,
|
|
|
|
API_PyArray_DescrFromType = 45,
|
|
|
|
API_PyArray_FromAny = 69,
|
|
|
|
API_PyArray_NewCopy = 85,
|
|
|
|
API_PyArray_NewFromDescr = 94,
|
2016-01-17 21:36:38 +00:00
|
|
|
|
|
|
|
NPY_C_CONTIGUOUS_ = 0x0001,
|
|
|
|
NPY_F_CONTIGUOUS_ = 0x0002,
|
|
|
|
NPY_ARRAY_FORCECAST_ = 0x0010,
|
|
|
|
NPY_ENSURE_ARRAY_ = 0x0040,
|
|
|
|
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_
|
2015-07-26 14:33:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static API lookup() {
|
2016-01-17 21:36:38 +00:00
|
|
|
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-01-17 21:36:38 +00:00
|
|
|
void **api_ptr = (void **) (c ? PyCapsule_GetPointer(c.ptr(), NULL) : nullptr);
|
2015-09-04 21:42:12 +00:00
|
|
|
#else
|
2016-01-17 21:36:38 +00:00
|
|
|
void **api_ptr = (void **) (c ? PyCObject_AsVoidPtr(c.ptr()) : nullptr);
|
2015-09-04 21:42:12 +00:00
|
|
|
#endif
|
2015-07-26 14:33:49 +00:00
|
|
|
API api;
|
2016-01-17 21:36:38 +00:00
|
|
|
api.PyArray_Type_ = (decltype(api.PyArray_Type_)) api_ptr[API_PyArray_Type];
|
|
|
|
api.PyArray_DescrFromType_ = (decltype(api.PyArray_DescrFromType_)) api_ptr[API_PyArray_DescrFromType];
|
|
|
|
api.PyArray_FromAny_ = (decltype(api.PyArray_FromAny_)) api_ptr[API_PyArray_FromAny];
|
|
|
|
api.PyArray_NewCopy_ = (decltype(api.PyArray_NewCopy_)) api_ptr[API_PyArray_NewCopy];
|
|
|
|
api.PyArray_NewFromDescr_ = (decltype(api.PyArray_NewFromDescr_)) api_ptr[API_PyArray_NewFromDescr];
|
2015-07-26 14:33:49 +00:00
|
|
|
return api;
|
|
|
|
}
|
|
|
|
|
2016-01-17 21:36:38 +00:00
|
|
|
bool PyArray_Check_(PyObject *obj) const { return (bool) PyObject_TypeCheck(obj, PyArray_Type_); }
|
2015-07-26 14:33:49 +00:00
|
|
|
|
2016-01-17 21:36:38 +00:00
|
|
|
PyObject *(*PyArray_DescrFromType_)(int);
|
|
|
|
PyObject *(*PyArray_NewFromDescr_)
|
2015-07-26 14:33:49 +00:00
|
|
|
(PyTypeObject *, PyObject *, int, Py_intptr_t *,
|
|
|
|
Py_intptr_t *, void *, int, PyObject *);
|
2016-01-17 21:36:38 +00:00
|
|
|
PyObject *(*PyArray_NewCopy_)(PyObject *, int);
|
|
|
|
PyTypeObject *PyArray_Type_;
|
|
|
|
PyObject *(*PyArray_FromAny_) (PyObject *, PyObject *, int, int, int, PyObject *);
|
2015-07-26 14:33:49 +00:00
|
|
|
};
|
2015-07-28 14:12:20 +00:00
|
|
|
|
2016-01-17 21:36:38 +00:00
|
|
|
PYBIND11_OBJECT_DEFAULT(array, buffer, lookup_api().PyArray_Check_)
|
2015-07-26 14:33:49 +00:00
|
|
|
|
2016-05-05 08:00:00 +00:00
|
|
|
enum {
|
|
|
|
c_style = API::NPY_C_CONTIGUOUS_,
|
2016-05-19 14:02:09 +00:00
|
|
|
f_style = API::NPY_F_CONTIGUOUS_,
|
|
|
|
forcecast = API::NPY_ARRAY_FORCECAST_
|
2016-05-05 08:00:00 +00:00
|
|
|
};
|
|
|
|
|
2015-07-26 14:33:49 +00:00
|
|
|
template <typename Type> array(size_t size, const Type *ptr) {
|
|
|
|
API& api = lookup_api();
|
2016-05-05 18:33:54 +00:00
|
|
|
PyObject *descr = api.PyArray_DescrFromType_(detail::npy_format_descriptor<Type>::value);
|
2015-07-26 14:33:49 +00:00
|
|
|
if (descr == nullptr)
|
2016-01-17 21:36:44 +00:00
|
|
|
pybind11_fail("NumPy: unsupported buffer format!");
|
2015-07-26 14:33:49 +00:00
|
|
|
Py_intptr_t shape = (Py_intptr_t) size;
|
2016-01-17 21:36:39 +00:00
|
|
|
object tmp = object(api.PyArray_NewFromDescr_(
|
|
|
|
api.PyArray_Type_, descr, 1, &shape, nullptr, (void *) ptr, 0, nullptr), false);
|
|
|
|
if (ptr && tmp)
|
|
|
|
tmp = object(api.PyArray_NewCopy_(tmp.ptr(), -1 /* any order */), false);
|
|
|
|
if (!tmp)
|
2016-01-17 21:36:44 +00:00
|
|
|
pybind11_fail("NumPy: unable to create array!");
|
|
|
|
m_ptr = tmp.release().ptr();
|
2015-07-26 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
array(const buffer_info &info) {
|
|
|
|
API& api = lookup_api();
|
2015-12-15 03:16:49 +00:00
|
|
|
if ((info.format.size() < 1) || (info.format.size() > 2))
|
2016-01-17 21:36:44 +00:00
|
|
|
pybind11_fail("Unsupported buffer format!");
|
2015-07-28 14:12:20 +00:00
|
|
|
int fmt = (int) info.format[0];
|
2016-01-17 21:36:38 +00:00
|
|
|
if (info.format == "Zd") fmt = API::NPY_CDOUBLE_;
|
|
|
|
else if (info.format == "Zf") fmt = API::NPY_CFLOAT_;
|
|
|
|
|
|
|
|
PyObject *descr = api.PyArray_DescrFromType_(fmt);
|
2015-07-26 14:33:49 +00:00
|
|
|
if (descr == nullptr)
|
2016-01-17 21:36:44 +00:00
|
|
|
pybind11_fail("NumPy: unsupported buffer format '" + info.format + "'!");
|
2016-01-17 21:36:39 +00:00
|
|
|
object tmp(api.PyArray_NewFromDescr_(
|
2016-01-17 21:36:38 +00:00
|
|
|
api.PyArray_Type_, descr, info.ndim, (Py_intptr_t *) &info.shape[0],
|
2016-01-17 21:36:39 +00:00
|
|
|
(Py_intptr_t *) &info.strides[0], info.ptr, 0, nullptr), false);
|
|
|
|
if (info.ptr && tmp)
|
|
|
|
tmp = object(api.PyArray_NewCopy_(tmp.ptr(), -1 /* any order */), false);
|
|
|
|
if (!tmp)
|
2016-01-17 21:36:44 +00:00
|
|
|
pybind11_fail("NumPy: unable to create array!");
|
|
|
|
m_ptr = tmp.release().ptr();
|
2015-07-26 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static API &lookup_api() {
|
|
|
|
static API api = API::lookup();
|
|
|
|
return api;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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));
|
2015-10-13 15:38:22 +00:00
|
|
|
array_t() : array() { }
|
2016-05-11 09:25:15 +00:00
|
|
|
array_t(const buffer_info& info) : array(info) {}
|
2015-07-26 14:33:49 +00:00
|
|
|
static bool is_non_null(PyObject *ptr) { return ptr != nullptr; }
|
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;
|
2015-07-26 14:33:49 +00:00
|
|
|
API &api = lookup_api();
|
2016-05-05 18:33:54 +00:00
|
|
|
PyObject *descr = api.PyArray_DescrFromType_(detail::npy_format_descriptor<T>::value);
|
2016-05-19 14:02:09 +00:00
|
|
|
PyObject *result = api.PyArray_FromAny_(ptr, descr, 0, 0, API::NPY_ENSURE_ARRAY_ | ExtraFlags, nullptr);
|
|
|
|
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-05-05 18:33:54 +00:00
|
|
|
NAMESPACE_BEGIN(detail)
|
|
|
|
|
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-05-04 20:22:48 +00:00
|
|
|
array::API::NPY_BYTE_, array::API::NPY_UBYTE_, array::API::NPY_SHORT_, array::API::NPY_USHORT_,
|
|
|
|
array::API::NPY_INT_, array::API::NPY_UINT_, array::API::NPY_LONGLONG_, array::API::NPY_ULONGLONG_ };
|
|
|
|
public:
|
|
|
|
enum { value = values[detail::log2(sizeof(T)) * 2 + (std::is_unsigned<T>::value ? 1 : 0)] };
|
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> { \
|
|
|
|
enum { value = array::API::NumPyName }; \
|
|
|
|
static PYBIND11_DESCR name() { return _(Name); } }
|
|
|
|
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-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)
|
|
|
|
*strides_iter = static_cast<int>(*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) {
|
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>
|
|
|
|
bool broadcast(const std::array<buffer_info, N>& buffers, int& ndim, std::vector<size_t>& shape) {
|
|
|
|
ndim = std::accumulate(buffers.begin(), buffers.end(), 0, [](int res, const buffer_info& buf) {
|
|
|
|
return std::max(res, buf.ndim);
|
|
|
|
});
|
|
|
|
|
|
|
|
shape = std::vector<size_t>(static_cast<size_t>(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);
|
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-02-11 09:47:11 +00:00
|
|
|
int ndim = 0;
|
|
|
|
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-02-11 09:47:11 +00:00
|
|
|
for (int i = ndim - 1; i > 0; --i) {
|
|
|
|
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-01-17 21:36:39 +00:00
|
|
|
array result(buffer_info(nullptr, sizeof(Return),
|
2016-05-04 20:22:48 +00:00
|
|
|
format_descriptor<Return>::value,
|
2015-07-26 14:33:49 +00:00
|
|
|
ndim, shape, strides));
|
2016-01-17 21:36:39 +00:00
|
|
|
|
|
|
|
buffer_info buf = result.request();
|
|
|
|
Return *output = (Return *) buf.ptr;
|
|
|
|
|
2016-05-05 18:33:54 +00:00
|
|
|
if (trivial_broadcast) {
|
2016-02-11 09:47:11 +00:00
|
|
|
/* Call the function */
|
|
|
|
for (size_t i=0; i<size; ++i) {
|
|
|
|
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-05-05 18:33:54 +00:00
|
|
|
static PYBIND11_DESCR name() { return _("numpy.ndarray[dtype=") + 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
|