Go to the documentation of this file.
14 #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
15 static_assert(__GNUC__ > 5,
"Eigen Tensor support in pybind11 requires GCC > 5.0");
22 #if defined(__MINGW32__)
26 #include <unsupported/Eigen/CXX11/Tensor>
31 "Eigen Tensor support in pybind11 requires Eigen >= 3.3.0");
45 static_assert((
static_cast<int>(T::Layout) ==
static_cast<int>(
Eigen::RowMajor))
47 "Layout must be row or column major");
55 template <
typename Scalar_,
int NumIndices_,
int Options_,
typename IndexType>
61 return f.dimensions();
72 template <
size_t... Is>
77 static constexpr
auto dimensions_descriptor
78 = helper<decltype(make_index_sequence<Type::NumIndices>())>::
value;
80 template <
typename... Args>
82 return new Type(std::forward<Args>(
args)...);
85 static void free(
Type *tensor) {
delete tensor; }
88 template <
typename Scalar_,
typename std::ptrdiff_t...
Indices,
int Options_,
typename IndexType>
90 Eigen::TensorFixedSize<Scalar_, Eigen::Sizes<Indices...>, Options_, IndexType>> {
105 return get_shape() == shape;
108 static constexpr
auto dimensions_descriptor
111 template <
typename... Args>
124 template <
typename Type,
bool ShowDetails,
bool NeedsWriteable = false>
127 = const_name<NeedsWriteable>(
", flags.writeable",
"")
128 + const_name<static_cast<int>(Type::Layout) ==
static_cast<int>(
Eigen::RowMajor)>(
129 ", flags.c_contiguous",
", flags.f_contiguous");
130 static constexpr
auto value
141 PYBIND11_WARNING_PUSH
144 template <typename
T,
int size>
148 for (
size_t i = 0;
i <
size;
i++) {
155 template <
typename T,
int size>
158 const T *shape =
arr.shape();
159 for (
size_t i = 0;
i <
size;
i++) {
168 template <
typename Type>
178 if (!isinstance<array>(src)) {
186 if (!temp.
dtype().is(dtype::of<typename Type::Scalar>())) {
192 reinterpret_borrow<object>(src));
194 if (
arr.ndim() != Type::NumIndices) {
197 auto shape = get_shape_for_array<typename Type::Index, Type::NumIndices>(
arr);
199 if (!Helper::is_correct_shape(shape)) {
203 #if EIGEN_VERSION_AT_LEAST(3, 4, 0)
204 auto data_pointer =
arr.data();
222 pybind11_fail(
"Cannot use a reference return value policy for an rvalue");
230 pybind11_fail(
"Cannot use a reference return value policy for an rvalue");
240 return cast_impl(&src, policy, parent);
248 return cast(&src, policy, parent);
257 return cast_impl(src, policy, parent);
266 return cast_impl(src, policy, parent);
269 template <
typename C>
271 object parent_object;
272 bool writeable =
false;
279 src = Helper::alloc(std::move(*src));
282 =
capsule(src, [](
void *ptr) { Helper::free(
reinterpret_cast<Type *
>(ptr)); });
290 Helper::free(
const_cast<Type *
>(src));
295 =
capsule(src, [](
void *ptr) { Helper::free(
reinterpret_cast<Type *
>(ptr)); });
304 parent_object =
none();
311 pybind11_fail(
"Cannot use reference internal when there is no parent");
313 parent_object = reinterpret_borrow<object>(parent);
318 pybind11_fail(
"pybind11 bug in eigen.h, please file a bug report");
332 template <
typename StoragePointerType,
333 bool needs_writeable,
336 #if EIGEN_VERSION_AT_LEAST(3, 4, 0)
337 return reinterpret_cast<StoragePointerType
>(
arr.data());
340 return reinterpret_cast<StoragePointerType
>(
const_cast<void *
>(
arr.data()));
344 template <
typename StoragePointerType,
345 bool needs_writeable,
348 return reinterpret_cast<StoragePointerType
>(
arr.mutable_data());
351 template <
typename T,
typename =
void>
354 template <
typename MapType>
356 using SPT =
typename MapType::StoragePointerType;
359 template <
typename MapType>
361 using SPT =
typename MapType::PointerArgType;
364 template <
typename Type,
int Options>
374 if (!isinstance<array>(src)) {
377 auto arr = reinterpret_borrow<array>(src);
378 if ((
arr.flags() & compute_array_flag_from_tensor<Type>()) == 0) {
382 if (!
arr.dtype().is(dtype::of<typename Type::Scalar>())) {
386 if (
arr.ndim() != Type::NumIndices) {
396 auto shape = get_shape_for_array<typename Type::Index, Type::NumIndices>(
arr);
398 if (!Helper::is_correct_shape(shape)) {
402 if (needs_writeable && !
arr.writeable()) {
406 auto result = get_array_data_for_type<typename get_storage_pointer_type<MapType>::SPT,
407 needs_writeable>(
arr);
415 return cast_impl(&src, policy, parent);
419 return cast_impl(&src, policy, parent);
427 return cast_impl(&src, policy, parent);
435 return cast(&src, policy, parent);
444 return cast_impl(src, policy, parent);
453 return cast_impl(src, policy, parent);
456 template <
typename C>
458 object parent_object;
462 parent_object =
none();
468 pybind11_fail(
"Cannot use reference internal when there is no parent");
470 parent_object = reinterpret_borrow<object>(parent);
475 pybind11_fail(
"Invalid return_value_policy for Eigen Map type, must be either "
476 "reference or reference_internal");
482 std::move(parent_object));
491 #if EIGEN_VERSION_AT_LEAST(3, 4, 0)
493 static constexpr
bool needs_writeable = !std::is_const<
typename std::remove_pointer<
510 template <
typename T_>
static handle cast(const MapType &&src, return_value_policy policy, handle parent)
static bool is_correct_shape(const Eigen::DSizes< typename Type::Index, Type::NumIndices > &shape)
Namespace containing all symbols from the Eigen library.
Annotation for function names.
typename MapType::PointerArgType SPT
#define PYBIND11_WARNING_DISABLE_GCC(name)
constexpr descr< N - 1 > const_name(char const (&text)[N])
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
void deallocate(pointer p, size_type)
typename void_t_impl< Ts... >::type void_t
static constexpr auto value
static handle cast_impl(C *src, return_value_policy policy, handle parent)
PYBIND11_WARNING_PUSH PYBIND11_WARNING_POP
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)
static constexpr Eigen::DSizes< typename Type::Index, Type::NumIndices > get_shape(const Type &)
static void free(Type *tensor)
static handle cast(const itype &src, return_value_policy policy, handle parent)
StoragePointerType get_array_data_for_type(array &arr)
STL compatible allocator to use with types requiring a non standrad alignment.
constexpr int compute_array_flag_from_tensor()
::pybind11::detail::movable_cast_op_type< T_ > cast_op_type
bool load(handle src, bool convert)
static handle cast(Type &src, return_value_policy policy, handle parent)
PYBIND11_WARNING_PUSH std::vector< T > convert_dsizes_to_vector(const Eigen::DSizes< T, size > &arr)
bool load(handle src, bool)
bool is_tensor_aligned(const void *data)
static handle cast(const MapType &src, return_value_policy policy, handle parent)
static void free(Type *tensor)
static handle cast(MapType *src, return_value_policy policy, handle parent)
A tensor expression mapping an existing array of data.
static handle cast(Type &&src, return_value_policy policy, handle parent)
static handle cast(MapType &&src, return_value_policy policy, handle parent)
static constexpr auto details
std::unique_ptr< MapType > value
typename MapType::StoragePointerType SPT
A small structure to hold a non zero as a triplet (i,j,value).
constexpr descr< 0 > concat()
static Type * alloc(Args &&...args)
static handle cast(const MapType *src, return_value_policy policy, handle parent)
PyExc_RuntimeError PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
static Eigen::DSizes< typename Type::Index, Type::NumIndices > get_shape(const Type &f)
static handle cast(const Type *src, return_value_policy policy, handle parent)
Eigen::DSizes< T, size > get_shape_for_array(const array &arr)
#define PYBIND11_EIGEN_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
#define PYBIND11_TYPE_CASTER(type, py_name)
static Type * alloc(Args &&...args)
Matrix< Scalar, Dynamic, Dynamic > C
PYBIND11_WARNING_PUSH PYBIND11_WARNING_DISABLE_MSVC(5054) PYBIND11_WARNING_POP static_assert(EIGEN_VERSION_AT_LEAST(3
pybind11::dtype dtype() const
Array descriptor (dtype)
static handle cast(Type *src, return_value_policy policy, handle parent)
static array ensure(handle h, int ExtraFlags=0)
static handle cast(const Type &src, return_value_policy policy, handle parent)
static handle cast(MapType &src, return_value_policy policy, handle parent)
The fixed sized version of the tensor class.
static constexpr bool is_correct_shape(const Eigen::DSizes< typename Type::Index, Type::NumIndices > &)
static constexpr Eigen::DSizes< typename Type::Index, Type::NumIndices > get_shape()
static handle cast_impl(C *src, return_value_policy policy, handle parent)
static handle cast(const Type &&src, return_value_policy policy, handle parent)
pointer allocate(size_type num, const void *=0)
std::vector< size_t > Indices
#define EIGEN_VERSION_AT_LEAST(x, y, z)
static BinaryMeasurement< Rot3 > convert(const BetweenFactor< Pose3 >::shared_ptr &f)
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
Map< MatrixType > MapType
PyArray_Proxy * array_proxy(void *ptr)
gtsam
Author(s):
autogenerated on Thu Apr 10 2025 03:04:28