numpy.h
Go to the documentation of this file.
1 /*
2  pybind11/numpy.h: Basic NumPy support, vectorize() wrapper
3 
4  Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5 
6  All rights reserved. Use of this source code is governed by a
7  BSD-style license that can be found in the LICENSE file.
8 */
9 
10 #pragma once
11 
12 #include "pybind11.h"
13 #include "complex.h"
14 
15 #include <algorithm>
16 #include <array>
17 #include <cstdint>
18 #include <cstdlib>
19 #include <cstring>
20 #include <functional>
21 #include <numeric>
22 #include <sstream>
23 #include <string>
24 #include <type_traits>
25 #include <typeindex>
26 #include <utility>
27 #include <vector>
28 
29 /* This will be true on all flat address space platforms and allows us to reduce the
30  whole npy_intp / ssize_t / Py_intptr_t business down to just ssize_t for all size
31  and dimension types (e.g. shape, strides, indexing), instead of inflicting this
32  upon the library user. */
33 static_assert(sizeof(::pybind11::ssize_t) == sizeof(Py_intptr_t), "ssize_t != Py_intptr_t");
34 static_assert(std::is_signed<Py_intptr_t>::value, "Py_intptr_t must be signed");
35 // We now can reinterpret_cast between py::ssize_t and Py_intptr_t (MSVC + PyPy cares)
36 
38 
40 
41 class array; // Forward declaration
42 
44 
45 template <>
47  static constexpr auto name = const_name("numpy.ndarray");
48 };
49 
50 template <typename type, typename SFINAE = void>
52 
54  PyObject_HEAD
55  PyObject *typeobj;
56  char kind;
57  char type;
58  char byteorder;
59  char flags;
60  int type_num;
61  int elsize;
62  int alignment;
63  char *subarray;
64  PyObject *fields;
65  PyObject *names;
66 };
67 
68 struct PyArray_Proxy {
69  PyObject_HEAD
70  char *data;
71  int nd;
74  PyObject *base;
75  PyObject *descr;
76  int flags;
77 };
78 
80  PyObject_VAR_HEAD char *obval;
82  int flags;
83  PyObject *base;
84 };
85 
87  PyObject *dtype_ptr;
88  std::string format_str;
89 };
90 
92  std::unordered_map<std::type_index, numpy_type_info> registered_dtypes;
93 
94  numpy_type_info *get_type_info(const std::type_info &tinfo, bool throw_if_missing = true) {
95  auto it = registered_dtypes.find(std::type_index(tinfo));
96  if (it != registered_dtypes.end()) {
97  return &(it->second);
98  }
99  if (throw_if_missing) {
100  pybind11_fail(std::string("NumPy type info missing for ") + tinfo.name());
101  }
102  return nullptr;
103  }
104 
105  template <typename T>
106  numpy_type_info *get_type_info(bool throw_if_missing = true) {
107  return get_type_info(typeid(typename std::remove_cv<T>::type), throw_if_missing);
108  }
109 };
110 
112  ptr = &get_or_create_shared_data<numpy_internals>("_numpy_internals");
113 }
114 
116  static numpy_internals *ptr = nullptr;
117  if (!ptr) {
119  }
120  return *ptr;
121 }
122 
123 template <typename T>
124 struct same_size {
125  template <typename U>
126  using as = bool_constant<sizeof(T) == sizeof(U)>;
127 };
128 
129 template <typename Concrete>
130 constexpr int platform_lookup() {
131  return -1;
132 }
133 
134 // Lookup a type according to its size, and return a value corresponding to the NumPy typenum.
135 template <typename Concrete, typename T, typename... Ts, typename... Ints>
136 constexpr int platform_lookup(int I, Ints... Is) {
137  return sizeof(Concrete) == sizeof(T) ? I : platform_lookup<Concrete, Ts...>(Is...);
138 }
139 
140 struct npy_api {
141  enum constants {
170  // Platform-dependent normalization
175  // `npy_common.h` defines the integer aliases. In order, it checks:
176  // NPY_BITSOF_LONG, NPY_BITSOF_LONGLONG, NPY_BITSOF_INT, NPY_BITSOF_SHORT, NPY_BITSOF_CHAR
177  // and assigns the alias to the first matching size, so we should check in this order.
179  = platform_lookup<std::int32_t, long, int, short>(NPY_LONG_, NPY_INT_, NPY_SHORT_),
180  NPY_UINT32_ = platform_lookup<std::uint32_t, unsigned long, unsigned int, unsigned short>(
183  = platform_lookup<std::int64_t, long, long long, int>(NPY_LONG_, NPY_LONGLONG_, NPY_INT_),
185  = platform_lookup<std::uint64_t, unsigned long, unsigned long long, unsigned int>(
187  };
188 
189  struct PyArray_Dims {
190  Py_intptr_t *ptr;
191  int len;
192  };
193 
194  static npy_api &get() {
195  static npy_api api = lookup();
196  return api;
197  }
198 
199  bool PyArray_Check_(PyObject *obj) const {
200  return PyObject_TypeCheck(obj, PyArray_Type_) != 0;
201  }
202  bool PyArrayDescr_Check_(PyObject *obj) const {
203  return PyObject_TypeCheck(obj, PyArrayDescr_Type_) != 0;
204  }
205 
207  PyObject *(*PyArray_DescrFromType_)(int);
208  PyObject *(*PyArray_NewFromDescr_)(PyTypeObject *,
209  PyObject *,
210  int,
211  Py_intptr_t const *,
212  Py_intptr_t const *,
213  void *,
214  int,
215  PyObject *);
216  // Unused. Not removed because that affects ABI of the class.
217  PyObject *(*PyArray_DescrNewFromType_)(int);
218  int (*PyArray_CopyInto_)(PyObject *, PyObject *);
219  PyObject *(*PyArray_NewCopy_)(PyObject *, int);
220  PyTypeObject *PyArray_Type_;
221  PyTypeObject *PyVoidArrType_Type_;
222  PyTypeObject *PyArrayDescr_Type_;
223  PyObject *(*PyArray_DescrFromScalar_)(PyObject *);
224  PyObject *(*PyArray_FromAny_)(PyObject *, PyObject *, int, int, int, PyObject *);
225  int (*PyArray_DescrConverter_)(PyObject *, PyObject **);
226  bool (*PyArray_EquivTypes_)(PyObject *, PyObject *);
228  PyObject *,
229  unsigned char,
230  PyObject **,
231  int *,
232  Py_intptr_t *,
233  PyObject **,
234  PyObject *);
235  PyObject *(*PyArray_Squeeze_)(PyObject *);
236  // Unused. Not removed because that affects ABI of the class.
237  int (*PyArray_SetBaseObject_)(PyObject *, PyObject *);
238  PyObject *(*PyArray_Resize_)(PyObject *, PyArray_Dims *, int, int);
239  PyObject *(*PyArray_Newshape_)(PyObject *, PyArray_Dims *, int);
240  PyObject *(*PyArray_View_)(PyObject *, PyObject *, PyObject *);
241 
242 private:
243  enum functions {
263  };
264 
265  static npy_api lookup() {
266  module_ m = module_::import("numpy.core.multiarray");
267  auto c = m.attr("_ARRAY_API");
268  void **api_ptr = (void **) PyCapsule_GetPointer(c.ptr(), nullptr);
269  npy_api api;
270 #define DECL_NPY_API(Func) api.Func##_ = (decltype(api.Func##_)) api_ptr[API_##Func];
271  DECL_NPY_API(PyArray_GetNDArrayCFeatureVersion);
272  if (api.PyArray_GetNDArrayCFeatureVersion_() < 0x7) {
273  pybind11_fail("pybind11 numpy support requires numpy >= 1.7.0");
274  }
275  DECL_NPY_API(PyArray_Type);
276  DECL_NPY_API(PyVoidArrType_Type);
277  DECL_NPY_API(PyArrayDescr_Type);
278  DECL_NPY_API(PyArray_DescrFromType);
279  DECL_NPY_API(PyArray_DescrFromScalar);
280  DECL_NPY_API(PyArray_FromAny);
281  DECL_NPY_API(PyArray_Resize);
282  DECL_NPY_API(PyArray_CopyInto);
283  DECL_NPY_API(PyArray_NewCopy);
284  DECL_NPY_API(PyArray_NewFromDescr);
285  DECL_NPY_API(PyArray_DescrNewFromType);
286  DECL_NPY_API(PyArray_Newshape);
287  DECL_NPY_API(PyArray_Squeeze);
288  DECL_NPY_API(PyArray_View);
289  DECL_NPY_API(PyArray_DescrConverter);
290  DECL_NPY_API(PyArray_EquivTypes);
291  DECL_NPY_API(PyArray_GetArrayParamsFromObject);
292  DECL_NPY_API(PyArray_SetBaseObject);
293 
294 #undef DECL_NPY_API
295  return api;
296  }
297 };
298 
299 inline PyArray_Proxy *array_proxy(void *ptr) { return reinterpret_cast<PyArray_Proxy *>(ptr); }
300 
301 inline const PyArray_Proxy *array_proxy(const void *ptr) {
302  return reinterpret_cast<const PyArray_Proxy *>(ptr);
303 }
304 
306  return reinterpret_cast<PyArrayDescr_Proxy *>(ptr);
307 }
308 
309 inline const PyArrayDescr_Proxy *array_descriptor_proxy(const PyObject *ptr) {
310  return reinterpret_cast<const PyArrayDescr_Proxy *>(ptr);
311 }
312 
313 inline bool check_flags(const void *ptr, int flag) {
314  return (flag == (array_proxy(ptr)->flags & flag));
315 }
316 
317 template <typename T>
318 struct is_std_array : std::false_type {};
319 template <typename T, size_t N>
320 struct is_std_array<std::array<T, N>> : std::true_type {};
321 template <typename T>
322 struct is_complex : std::false_type {};
323 template <typename T>
324 struct is_complex<std::complex<T>> : std::true_type {};
325 
326 template <typename T>
328  using type = T;
329  static constexpr bool is_array = false;
330  static constexpr bool is_empty = false;
331  static constexpr auto extents = const_name("");
332  static void append_extents(list & /* shape */) {}
333 };
334 // Computes underlying type and a comma-separated list of extents for array
335 // types (any mix of std::array and built-in arrays). An array of char is
336 // treated as scalar because it gets special handling.
337 template <typename T>
339 template <typename T, size_t N>
340 struct array_info<std::array<T, N>> {
341  using type = typename array_info<T>::type;
342  static constexpr bool is_array = true;
343  static constexpr bool is_empty = (N == 0) || array_info<T>::is_empty;
344  static constexpr size_t extent = N;
345 
346  // appends the extents to shape
347  static void append_extents(list &shape) {
348  shape.append(N);
350  }
351 
352  static constexpr auto extents = const_name<array_info<T>::is_array>(
353  concat(const_name<N>(), array_info<T>::extents), const_name<N>());
354 };
355 // For numpy we have special handling for arrays of characters, so we don't include
356 // the size in the array extents.
357 template <size_t N>
358 struct array_info<char[N]> : array_info_scalar<char[N]> {};
359 template <size_t N>
360 struct array_info<std::array<char, N>> : array_info_scalar<std::array<char, N>> {};
361 template <typename T, size_t N>
362 struct array_info<T[N]> : array_info<std::array<T, N>> {};
363 template <typename T>
365 
366 template <typename T>
367 using is_pod_struct
368  = all_of<std::is_standard_layout<T>, // since we're accessing directly in memory
369  // we need a standard layout type
370 #if defined(__GLIBCXX__) \
371  && (__GLIBCXX__ < 20150422 || __GLIBCXX__ == 20150426 || __GLIBCXX__ == 20150623 \
372  || __GLIBCXX__ == 20150626 || __GLIBCXX__ == 20160803)
373  // libstdc++ < 5 (including versions 4.8.5, 4.9.3 and 4.9.4 which were released after
374  // 5) don't implement is_trivially_copyable, so approximate it
375  std::is_trivially_destructible<T>,
377 #else
378  std::is_trivially_copyable<T>,
379 #endif
381  std::is_reference,
382  std::is_array,
383  is_std_array,
384  std::is_arithmetic,
385  is_complex,
386  std::is_enum>>;
387 
388 // Replacement for std::is_pod (deprecated in C++20)
389 template <typename T>
390 using is_pod = all_of<std::is_standard_layout<T>, std::is_trivial<T>>;
391 
392 template <ssize_t Dim = 0, typename Strides>
393 ssize_t byte_offset_unsafe(const Strides &) {
394  return 0;
395 }
396 template <ssize_t Dim = 0, typename Strides, typename... Ix>
397 ssize_t byte_offset_unsafe(const Strides &strides, ssize_t i, Ix... index) {
398  return i * strides[Dim] + byte_offset_unsafe<Dim + 1>(strides, index...);
399 }
400 
406 template <typename T, ssize_t Dims>
408 protected:
409  static constexpr bool Dynamic = Dims < 0;
410  const unsigned char *data_;
411  // Storing the shape & strides in local variables (i.e. these arrays) allows the compiler to
412  // make large performance gains on big, nested loops, but requires compile-time dimensions
414  const ssize_t dims_;
415 
416  friend class pybind11::array;
417  // Constructor for compile-time dimensions:
418  template <bool Dyn = Dynamic>
420  const ssize_t *shape,
421  const ssize_t *strides,
423  : data_{reinterpret_cast<const unsigned char *>(data)}, dims_{Dims} {
424  for (size_t i = 0; i < (size_t) dims_; i++) {
425  shape_[i] = shape[i];
426  strides_[i] = strides[i];
427  }
428  }
429  // Constructor for runtime dimensions:
430  template <bool Dyn = Dynamic>
432  const ssize_t *shape,
433  const ssize_t *strides,
435  : data_{reinterpret_cast<const unsigned char *>(data)}, shape_{shape}, strides_{strides},
436  dims_{dims} {}
437 
438 public:
444  template <typename... Ix>
445  const T &operator()(Ix... index) const {
446  static_assert(ssize_t{sizeof...(Ix)} == Dims || Dynamic,
447  "Invalid number of indices for unchecked array reference");
448  return *reinterpret_cast<const T *>(data_
449  + byte_offset_unsafe(strides_, ssize_t(index)...));
450  }
455  template <ssize_t D = Dims, typename = enable_if_t<D == 1 || Dynamic>>
456  const T &operator[](ssize_t index) const {
457  return operator()(index);
458  }
459 
461  template <typename... Ix>
462  const T *data(Ix... ix) const {
463  return &operator()(ssize_t(ix)...);
464  }
465 
467  constexpr static ssize_t itemsize() { return sizeof(T); }
468 
470  ssize_t shape(ssize_t dim) const { return shape_[(size_t) dim]; }
471 
473  ssize_t ndim() const { return dims_; }
474 
477  template <bool Dyn = Dynamic>
479  return std::accumulate(
480  shape_.begin(), shape_.end(), (ssize_t) 1, std::multiplies<ssize_t>());
481  }
482  template <bool Dyn = Dynamic>
484  return std::accumulate(shape_, shape_ + ndim(), (ssize_t) 1, std::multiplies<ssize_t>());
485  }
486 
490  ssize_t nbytes() const { return size() * itemsize(); }
491 };
492 
493 template <typename T, ssize_t Dims>
495  friend class pybind11::array;
497  using ConstBase::ConstBase;
498  using ConstBase::Dynamic;
499 
500 public:
501  // Bring in const-qualified versions from base class
502  using ConstBase::operator();
503  using ConstBase::operator[];
504 
506  template <typename... Ix>
507  T &operator()(Ix... index) {
508  static_assert(ssize_t{sizeof...(Ix)} == Dims || Dynamic,
509  "Invalid number of indices for unchecked array reference");
510  return const_cast<T &>(ConstBase::operator()(index...));
511  }
517  template <ssize_t D = Dims, typename = enable_if_t<D == 1 || Dynamic>>
518  T &operator[](ssize_t index) {
519  return operator()(index);
520  }
521 
523  template <typename... Ix>
524  T *mutable_data(Ix... ix) {
525  return &operator()(ssize_t(ix)...);
526  }
527 };
528 
529 template <typename T, ssize_t Dim>
531  static_assert(Dim == 0 && Dim > 0 /* always fail */,
532  "unchecked array proxy object is not castable");
533 };
534 template <typename T, ssize_t Dim>
536  : type_caster<unchecked_reference<T, Dim>> {};
537 
539 
540 class dtype : public object {
541 public:
542  PYBIND11_OBJECT_DEFAULT(dtype, object, detail::npy_api::get().PyArrayDescr_Check_)
543 
544  explicit dtype(const buffer_info &info) {
545  dtype descr(_dtype_from_pep3118()(pybind11::str(info.format)));
546  // If info.itemsize == 0, use the value calculated from the format string
547  m_ptr = descr.strip_padding(info.itemsize != 0 ? info.itemsize : descr.itemsize())
548  .release()
549  .ptr();
550  }
551 
552  explicit dtype(const pybind11::str &format) : dtype(from_args(format)) {}
553 
554  explicit dtype(const std::string &format) : dtype(pybind11::str(format)) {}
555 
556  explicit dtype(const char *format) : dtype(pybind11::str(format)) {}
557 
558  dtype(list names, list formats, list offsets, ssize_t itemsize) {
559  dict args;
560  args["names"] = std::move(names);
561  args["formats"] = std::move(formats);
562  args["offsets"] = std::move(offsets);
563  args["itemsize"] = pybind11::int_(itemsize);
564  m_ptr = from_args(args).release().ptr();
565  }
566 
569  explicit dtype(int typenum)
570  : object(detail::npy_api::get().PyArray_DescrFromType_(typenum), stolen_t{}) {
571  if (m_ptr == nullptr) {
572  throw error_already_set();
573  }
574  }
575 
577  static dtype from_args(const object &args) {
578  PyObject *ptr = nullptr;
579  if ((detail::npy_api::get().PyArray_DescrConverter_(args.ptr(), &ptr) == 0) || !ptr) {
580  throw error_already_set();
581  }
582  return reinterpret_steal<dtype>(ptr);
583  }
584 
586  template <typename T>
587  static dtype of() {
589  }
590 
593 
595  bool has_fields() const { return detail::array_descriptor_proxy(m_ptr)->names != nullptr; }
596 
599  char kind() const { return detail::array_descriptor_proxy(m_ptr)->kind; }
600 
603  char char_() const {
604  // Note: The signature, `dtype::char_` follows the naming of NumPy's
605  // public Python API (i.e., ``dtype.char``), rather than its internal
606  // C API (``PyArray_Descr::type``).
607  return detail::array_descriptor_proxy(m_ptr)->type;
608  }
609 
611  int num() const {
612  // Note: The signature, `dtype::num` follows the naming of NumPy's public
613  // Python API (i.e., ``dtype.num``), rather than its internal
614  // C API (``PyArray_Descr::type_num``).
616  }
617 
619  char byteorder() const { return detail::array_descriptor_proxy(m_ptr)->byteorder; }
620 
622  int alignment() const { return detail::array_descriptor_proxy(m_ptr)->alignment; }
623 
625  char flags() const { return detail::array_descriptor_proxy(m_ptr)->flags; }
626 
627 private:
628  static object _dtype_from_pep3118() {
629  static PyObject *obj = module_::import("numpy.core._internal")
630  .attr("_dtype_from_pep3118")
631  .cast<object>()
632  .release()
633  .ptr();
634  return reinterpret_borrow<object>(obj);
635  }
636 
638  // Recursively strip all void fields with empty names that are generated for
639  // padding fields (as of NumPy v1.11).
640  if (!has_fields()) {
641  return *this;
642  }
643 
644  struct field_descr {
646  object format;
647  pybind11::int_ offset;
648  field_descr(pybind11::str &&name, object &&format, pybind11::int_ &&offset)
649  : name{std::move(name)}, format{std::move(format)}, offset{std::move(offset)} {};
650  };
651  auto field_dict = attr("fields").cast<dict>();
652  std::vector<field_descr> field_descriptors;
653  field_descriptors.reserve(field_dict.size());
654 
655  for (auto field : field_dict.attr("items")()) {
656  auto spec = field.cast<tuple>();
657  auto name = spec[0].cast<pybind11::str>();
658  auto spec_fo = spec[1].cast<tuple>();
659  auto format = spec_fo[0].cast<dtype>();
660  auto offset = spec_fo[1].cast<pybind11::int_>();
661  if ((len(name) == 0u) && format.kind() == 'V') {
662  continue;
663  }
664  field_descriptors.emplace_back(
665  std::move(name), format.strip_padding(format.itemsize()), std::move(offset));
666  }
667 
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>();
672  });
673 
674  list names, formats, offsets;
675  for (auto &descr : field_descriptors) {
676  names.append(std::move(descr.name));
677  formats.append(std::move(descr.format));
678  offsets.append(std::move(descr.offset));
679  }
680  return dtype(std::move(names), std::move(formats), std::move(offsets), itemsize);
681  }
682 };
683 
684 class array : public buffer {
685 public:
687 
688  enum {
689  c_style = detail::npy_api::NPY_ARRAY_C_CONTIGUOUS_,
690  f_style = detail::npy_api::NPY_ARRAY_F_CONTIGUOUS_,
691  forcecast = detail::npy_api::NPY_ARRAY_FORCECAST_
692  };
693 
694  array() : array(0, static_cast<const double *>(nullptr)) {}
695 
696  using ShapeContainer = detail::any_container<ssize_t>;
697  using StridesContainer = detail::any_container<ssize_t>;
698 
699  // Constructs an array taking shape/strides from arbitrary container types
703  const void *ptr = nullptr,
704  handle base = handle()) {
705 
706  if (strides->empty()) {
707  *strides = detail::c_strides(*shape, dt.itemsize());
708  }
709 
710  auto ndim = shape->size();
711  if (ndim != strides->size()) {
712  pybind11_fail("NumPy: shape ndim doesn't match strides ndim");
713  }
714  auto descr = dt;
715 
716  int flags = 0;
717  if (base && ptr) {
718  if (isinstance<array>(base)) {
719  /* Copy flags from base (except ownership bit) */
720  flags = reinterpret_borrow<array>(base).flags()
722  } else {
723  /* Writable by default, easy to downgrade later on if needed */
724  flags = detail::npy_api::NPY_ARRAY_WRITEABLE_;
725  }
726  }
727 
728  auto &api = detail::npy_api::get();
729  auto tmp = reinterpret_steal<object>(api.PyArray_NewFromDescr_(
730  api.PyArray_Type_,
731  descr.release().ptr(),
732  (int) ndim,
733  // Use reinterpret_cast for PyPy on Windows (remove if fixed, checked on 7.3.1)
734  reinterpret_cast<Py_intptr_t *>(shape->data()),
735  reinterpret_cast<Py_intptr_t *>(strides->data()),
736  const_cast<void *>(ptr),
737  flags,
738  nullptr));
739  if (!tmp) {
740  throw error_already_set();
741  }
742  if (ptr) {
743  if (base) {
744  api.PyArray_SetBaseObject_(tmp.ptr(), base.inc_ref().ptr());
745  } else {
746  tmp = reinterpret_steal<object>(
747  api.PyArray_NewCopy_(tmp.ptr(), -1 /* any order */));
748  }
749  }
750  m_ptr = tmp.release().ptr();
751  }
752 
755  const void *ptr = nullptr,
756  handle base = handle())
757  : array(dt, std::move(shape), {}, ptr, base) {}
758 
759  template <typename T,
760  typename
762  array(const pybind11::dtype &dt, T count, const void *ptr = nullptr, handle base = handle())
763  : array(dt, {{count}}, ptr, base) {}
764 
765  template <typename T>
767  : array(pybind11::dtype::of<T>(), std::move(shape), std::move(strides), ptr, base) {}
768 
769  template <typename T>
771  : array(std::move(shape), {}, ptr, base) {}
772 
773  template <typename T>
774  explicit array(ssize_t count, const T *ptr, handle base = handle())
775  : array({count}, {}, ptr, base) {}
776 
777  explicit array(const buffer_info &info, handle base = handle())
779 
782  return reinterpret_borrow<pybind11::dtype>(detail::array_proxy(m_ptr)->descr);
783  }
784 
786  ssize_t size() const {
787  return std::accumulate(shape(), shape() + ndim(), (ssize_t) 1, std::multiplies<ssize_t>());
788  }
789 
791  ssize_t itemsize() const {
793  }
794 
796  ssize_t nbytes() const { return size() * itemsize(); }
797 
799  ssize_t ndim() const { return detail::array_proxy(m_ptr)->nd; }
800 
802  object base() const { return reinterpret_borrow<object>(detail::array_proxy(m_ptr)->base); }
803 
805  const ssize_t *shape() const { return detail::array_proxy(m_ptr)->dimensions; }
806 
808  ssize_t shape(ssize_t dim) const {
809  if (dim >= ndim()) {
810  fail_dim_check(dim, "invalid axis");
811  }
812  return shape()[dim];
813  }
814 
816  const ssize_t *strides() const { return detail::array_proxy(m_ptr)->strides; }
817 
819  ssize_t strides(ssize_t dim) const {
820  if (dim >= ndim()) {
821  fail_dim_check(dim, "invalid axis");
822  }
823  return strides()[dim];
824  }
825 
827  int flags() const { return detail::array_proxy(m_ptr)->flags; }
828 
830  bool writeable() const {
831  return detail::check_flags(m_ptr, detail::npy_api::NPY_ARRAY_WRITEABLE_);
832  }
833 
835  bool owndata() const {
836  return detail::check_flags(m_ptr, detail::npy_api::NPY_ARRAY_OWNDATA_);
837  }
838 
841  template <typename... Ix>
842  const void *data(Ix... index) const {
843  return static_cast<const void *>(detail::array_proxy(m_ptr)->data + offset_at(index...));
844  }
845 
849  template <typename... Ix>
850  void *mutable_data(Ix... index) {
851  check_writeable();
852  return static_cast<void *>(detail::array_proxy(m_ptr)->data + offset_at(index...));
853  }
854 
857  template <typename... Ix>
858  ssize_t offset_at(Ix... index) const {
859  if ((ssize_t) sizeof...(index) > ndim()) {
860  fail_dim_check(sizeof...(index), "too many indices for an array");
861  }
862  return byte_offset(ssize_t(index)...);
863  }
864 
865  ssize_t offset_at() const { return 0; }
866 
869  template <typename... Ix>
870  ssize_t index_at(Ix... index) const {
871  return offset_at(index...) / itemsize();
872  }
873 
880  template <typename T, ssize_t Dims = -1>
881  detail::unchecked_mutable_reference<T, Dims> mutable_unchecked() & {
882  if (Dims >= 0 && ndim() != Dims) {
883  throw std::domain_error("array has incorrect number of dimensions: "
884  + std::to_string(ndim()) + "; expected "
885  + std::to_string(Dims));
886  }
887  return detail::unchecked_mutable_reference<T, Dims>(
888  mutable_data(), shape(), strides(), ndim());
889  }
890 
898  template <typename T, ssize_t Dims = -1>
899  detail::unchecked_reference<T, Dims> unchecked() const & {
900  if (Dims >= 0 && ndim() != Dims) {
901  throw std::domain_error("array has incorrect number of dimensions: "
902  + std::to_string(ndim()) + "; expected "
903  + std::to_string(Dims));
904  }
905  return detail::unchecked_reference<T, Dims>(data(), shape(), strides(), ndim());
906  }
907 
910  auto &api = detail::npy_api::get();
911  return reinterpret_steal<array>(api.PyArray_Squeeze_(m_ptr));
912  }
913 
917  void resize(ShapeContainer new_shape, bool refcheck = true) {
918  detail::npy_api::PyArray_Dims d
919  = {// Use reinterpret_cast for PyPy on Windows (remove if fixed, checked on 7.3.1)
920  reinterpret_cast<Py_intptr_t *>(new_shape->data()),
921  int(new_shape->size())};
922  // try to resize, set ordering param to -1 cause it's not used anyway
923  auto new_array = reinterpret_steal<object>(
924  detail::npy_api::get().PyArray_Resize_(m_ptr, &d, int(refcheck), -1));
925  if (!new_array) {
926  throw error_already_set();
927  }
928  if (isinstance<array>(new_array)) {
929  *this = std::move(new_array);
930  }
931  }
932 
935  detail::npy_api::PyArray_Dims d
936  = {reinterpret_cast<Py_intptr_t *>(new_shape->data()), int(new_shape->size())};
937  auto new_array
938  = reinterpret_steal<array>(detail::npy_api::get().PyArray_Newshape_(m_ptr, &d, 0));
939  if (!new_array) {
940  throw error_already_set();
941  }
942  return new_array;
943  }
944 
950  array view(const std::string &dtype) {
951  auto &api = detail::npy_api::get();
952  auto new_view = reinterpret_steal<array>(api.PyArray_View_(
954  if (!new_view) {
955  throw error_already_set();
956  }
957  return new_view;
958  }
959 
962  static array ensure(handle h, int ExtraFlags = 0) {
963  auto result = reinterpret_steal<array>(raw_array(h.ptr(), ExtraFlags));
964  if (!result) {
965  PyErr_Clear();
966  }
967  return result;
968  }
969 
970 protected:
971  template <typename, typename>
972  friend struct detail::npy_format_descriptor;
973 
974  void fail_dim_check(ssize_t dim, const std::string &msg) const {
975  throw index_error(msg + ": " + std::to_string(dim) + " (ndim = " + std::to_string(ndim())
976  + ')');
977  }
978 
979  template <typename... Ix>
980  ssize_t byte_offset(Ix... index) const {
981  check_dimensions(index...);
982  return detail::byte_offset_unsafe(strides(), ssize_t(index)...);
983  }
984 
985  void check_writeable() const {
986  if (!writeable()) {
987  throw std::domain_error("array is not writeable");
988  }
989  }
990 
991  template <typename... Ix>
992  void check_dimensions(Ix... index) const {
993  check_dimensions_impl(ssize_t(0), shape(), ssize_t(index)...);
994  }
995 
996  void check_dimensions_impl(ssize_t, const ssize_t *) const {}
997 
998  template <typename... Ix>
999  void check_dimensions_impl(ssize_t axis, const ssize_t *shape, ssize_t i, Ix... index) const {
1000  if (i >= *shape) {
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));
1004  }
1005  check_dimensions_impl(axis + 1, shape + 1, index...);
1006  }
1007 
1009  static PyObject *raw_array(PyObject *ptr, int ExtraFlags = 0) {
1010  if (ptr == nullptr) {
1011  PyErr_SetString(PyExc_ValueError, "cannot create a pybind11::array from a nullptr");
1012  return nullptr;
1013  }
1014  return detail::npy_api::get().PyArray_FromAny_(
1015  ptr, nullptr, 0, 0, detail::npy_api::NPY_ARRAY_ENSUREARRAY_ | ExtraFlags, nullptr);
1016  }
1017 };
1018 
1019 template <typename T, int ExtraFlags = array::forcecast>
1020 class array_t : public array {
1021 private:
1022  struct private_ctor {};
1023  // Delegating constructor needed when both moving and accessing in the same constructor
1027  const T *ptr,
1028  handle base)
1029  : array(std::move(shape), std::move(strides), ptr, base) {}
1030 
1031 public:
1032  static_assert(!detail::array_info<T>::is_array, "Array types cannot be used with array_t");
1033 
1034  using value_type = T;
1035 
1036  array_t() : array(0, static_cast<const T *>(nullptr)) {}
1039 
1040  PYBIND11_DEPRECATED("Use array_t<T>::ensure() instead")
1042  if (!m_ptr) {
1043  PyErr_Clear();
1044  }
1045  if (!is_borrowed) {
1046  Py_XDECREF(h.ptr());
1047  }
1048  }
1049 
1050  // NOLINTNEXTLINE(google-explicit-constructor)
1051  array_t(const object &o) : array(raw_array_t(o.ptr()), stolen_t{}) {
1052  if (!m_ptr) {
1053  throw error_already_set();
1054  }
1055  }
1056 
1057  explicit array_t(const buffer_info &info, handle base = handle()) : array(info, base) {}
1058 
1061  const T *ptr = nullptr,
1062  handle base = handle())
1063  : array(std::move(shape), std::move(strides), ptr, base) {}
1064 
1065  explicit array_t(ShapeContainer shape, const T *ptr = nullptr, handle base = handle())
1066  : array_t(private_ctor{},
1067  std::move(shape),
1068  (ExtraFlags & f_style) != 0 ? detail::f_strides(*shape, itemsize())
1070  ptr,
1071  base) {}
1072 
1073  explicit array_t(ssize_t count, const T *ptr = nullptr, handle base = handle())
1074  : array({count}, {}, ptr, base) {}
1075 
1076  constexpr ssize_t itemsize() const { return sizeof(T); }
1077 
1078  template <typename... Ix>
1079  ssize_t index_at(Ix... index) const {
1080  return offset_at(index...) / itemsize();
1081  }
1082 
1083  template <typename... Ix>
1084  const T *data(Ix... index) const {
1085  return static_cast<const T *>(array::data(index...));
1086  }
1087 
1088  template <typename... Ix>
1089  T *mutable_data(Ix... index) {
1090  return static_cast<T *>(array::mutable_data(index...));
1091  }
1092 
1093  // Reference to element at a given index
1094  template <typename... Ix>
1095  const T &at(Ix... index) const {
1096  if ((ssize_t) sizeof...(index) != ndim()) {
1097  fail_dim_check(sizeof...(index), "index dimension mismatch");
1098  }
1099  return *(static_cast<const T *>(array::data())
1100  + byte_offset(ssize_t(index)...) / itemsize());
1101  }
1102 
1103  // Mutable reference to element at a given index
1104  template <typename... Ix>
1105  T &mutable_at(Ix... index) {
1106  if ((ssize_t) sizeof...(index) != ndim()) {
1107  fail_dim_check(sizeof...(index), "index dimension mismatch");
1108  }
1109  return *(static_cast<T *>(array::mutable_data())
1110  + byte_offset(ssize_t(index)...) / itemsize());
1111  }
1112 
1119  template <ssize_t Dims = -1>
1120  detail::unchecked_mutable_reference<T, Dims> mutable_unchecked() & {
1121  return array::mutable_unchecked<T, Dims>();
1122  }
1123 
1131  template <ssize_t Dims = -1>
1132  detail::unchecked_reference<T, Dims> unchecked() const & {
1133  return array::unchecked<T, Dims>();
1134  }
1135 
1139  auto result = reinterpret_steal<array_t>(raw_array_t(h.ptr()));
1140  if (!result) {
1141  PyErr_Clear();
1142  }
1143  return result;
1144  }
1145 
1146  static bool check_(handle h) {
1147  const auto &api = detail::npy_api::get();
1148  return api.PyArray_Check_(h.ptr())
1149  && api.PyArray_EquivTypes_(detail::array_proxy(h.ptr())->descr,
1150  dtype::of<T>().ptr())
1151  && detail::check_flags(h.ptr(), ExtraFlags & (array::c_style | array::f_style));
1152  }
1153 
1154 protected:
1156  static PyObject *raw_array_t(PyObject *ptr) {
1157  if (ptr == nullptr) {
1158  PyErr_SetString(PyExc_ValueError, "cannot create a pybind11::array_t from a nullptr");
1159  return nullptr;
1160  }
1161  return detail::npy_api::get().PyArray_FromAny_(ptr,
1162  dtype::of<T>().release().ptr(),
1163  0,
1164  0,
1165  detail::npy_api::NPY_ARRAY_ENSUREARRAY_
1166  | ExtraFlags,
1167  nullptr);
1168  }
1169 };
1170 
1171 template <typename T>
1172 struct format_descriptor<T, detail::enable_if_t<detail::is_pod_struct<T>::value>> {
1173  static std::string format() {
1175  }
1176 };
1177 
1178 template <size_t N>
1179 struct format_descriptor<char[N]> {
1180  static std::string format() { return std::to_string(N) + 's'; }
1181 };
1182 template <size_t N>
1183 struct format_descriptor<std::array<char, N>> {
1184  static std::string format() { return std::to_string(N) + 's'; }
1185 };
1186 
1187 template <typename T>
1188 struct format_descriptor<T, detail::enable_if_t<std::is_enum<T>::value>> {
1189  static std::string format() {
1190  return format_descriptor<
1192  }
1193 };
1194 
1195 template <typename T>
1196 struct format_descriptor<T, detail::enable_if_t<detail::array_info<T>::is_array>> {
1197  static std::string format() {
1198  using namespace detail;
1199  static constexpr auto extents = const_name("(") + array_info<T>::extents + const_name(")");
1200  return extents.text + format_descriptor<remove_all_extents_t<T>>::format();
1201  }
1202 };
1203 
1205 template <typename T, int ExtraFlags>
1206 struct pyobject_caster<array_t<T, ExtraFlags>> {
1208 
1209  bool load(handle src, bool convert) {
1210  if (!convert && !type::check_(src)) {
1211  return false;
1212  }
1213  value = type::ensure(src);
1214  return static_cast<bool>(value);
1215  }
1216 
1217  static handle cast(const handle &src, return_value_policy /* policy */, handle /* parent */) {
1218  return src.inc_ref();
1219  }
1221 };
1222 
1223 template <typename T>
1224 struct compare_buffer_info<T, detail::enable_if_t<detail::is_pod_struct<T>::value>> {
1225  static bool compare(const buffer_info &b) {
1226  return npy_api::get().PyArray_EquivTypes_(dtype::of<T>().ptr(), dtype(b).ptr());
1227  }
1228 };
1229 
1230 template <typename T, typename = void>
1232 
1233 template <typename T>
1236  const_name("bool"),
1237  const_name<std::is_signed<T>::value>("numpy.int", "numpy.uint")
1238  + const_name<sizeof(T) * 8>());
1239 };
1240 
1241 template <typename T>
1242 struct npy_format_descriptor_name<T, enable_if_t<std::is_floating_point<T>::value>> {
1247  > (const_name("numpy.float") + const_name<sizeof(T) * 8>(),
1248  const_name("numpy.longdouble"));
1249 };
1250 
1251 template <typename T>
1257  > (const_name("numpy.complex")
1258  + const_name<sizeof(typename T::value_type) * 16>(),
1259  const_name("numpy.longcomplex"));
1260 };
1261 
1262 template <typename T>
1264  T,
1265  enable_if_t<satisfies_any_of<T, std::is_arithmetic, is_complex>::value>>
1267 private:
1268  // NB: the order here must match the one in common.h
1269  constexpr static const int values[15] = {npy_api::NPY_BOOL_,
1284 
1285 public:
1286  static constexpr int value = values[detail::is_fmt_numeric<T>::index];
1287 
1288  static pybind11::dtype dtype() { return pybind11::dtype(/*typenum*/ value); }
1289 };
1290 
1291 template <typename T>
1293  static constexpr auto name = const_name("object");
1294 
1295  static constexpr int value = npy_api::NPY_OBJECT_;
1296 
1297  static pybind11::dtype dtype() { return pybind11::dtype(/*typenum*/ value); }
1298 };
1299 
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)); \
1304  }
1305 template <size_t N>
1306 struct npy_format_descriptor<char[N]> {
1308 };
1309 template <size_t N>
1310 struct npy_format_descriptor<std::array<char, N>> {
1312 };
1313 #undef PYBIND11_DECL_CHAR_FMT
1314 
1315 template <typename T>
1317 private:
1319 
1320 public:
1321  static_assert(!array_info<T>::is_empty, "Zero-sized arrays are not supported");
1322 
1323  static constexpr auto name
1326  list shape;
1328  return pybind11::dtype::from_args(
1329  pybind11::make_tuple(base_descr::dtype(), std::move(shape)));
1330  }
1331 };
1332 
1333 template <typename T>
1335 private:
1337 
1338 public:
1339  static constexpr auto name = base_descr::name;
1340  static pybind11::dtype dtype() { return base_descr::dtype(); }
1341 };
1342 
1344  const char *name;
1347  std::string format;
1349 };
1350 
1352  const std::type_info &tinfo,
1353  ssize_t itemsize,
1354  bool (*direct_converter)(PyObject *, void *&)) {
1355 
1357  if (numpy_internals.get_type_info(tinfo, false)) {
1358  pybind11_fail("NumPy: dtype is already registered");
1359  }
1360 
1361  // Use ordered fields because order matters as of NumPy 1.14:
1362  // https://docs.scipy.org/doc/numpy/release.html#multiple-field-indexing-assignment-of-structured-arrays
1363  std::vector<field_descriptor> ordered_fields(std::move(fields));
1364  std::sort(
1365  ordered_fields.begin(),
1366  ordered_fields.end(),
1367  [](const field_descriptor &a, const field_descriptor &b) { return a.offset < b.offset; });
1368 
1369  list names, formats, offsets;
1370  for (auto &field : ordered_fields) {
1371  if (!field.descr) {
1372  pybind11_fail(std::string("NumPy: unsupported field dtype: `") + field.name + "` @ "
1373  + tinfo.name());
1374  }
1375  names.append(pybind11::str(field.name));
1376  formats.append(field.descr);
1377  offsets.append(pybind11::int_(field.offset));
1378  }
1379  auto *dtype_ptr
1380  = pybind11::dtype(std::move(names), std::move(formats), std::move(offsets), itemsize)
1381  .release()
1382  .ptr();
1383 
1384  // There is an existing bug in NumPy (as of v1.11): trailing bytes are
1385  // not encoded explicitly into the format string. This will supposedly
1386  // get fixed in v1.12; for further details, see these:
1387  // - https://github.com/numpy/numpy/issues/7797
1388  // - https://github.com/numpy/numpy/pull/7798
1389  // Because of this, we won't use numpy's logic to generate buffer format
1390  // strings and will just do it ourselves.
1391  ssize_t offset = 0;
1392  std::ostringstream oss;
1393  // mark the structure as unaligned with '^', because numpy and C++ don't
1394  // always agree about alignment (particularly for complex), and we're
1395  // explicitly listing all our padding. This depends on none of the fields
1396  // overriding the endianness. Putting the ^ in front of individual fields
1397  // isn't guaranteed to work due to https://github.com/numpy/numpy/issues/9049
1398  oss << "^T{";
1399  for (auto &field : ordered_fields) {
1400  if (field.offset > offset) {
1401  oss << (field.offset - offset) << 'x';
1402  }
1403  oss << field.format << ':' << field.name << ':';
1404  offset = field.offset + field.size;
1405  }
1406  if (itemsize > offset) {
1407  oss << (itemsize - offset) << 'x';
1408  }
1409  oss << '}';
1410  auto format_str = oss.str();
1411 
1412  // Smoke test: verify that NumPy properly parses our buffer format string
1413  auto &api = npy_api::get();
1414  auto arr = array(buffer_info(nullptr, itemsize, format_str, 1));
1415  if (!api.PyArray_EquivTypes_(dtype_ptr, arr.dtype().ptr())) {
1416  pybind11_fail("NumPy: invalid buffer descriptor!");
1417  }
1418 
1419  auto tindex = std::type_index(tinfo);
1420  numpy_internals.registered_dtypes[tindex] = {dtype_ptr, std::move(format_str)};
1421  get_internals().direct_conversions[tindex].push_back(direct_converter);
1422 }
1423 
1424 template <typename T, typename SFINAE>
1425 struct npy_format_descriptor {
1426  static_assert(is_pod_struct<T>::value,
1427  "Attempt to use a non-POD or unimplemented POD type as a numpy dtype");
1428 
1429  static constexpr auto name = make_caster<T>::name;
1430 
1431  static pybind11::dtype dtype() { return reinterpret_borrow<pybind11::dtype>(dtype_ptr()); }
1432 
1433  static std::string format() {
1434  static auto format_str = get_numpy_internals().get_type_info<T>(true)->format_str;
1435  return format_str;
1436  }
1437 
1439  register_structured_dtype(std::move(fields),
1440  typeid(typename std::remove_cv<T>::type),
1441  sizeof(T),
1442  &direct_converter);
1443  }
1444 
1445 private:
1446  static PyObject *dtype_ptr() {
1447  static PyObject *ptr = get_numpy_internals().get_type_info<T>(true)->dtype_ptr;
1448  return ptr;
1449  }
1450 
1451  static bool direct_converter(PyObject *obj, void *&value) {
1452  auto &api = npy_api::get();
1453  if (!PyObject_TypeCheck(obj, api.PyVoidArrType_Type_)) {
1454  return false;
1455  }
1456  if (auto descr = reinterpret_steal<object>(api.PyArray_DescrFromScalar_(obj))) {
1457  if (api.PyArray_EquivTypes_(dtype_ptr(), descr.ptr())) {
1458  value = ((PyVoidScalarObject_Proxy *) obj)->obval;
1459  return true;
1460  }
1461  }
1462  return false;
1463  }
1464 };
1465 
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)
1469 #else
1470 
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() \
1477  }
1478 
1479 // Extract name, offset and format descriptor for a struct field
1480 # define PYBIND11_FIELD_DESCRIPTOR(T, Field) PYBIND11_FIELD_DESCRIPTOR_EX(T, Field, #Field)
1481 
1482 // The main idea of this macro is borrowed from https://github.com/swansontec/map-macro
1483 // (C) William Swanson, Paul Fultz
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))
1501 # else
1502 # define PYBIND11_MAP_LIST_NEXT1(test, next) \
1503  PYBIND11_MAP_NEXT0(test, PYBIND11_MAP_COMMA next, 0)
1504 # endif
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__)
1511 // PYBIND11_MAP_LIST(f, t, a1, a2, ...) expands to f(t, a1), f(t, a2), ...
1512 # define PYBIND11_MAP_LIST(f, t, ...) \
1513  PYBIND11_EVAL(PYBIND11_MAP_LIST1(f, t, __VA_ARGS__, (), 0))
1514 
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__)})
1519 
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))
1523 # else
1524 # define PYBIND11_MAP2_LIST_NEXT1(test, next) \
1525  PYBIND11_MAP_NEXT0(test, PYBIND11_MAP_COMMA next, 0)
1526 # endif
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__)
1533 // PYBIND11_MAP2_LIST(f, t, a1, a2, ...) expands to f(t, a1, a2), f(t, a3, a4), ...
1534 # define PYBIND11_MAP2_LIST(f, t, ...) \
1535  PYBIND11_EVAL(PYBIND11_MAP2_LIST1(f, t, __VA_ARGS__, (), 0))
1536 
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__)})
1541 
1542 #endif // __CLION_IDE__
1543 
1545 public:
1546  using container_type = std::vector<ssize_t>;
1547  using value_type = container_type::value_type;
1548  using size_type = container_type::size_type;
1549 
1550  common_iterator() : m_strides() {}
1551 
1552  common_iterator(void *ptr, const container_type &strides, const container_type &shape)
1553  : p_ptr(reinterpret_cast<char *>(ptr)), m_strides(strides.size()) {
1554  m_strides.back() = static_cast<value_type>(strides.back());
1555  for (size_type i = m_strides.size() - 1; i != 0; --i) {
1556  size_type j = i - 1;
1557  auto s = static_cast<value_type>(shape[i]);
1558  m_strides[j] = strides[j] + m_strides[i] - strides[i] * s;
1559  }
1560  }
1561 
1562  void increment(size_type dim) { p_ptr += m_strides[dim]; }
1563 
1564  void *data() const { return p_ptr; }
1565 
1566 private:
1567  char *p_ptr{nullptr};
1569 };
1570 
1571 template <size_t N>
1573 public:
1574  using container_type = std::vector<ssize_t>;
1575 
1576  multi_array_iterator(const std::array<buffer_info, N> &buffers, const container_type &shape)
1577  : m_shape(shape.size()), m_index(shape.size(), 0), m_common_iterator() {
1578 
1579  // Manual copy to avoid conversion warning if using std::copy
1580  for (size_t i = 0; i < shape.size(); ++i) {
1581  m_shape[i] = shape[i];
1582  }
1583 
1584  container_type strides(shape.size());
1585  for (size_t i = 0; i < N; ++i) {
1586  init_common_iterator(buffers[i], shape, m_common_iterator[i], strides);
1587  }
1588  }
1589 
1591  for (size_t j = m_index.size(); j != 0; --j) {
1592  size_t i = j - 1;
1593  if (++m_index[i] != m_shape[i]) {
1594  increment_common_iterator(i);
1595  break;
1596  }
1597  m_index[i] = 0;
1598  }
1599  return *this;
1600  }
1601 
1602  template <size_t K, class T = void>
1603  T *data() const {
1604  return reinterpret_cast<T *>(m_common_iterator[K].data());
1605  }
1606 
1607 private:
1609 
1611  const container_type &shape,
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();
1618 
1619  while (buffer_shape_iter != buffer.shape.rend()) {
1620  if (*shape_iter == *buffer_shape_iter) {
1621  *strides_iter = *buffer_strides_iter;
1622  } else {
1623  *strides_iter = 0;
1624  }
1625 
1626  ++buffer_shape_iter;
1627  ++buffer_strides_iter;
1628  ++shape_iter;
1629  ++strides_iter;
1630  }
1631 
1632  std::fill(strides_iter, strides.rend(), 0);
1633  iterator = common_iter(buffer.ptr, strides, shape);
1634  }
1635 
1636  void increment_common_iterator(size_t dim) {
1637  for (auto &iter : m_common_iterator) {
1638  iter.increment(dim);
1639  }
1640  }
1641 
1644  std::array<common_iter, N> m_common_iterator;
1645 };
1646 
1648 
1649 // Populates the shape and number of dimensions for the set of buffers. Returns a
1650 // broadcast_trivial enum value indicating whether the broadcast is "trivial"--that is, has each
1651 // buffer being either a singleton or a full-size, C-contiguous (`c_trivial`) or Fortran-contiguous
1652 // (`f_trivial`) storage buffer; returns `non_trivial` otherwise.
1653 template <size_t N>
1655 broadcast(const std::array<buffer_info, N> &buffers, ssize_t &ndim, std::vector<ssize_t> &shape) {
1656  ndim = std::accumulate(
1657  buffers.begin(), buffers.end(), ssize_t(0), [](ssize_t res, const buffer_info &buf) {
1658  return std::max(res, buf.ndim);
1659  });
1660 
1661  shape.clear();
1662  shape.resize((size_t) ndim, 1);
1663 
1664  // Figure out the output size, and make sure all input arrays conform (i.e. are either size 1
1665  // or the full size).
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;
1673 
1674  // Each input dimension can either be 1 or `n`, but `n` values must match across
1675  // buffers
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!");
1680  }
1681  }
1682  }
1683 
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) {
1688  continue;
1689  }
1690 
1691  // Require the same number of dimensions:
1692  if (buffers[i].ndim != ndim) {
1694  }
1695 
1696  // Require all dimensions be full-size:
1697  if (!std::equal(buffers[i].shape.cbegin(), buffers[i].shape.cend(), shape.cbegin())) {
1699  }
1700 
1701  // Check for C contiguity (but only if previous inputs were also C contiguous)
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;
1711  } else {
1712  trivial_broadcast_c = false;
1713  }
1714  }
1715  }
1716 
1717  // Check for Fortran contiguity (if previous inputs were also F contiguous)
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;
1727  } else {
1728  trivial_broadcast_f = false;
1729  }
1730  }
1731  }
1732  }
1733 
1734  return trivial_broadcast_c ? broadcast_trivial::c_trivial
1735  : trivial_broadcast_f ? broadcast_trivial::f_trivial
1737 }
1738 
1739 template <typename T>
1741  static_assert(!std::is_rvalue_reference<T>::value,
1742  "Functions with rvalue reference arguments cannot be vectorized");
1743  // The wrapped function gets called with this type:
1745  // Is this a vectorized argument?
1746  static constexpr bool vectorize
1749  std::is_pointer,
1750  std::is_array,
1751  is_std_array,
1752  std::is_enum>::value
1755  // Accept this type: an array for vectorized types, otherwise the type as-is:
1757 };
1758 
1759 // py::vectorize when a return type is present
1760 template <typename Func, typename Return, typename... Args>
1763 
1764  static Type create(broadcast_trivial trivial, const std::vector<ssize_t> &shape) {
1765  if (trivial == broadcast_trivial::f_trivial) {
1766  return array_t<Return, array::f_style>(shape);
1767  }
1768  return array_t<Return>(shape);
1769  }
1770 
1771  static Return *mutable_data(Type &array) { return array.mutable_data(); }
1772 
1773  static Return call(Func &f, Args &...args) { return f(args...); }
1774 
1775  static void call(Return *out, size_t i, Func &f, Args &...args) { out[i] = f(args...); }
1776 };
1777 
1778 // py::vectorize when a return type is not present
1779 template <typename Func, typename... Args>
1780 struct vectorize_returned_array<Func, void, Args...> {
1781  using Type = none;
1782 
1783  static Type create(broadcast_trivial, const std::vector<ssize_t> &) { return none(); }
1784 
1785  static void *mutable_data(Type &) { return nullptr; }
1786 
1787  static detail::void_type call(Func &f, Args &...args) {
1788  f(args...);
1789  return {};
1790  }
1791 
1792  static void call(void *, size_t, Func &f, Args &...args) { f(args...); }
1793 };
1794 
1795 template <typename Func, typename Return, typename... Args>
1797 
1798 // NVCC for some reason breaks if NVectorized is private
1799 #ifdef __CUDACC__
1800 public:
1801 #else
1802 private:
1803 #endif
1804 
1805  static constexpr size_t N = sizeof...(Args);
1806  static constexpr size_t NVectorized = constexpr_sum(vectorize_arg<Args>::vectorize...);
1807  static_assert(
1808  NVectorized >= 1,
1809  "pybind11::vectorize(...) requires a function with at least one vectorizable argument");
1810 
1811 public:
1812  template <typename T,
1813  // SFINAE to prevent shadowing the copy constructor.
1814  typename = detail::enable_if_t<
1816  explicit vectorize_helper(T &&f) : f(std::forward<T>(f)) {}
1817 
1819  return run(args...,
1823  }
1824 
1825 private:
1827 
1828  // Internal compiler error in MSVC 19.16.27025.1 (Visual Studio 2017 15.9.4), when compiling
1829  // with "/permissive-" flag when arg_call_types is manually inlined.
1830  using arg_call_types = std::tuple<typename vectorize_arg<Args>::call_type...>;
1831  template <size_t Index>
1833 
1834  using returned_array = vectorize_returned_array<Func, Return, Args...>;
1835 
1836  // Runs a vectorized function given arguments tuple and three index sequences:
1837  // - Index is the full set of 0 ... (N-1) argument indices;
1838  // - VIndex is the subset of argument indices with vectorized parameters, letting us access
1839  // vectorized arguments (anything not in this sequence is passed through)
1840  // - BIndex is a incremental sequence (beginning at 0) of the same size as VIndex, so that
1841  // we can store vectorized buffer_infos in an array (argument VIndex has its buffer at
1842  // index BIndex in the array).
1843  template <size_t... Index, size_t... VIndex, size_t... BIndex>
1844  object run(typename vectorize_arg<Args>::type &...args,
1847  index_sequence<BIndex...> bi_seq) {
1848 
1849  // Pointers to values the function was called with; the vectorized ones set here will start
1850  // out as array_t<T> pointers, but they will be changed them to T pointers before we make
1851  // call the wrapped function. Non-vectorized pointers are left as-is.
1852  std::array<void *, N> params{{&args...}};
1853 
1854  // The array of `buffer_info`s of vectorized arguments:
1855  std::array<buffer_info, NVectorized> buffers{
1856  {reinterpret_cast<array *>(params[VIndex])->request()...}};
1857 
1858  /* Determine dimensions parameters of output array */
1859  ssize_t nd = 0;
1860  std::vector<ssize_t> shape(0);
1861  auto trivial = broadcast(buffers, nd, shape);
1862  auto ndim = (size_t) nd;
1863 
1864  size_t size
1865  = std::accumulate(shape.begin(), shape.end(), (size_t) 1, std::multiplies<size_t>());
1866 
1867  // If all arguments are 0-dimension arrays (i.e. single values) return a plain value (i.e.
1868  // not wrapped in an array).
1869  if (size == 1 && ndim == 0) {
1870  PYBIND11_EXPAND_SIDE_EFFECTS(params[VIndex] = buffers[BIndex].ptr);
1871  return cast(
1872  returned_array::call(f, *reinterpret_cast<param_n_t<Index> *>(params[Index])...));
1873  }
1874 
1875  auto result = returned_array::create(trivial, shape);
1876 
1877  PYBIND11_WARNING_PUSH
1878 #ifdef PYBIND11_DETECTED_CLANG_WITH_MISLEADING_CALL_STD_MOVE_EXPLICITLY_WARNING
1879  PYBIND11_WARNING_DISABLE_CLANG("-Wreturn-std-move")
1880 #endif
1881 
1882  if (size == 0) {
1883  return result;
1884  }
1885 
1886  /* Call the function */
1887  auto *mutable_data = returned_array::mutable_data(result);
1888  if (trivial == broadcast_trivial::non_trivial) {
1889  apply_broadcast(buffers, params, mutable_data, size, shape, i_seq, vi_seq, bi_seq);
1890  } else {
1891  apply_trivial(buffers, params, mutable_data, size, i_seq, vi_seq, bi_seq);
1892  }
1893 
1894  return result;
1896  }
1897 
1898  template <size_t... Index, size_t... VIndex, size_t... BIndex>
1899  void apply_trivial(std::array<buffer_info, NVectorized> &buffers,
1900  std::array<void *, N> &params,
1901  Return *out,
1902  size_t size,
1906 
1907  // Initialize an array of mutable byte references and sizes with references set to the
1908  // appropriate pointer in `params`; as we iterate, we'll increment each pointer by its size
1909  // (except for singletons, which get an increment of 0).
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),
1913  buffers[BIndex].size == 1 ? 0 : sizeof(param_n_t<VIndex>))...}};
1914 
1915  for (size_t i = 0; i < size; ++i) {
1916  returned_array::call(
1917  out, i, f, *reinterpret_cast<param_n_t<Index> *>(params[Index])...);
1918  for (auto &x : vecparams) {
1919  x.first += x.second;
1920  }
1921  }
1922  }
1923 
1924  template <size_t... Index, size_t... VIndex, size_t... BIndex>
1925  void apply_broadcast(std::array<buffer_info, NVectorized> &buffers,
1926  std::array<void *, N> &params,
1927  Return *out,
1928  size_t size,
1929  const std::vector<ssize_t> &output_shape,
1933 
1934  multi_array_iterator<NVectorized> input_iter(buffers, output_shape);
1935 
1936  for (size_t i = 0; i < size; ++i, ++input_iter) {
1937  PYBIND11_EXPAND_SIDE_EFFECTS((params[VIndex] = input_iter.template data<BIndex>()));
1938  returned_array::call(
1939  out, i, f, *reinterpret_cast<param_n_t<Index> *>(std::get<Index>(params))...);
1940  }
1941  }
1942 };
1943 
1944 template <typename Func, typename Return, typename... Args>
1945 vectorize_helper<Func, Return, Args...> vectorize_extractor(const Func &f, Return (*)(Args...)) {
1946  return detail::vectorize_helper<Func, Return, Args...>(f);
1947 }
1948 
1949 template <typename T, int Flags>
1950 struct handle_type_name<array_t<T, Flags>> {
1951  static constexpr auto name
1953 };
1954 
1956 
1957 // Vanilla pointer vectorizer:
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);
1961 }
1962 
1963 // lambda vectorizer:
1965 auto vectorize(Func &&f)
1966  -> decltype(detail::vectorize_extractor(std::forward<Func>(f),
1967  (detail::function_signature_t<Func> *) nullptr)) {
1968  return detail::vectorize_extractor(std::forward<Func>(f),
1969  (detail::function_signature_t<Func> *) nullptr);
1970 }
1971 
1972 // Vectorize a class method (non-const):
1973 template <typename Return,
1974  typename Class,
1975  typename... Args,
1976  typename Helper = detail::vectorize_helper<
1977  decltype(std::mem_fn(std::declval<Return (Class::*)(Args...)>())),
1978  Return,
1979  Class *,
1980  Args...>>
1981 Helper vectorize(Return (Class::*f)(Args...)) {
1982  return Helper(std::mem_fn(f));
1983 }
1984 
1985 // Vectorize a class method (const):
1986 template <typename Return,
1987  typename Class,
1988  typename... Args,
1989  typename Helper = detail::vectorize_helper<
1990  decltype(std::mem_fn(std::declval<Return (Class::*)(Args...) const>())),
1991  Return,
1992  const Class *,
1993  Args...>>
1994 Helper vectorize(Return (Class::*f)(Args...) const) {
1995  return Helper(std::mem_fn(f));
1996 }
1997 
dtype::flags
char flags() const
Flags for the array descriptor.
Definition: numpy.h:625
common_iterator::m_strides
container_type m_strides
Definition: numpy.h:1568
array_t::itemsize
constexpr ssize_t itemsize() const
Definition: numpy.h:1076
select_indices
typename select_indices_impl< index_sequence<>, 0, Bs... >::type select_indices
Definition: wrap/pybind11/include/pybind11/detail/common.h:693
Dims
std::vector< Eigen::Index > Dims
Definition: testGaussianConditional.cpp:46
module_::import
static module_ import(const char *name)
Import and return a module or throws error_already_set.
Definition: pybind11.h:1211
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
npy_format_descriptor::dtype_ptr
static PyObject * dtype_ptr()
Definition: numpy.h:1446
create
ADT create(const Signature &signature)
Definition: testAlgebraicDecisionTree.cpp:129
npy_api::NPY_ARRAY_WRITEABLE_
@ NPY_ARRAY_WRITEABLE_
Definition: numpy.h:148
npy_api::API_PyArray_DescrFromType
@ API_PyArray_DescrFromType
Definition: numpy.h:248
array::forcecast
@ forcecast
Definition: numpy.h:691
dtype::byteorder
char byteorder() const
Single character for byteorder.
Definition: numpy.h:619
npy_api::NPY_DOUBLE_
@ NPY_DOUBLE_
Definition: numpy.h:161
format_descriptor
Definition: wrap/pybind11/include/pybind11/detail/common.h:1023
vectorize_helper
Definition: numpy.h:1796
return_value_policy::move
@ move
all_of
std::is_same< bools< Ts::value..., true >, bools< true, Ts::value... > > all_of
Definition: wrap/pybind11/include/pybind11/detail/common.h:726
array_t::ensure
static array_t ensure(handle h)
Definition: numpy.h:1138
vectorize_helper::f
remove_reference_t< Func > f
Definition: numpy.h:1826
array::strides
ssize_t strides(ssize_t dim) const
Stride along a given axis.
Definition: numpy.h:819
npy_format_descriptor
Definition: numpy.h:51
name
Annotation for function names.
Definition: attr.h:51
vectorize_helper::vectorize_helper
vectorize_helper(T &&f)
Definition: numpy.h:1816
PyArrayDescr_Proxy::alignment
int alignment
Definition: numpy.h:62
PYBIND11_EXPAND_SIDE_EFFECTS
#define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN)
Definition: wrap/pybind11/include/pybind11/detail/common.h:978
array::fail_dim_check
void fail_dim_check(ssize_t dim, const std::string &msg) const
Definition: numpy.h:974
unchecked_reference::shape_
conditional_t< Dynamic, const ssize_t *, std::array< ssize_t,(size_t) Dims > > shape_
Definition: numpy.h:413
is_std_array
Definition: numpy.h:318
base
Annotation indicating that a class derives from another given type.
Definition: attr.h:64
npy_api::NPY_INT16_
@ NPY_INT16_
Definition: numpy.h:173
multi_array_iterator::m_index
container_type m_index
Definition: numpy.h:1643
array::unchecked
detail::unchecked_reference< T, Dims > unchecked() const &
Definition: numpy.h:899
broadcast_trivial::c_trivial
@ c_trivial
npy_api::NPY_CDOUBLE_
@ NPY_CDOUBLE_
Definition: numpy.h:164
npy_api::NPY_UINT32_
@ NPY_UINT32_
Definition: numpy.h:180
npy_api::NPY_INT8_
@ NPY_INT8_
Definition: numpy.h:171
npy_api::API_PyArray_Squeeze
@ API_PyArray_Squeeze
Definition: numpy.h:257
format
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
Definition: openglsupport.cpp:226
npy_api::PyArray_Check_
bool PyArray_Check_(PyObject *obj) const
Definition: numpy.h:199
array
int array[24]
Definition: Map_general_stride.cpp:1
array::view
array view(const std::string &dtype)
Definition: numpy.h:950
Eigen::internal::strides
EIGEN_ALWAYS_INLINE DSizes< IndexType, NumDims > strides(const DSizes< IndexType, NumDims > &dimensions)
Definition: TensorBlock.h:26
index_sequence
Index sequences.
Definition: wrap/pybind11/include/pybind11/detail/common.h:671
npy_api::NPY_BOOL_
@ NPY_BOOL_
Definition: numpy.h:149
vectorize_arg
Definition: numpy.h:1740
gtsam.examples.DogLegOptimizerExample.type
type
Definition: DogLegOptimizerExample.py:111
ssize_t
Py_ssize_t ssize_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:475
array::check_dimensions
void check_dimensions(Ix... index) const
Definition: numpy.h:992
array::array
array(ShapeContainer shape, StridesContainer strides, const T *ptr, handle base=handle())
Definition: numpy.h:766
error_already_set
Definition: pytypes.h:722
object::stolen_t
Definition: pytypes.h:421
npy_api::API_PyArray_DescrConverter
@ API_PyArray_DescrConverter
Definition: numpy.h:259
array::mutable_data
void * mutable_data(Ix... index)
Definition: numpy.h:850
numpy_type_info::format_str
std::string format_str
Definition: numpy.h:88
s
RealScalar s
Definition: level1_cplx_impl.h:126
const_name
constexpr descr< N - 1 > const_name(char const (&text)[N])
Definition: descr.h:60
d
static const double d[K][N]
Definition: igam.h:11
broadcast_trivial
broadcast_trivial
Definition: numpy.h:1647
return_value_policy
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
Definition: wrap/pybind11/include/pybind11/detail/common.h:485
npy_api::API_PyArray_NewFromDescr
@ API_PyArray_NewFromDescr
Definition: numpy.h:254
array::shape
ssize_t shape(ssize_t dim) const
Dimension along a given axis.
Definition: numpy.h:808
array< double >::ShapeContainer
detail::any_container< ssize_t > ShapeContainer
Definition: numpy.h:696
list
Definition: pytypes.h:2124
npy_api
Definition: numpy.h:140
PyVoidScalarObject_Proxy::base
PyObject * base
Definition: numpy.h:83
npy_api::NPY_STRING_
@ NPY_STRING_
Definition: numpy.h:167
PyArrayDescr_Proxy::subarray
char * subarray
Definition: numpy.h:63
field_descriptor::offset
ssize_t offset
Definition: numpy.h:1345
npy_api::NPY_INT_
@ NPY_INT_
Definition: numpy.h:154
array_t::mutable_data
T * mutable_data(Ix... index)
Definition: numpy.h:1089
vectorize_helper::run
object run(typename vectorize_arg< Args >::type &...args, index_sequence< Index... > i_seq, index_sequence< VIndex... > vi_seq, index_sequence< BIndex... > bi_seq)
Definition: numpy.h:1844
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
array::array
array(const pybind11::dtype &dt, ShapeContainer shape, StridesContainer strides, const void *ptr=nullptr, handle base=handle())
Definition: numpy.h:700
b
Scalar * b
Definition: benchVecAdd.cpp:17
f_strides
std::vector< ssize_t > f_strides(const std::vector< ssize_t > &shape, ssize_t itemsize)
Definition: buffer_info.h:31
constexpr_sum
constexpr size_t constexpr_sum()
Compile-time integer sum.
Definition: wrap/pybind11/include/pybind11/detail/common.h:814
PYBIND11_OBJECT_CVT
#define PYBIND11_OBJECT_CVT(Name, Parent, CheckFun, ConvertFun)
Definition: pytypes.h:1373
unchecked_reference
Definition: numpy.h:407
numpy_internals
Definition: numpy.h:91
npy_api::PyArray_Type_
PyTypeObject * PyArray_Type_
Definition: numpy.h:220
array_t::array_t
array_t(handle h, stolen_t)
Definition: numpy.h:1038
array_t::data
const T * data(Ix... index) const
Definition: numpy.h:1084
npy_api::API_PyArray_Newshape
@ API_PyArray_Newshape
Definition: numpy.h:256
common_iterator::value_type
container_type::value_type value_type
Definition: numpy.h:1547
format_descriptor< T, detail::enable_if_t< detail::array_info< T >::is_array > >::format
static std::string format()
Definition: numpy.h:1197
x
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
Definition: gnuplot_common_settings.hh:12
array::ndim
ssize_t ndim() const
Number of dimensions.
Definition: numpy.h:799
handle::inc_ref
const handle & inc_ref() const &
Definition: pytypes.h:249
array::itemsize
ssize_t itemsize() const
Byte size of a single element.
Definition: numpy.h:791
npy_api::NPY_BYTE_
@ NPY_BYTE_
Definition: numpy.h:150
array_t::array_t
array_t()
Definition: numpy.h:1036
vectorize_returned_array< Func, void, Args... >::mutable_data
static void * mutable_data(Type &)
Definition: numpy.h:1785
npy_api::NPY_LONG_
@ NPY_LONG_
Definition: numpy.h:156
npy_api::PyArrayDescr_Check_
bool PyArrayDescr_Check_(PyObject *obj) const
Definition: numpy.h:202
pyobject_caster< array_t< T, ExtraFlags > >::cast
static handle cast(const handle &src, return_value_policy, handle)
Definition: numpy.h:1217
dt
const double dt
Definition: testVelocityConstraint.cpp:15
PYBIND11_WARNING_POP
PYBIND11_WARNING_PUSH PYBIND11_WARNING_POP
Definition: tensor.h:30
dtype::strip_padding
dtype strip_padding(ssize_t itemsize)
Definition: numpy.h:637
npy_api::API_PyArray_Resize
@ API_PyArray_Resize
Definition: numpy.h:251
array_info< std::array< T, N > >::append_extents
static void append_extents(list &shape)
Definition: numpy.h:347
vectorize_returned_array::mutable_data
static Return * mutable_data(Type &array)
Definition: numpy.h:1771
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:80
dtype::has_fields
bool has_fields() const
Returns true for structured data types.
Definition: numpy.h:595
PYBIND11_NOINLINE
#define PYBIND11_NOINLINE
Definition: wrap/pybind11/include/pybind11/detail/common.h:186
unchecked_reference::data_
const unsigned char * data_
Definition: numpy.h:410
broadcast_trivial::non_trivial
@ non_trivial
npy_api::PyArray_GetNDArrayCFeatureVersion_
unsigned int(* PyArray_GetNDArrayCFeatureVersion_)()
Definition: numpy.h:206
type
Definition: pytypes.h:1491
npy_api::NPY_INT64_
@ NPY_INT64_
Definition: numpy.h:182
array::array
array()
Definition: numpy.h:694
npy_api::NPY_ARRAY_F_CONTIGUOUS_
@ NPY_ARRAY_F_CONTIGUOUS_
Definition: numpy.h:143
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
handle::m_ptr
PyObject * m_ptr
Definition: pytypes.h:297
npy_api::PyArray_DescrConverter_
int(* PyArray_DescrConverter_)(PyObject *, PyObject **)
Definition: numpy.h:225
compare_buffer_info< T, detail::enable_if_t< detail::is_pod_struct< T >::value > >::compare
static bool compare(const buffer_info &b)
Definition: numpy.h:1225
dtype::dtype
dtype(const pybind11::str &format)
Definition: numpy.h:552
broadcast
broadcast_trivial broadcast(const std::array< buffer_info, N > &buffers, ssize_t &ndim, std::vector< ssize_t > &shape)
Definition: numpy.h:1655
detail
Definition: testSerializationNonlinear.cpp:70
PyArray_Proxy::nd
int nd
Definition: numpy.h:71
npy_format_descriptor::direct_converter
static bool direct_converter(PyObject *obj, void *&value)
Definition: numpy.h:1451
buffer
Definition: pytypes.h:2223
array_info_scalar
Definition: numpy.h:327
get_internals
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
Definition: internals.h:467
npy_api::NPY_ULONGLONG_
@ NPY_ULONGLONG_
Definition: numpy.h:159
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:76
PyVoidScalarObject_Proxy::flags
int flags
Definition: numpy.h:82
npy_api::NPY_ARRAY_ENSUREARRAY_
@ NPY_ARRAY_ENSUREARRAY_
Definition: numpy.h:146
iterator
Definition: pytypes.h:1426
res
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
npy_api::NPY_ARRAY_C_CONTIGUOUS_
@ NPY_ARRAY_C_CONTIGUOUS_
Definition: numpy.h:142
byte_offset_unsafe
ssize_t byte_offset_unsafe(const Strides &)
Definition: numpy.h:393
array_t::check_
static bool check_(handle h)
Definition: numpy.h:1146
array_t::array_t
array_t(ssize_t count, const T *ptr=nullptr, handle base=handle())
Definition: numpy.h:1073
h
const double h
Definition: testSimpleHelicopter.cpp:19
PyArray_Proxy::strides
ssize_t * strides
Definition: numpy.h:73
unchecked_mutable_reference::mutable_data
T * mutable_data(Ix... ix)
Mutable pointer access to the data at the given indices.
Definition: numpy.h:524
vanilla::params
static const SmartProjectionParams params
Definition: smartFactorScenarios.h:69
array_info_scalar::is_array
static constexpr bool is_array
Definition: numpy.h:329
same_size
Definition: numpy.h:124
result
Values result
Definition: OdometryOptimize.cpp:8
is_complex
Definition: numpy.h:322
descr
Definition: descr.h:25
object::PYBIND11_DEPRECATED
PYBIND11_DEPRECATED("Use reinterpret_borrow<object>() or reinterpret_steal<object>()") object(handle h
array::shape
const ssize_t * shape() const
Dimensions of the array.
Definition: numpy.h:805
unchecked_reference::strides_
conditional_t< Dynamic, const ssize_t *, std::array< ssize_t,(size_t) Dims > > strides_
Definition: numpy.h:413
PyArray_Proxy
Definition: numpy.h:68
npy_api::NPY_ARRAY_ALIGNED_
@ NPY_ARRAY_ALIGNED_
Definition: numpy.h:147
npy_api::NPY_ARRAY_FORCECAST_
@ NPY_ARRAY_FORCECAST_
Definition: numpy.h:145
conditional_t
typename std::conditional< B, T, F >::type conditional_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:642
unchecked_mutable_reference
Definition: numpy.h:494
name
static char name[]
Definition: rgamma.c:72
PyVoidScalarObject_Proxy::obval
PyObject_VAR_HEAD char * obval
Definition: numpy.h:80
npy_api::PyArray_Dims::ptr
Py_intptr_t * ptr
Definition: numpy.h:190
unchecked_reference::Dynamic
static constexpr bool Dynamic
Definition: numpy.h:409
npy_format_descriptor::format
static std::string format()
Definition: numpy.h:1433
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
dtype::_dtype_from_pep3118
static object _dtype_from_pep3118()
Definition: numpy.h:628
is_pod
all_of< std::is_standard_layout< T >, std::is_trivial< T > > is_pod
Definition: numpy.h:390
process_shonan_timing_results.args
args
Definition: process_shonan_timing_results.py:163
array::offset_at
ssize_t offset_at() const
Definition: numpy.h:865
field_descriptor::name
const char * name
Definition: numpy.h:1344
vectorize_returned_array
Definition: numpy.h:1761
vectorize_returned_array::call
static void call(Return *out, size_t i, Func &f, Args &...args)
Definition: numpy.h:1775
any_container
Definition: wrap/pybind11/include/pybind11/detail/common.h:1141
array_t
Definition: numpy.h:1020
npy_api::PyArrayDescr_Type_
PyTypeObject * PyArrayDescr_Type_
Definition: numpy.h:222
object
Definition: pytypes.h:347
PyArrayDescr_Proxy::type
char type
Definition: numpy.h:57
npy_api::API_PyArray_FromAny
@ API_PyArray_FromAny
Definition: numpy.h:250
npy_api::NPY_UINT8_
@ NPY_UINT8_
Definition: numpy.h:172
complex.h
multi_array_iterator::data
T * data() const
Definition: numpy.h:1603
vectorize_arg::type
conditional_t< vectorize, array_t< remove_cv_t< call_type >, array::forcecast >, T > type
Definition: numpy.h:1756
vectorize_returned_array< Func, void, Args... >::call
static void call(void *, size_t, Func &f, Args &...args)
Definition: numpy.h:1792
dict
Definition: pytypes.h:2065
dtype
Definition: numpy.h:540
array::owndata
bool owndata() const
If set, the array owns the data (will be freed when the array is deleted)
Definition: numpy.h:835
array_t::array_t
array_t(const object &o)
Definition: numpy.h:1051
array_t::array_t
array_t(ShapeContainer shape, const T *ptr=nullptr, handle base=handle())
Definition: numpy.h:1065
npy_api::lookup
static npy_api lookup()
Definition: numpy.h:265
array::array
array(const buffer_info &info, handle base=handle())
Definition: numpy.h:777
array< double >::StridesContainer
detail::any_container< ssize_t > StridesContainer
Definition: numpy.h:697
handle
Definition: pytypes.h:217
npy_api::API_PyArray_EquivTypes
@ API_PyArray_EquivTypes
Definition: numpy.h:260
type_caster
Definition: cast.h:38
vectorize_arg::call_type
remove_reference_t< T > call_type
Definition: numpy.h:1744
dtype::num
int num() const
type number of dtype.
Definition: numpy.h:611
npy_api::NPY_LONGLONG_
@ NPY_LONGLONG_
Definition: numpy.h:158
conf.release
release
Definition: gtsam/3rdparty/GeographicLib/python/doc/conf.py:69
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
make_tuple
tuple make_tuple()
Definition: cast.h:1248
multi_array_iterator::increment_common_iterator
void increment_common_iterator(size_t dim)
Definition: numpy.h:1636
PyArrayDescr_Proxy::type_num
int type_num
Definition: numpy.h:60
I
#define I
Definition: main.h:112
npy_api::PyArray_SetBaseObject_
int(* PyArray_SetBaseObject_)(PyObject *, PyObject *)
Definition: numpy.h:237
get_numpy_internals
numpy_internals & get_numpy_internals()
Definition: numpy.h:115
array_t::private_ctor
Definition: numpy.h:1022
field_descriptor::descr
dtype descr
Definition: numpy.h:1348
npy_format_descriptor< T, enable_if_t< satisfies_any_of< T, std::is_arithmetic, is_complex >::value > >::dtype
static pybind11::dtype dtype()
Definition: numpy.h:1288
object::release
handle release()
Definition: pytypes.h:368
vectorize_helper::arg_call_types
std::tuple< typename vectorize_arg< Args >::call_type... > arg_call_types
Definition: numpy.h:1830
PyArrayDescr_Proxy::elsize
int elsize
Definition: numpy.h:61
array_info_scalar::extents
static constexpr auto extents
Definition: numpy.h:331
PyArray_Proxy::base
PyObject * base
Definition: numpy.h:74
multi_array_iterator
Definition: numpy.h:1572
multi_array_iterator::container_type
std::vector< ssize_t > container_type
Definition: numpy.h:1574
npy_api::functions
functions
Definition: numpy.h:243
array::f_style
@ f_style
Definition: numpy.h:690
numpy_internals::registered_dtypes
std::unordered_map< std::type_index, numpy_type_info > registered_dtypes
Definition: numpy.h:92
npy_api::API_PyVoidArrType_Type
@ API_PyVoidArrType_Type
Definition: numpy.h:247
array::nbytes
ssize_t nbytes() const
Total number of bytes.
Definition: numpy.h:796
common_iterator
Definition: numpy.h:1544
numpy_internals::get_type_info
numpy_type_info * get_type_info(bool throw_if_missing=true)
Definition: numpy.h:106
npy_api::API_PyArray_NewCopy
@ API_PyArray_NewCopy
Definition: numpy.h:253
npy_api::NPY_UINT_
@ NPY_UINT_
Definition: numpy.h:155
vectorize
detail::vectorize_helper< Return(*)(Args...), Return, Args... > vectorize(Return(*f)(Args...))
Definition: numpy.h:1959
negation
Definition: wrap/pybind11/include/pybind11/detail/common.h:699
PyArray_Proxy::data
PyObject_HEAD char * data
Definition: numpy.h:70
remove_all_extents_t
typename array_info< T >::type remove_all_extents_t
Definition: numpy.h:364
info
else if n * info
Definition: 3rdparty/Eigen/lapack/cholesky.cpp:18
npy_api::PyArray_EquivTypes_
bool(* PyArray_EquivTypes_)(PyObject *, PyObject *)
Definition: numpy.h:226
buffer_info::ndim
ssize_t ndim
Definition: buffer_info.h:52
array::array
array(ShapeContainer shape, const T *ptr, handle base=handle())
Definition: numpy.h:770
object::borrowed_t
Definition: pytypes.h:420
gtsam.examples.DogLegOptimizerExample.run
def run(args)
Definition: DogLegOptimizerExample.py:21
npy_api::API_PyArray_DescrFromScalar
@ API_PyArray_DescrFromScalar
Definition: numpy.h:249
common_iterator::common_iterator
common_iterator()
Definition: numpy.h:1550
unchecked_reference::unchecked_reference
unchecked_reference(const void *data, const ssize_t *shape, const ssize_t *strides, enable_if_t< Dyn, ssize_t > dims)
Definition: numpy.h:431
npy_api::NPY_UBYTE_
@ NPY_UBYTE_
Definition: numpy.h:151
PYBIND11_NAMESPACE
Definition: test_custom_type_casters.cpp:24
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
array_t::mutable_unchecked
detail::unchecked_mutable_reference< T, Dims > mutable_unchecked() &
Definition: numpy.h:1120
npy_api::NPY_USHORT_
@ NPY_USHORT_
Definition: numpy.h:153
Eigen::Triplet
A small structure to hold a non zero as a triplet (i,j,value).
Definition: SparseUtil.h:162
array_info_scalar::append_extents
static void append_extents(list &)
Definition: numpy.h:332
concat
constexpr descr< 0 > concat()
Definition: descr.h:139
unchecked_mutable_reference::operator()
T & operator()(Ix... index)
Mutable, unchecked access to data at the given indices.
Definition: numpy.h:507
pyobject_caster
Definition: cast.h:914
vectorize_returned_array::call
static Return call(Func &f, Args &...args)
Definition: numpy.h:1773
npy_api::API_PyArrayDescr_Type
@ API_PyArrayDescr_Type
Definition: numpy.h:246
unchecked_reference::shape
ssize_t shape(ssize_t dim) const
Returns the shape (i.e. size) of dimension dim
Definition: numpy.h:470
pybind11_fail
PyExc_RuntimeError PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
Definition: wrap/pybind11/include/pybind11/detail/common.h:1013
vectorize_returned_array::create
static Type create(broadcast_trivial trivial, const std::vector< ssize_t > &shape)
Definition: numpy.h:1764
format_descriptor< std::array< char, N > >::format
static std::string format()
Definition: numpy.h:1184
is_pod_struct
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
Definition: numpy.h:386
common_iterator::common_iterator
common_iterator(void *ptr, const container_type &strides, const container_type &shape)
Definition: numpy.h:1552
size_t
std::size_t size_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:476
unchecked_reference::nbytes
ssize_t nbytes() const
Definition: numpy.h:490
npy_api::NPY_UINT16_
@ NPY_UINT16_
Definition: numpy.h:174
PyVoidScalarObject_Proxy
Definition: numpy.h:79
dtype::dtype
dtype(const std::string &format)
Definition: numpy.h:554
out
std::ofstream out("Result.txt")
array_t::at
const T & at(Ix... index) const
Definition: numpy.h:1095
npy_api::PyArray_Dims
Definition: numpy.h:189
PyArrayDescr_Proxy::fields
PyObject * fields
Definition: numpy.h:64
multi_array_iterator::operator++
multi_array_iterator & operator++()
Definition: numpy.h:1590
check_flags
bool check_flags(const void *ptr, int flag)
Definition: numpy.h:313
npy_format_descriptor< T, enable_if_t< array_info< T >::is_array > >::dtype
static pybind11::dtype dtype()
Definition: numpy.h:1325
format_descriptor< T, detail::enable_if_t< detail::is_pod_struct< T >::value > >::format
static std::string format()
Definition: numpy.h:1173
unchecked_reference::ndim
ssize_t ndim() const
Returns the number of dimensions of the array.
Definition: numpy.h:473
offset
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
Definition: gnuplot_common_settings.hh:64
dtype::char_
char char_() const
Definition: numpy.h:603
str
Definition: pytypes.h:1524
npy_api::PyArray_CopyInto_
int(* PyArray_CopyInto_)(PyObject *, PyObject *)
Definition: numpy.h:218
common_iterator::size_type
container_type::size_type size_type
Definition: numpy.h:1548
npy_format_descriptor::register_dtype
static void register_dtype(any_container< field_descriptor > fields)
Definition: numpy.h:1438
PyArrayDescr_Proxy::byteorder
char byteorder
Definition: numpy.h:58
is_same_ignoring_cvref
std::is_same< detail::remove_cvref_t< T >, U > is_same_ignoring_cvref
Example usage: is_same_ignoring_cvref<T, PyObject *>::value.
Definition: wrap/pybind11/include/pybind11/detail/common.h:663
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
numpy_type_info
Definition: numpy.h:86
multi_array_iterator::m_shape
container_type m_shape
Definition: numpy.h:1642
array::size
ssize_t size() const
Total number of elements.
Definition: numpy.h:786
npy_api::API_PyArray_CopyInto
@ API_PyArray_CopyInto
Definition: numpy.h:252
PYBIND11_TYPE_CASTER
#define PYBIND11_TYPE_CASTER(type, py_name)
Definition: cast.h:85
array::mutable_unchecked
detail::unchecked_mutable_reference< T, Dims > mutable_unchecked() &
Definition: numpy.h:881
field_descriptor::size
ssize_t size
Definition: numpy.h:1346
process_shonan_timing_results.names
dictionary names
Definition: process_shonan_timing_results.py:175
array::check_dimensions_impl
void check_dimensions_impl(ssize_t, const ssize_t *) const
Definition: numpy.h:996
dtype::of
static dtype of()
Return dtype associated with a C++ type.
Definition: numpy.h:587
array::check_writeable
void check_writeable() const
Definition: numpy.h:985
array::array
array(const pybind11::dtype &dt, ShapeContainer shape, const void *ptr=nullptr, handle base=handle())
Definition: numpy.h:753
bool_constant
std::integral_constant< bool, B > bool_constant
Backports of std::bool_constant and std::negation to accommodate older compilers.
Definition: wrap/pybind11/include/pybind11/detail/common.h:697
npy_api::API_PyArray_GetNDArrayCFeatureVersion
@ API_PyArray_GetNDArrayCFeatureVersion
Definition: numpy.h:244
npy_api::NPY_ULONG_
@ NPY_ULONG_
Definition: numpy.h:157
npy_api::NPY_CFLOAT_
@ NPY_CFLOAT_
Definition: numpy.h:163
unchecked_mutable_reference::operator[]
T & operator[](ssize_t index)
Definition: numpy.h:518
unchecked_reference::itemsize
constexpr static ssize_t itemsize()
Returns the item size, i.e. sizeof(T)
Definition: numpy.h:467
pybind11.h
array::array
array(const pybind11::dtype &dt, T count, const void *ptr=nullptr, handle base=handle())
Definition: numpy.h:762
numpy_internals::get_type_info
numpy_type_info * get_type_info(const std::type_info &tinfo, bool throw_if_missing=true)
Definition: numpy.h:94
npy_format_descriptor::dtype
static pybind11::dtype dtype()
Definition: numpy.h:1431
multi_array_iterator::m_common_iterator
std::array< common_iter, N > m_common_iterator
Definition: numpy.h:1644
npy_api::NPY_OBJECT_
@ NPY_OBJECT_
Definition: numpy.h:166
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
PYBIND11_OBJECT_DEFAULT
#define PYBIND11_OBJECT_DEFAULT(Name, Parent, CheckFun)
Definition: pytypes.h:1411
platform_lookup
constexpr int platform_lookup()
Definition: numpy.h:130
array
Definition: numpy.h:684
field_descriptor
Definition: numpy.h:1343
array::squeeze
array squeeze()
Return a new view with all of the dimensions of length 1 removed.
Definition: numpy.h:909
arr
py::array arr
Definition: test_numpy_array.cpp:77
npy_api::NPY_SHORT_
@ NPY_SHORT_
Definition: numpy.h:152
internals::direct_conversions
type_map< std::vector< bool(*)(PyObject *, void *&)> > direct_conversions
Definition: internals.h:176
npy_api::NPY_FLOAT_
@ NPY_FLOAT_
Definition: numpy.h:160
make_index_sequence
typename make_index_sequence_impl< N >::type make_index_sequence
Definition: wrap/pybind11/include/pybind11/detail/common.h:679
handle_type_name
Definition: cast.h:873
handle::ptr
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:241
array::data
const void * data(Ix... index) const
Definition: numpy.h:842
multi_array_iterator::multi_array_iterator
multi_array_iterator(const std::array< buffer_info, N > &buffers, const container_type &shape)
Definition: numpy.h:1576
Eigen::array::back
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T & back()
Definition: EmulateArray.h:39
array::raw_array
static PyObject * raw_array(PyObject *ptr, int ExtraFlags=0)
Create array from any object – always returns a new reference.
Definition: numpy.h:1009
forward
Definition: testScenarioRunner.cpp:79
module_
Wrapper for Python extension modules.
Definition: pybind11.h:1153
K
#define K
Definition: igam.h:8
npy_api::API_PyArray_SetBaseObject
@ API_PyArray_SetBaseObject
Definition: numpy.h:262
dtype::dtype
dtype(int typenum)
Definition: numpy.h:569
format_descriptor< T, detail::enable_if_t< std::is_enum< T >::value > >::format
static std::string format()
Definition: numpy.h:1189
PYBIND11_WARNING_DISABLE_MSVC
PYBIND11_WARNING_PUSH PYBIND11_WARNING_DISABLE_MSVC(5054) PYBIND11_WARNING_POP static_assert(EIGEN_VERSION_AT_LEAST(3
array::dtype
pybind11::dtype dtype() const
Array descriptor (dtype)
Definition: numpy.h:781
npy_api::NPY_LONGDOUBLE_
@ NPY_LONGDOUBLE_
Definition: numpy.h:162
handle::handle
handle()=default
The default constructor creates a handle with a nullptr-valued pointer.
leaf::values
leaf::MyValues values
PyArray_Proxy::dimensions
ssize_t * dimensions
Definition: numpy.h:72
array_t::unchecked
detail::unchecked_reference< T, Dims > unchecked() const &
Definition: numpy.h:1132
array_t::raw_array_t
static PyObject * raw_array_t(PyObject *ptr)
Create array from any object – always returns a new reference.
Definition: numpy.h:1156
npy_api::PyArray_GetArrayParamsFromObject_
int(* PyArray_GetArrayParamsFromObject_)(PyObject *, PyObject *, unsigned char, PyObject **, int *, Py_intptr_t *, PyObject **, PyObject *)
Definition: numpy.h:227
array::byte_offset
ssize_t byte_offset(Ix... index) const
Definition: numpy.h:980
array::reshape
array reshape(ShapeContainer new_shape)
Optional order parameter omitted, to be added as needed.
Definition: numpy.h:934
iter
iterator iter(handle obj)
Definition: pytypes.h:2428
DECL_NPY_API
#define DECL_NPY_API(Func)
vectorize_helper::apply_broadcast
void apply_broadcast(std::array< buffer_info, NVectorized > &buffers, std::array< void *, N > &params, Return *out, size_t size, const std::vector< ssize_t > &output_shape, index_sequence< Index... >, index_sequence< VIndex... >, index_sequence< BIndex... >)
Definition: numpy.h:1925
array::ensure
static array ensure(handle h, int ExtraFlags=0)
Definition: numpy.h:962
std
Definition: BFloat16.h:88
array::c_style
@ c_style
Definition: numpy.h:689
vectorize_helper::param_n_t
typename std::tuple_element< Index, arg_call_types >::type param_n_t
Definition: numpy.h:1832
args
Definition: pytypes.h:2163
test_eigen_tensor.dtype
dtype
Definition: test_eigen_tensor.py:24
array_t::array_t
array_t(ShapeContainer shape, StridesContainer strides, const T *ptr=nullptr, handle base=handle())
Definition: numpy.h:1059
vectorize_returned_array< Func, void, Args... >::create
static Type create(broadcast_trivial, const std::vector< ssize_t > &)
Definition: numpy.h:1783
buffer_info
Information record describing a Python buffer object.
Definition: buffer_info.h:46
array_t::array_t
array_t(const buffer_info &info, handle base=handle())
Definition: numpy.h:1057
npy_api::get
static npy_api & get()
Definition: numpy.h:194
npy_api::NPY_UINT64_
@ NPY_UINT64_
Definition: numpy.h:184
npy_api::PyVoidArrType_Type_
PyTypeObject * PyVoidArrType_Type_
Definition: numpy.h:221
unchecked_reference::size
enable_if_t<!Dyn, ssize_t > size() const
Definition: numpy.h:478
array_t::array_t
array_t(handle h, borrowed_t)
Definition: numpy.h:1037
array::resize
void resize(ShapeContainer new_shape, bool refcheck=true)
Definition: numpy.h:917
dtype::kind
char kind() const
Definition: numpy.h:599
tuple
Definition: pytypes.h:2035
make_changelog.api
api
Definition: make_changelog.py:25
offsets
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
Definition: gnuplot_common_settings.hh:27
array_info
Definition: numpy.h:338
multi_array_iterator::init_common_iterator
void init_common_iterator(const buffer_info &buffer, const container_type &shape, common_iter &iterator, container_type &strides)
Definition: numpy.h:1610
field_descriptor::format
std::string format
Definition: numpy.h:1347
list::append
void append(T &&val)
Definition: pytypes.h:2145
array_t::mutable_at
T & mutable_at(Ix... index)
Definition: numpy.h:1105
dtype::alignment
int alignment() const
Alignment of the data type.
Definition: numpy.h:622
unchecked_reference::data
const T * data(Ix... ix) const
Pointer access to the data at the given indices.
Definition: numpy.h:462
unchecked_reference::size
enable_if_t< Dyn, ssize_t > size() const
Definition: numpy.h:483
array_t::index_at
ssize_t index_at(Ix... index) const
Definition: numpy.h:1079
vectorize_returned_array< Func, void, Args... >::call
static detail::void_type call(Func &f, Args &...args)
Definition: numpy.h:1787
numpy_type_info::dtype_ptr
PyObject * dtype_ptr
Definition: numpy.h:87
U
@ U
Definition: testDecisionTree.cpp:296
pybind11
Definition: wrap/pybind11/pybind11/__init__.py:1
PyArray_Proxy::flags
int flags
Definition: numpy.h:76
format_descriptor< char[N]>::format
static std::string format()
Definition: numpy.h:1180
array::base
object base() const
Base object.
Definition: numpy.h:802
PyArrayDescr_Proxy::flags
char flags
Definition: numpy.h:59
npy_api::NPY_ARRAY_OWNDATA_
@ NPY_ARRAY_OWNDATA_
Definition: numpy.h:144
vectorize_extractor
vectorize_helper< Func, Return, Args... > vectorize_extractor(const Func &f, Return(*)(Args...))
Definition: numpy.h:1945
PYBIND11_DECL_CHAR_FMT
#define PYBIND11_DECL_CHAR_FMT
Definition: numpy.h:1300
PyVoidScalarObject_Proxy::descr
PyArrayDescr_Proxy * descr
Definition: numpy.h:81
unchecked_reference::dims_
const ssize_t dims_
Definition: numpy.h:414
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2399
N
#define N
Definition: igam.h:9
register_structured_dtype
PYBIND11_NOINLINE void register_structured_dtype(any_container< field_descriptor > fields, const std::type_info &tinfo, ssize_t itemsize, bool(*direct_converter)(PyObject *, void *&))
Definition: numpy.h:1351
unchecked_reference::operator[]
const T & operator[](ssize_t index) const
Definition: numpy.h:456
object::cast
T cast() const &
Definition: cast.h:1169
array_descriptor_proxy
PyArrayDescr_Proxy * array_descriptor_proxy(PyObject *ptr)
Definition: numpy.h:305
PyArray_Proxy::descr
PyObject * descr
Definition: numpy.h:75
compare_buffer_info
Definition: buffer_info.h:41
Eigen::placeholders::end
static const EIGEN_DEPRECATED end_t end
Definition: IndexedViewHelper.h:181
none
Definition: pytypes.h:1744
load_numpy_internals
PYBIND11_NOINLINE void load_numpy_internals(numpy_internals *&ptr)
Definition: numpy.h:111
unchecked_reference::unchecked_reference
unchecked_reference(const void *data, const ssize_t *shape, const ssize_t *strides, enable_if_t<!Dyn, ssize_t >)
Definition: numpy.h:419
npy_api::NPY_INT32_
@ NPY_INT32_
Definition: numpy.h:178
npy_api::PyArray_Dims::len
int len
Definition: numpy.h:191
c_strides
std::vector< ssize_t > c_strides(const std::vector< ssize_t > &shape, ssize_t itemsize)
Definition: buffer_info.h:19
array_t::array_t
array_t(private_ctor, ShapeContainer &&shape, StridesContainer &&strides, const T *ptr, handle base)
Definition: numpy.h:1024
array_info_scalar::is_empty
static constexpr bool is_empty
Definition: numpy.h:330
npy_format_descriptor_name
Definition: numpy.h:1231
common_iterator::container_type
std::vector< ssize_t > container_type
Definition: numpy.h:1546
max
#define max(a, b)
Definition: datatypes.h:20
npy_api::constants
constants
Definition: numpy.h:141
common_iterator::increment
void increment(size_type dim)
Definition: numpy.h:1562
vectorize_helper::operator()
object operator()(typename vectorize_arg< Args >::type... args)
Definition: numpy.h:1818
gtsam::convert
static BinaryMeasurement< Rot3 > convert(const BetweenFactor< Pose3 >::shared_ptr &f)
Definition: ShonanAveraging.cpp:998
gtsam.examples.ShonanAveragingCLI.str
str
Definition: ShonanAveragingCLI.py:115
npy_api::API_PyArray_DescrNewFromType
@ API_PyArray_DescrNewFromType
Definition: numpy.h:255
get
Container::iterator get(Container &c, Position position)
Definition: stdlist_overload.cpp:29
npy_api::API_PyArray_View
@ API_PyArray_View
Definition: numpy.h:258
enable_if_t
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
Definition: wrap/pybind11/include/pybind11/detail/common.h:640
npy_api::API_PyArray_Type
@ API_PyArray_Type
Definition: numpy.h:245
same_size::as
bool_constant< sizeof(T)==sizeof(U)> as
Definition: numpy.h:126
gtsam::equal
bool equal(const T &obj1, const T &obj2, double tol)
Definition: Testable.h:85
array::flags
int flags() const
Return the NumPy array flags.
Definition: numpy.h:827
PyArrayDescr_Proxy::typeobj
PyObject_HEAD PyObject * typeobj
Definition: numpy.h:55
test_callbacks.value
value
Definition: test_callbacks.py:158
array::writeable
bool writeable() const
If set, the array is writeable (otherwise the buffer is read-only)
Definition: numpy.h:830
object::is_borrowed
bool is_borrowed
Definition: pytypes.h:353
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
dtype::itemsize
ssize_t itemsize() const
Size of the data type in bytes.
Definition: numpy.h:592
dtype::from_args
static dtype from_args(const object &args)
This is essentially the same as calling numpy.dtype(args) in Python.
Definition: numpy.h:577
Eigen::internal::cast
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
Definition: Eigen/src/Core/MathFunctions.h:460
pyobject_caster< array_t< T, ExtraFlags > >::load
bool load(handle src, bool convert)
Definition: numpy.h:1209
common_iterator::data
void * data() const
Definition: numpy.h:1564
array_proxy
PyArray_Proxy * array_proxy(void *ptr)
Definition: numpy.h:299
array::offset_at
ssize_t offset_at(Ix... index) const
Definition: numpy.h:858
array::strides
const ssize_t * strides() const
Strides of the array.
Definition: numpy.h:816
npy_api::API_PyArray_GetArrayParamsFromObject
@ API_PyArray_GetArrayParamsFromObject
Definition: numpy.h:261
unchecked_reference::operator()
const T & operator()(Ix... index) const
Definition: numpy.h:445
broadcast_trivial::f_trivial
@ f_trivial
remove_reference_t
typename std::remove_reference< T >::type remove_reference_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:646
npy_api::NPY_CLONGDOUBLE_
@ NPY_CLONGDOUBLE_
Definition: numpy.h:165
npy_api::NPY_UNICODE_
@ NPY_UNICODE_
Definition: numpy.h:168
dtype::dtype
dtype(const char *format)
Definition: numpy.h:556
array::index_at
ssize_t index_at(Ix... index) const
Definition: numpy.h:870
array::check_dimensions_impl
void check_dimensions_impl(ssize_t axis, const ssize_t *shape, ssize_t i, Ix... index) const
Definition: numpy.h:999
complex
Definition: datatypes.h:12
array::array
array(ssize_t count, const T *ptr, handle base=handle())
Definition: numpy.h:774
PyArrayDescr_Proxy::names
PyObject * names
Definition: numpy.h:65
dtype::dtype
dtype(list names, list formats, list offsets, ssize_t itemsize)
Definition: numpy.h:558
PYBIND11_WARNING_DISABLE_CLANG
#define PYBIND11_WARNING_DISABLE_CLANG(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:61
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
PyArrayDescr_Proxy::kind
char kind
Definition: numpy.h:56
pybind11.msg
msg
Definition: wrap/pybind11/pybind11/__init__.py:4
npy_format_descriptor< T, enable_if_t< is_same_ignoring_cvref< T, PyObject * >::value > >::dtype
static pybind11::dtype dtype()
Definition: numpy.h:1297
npy_format_descriptor< T, enable_if_t< std::is_enum< T >::value > >::dtype
static pybind11::dtype dtype()
Definition: numpy.h:1340
vectorize_helper::apply_trivial
void apply_trivial(std::array< buffer_info, NVectorized > &buffers, std::array< void *, N > &params, Return *out, size_t size, index_sequence< Index... >, index_sequence< VIndex... >, index_sequence< BIndex... >)
Definition: numpy.h:1899
PyArrayDescr_Proxy
Definition: numpy.h:53
npy_api::NPY_VOID_
@ NPY_VOID_
Definition: numpy.h:169


gtsam
Author(s):
autogenerated on Sat Jun 1 2024 03:02:27