26 # pragma warning(push) 27 # pragma warning(disable : 4127) // C4127: conditional expression is constant 28 # pragma warning(disable : 5054) // https://github.com/pybind/pybind11/pull/3741 33 #include <Eigen/SparseCore> 43 "Eigen support in pybind11 requires Eigen >= 3.2.7");
51 template <typename MatrixType>
56 #if EIGEN_VERSION_AT_LEAST(3, 3, 0) 58 template <
typename Scalar,
int Flags,
typename StorageIndex>
62 template <
typename Scalar,
int Flags,
typename StorageIndex>
69 std::is_base_of<Eigen::MapBase<T, Eigen::ReadOnlyAccessors>,
T>>;
87 template <
bool EigenRowMajor>
98 : conformable{
true}, rows{r},
cols{
c},
101 stride{EigenRowMajor ? (rstride > 0 ? rstride : 0)
102 : (cstride > 0 ? cstride : 0) ,
103 EigenRowMajor ? (cstride > 0 ? cstride : 0)
104 : (rstride > 0 ? rstride : 0) },
105 negativestrides{rstride < 0 || cstride < 0} {}
108 :
EigenConformable(r, c, r == 1 ? c * stride : stride, c == 1 ? r : r * stride) {}
110 template <
typename props>
116 if (negativestrides) {
119 if (rows == 0 ||
cols == 0) {
123 || (EigenRowMajor ?
cols :
rows) == 1)
125 || (EigenRowMajor ?
rows :
cols) == 1);
131 template <
typename Type>
135 template <
typename PlainObjectType,
int MapOptions,
typename Str
ideType>
139 template <
typename PlainObjectType,
int Options,
typename Str
ideType>
145 template <
typename Type_>
151 size = Type::SizeAtCompileTime;
152 static constexpr
bool row_major = Type::IsRowMajor,
154 = Type::IsVectorAtCompileTime,
157 dynamic = !fixed_rows && !fixed_cols;
159 template <EigenIndex i, EigenIndex ifzero>
160 using if_zero = std::integral_constant<EigenIndex, i == 0 ? ifzero : i>;
163 outer_stride =
if_zero < StrideType::OuterStrideAtCompileTime,
167 static constexpr
bool dynamic_stride
169 static constexpr
bool requires_row_major
170 = !dynamic_stride && !vector && (row_major ? inner_stride : outer_stride) == 1;
171 static constexpr
bool requires_col_major
172 = !dynamic_stride && !vector && (row_major ? outer_stride : inner_stride) == 1;
178 const auto dims = a.
ndim();
179 if (dims < 1 || dims > 2) {
193 return {np_rows, np_cols, np_rstride, np_cstride};
205 return {rows == 1 ? 1 :
n, cols == 1 ? 1 :
n,
stride};
225 static constexpr
bool show_writeable
228 static constexpr
bool show_c_contiguous = show_order && requires_row_major;
229 static constexpr
bool show_f_contiguous
230 = !show_c_contiguous && show_order && requires_col_major;
232 static constexpr
auto descriptor
243 const_name<show_writeable>(
", flags.writeable",
"")
244 + const_name<show_c_contiguous>(
", flags.c_contiguous",
"")
245 + const_name<show_f_contiguous>(
", flags.f_contiguous",
"") +
const_name(
"]");
250 template <
typename props>
256 a =
array({src.size()}, {elem_size * src.innerStride()}, src.data(),
base);
258 a =
array({src.rows(), src.cols()},
259 {elem_size * src.rowStride(), elem_size * src.colStride()},
265 array_proxy(a.
ptr())->flags &= ~detail::npy_api::NPY_ARRAY_WRITEABLE_;
275 template <
typename props,
typename Type>
289 return eigen_ref_array<props>(*src,
base);
294 template <
typename Type>
312 auto dims = buf.ndim();
313 if (dims < 1 || dims > 2) {
317 auto fits = props::conformable(buf);
324 auto ref = reinterpret_steal<array>(eigen_ref_array<props>(
value));
327 }
else if (
ref.ndim() == 1) {
343 template <
typename CType>
348 return eigen_encapsulate<props>(src);
350 return eigen_encapsulate<props>(
new CType(std::move(*src)));
355 return eigen_ref_array<props>(*src);
357 return eigen_ref_array<props>(*src, parent);
359 throw cast_error(
"unhandled return_value_policy: should not happen!");
378 return cast_impl(&src, policy, parent);
386 return cast(&src, policy, parent);
390 return cast_impl(src, policy, parent);
394 return cast_impl(src, policy, parent);
397 static constexpr
auto name = props::descriptor;
405 template <
typename T>
413 template <
typename MapType>
437 pybind11_fail(
"Invalid return_value_policy for Eigen Map/Ref/Block type");
441 static constexpr
auto name = props::descriptor;
446 bool load(
handle,
bool) =
delete;
453 template <
typename Type>
458 template <
typename PlainObjectType,
typename Str
ideType>
460 Eigen::Ref<PlainObjectType, 0, StrideType>,
461 enable_if_t<is_eigen_dense_map<Eigen::Ref<PlainObjectType, 0, StrideType>>::value>>
471 | ((props::row_major ? props::inner_stride : props::outer_stride) == 1
473 : (props::row_major ? props::outer_stride : props::inner_stride) == 1
478 std::unique_ptr<MapType>
map;
479 std::unique_ptr<Type>
ref;
492 bool need_copy = !isinstance<Array>(src);
498 auto aref = reinterpret_borrow<Array>(src);
500 if (aref && (!need_writeable || aref.writeable())) {
501 fits = props::conformable(aref);
505 if (!fits.template stride_compatible<props>()) {
508 copy_or_ref = std::move(aref);
519 if (!convert || need_writeable) {
527 fits = props::conformable(copy);
528 if (!fits || !fits.template stride_compatible<props>()) {
531 copy_or_ref = std::move(copy);
539 make_stride(fits.stride.outer(), fits.stride.inner())));
540 ref.reset(
new Type(*map));
546 operator Type *() {
return ref.get(); }
548 operator Type &() {
return *
ref; }
549 template <
typename _T>
565 template <
typename S>
571 template <
typename S>
577 template <
typename S>
583 template <
typename S>
596 return S(outer, inner);
612 template <
typename Type>
625 return cast(*src, policy, parent);
628 static constexpr
auto name = props::descriptor;
633 bool load(
handle,
bool) =
delete;
634 operator Type() =
delete;
639 template <
typename Type>
644 static constexpr
bool rowMajor = Type::IsRowMajor;
651 auto obj = reinterpret_borrow<object>(src);
653 object matrix_type = sparse_module.attr(rowMajor ?
"csr_matrix" :
"csc_matrix");
657 obj = matrix_type(obj);
666 auto shape = pybind11::tuple((pybind11::object) obj.attr(
"shape"));
667 auto nnz = obj.attr(
"nnz").
cast<
Index>();
669 if (!
values || !innerIndices || !outerIndices) {
689 =
module_::import(
"scipy.sparse").attr(rowMajor ?
"csr_matrix" :
"csc_matrix");
691 array data(src.nonZeros(), src.valuePtr());
692 array outerIndices((rowMajor ? src.rows() : src.cols()) + 1, src.outerIndexPtr());
693 array innerIndices(src.nonZeros(), src.innerIndexPtr());
696 std::move(
data), std::move(innerIndices), std::move(outerIndices)),
702 const_name<(Type::IsRowMajor) != 0>(
"scipy.sparse.csr_matrix[",
703 "scipy.sparse.csc_matrix[")
#define EIGEN_DEFAULT_DENSE_INDEX_TYPE
ssize_t ndim() const
Number of dimensions.
static handle cast(const Type &src, return_value_policy policy, handle parent)
typename Type::Index Index
static handle handle_of()
typename eigen_extract_stride< Type >::type StrideType
bool load(handle src, bool convert)
const ssize_t * strides() const
Strides of the array.
A matrix or vector expression mapping an existing array of data.
const ssize_t * shape() const
Dimensions of the array.
pybind11::detail::cast_op_type< _T > cast_op_type
#define PYBIND11_SILENCE_MSVC_C4127(...)
all_of< is_template_base_of< Eigen::EigenBase, T >, negation< any_of< is_eigen_dense_map< T >, is_eigen_dense_plain< T >, is_eigen_sparse< T > >> > is_eigen_other
bool_constant< S::InnerStrideAtCompileTime !=Eigen::Dynamic &&S::OuterStrideAtCompileTime !=Eigen::Dynamic &&std::is_default_constructible< S >::value > stride_ctor_default
decltype(is_template_base_of_impl< Base >::check((intrinsic_t< T > *) nullptr)) is_template_base_of
Namespace containing all symbols from the Eigen library.
bool load(handle src, bool)
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index outer() const
bool isinstance(handle obj)
static handle cast(const Type *src, return_value_policy policy, handle parent)
#define PYBIND11_NAMESPACE
handle eigen_ref_array(Type &src, handle parent=none())
static S make_stride(EigenIndex outer, EigenIndex)
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index inner() const
static handle cast(const MapType &src, return_value_policy policy, handle parent)
PyExc_RuntimeError [[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
handle eigen_encapsulate(Type *src)
bool_constant<!any_of< stride_ctor_default< S >, stride_ctor_dual< S > >::value &&S::OuterStrideAtCompileTime==Eigen::Dynamic &&S::InnerStrideAtCompileTime !=Eigen::Dynamic &&std::is_constructible< S, EigenIndex >::value > stride_ctor_outer
const Scalar * data(Array &a)
movable_cast_op_type< T > cast_op_type
static handle cast(Type *src, return_value_policy policy, handle parent)
#define EIGEN_VERSION_AT_LEAST(x, y, z)
std::integral_constant< EigenIndex, i==0 ? ifzero :i > if_zero
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
static handle cast(const Type &&src, return_value_policy, handle parent)
std::integral_constant< bool, B > bool_constant
Backports of std::bool_constant and std::negation to accommodate older compilers. ...
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
static handle cast(Type &src, return_value_policy policy, handle parent)
conditional_t< std::is_pointer< typename std::remove_reference< T >::type >::value, typename std::add_pointer< intrinsic_t< T > >::type, conditional_t< std::is_rvalue_reference< T >::value, typename std::add_rvalue_reference< intrinsic_t< T > >::type, typename std::add_lvalue_reference< intrinsic_t< T > >::type > > movable_cast_op_type
typename std::remove_reference< T >::type remove_reference_t
std::is_base_of< Eigen::MapBase< T, Eigen::WriteAccessors >, T > is_eigen_mutable_map
all_of< is_template_base_of< Eigen::DenseBase, T >, std::is_base_of< Eigen::MapBase< T, Eigen::ReadOnlyAccessors >, T > > is_eigen_dense_map
static handle cast(const Type &src, return_value_policy, handle)
std::unique_ptr< MapType > map
Reference counting helper.
static handle cast(Type &&src, return_value_policy, handle parent)
static handle cast(const Type &src, return_value_policy, handle)
A matrix or vector expression mapping an existing expression.
std::is_same< bools< Ts::value..., true >, bools< true, Ts::value... > > all_of
const T * data(Ix... index) const
T * mutable_data(Ix... index)
static array ensure(handle h, int ExtraFlags=0)
EIGEN_DEFAULT_DENSE_INDEX_TYPE EigenIndex
static S make_stride(EigenIndex, EigenIndex inner)
Map< MatrixType > MapType
static module_ import(const char *name)
Import and return a module or throws error_already_set.
all_of< negation< is_eigen_dense_map< T > >, is_template_base_of< Eigen::PlainObjectBase, T > > is_eigen_dense_plain
handle eigen_array_cast(typename props::Type const &src, handle base=handle(), bool writeable=true)
remove_reference_t< decltype(*std::declval< Type >().outerIndexPtr())> StorageIndex
is_template_base_of< Eigen::SparseMatrixBase, T > is_eigen_sparse
typename Type::Scalar Scalar
static S make_stride(EigenIndex outer, EigenIndex inner)
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
bool_constant<!any_of< stride_ctor_default< S >, stride_ctor_dual< S > >::value &&S::InnerStrideAtCompileTime==Eigen::Dynamic &&S::OuterStrideAtCompileTime !=Eigen::Dynamic &&std::is_constructible< S, EigenIndex >::value > stride_ctor_inner
std::unique_ptr< Type > ref
Annotation for function names.
typename props::Scalar Scalar
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > Matrix
Annotation indicating that a class derives from another given type.
PyObject * ptr() const
Return the underlying PyObject * pointer.
Container::iterator get(Container &c, Position position)
static handle cast_impl(CType *src, return_value_policy policy, handle parent)
The matrix class, also used for vectors and row-vectors.
bool_constant<!stride_ctor_default< S >::value &&std::is_constructible< S, EigenIndex, EigenIndex >::value > stride_ctor_dual
static EigenConformable< row_major > conformable(const array &a)
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.
static S make_stride(EigenIndex, EigenIndex)
typename Type::Scalar Scalar
static PYBIND11_NOINLINE void add_patient(handle h)
#define PYBIND11_TYPE_CASTER(type, py_name)
#define PYBIND11_NAMESPACE_END(name)
static BinaryMeasurement< Rot3 > convert(const BetweenFactor< Pose3 >::shared_ptr &f)
static handle cast(const Type *src, return_value_policy policy, handle parent)
#define PYBIND11_NAMESPACE_BEGIN(name)
PyArray_Proxy * array_proxy(void *ptr)
bool load(handle src, bool convert)