Go to the documentation of this file.
24 #include <type_traits>
33 static_assert(
sizeof(::
pybind11::ssize_t) ==
sizeof(Py_intptr_t),
"ssize_t != Py_intptr_t");
50 template <
typename type,
typename SFINAE =
void>
99 if (throw_if_missing) {
100 pybind11_fail(std::string(
"NumPy type info missing for ") + tinfo.name());
105 template <
typename T>
112 ptr = &get_or_create_shared_data<numpy_internals>(
"_numpy_internals");
123 template <
typename T>
125 template <
typename U>
129 template <
typename Concrete>
135 template <
typename Concrete,
typename T,
typename... Ts,
typename... Ints>
137 return sizeof(Concrete) ==
sizeof(
T) ?
I :
platform_lookup<Concrete, Ts...>(Is...);
180 NPY_UINT32_ = platform_lookup<std::uint32_t, unsigned long, unsigned int, unsigned short>(
185 = platform_lookup<std::uint64_t, unsigned long, unsigned long long, unsigned int>(
207 PyObject *(*PyArray_DescrFromType_)(
int);
208 PyObject *(*PyArray_NewFromDescr_)(PyTypeObject *,
217 PyObject *(*PyArray_DescrNewFromType_)(
int);
219 PyObject *(*PyArray_NewCopy_)(PyObject *,
int);
223 PyObject *(*PyArray_DescrFromScalar_)(PyObject *);
224 PyObject *(*PyArray_FromAny_)(PyObject *, PyObject *,
int,
int,
int, PyObject *);
235 PyObject *(*PyArray_Squeeze_)(PyObject *);
240 PyObject *(*PyArray_View_)(PyObject *, PyObject *, PyObject *);
267 auto c =
m.attr(
"_ARRAY_API");
268 void **api_ptr = (
void **) PyCapsule_GetPointer(
c.ptr(),
nullptr);
270 #define DECL_NPY_API(Func) api.Func##_ = (decltype(api.Func##_)) api_ptr[API_##Func];
272 if (
api.PyArray_GetNDArrayCFeatureVersion_() < 0x7) {
273 pybind11_fail(
"pybind11 numpy support requires numpy >= 1.7.0");
317 template <
typename T>
319 template <
typename T,
size_t N>
321 template <
typename T>
323 template <
typename T>
326 template <
typename T>
337 template <
typename T>
339 template <
typename T,
size_t N>
344 static constexpr
size_t extent =
N;
352 static constexpr
auto extents = const_name<array_info<T>::is_array>(
361 template <
typename T,
size_t N>
363 template <
typename T>
366 template <
typename T>
370 #if defined(__GLIBCXX__) \
371 && (__GLIBCXX__ < 20150422 || __GLIBCXX__ == 20150426 || __GLIBCXX__ == 20150623 \
372 || __GLIBCXX__ == 20150626 || __GLIBCXX__ == 20160803)
375 std::is_trivially_destructible<T>,
378 std::is_trivially_copyable<T>,
389 template <
typename T>
392 template <s
size_t Dim = 0,
typename Str
ides>
396 template <
ssize_t Dim = 0,
typename Strides,
typename... Ix>
398 return i *
strides[Dim] + byte_offset_unsafe<Dim + 1>(
strides, index...);
406 template <
typename T, s
size_t Dims>
418 template <
bool Dyn = Dynamic>
430 template <
bool Dyn = Dynamic>
444 template <
typename... Ix>
447 "Invalid number of indices for unchecked array reference");
448 return *
reinterpret_cast<const T *
>(
data_
455 template <s
size_t D = Dims,
typename = enable_if_t<D == 1 || Dynamic>>
461 template <
typename... Ix>
477 template <
bool Dyn = Dynamic>
479 return std::accumulate(
482 template <
bool Dyn = Dynamic>
493 template <
typename T, s
size_t Dims>
497 using ConstBase::ConstBase;
502 using ConstBase::operator();
503 using ConstBase::operator[];
506 template <
typename... Ix>
509 "Invalid number of indices for unchecked array reference");
517 template <s
size_t D = Dims,
typename = enable_if_t<D == 1 || Dynamic>>
523 template <
typename... Ix>
529 template <
typename T, s
size_t Dim>
531 static_assert(Dim == 0 && Dim > 0 ,
532 "unchecked array proxy object is not castable");
534 template <
typename T, s
size_t Dim>
561 args[
"formats"] = std::move(formats);
563 args[
"itemsize"] = pybind11::int_(itemsize);
564 m_ptr = from_args(
args).release().ptr();
571 if (m_ptr ==
nullptr) {
578 PyObject *ptr =
nullptr;
582 return reinterpret_steal<dtype>(ptr);
586 template <
typename T>
630 .attr(
"_dtype_from_pep3118")
634 return reinterpret_borrow<object>(obj);
651 auto field_dict = attr(
"fields").
cast<
dict>();
652 std::vector<field_descr> field_descriptors;
653 field_descriptors.reserve(field_dict.size());
655 for (
auto field : field_dict.attr(
"items")()) {
658 auto spec_fo = spec[1].cast<
tuple>();
660 auto offset = spec_fo[1].
cast<pybind11::int_>();
664 field_descriptors.emplace_back(
668 std::sort(field_descriptors.begin(),
669 field_descriptors.end(),
670 [](
const field_descr &
a,
const field_descr &
b) {
671 return a.offset.cast<int>() < b.offset.cast<int>();
675 for (
auto &
descr : field_descriptors) {
680 return dtype(std::move(
names), std::move(formats), std::move(
offsets), itemsize);
689 c_style = detail::npy_api::NPY_ARRAY_C_CONTIGUOUS_,
690 f_style = detail::npy_api::NPY_ARRAY_F_CONTIGUOUS_,
703 const void *
ptr =
nullptr,
712 pybind11_fail(
"NumPy: shape ndim doesn't match strides ndim");
718 if (isinstance<array>(
base)) {
720 flags = reinterpret_borrow<array>(
base).flags()
724 flags = detail::npy_api::NPY_ARRAY_WRITEABLE_;
729 auto tmp = reinterpret_steal<object>(
api.PyArray_NewFromDescr_(
731 descr.release().ptr(),
734 reinterpret_cast<Py_intptr_t *
>(
shape->data()),
735 reinterpret_cast<Py_intptr_t *
>(
strides->data()),
736 const_cast<void *
>(
ptr),
744 api.PyArray_SetBaseObject_(tmp.ptr(),
base.inc_ref().ptr());
746 tmp = reinterpret_steal<object>(
747 api.PyArray_NewCopy_(tmp.ptr(), -1 ));
750 m_ptr = tmp.release().ptr();
755 const void *
ptr =
nullptr,
759 template <
typename T,
765 template <
typename T>
769 template <
typename T>
773 template <
typename T>
841 template <
typename... Ix>
842 const void *
data(Ix... index)
const {
849 template <
typename... Ix>
857 template <
typename... Ix>
860 fail_dim_check(
sizeof...(index),
"too many indices for an array");
869 template <
typename... Ix>
883 throw std::domain_error(
"array has incorrect number of dimensions: "
884 + std::to_string(
ndim()) +
"; expected "
885 + std::to_string(
Dims));
887 return detail::unchecked_mutable_reference<T, Dims>(
899 detail::unchecked_reference<T, Dims>
unchecked() const & {
901 throw std::domain_error(
"array has incorrect number of dimensions: "
902 + std::to_string(
ndim()) +
"; expected "
903 + std::to_string(
Dims));
911 return reinterpret_steal<array>(
api.PyArray_Squeeze_(
m_ptr));
918 detail::npy_api::PyArray_Dims
d
920 reinterpret_cast<Py_intptr_t *
>(new_shape->data()),
921 int(new_shape->size())};
923 auto new_array = reinterpret_steal<object>(
928 if (isinstance<array>(new_array)) {
929 *
this = std::move(new_array);
935 detail::npy_api::PyArray_Dims
d
936 = {
reinterpret_cast<Py_intptr_t *
>(new_shape->data()),
int(new_shape->size())};
952 auto new_view = reinterpret_steal<array>(
api.PyArray_View_(
971 template <
typename,
typename>
972 friend struct detail::npy_format_descriptor;
975 throw index_error(
msg +
": " + std::to_string(dim) +
" (ndim = " + std::to_string(
ndim())
979 template <
typename... Ix>
987 throw std::domain_error(
"array is not writeable");
991 template <
typename... Ix>
998 template <
typename... Ix>
1001 throw index_error(std::string(
"index ") + std::to_string(
i)
1002 +
" is out of bounds for axis " + std::to_string(axis)
1003 +
" with size " + std::to_string(*
shape));
1010 if (
ptr ==
nullptr) {
1011 PyErr_SetString(PyExc_ValueError,
"cannot create a pybind11::array from a nullptr");
1015 ptr,
nullptr, 0, 0, detail::npy_api::NPY_ARRAY_ENSUREARRAY_ | ExtraFlags,
nullptr);
1019 template <
typename T,
int ExtraFlags = array::forcecast>
1032 static_assert(!detail::array_info<T>::is_array,
"Array types cannot be used with array_t");
1046 Py_XDECREF(
h.ptr());
1061 const T *
ptr =
nullptr,
1078 template <
typename... Ix>
1083 template <
typename... Ix>
1088 template <
typename... Ix>
1094 template <
typename... Ix>
1095 const T &
at(Ix... index)
const {
1104 template <
typename... Ix>
1121 return array::mutable_unchecked<T, Dims>();
1133 return array::unchecked<T, Dims>();
1148 return api.PyArray_Check_(
h.ptr())
1150 dtype::of<T>().ptr())
1157 if (
ptr ==
nullptr) {
1158 PyErr_SetString(PyExc_ValueError,
"cannot create a pybind11::array_t from a nullptr");
1165 detail::npy_api::NPY_ARRAY_ENSUREARRAY_
1171 template <
typename T>
1180 static std::string
format() {
return std::to_string(
N) +
's'; }
1184 static std::string
format() {
return std::to_string(
N) +
's'; }
1187 template <
typename T>
1195 template <
typename T>
1205 template <
typename T,
int ExtraFlags>
1210 if (!
convert && !type::check_(src)) {
1213 value = type::ensure(src);
1214 return static_cast<bool>(
value);
1223 template <
typename T>
1230 template <
typename T,
typename =
void>
1233 template <
typename T>
1241 template <
typename T>
1247 > (
const_name(
"numpy.float") + const_name<sizeof(T) * 8>(),
1251 template <
typename T>
1258 + const_name<sizeof(typename T::value_type) * 16>(),
1262 template <
typename T>
1286 static constexpr
int value =
values[detail::is_fmt_numeric<T>::index];
1291 template <
typename T>
1300 #define PYBIND11_DECL_CHAR_FMT \
1301 static constexpr auto name = const_name("S") + const_name<N>(); \
1302 static pybind11::dtype dtype() { \
1303 return pybind11::dtype(std::string("S") + std::to_string(N)); \
1313 #undef PYBIND11_DECL_CHAR_FMT
1315 template <
typename T>
1323 static constexpr
auto name
1328 return pybind11::dtype::from_args(
1333 template <
typename T>
1352 const std::type_info &tinfo,
1354 bool (*direct_converter)(PyObject *,
void *&)) {
1363 std::vector<field_descriptor> ordered_fields(std::move(fields));
1365 ordered_fields.begin(),
1366 ordered_fields.end(),
1370 for (
auto &field : ordered_fields) {
1372 pybind11_fail(std::string(
"NumPy: unsupported field dtype: `") + field.name +
"` @ "
1376 formats.append(field.descr);
1377 offsets.append(pybind11::int_(field.offset));
1392 std::ostringstream oss;
1399 for (
auto &field : ordered_fields) {
1400 if (field.offset >
offset) {
1401 oss << (field.offset -
offset) <<
'x';
1403 oss << field.format <<
':' << field.name <<
':';
1404 offset = field.offset + field.size;
1407 oss << (itemsize -
offset) <<
'x';
1410 auto format_str = oss.str();
1415 if (!
api.PyArray_EquivTypes_(dtype_ptr,
arr.dtype().ptr())) {
1419 auto tindex = std::type_index(tinfo);
1424 template <
typename T,
typename SFINAE>
1427 "Attempt to use a non-POD or unimplemented POD type as a numpy dtype");
1453 if (!PyObject_TypeCheck(obj,
api.PyVoidArrType_Type_)) {
1456 if (
auto descr = reinterpret_steal<object>(
api.PyArray_DescrFromScalar_(obj))) {
1457 if (
api.PyArray_EquivTypes_(dtype_ptr(),
descr.ptr())) {
1466 #ifdef __CLION_IDE__ // replace heavy macro with dummy code for the IDE (doesn't affect code)
1467 # define PYBIND11_NUMPY_DTYPE(Type, ...) ((void) 0)
1468 # define PYBIND11_NUMPY_DTYPE_EX(Type, ...) ((void) 0)
1471 # define PYBIND11_FIELD_DESCRIPTOR_EX(T, Field, Name) \
1472 ::pybind11::detail::field_descriptor { \
1473 Name, offsetof(T, Field), sizeof(decltype(std::declval<T>().Field)), \
1474 ::pybind11::format_descriptor<decltype(std::declval<T>().Field)>::format(), \
1475 ::pybind11::detail::npy_format_descriptor< \
1476 decltype(std::declval<T>().Field)>::dtype() \
1480 # define PYBIND11_FIELD_DESCRIPTOR(T, Field) PYBIND11_FIELD_DESCRIPTOR_EX(T, Field, #Field)
1484 # define PYBIND11_EVAL0(...) __VA_ARGS__
1485 # define PYBIND11_EVAL1(...) PYBIND11_EVAL0(PYBIND11_EVAL0(PYBIND11_EVAL0(__VA_ARGS__)))
1486 # define PYBIND11_EVAL2(...) PYBIND11_EVAL1(PYBIND11_EVAL1(PYBIND11_EVAL1(__VA_ARGS__)))
1487 # define PYBIND11_EVAL3(...) PYBIND11_EVAL2(PYBIND11_EVAL2(PYBIND11_EVAL2(__VA_ARGS__)))
1488 # define PYBIND11_EVAL4(...) PYBIND11_EVAL3(PYBIND11_EVAL3(PYBIND11_EVAL3(__VA_ARGS__)))
1489 # define PYBIND11_EVAL(...) PYBIND11_EVAL4(PYBIND11_EVAL4(PYBIND11_EVAL4(__VA_ARGS__)))
1490 # define PYBIND11_MAP_END(...)
1491 # define PYBIND11_MAP_OUT
1492 # define PYBIND11_MAP_COMMA ,
1493 # define PYBIND11_MAP_GET_END() 0, PYBIND11_MAP_END
1494 # define PYBIND11_MAP_NEXT0(test, next, ...) next PYBIND11_MAP_OUT
1495 # define PYBIND11_MAP_NEXT1(test, next) PYBIND11_MAP_NEXT0(test, next, 0)
1496 # define PYBIND11_MAP_NEXT(test, next) PYBIND11_MAP_NEXT1(PYBIND11_MAP_GET_END test, next)
1497 # if defined(_MSC_VER) \
1498 && !defined(__clang__) // MSVC is not as eager to expand macros, hence this workaround
1499 # define PYBIND11_MAP_LIST_NEXT1(test, next) \
1500 PYBIND11_EVAL0(PYBIND11_MAP_NEXT0(test, PYBIND11_MAP_COMMA next, 0))
1502 # define PYBIND11_MAP_LIST_NEXT1(test, next) \
1503 PYBIND11_MAP_NEXT0(test, PYBIND11_MAP_COMMA next, 0)
1505 # define PYBIND11_MAP_LIST_NEXT(test, next) \
1506 PYBIND11_MAP_LIST_NEXT1(PYBIND11_MAP_GET_END test, next)
1507 # define PYBIND11_MAP_LIST0(f, t, x, peek, ...) \
1508 f(t, x) PYBIND11_MAP_LIST_NEXT(peek, PYBIND11_MAP_LIST1)(f, t, peek, __VA_ARGS__)
1509 # define PYBIND11_MAP_LIST1(f, t, x, peek, ...) \
1510 f(t, x) PYBIND11_MAP_LIST_NEXT(peek, PYBIND11_MAP_LIST0)(f, t, peek, __VA_ARGS__)
1512 # define PYBIND11_MAP_LIST(f, t, ...) \
1513 PYBIND11_EVAL(PYBIND11_MAP_LIST1(f, t, __VA_ARGS__, (), 0))
1515 # define PYBIND11_NUMPY_DTYPE(Type, ...) \
1516 ::pybind11::detail::npy_format_descriptor<Type>::register_dtype( \
1517 ::std::vector<::pybind11::detail::field_descriptor>{ \
1518 PYBIND11_MAP_LIST(PYBIND11_FIELD_DESCRIPTOR, Type, __VA_ARGS__)})
1520 # if defined(_MSC_VER) && !defined(__clang__)
1521 # define PYBIND11_MAP2_LIST_NEXT1(test, next) \
1522 PYBIND11_EVAL0(PYBIND11_MAP_NEXT0(test, PYBIND11_MAP_COMMA next, 0))
1524 # define PYBIND11_MAP2_LIST_NEXT1(test, next) \
1525 PYBIND11_MAP_NEXT0(test, PYBIND11_MAP_COMMA next, 0)
1527 # define PYBIND11_MAP2_LIST_NEXT(test, next) \
1528 PYBIND11_MAP2_LIST_NEXT1(PYBIND11_MAP_GET_END test, next)
1529 # define PYBIND11_MAP2_LIST0(f, t, x1, x2, peek, ...) \
1530 f(t, x1, x2) PYBIND11_MAP2_LIST_NEXT(peek, PYBIND11_MAP2_LIST1)(f, t, peek, __VA_ARGS__)
1531 # define PYBIND11_MAP2_LIST1(f, t, x1, x2, peek, ...) \
1532 f(t, x1, x2) PYBIND11_MAP2_LIST_NEXT(peek, PYBIND11_MAP2_LIST0)(f, t, peek, __VA_ARGS__)
1534 # define PYBIND11_MAP2_LIST(f, t, ...) \
1535 PYBIND11_EVAL(PYBIND11_MAP2_LIST1(f, t, __VA_ARGS__, (), 0))
1537 # define PYBIND11_NUMPY_DTYPE_EX(Type, ...) \
1538 ::pybind11::detail::npy_format_descriptor<Type>::register_dtype( \
1539 ::std::vector<::pybind11::detail::field_descriptor>{ \
1540 PYBIND11_MAP2_LIST(PYBIND11_FIELD_DESCRIPTOR_EX, Type, __VA_ARGS__)})
1542 #endif // __CLION_IDE__
1553 : p_ptr(reinterpret_cast<char *>(ptr)), m_strides(
strides.
size()) {
1555 for (
size_type i = m_strides.size() - 1;
i != 0; --
i) {
1564 void *
data()
const {
return p_ptr; }
1567 char *p_ptr{
nullptr};
1577 : m_shape(shape.
size()), m_index(shape.
size(), 0), m_common_iterator() {
1580 for (
size_t i = 0;
i < shape.size(); ++
i) {
1581 m_shape[
i] = shape[
i];
1585 for (
size_t i = 0;
i <
N; ++
i) {
1586 init_common_iterator(buffers[
i], shape, m_common_iterator[
i],
strides);
1591 for (
size_t j = m_index.size();
j != 0; --
j) {
1593 if (++m_index[
i] != m_shape[
i]) {
1594 increment_common_iterator(
i);
1602 template <
size_t K,
class T =
void>
1604 return reinterpret_cast<T *
>(m_common_iterator[
K].data());
1614 auto buffer_shape_iter =
buffer.shape.rbegin();
1615 auto buffer_strides_iter =
buffer.strides.rbegin();
1616 auto shape_iter = shape.rbegin();
1617 auto strides_iter =
strides.rbegin();
1619 while (buffer_shape_iter !=
buffer.shape.rend()) {
1620 if (*shape_iter == *buffer_shape_iter) {
1621 *strides_iter = *buffer_strides_iter;
1626 ++buffer_shape_iter;
1627 ++buffer_strides_iter;
1632 std::fill(strides_iter,
strides.rend(), 0);
1637 for (
auto &
iter : m_common_iterator) {
1638 iter.increment(dim);
1655 broadcast(
const std::array<buffer_info, N> &buffers,
ssize_t &ndim, std::vector<ssize_t> &shape) {
1656 ndim = std::accumulate(
1662 shape.resize((
size_t) ndim, 1);
1666 for (
size_t i = 0;
i <
N; ++
i) {
1667 auto res_iter = shape.rbegin();
1668 auto end = buffers[
i].shape.rend();
1669 for (
auto shape_iter = buffers[
i].shape.rbegin(); shape_iter !=
end;
1670 ++shape_iter, ++res_iter) {
1671 const auto &dim_size_in = *shape_iter;
1672 auto &dim_size_out = *res_iter;
1676 if (dim_size_out == 1) {
1677 dim_size_out = dim_size_in;
1678 }
else if (dim_size_in != 1 && dim_size_in != dim_size_out) {
1679 pybind11_fail(
"pybind11::vectorize: incompatible size/dimension of inputs!");
1684 bool trivial_broadcast_c =
true;
1685 bool trivial_broadcast_f =
true;
1686 for (
size_t i = 0;
i <
N && (trivial_broadcast_c || trivial_broadcast_f); ++
i) {
1687 if (buffers[
i].
size == 1) {
1692 if (buffers[
i].ndim != ndim) {
1697 if (!
std::equal(buffers[
i].shape.cbegin(), buffers[
i].shape.cend(), shape.cbegin())) {
1702 if (trivial_broadcast_c) {
1703 ssize_t expect_stride = buffers[
i].itemsize;
1704 auto end = buffers[
i].shape.crend();
1705 for (
auto shape_iter = buffers[
i].shape.crbegin(),
1706 stride_iter = buffers[
i].strides.crbegin();
1707 trivial_broadcast_c && shape_iter !=
end;
1708 ++shape_iter, ++stride_iter) {
1709 if (expect_stride == *stride_iter) {
1710 expect_stride *= *shape_iter;
1712 trivial_broadcast_c =
false;
1718 if (trivial_broadcast_f) {
1719 ssize_t expect_stride = buffers[
i].itemsize;
1720 auto end = buffers[
i].shape.cend();
1721 for (
auto shape_iter = buffers[
i].shape.cbegin(),
1722 stride_iter = buffers[
i].strides.cbegin();
1723 trivial_broadcast_f && shape_iter !=
end;
1724 ++shape_iter, ++stride_iter) {
1725 if (expect_stride == *stride_iter) {
1726 expect_stride *= *shape_iter;
1728 trivial_broadcast_f =
false;
1739 template <
typename T>
1742 "Functions with rvalue reference arguments cannot be vectorized");
1760 template <
typename Func,
typename Return,
typename... Args>
1779 template <
typename Func,
typename... Args>
1795 template <
typename Func,
typename Return,
typename... Args>
1805 static constexpr
size_t N =
sizeof...(Args);
1809 "pybind11::vectorize(...) requires a function with at least one vectorizable argument");
1812 template <
typename T,
1831 template <
size_t Index>
1843 template <
size_t...
Index,
size_t... VIndex,
size_t... BIndex>
1855 std::array<buffer_info, NVectorized> buffers{
1856 {
reinterpret_cast<array *
>(
params[VIndex])->request()...}};
1860 std::vector<ssize_t> shape(0);
1861 auto trivial =
broadcast(buffers, nd, shape);
1865 = std::accumulate(shape.begin(), shape.end(), (
size_t) 1, std::multiplies<size_t>());
1869 if (
size == 1 && ndim == 0) {
1877 PYBIND11_WARNING_PUSH
1878 #ifdef PYBIND11_DETECTED_CLANG_WITH_MISLEADING_CALL_STD_MOVE_EXPLICITLY_WARNING
1887 auto *mutable_data = returned_array::mutable_data(
result);
1889 apply_broadcast(buffers,
params, mutable_data,
size, shape, i_seq, vi_seq, bi_seq);
1891 apply_trivial(buffers,
params, mutable_data,
size, i_seq, vi_seq, bi_seq);
1898 template <
size_t...
Index,
size_t... VIndex,
size_t... BIndex>
1900 std::array<void *, N> &
params,
1910 std::array<std::pair<unsigned char *&, const size_t>, NVectorized> vecparams{
1911 {std::pair<unsigned char *&, const size_t>(
1912 reinterpret_cast<unsigned char *&
>(
params[VIndex] = buffers[BIndex].ptr),
1915 for (
size_t i = 0;
i <
size; ++
i) {
1916 returned_array::call(
1918 for (
auto &
x : vecparams) {
1919 x.first +=
x.second;
1924 template <
size_t...
Index,
size_t... VIndex,
size_t... BIndex>
1926 std::array<void *, N> &
params,
1929 const std::vector<ssize_t> &output_shape,
1936 for (
size_t i = 0;
i <
size; ++
i, ++input_iter) {
1938 returned_array::call(
1944 template <
typename Func,
typename Return,
typename... Args>
1946 return detail::vectorize_helper<Func, Return, Args...>(
f);
1949 template <
typename T,
int Flags>
1951 static constexpr
auto name
1958 template <
typename Return,
typename... Args>
1959 detail::vectorize_helper<Return (*)(Args...), Return, Args...>
vectorize(Return (*
f)(Args...)) {
1960 return detail::vectorize_helper<Return (*)(Args...), Return, Args...>(
f);
1967 (detail::function_signature_t<Func> *)
nullptr)) {
1969 (detail::function_signature_t<Func> *)
nullptr);
1973 template <
typename Return,
1976 typename Helper = detail::vectorize_helper<
1977 decltype(std::mem_fn(std::declval<Return (Class::*)(Args...)>())),
1982 return Helper(std::mem_fn(
f));
1986 template <
typename Return,
1989 typename Helper = detail::vectorize_helper<
1990 decltype(std::mem_fn(std::declval<Return (Class::*)(Args...)
const>())),
1994 Helper
vectorize(Return (Class::*
f)(Args...)
const) {
1995 return Helper(std::mem_fn(
f));
char flags() const
Flags for the array descriptor.
constexpr ssize_t itemsize() const
typename select_indices_impl< index_sequence<>, 0, Bs... >::type select_indices
std::vector< Eigen::Index > Dims
static module_ import(const char *name)
Import and return a module or throws error_already_set.
ADT create(const Signature &signature)
@ API_PyArray_DescrFromType
char byteorder() const
Single character for byteorder.
std::is_same< bools< Ts::value..., true >, bools< true, Ts::value... > > all_of
static array_t ensure(handle h)
remove_reference_t< Func > f
ssize_t strides(ssize_t dim) const
Stride along a given axis.
Annotation for function names.
#define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN)
void fail_dim_check(ssize_t dim, const std::string &msg) const
conditional_t< Dynamic, const ssize_t *, std::array< ssize_t,(size_t) Dims > > shape_
Annotation indicating that a class derives from another given type.
detail::unchecked_reference< T, Dims > unchecked() const &
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
bool PyArray_Check_(PyObject *obj) const
array view(const std::string &dtype)
EIGEN_ALWAYS_INLINE DSizes< IndexType, NumDims > strides(const DSizes< IndexType, NumDims > &dimensions)
void check_dimensions(Ix... index) const
array(ShapeContainer shape, StridesContainer strides, const T *ptr, handle base=handle())
@ API_PyArray_DescrConverter
void * mutable_data(Ix... index)
constexpr descr< N - 1 > const_name(char const (&text)[N])
static const double d[K][N]
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
@ API_PyArray_NewFromDescr
ssize_t shape(ssize_t dim) const
Dimension along a given axis.
detail::any_container< ssize_t > ShapeContainer
T * mutable_data(Ix... index)
object run(typename vectorize_arg< Args >::type &...args, index_sequence< Index... > i_seq, index_sequence< VIndex... > vi_seq, index_sequence< BIndex... > bi_seq)
array(const pybind11::dtype &dt, ShapeContainer shape, StridesContainer strides, const void *ptr=nullptr, handle base=handle())
std::vector< ssize_t > f_strides(const std::vector< ssize_t > &shape, ssize_t itemsize)
constexpr size_t constexpr_sum()
Compile-time integer sum.
#define PYBIND11_OBJECT_CVT(Name, Parent, CheckFun, ConvertFun)
PyTypeObject * PyArray_Type_
array_t(handle h, stolen_t)
const T * data(Ix... index) const
container_type::value_type value_type
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
ssize_t ndim() const
Number of dimensions.
const handle & inc_ref() const &
ssize_t itemsize() const
Byte size of a single element.
static void * mutable_data(Type &)
bool PyArrayDescr_Check_(PyObject *obj) const
PYBIND11_WARNING_PUSH PYBIND11_WARNING_POP
dtype strip_padding(ssize_t itemsize)
static void append_extents(list &shape)
static Return * mutable_data(Type &array)
#define PYBIND11_NAMESPACE_END(name)
bool has_fields() const
Returns true for structured data types.
#define PYBIND11_NOINLINE
const unsigned char * data_
unsigned int(* PyArray_GetNDArrayCFeatureVersion_)()
@ NPY_ARRAY_F_CONTIGUOUS_
Eigen::Triplet< double > T
int(* PyArray_DescrConverter_)(PyObject *, PyObject **)
static bool compare(const buffer_info &b)
dtype(const pybind11::str &format)
broadcast_trivial broadcast(const std::array< buffer_info, N > &buffers, ssize_t &ndim, std::vector< ssize_t > &shape)
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
#define PYBIND11_NAMESPACE_BEGIN(name)
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
@ NPY_ARRAY_C_CONTIGUOUS_
ssize_t byte_offset_unsafe(const Strides &)
static bool check_(handle h)
array_t(ssize_t count, const T *ptr=nullptr, handle base=handle())
T * mutable_data(Ix... ix)
Mutable pointer access to the data at the given indices.
static const SmartProjectionParams params
static constexpr bool is_array
PYBIND11_DEPRECATED("Use reinterpret_borrow<object>() or reinterpret_steal<object>()") object(handle h
const ssize_t * shape() const
Dimensions of the array.
conditional_t< Dynamic, const ssize_t *, std::array< ssize_t,(size_t) Dims > > strides_
typename std::conditional< B, T, F >::type conditional_t
PyObject_VAR_HEAD char * obval
static constexpr bool Dynamic
static object _dtype_from_pep3118()
all_of< std::is_standard_layout< T >, std::is_trivial< T > > is_pod
ssize_t offset_at() const
static void call(Return *out, size_t i, Func &f, Args &...args)
PyTypeObject * PyArrayDescr_Type_
conditional_t< vectorize, array_t< remove_cv_t< call_type >, array::forcecast >, T > type
static void call(void *, size_t, Func &f, Args &...args)
bool owndata() const
If set, the array owns the data (will be freed when the array is deleted)
array_t(ShapeContainer shape, const T *ptr=nullptr, handle base=handle())
array(const buffer_info &info, handle base=handle())
detail::any_container< ssize_t > StridesContainer
remove_reference_t< T > call_type
int num() const
type number of dtype.
void increment_common_iterator(size_t dim)
int(* PyArray_SetBaseObject_)(PyObject *, PyObject *)
numpy_internals & get_numpy_internals()
std::tuple< typename vectorize_arg< Args >::call_type... > arg_call_types
static constexpr auto extents
std::vector< ssize_t > container_type
std::unordered_map< std::type_index, numpy_type_info > registered_dtypes
ssize_t nbytes() const
Total number of bytes.
numpy_type_info * get_type_info(bool throw_if_missing=true)
detail::vectorize_helper< Return(*)(Args...), Return, Args... > vectorize(Return(*f)(Args...))
PyObject_HEAD char * data
typename array_info< T >::type remove_all_extents_t
bool(* PyArray_EquivTypes_)(PyObject *, PyObject *)
array(ShapeContainer shape, const T *ptr, handle base=handle())
@ API_PyArray_DescrFromScalar
unchecked_reference(const void *data, const ssize_t *shape, const ssize_t *strides, enable_if_t< Dyn, ssize_t > dims)
detail::unchecked_mutable_reference< T, Dims > mutable_unchecked() &
A small structure to hold a non zero as a triplet (i,j,value).
static void append_extents(list &)
constexpr descr< 0 > concat()
T & operator()(Ix... index)
Mutable, unchecked access to data at the given indices.
static Return call(Func &f, Args &...args)
ssize_t shape(ssize_t dim) const
Returns the shape (i.e. size) of dimension dim
PyExc_RuntimeError PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
static Type create(broadcast_trivial trivial, const std::vector< ssize_t > &shape)
all_of< std::is_standard_layout< T >, std::is_trivially_copyable< T >, satisfies_none_of< T, std::is_reference, std::is_array, is_std_array, std::is_arithmetic, is_complex, std::is_enum > > is_pod_struct
common_iterator(void *ptr, const container_type &strides, const container_type &shape)
dtype(const std::string &format)
std::ofstream out("Result.txt")
const T & at(Ix... index) const
multi_array_iterator & operator++()
bool check_flags(const void *ptr, int flag)
ssize_t ndim() const
Returns the number of dimensions of the array.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate offset
int(* PyArray_CopyInto_)(PyObject *, PyObject *)
container_type::size_type size_type
std::is_same< detail::remove_cvref_t< T >, U > is_same_ignoring_cvref
Example usage: is_same_ignoring_cvref<T, PyObject *>::value.
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
ssize_t size() const
Total number of elements.
#define PYBIND11_TYPE_CASTER(type, py_name)
detail::unchecked_mutable_reference< T, Dims > mutable_unchecked() &
void check_dimensions_impl(ssize_t, const ssize_t *) const
static dtype of()
Return dtype associated with a C++ type.
void check_writeable() const
array(const pybind11::dtype &dt, ShapeContainer shape, const void *ptr=nullptr, handle base=handle())
std::integral_constant< bool, B > bool_constant
Backports of std::bool_constant and std::negation to accommodate older compilers.
@ API_PyArray_GetNDArrayCFeatureVersion
T & operator[](ssize_t index)
constexpr static ssize_t itemsize()
Returns the item size, i.e. sizeof(T)
array(const pybind11::dtype &dt, T count, const void *ptr=nullptr, handle base=handle())
numpy_type_info * get_type_info(const std::type_info &tinfo, bool throw_if_missing=true)
std::array< common_iter, N > m_common_iterator
#define PYBIND11_OBJECT_DEFAULT(Name, Parent, CheckFun)
constexpr int platform_lookup()
array squeeze()
Return a new view with all of the dimensions of length 1 removed.
type_map< std::vector< bool(*)(PyObject *, void *&)> > direct_conversions
typename make_index_sequence_impl< N >::type make_index_sequence
PyObject * ptr() const
Return the underlying PyObject * pointer.
const void * data(Ix... index) const
multi_array_iterator(const std::array< buffer_info, N > &buffers, const container_type &shape)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T & back()
static PyObject * raw_array(PyObject *ptr, int ExtraFlags=0)
Create array from any object – always returns a new reference.
Wrapper for Python extension modules.
@ API_PyArray_SetBaseObject
PYBIND11_WARNING_PUSH PYBIND11_WARNING_DISABLE_MSVC(5054) PYBIND11_WARNING_POP static_assert(EIGEN_VERSION_AT_LEAST(3
pybind11::dtype dtype() const
Array descriptor (dtype)
handle()=default
The default constructor creates a handle with a nullptr-valued pointer.
detail::unchecked_reference< T, Dims > unchecked() const &
static PyObject * raw_array_t(PyObject *ptr)
Create array from any object – always returns a new reference.
int(* PyArray_GetArrayParamsFromObject_)(PyObject *, PyObject *, unsigned char, PyObject **, int *, Py_intptr_t *, PyObject **, PyObject *)
ssize_t byte_offset(Ix... index) const
array reshape(ShapeContainer new_shape)
Optional order parameter omitted, to be added as needed.
iterator iter(handle obj)
#define DECL_NPY_API(Func)
void apply_broadcast(std::array< buffer_info, NVectorized > &buffers, std::array< void *, N > ¶ms, Return *out, size_t size, const std::vector< ssize_t > &output_shape, index_sequence< Index... >, index_sequence< VIndex... >, index_sequence< BIndex... >)
static array ensure(handle h, int ExtraFlags=0)
typename std::tuple_element< Index, arg_call_types >::type param_n_t
array_t(ShapeContainer shape, StridesContainer strides, const T *ptr=nullptr, handle base=handle())
static Type create(broadcast_trivial, const std::vector< ssize_t > &)
Information record describing a Python buffer object.
array_t(const buffer_info &info, handle base=handle())
PyTypeObject * PyVoidArrType_Type_
enable_if_t<!Dyn, ssize_t > size() const
array_t(handle h, borrowed_t)
void resize(ShapeContainer new_shape, bool refcheck=true)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set offsets
void init_common_iterator(const buffer_info &buffer, const container_type &shape, common_iter &iterator, container_type &strides)
T & mutable_at(Ix... index)
int alignment() const
Alignment of the data type.
const T * data(Ix... ix) const
Pointer access to the data at the given indices.
enable_if_t< Dyn, ssize_t > size() const
ssize_t index_at(Ix... index) const
static detail::void_type call(Func &f, Args &...args)
object base() const
Base object.
vectorize_helper< Func, Return, Args... > vectorize_extractor(const Func &f, Return(*)(Args...))
#define PYBIND11_DECL_CHAR_FMT
PyArrayDescr_Proxy * descr
size_t len(handle h)
Get the length of a Python object.
PYBIND11_NOINLINE void register_structured_dtype(any_container< field_descriptor > fields, const std::type_info &tinfo, ssize_t itemsize, bool(*direct_converter)(PyObject *, void *&))
const T & operator[](ssize_t index) const
PyArrayDescr_Proxy * array_descriptor_proxy(PyObject *ptr)
static const EIGEN_DEPRECATED end_t end
PYBIND11_NOINLINE void load_numpy_internals(numpy_internals *&ptr)
unchecked_reference(const void *data, const ssize_t *shape, const ssize_t *strides, enable_if_t<!Dyn, ssize_t >)
std::vector< ssize_t > c_strides(const std::vector< ssize_t > &shape, ssize_t itemsize)
array_t(private_ctor, ShapeContainer &&shape, StridesContainer &&strides, const T *ptr, handle base)
static constexpr bool is_empty
std::vector< ssize_t > container_type
void increment(size_type dim)
object operator()(typename vectorize_arg< Args >::type... args)
static BinaryMeasurement< Rot3 > convert(const BetweenFactor< Pose3 >::shared_ptr &f)
@ API_PyArray_DescrNewFromType
Container::iterator get(Container &c, Position position)
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
bool_constant< sizeof(T)==sizeof(U)> as
bool equal(const T &obj1, const T &obj2, double tol)
int flags() const
Return the NumPy array flags.
PyObject_HEAD PyObject * typeobj
bool writeable() const
If set, the array is writeable (otherwise the buffer is read-only)
ssize_t itemsize() const
Size of the data type in bytes.
static dtype from_args(const object &args)
This is essentially the same as calling numpy.dtype(args) in Python.
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
PyArray_Proxy * array_proxy(void *ptr)
ssize_t offset_at(Ix... index) const
const ssize_t * strides() const
Strides of the array.
@ API_PyArray_GetArrayParamsFromObject
const T & operator()(Ix... index) const
typename std::remove_reference< T >::type remove_reference_t
dtype(const char *format)
ssize_t index_at(Ix... index) const
void check_dimensions_impl(ssize_t axis, const ssize_t *shape, ssize_t i, Ix... index) const
array(ssize_t count, const T *ptr, handle base=handle())
dtype(list names, list formats, list offsets, ssize_t itemsize)
#define PYBIND11_WARNING_DISABLE_CLANG(name)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
void apply_trivial(std::array< buffer_info, NVectorized > &buffers, std::array< void *, N > ¶ms, Return *out, size_t size, index_sequence< Index... >, index_sequence< VIndex... >, index_sequence< BIndex... >)
gtsam
Author(s):
autogenerated on Sat Jun 1 2024 03:02:27