14 #if defined(__INTEL_COMPILER) 15 # pragma warning(disable: 1682) // implicit conversion of a 64-bit integral type to a smaller integral type (potential portability problem) 16 #elif defined(__GNUG__) || defined(__clang__) 17 # pragma GCC diagnostic push 18 # pragma GCC diagnostic ignored "-Wconversion" 19 # pragma GCC diagnostic ignored "-Wdeprecated-declarations" 23 # pragma GCC diagnostic ignored "-Wdeprecated" 26 # pragma GCC diagnostic ignored "-Wint-in-bool-context" 31 # pragma warning(push) 32 # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant 33 # pragma warning(disable: 4996) // warning C4996: std::unary_negate is deprecated in C++17 37 #include <Eigen/SparseCore> 49 template <typename MatrixType> using
EigenDMap =
Eigen::Map<MatrixType, 0, EigenDStride>;
53 #if EIGEN_VERSION_AT_LEAST(3,3,0) 61 template <
typename T>
using is_eigen_mutable_map = std::is_base_of<Eigen::MapBase<T, Eigen::WriteAccessors>, T>;
84 conformable{
true}, rows{r},
cols{
c} {
86 if (rstride < 0 || cstride < 0) {
87 negativestrides =
true;
89 stride = {EigenRowMajor ? rstride : cstride ,
90 EigenRowMajor ? cstride : rstride };
95 :
EigenConformable(r, c, r == 1 ? c*stride : stride, c == 1 ? r : r*stride) {}
103 (EigenRowMajor ?
cols :
rows) == 1) &&
105 (EigenRowMajor ? rows :
cols) == 1);
111 template <
typename PlainObjectType,
int MapOptions,
typename Str
ideType>
113 template <
typename PlainObjectType,
int Options,
typename Str
ideType>
122 rows = Type::RowsAtCompileTime,
123 cols = Type::ColsAtCompileTime,
124 size = Type::SizeAtCompileTime;
125 static constexpr
bool 126 row_major = Type::IsRowMajor,
127 vector = Type::IsVectorAtCompileTime,
131 dynamic = !fixed_rows && !fixed_cols;
133 template <EigenIndex i, EigenIndex ifzero>
using if_zero = std::integral_constant<EigenIndex, i == 0 ? ifzero : i>;
135 outer_stride =
if_zero<StrideType::OuterStrideAtCompileTime,
138 static constexpr
bool requires_row_major = !dynamic_stride && !vector && (row_major ? inner_stride : outer_stride) == 1;
139 static constexpr
bool requires_col_major = !dynamic_stride && !vector && (row_major ? outer_stride : inner_stride) == 1;
145 const auto dims = a.
ndim();
146 if (dims < 1 || dims > 2)
152 np_rows = a.
shape(0),
153 np_cols = a.
shape(1),
156 if ((fixed_rows && np_rows !=
rows) || (fixed_cols && np_cols !=
cols))
159 return {np_rows, np_cols, np_rstride, np_cstride};
168 if (fixed &&
size != n)
176 else if (fixed_cols) {
179 if (
cols != n)
return false;
184 if (fixed_rows &&
rows != n)
return false;
191 static constexpr
bool show_c_contiguous = show_order && requires_row_major;
192 static constexpr
bool show_f_contiguous = !show_c_contiguous && show_order && requires_col_major;
194 static constexpr
auto descriptor =
196 _(
"[") + _<fixed_rows>(_<(size_t) rows>(),
_(
"m")) +
197 _(
", ") + _<fixed_cols>(_<(size_t) cols>(),
_(
"n")) +
205 _<show_writeable>(
", flags.writeable",
"") +
206 _<show_c_contiguous>(
", flags.c_contiguous",
"") +
207 _<show_f_contiguous>(
", flags.f_contiguous",
"") +
217 a =
array({ src.size() }, { elem_size * src.innerStride() }, src.data(),
base);
219 a =
array({ src.rows(), src.cols() }, { elem_size * src.rowStride(), elem_size * src.colStride() },
223 array_proxy(a.
ptr())->flags &= ~detail::npy_api::NPY_ARRAY_WRITEABLE_;
232 template <
typename props,
typename Type>
246 return eigen_ref_array<props>(*src,
base);
251 template<
typename Type>
267 auto dims = buf.ndim();
268 if (dims < 1 || dims > 2)
271 auto fits = props::conformable(buf);
277 auto ref = reinterpret_steal<array>(eigen_ref_array<props>(
value));
278 if (dims == 1)
ref =
ref.squeeze();
279 else if (
ref.ndim() == 1) buf = buf.squeeze();
294 template <
typename CType>
299 return eigen_encapsulate<props>(src);
301 return eigen_encapsulate<props>(
new CType(std::move(*src)));
306 return eigen_ref_array<props>(*src);
308 return eigen_ref_array<props>(*src, parent);
310 throw cast_error(
"unhandled return_value_policy: should not happen!");
328 return cast_impl(&src, policy, parent);
334 return cast(&src, policy, parent);
338 return cast_impl(src, policy, parent);
342 return cast_impl(src, policy, parent);
345 static constexpr
auto name = props::descriptor;
381 pybind11_fail(
"Invalid return_value_policy for Eigen Map/Ref/Block type");
385 static constexpr
auto name = props::descriptor;
401 template <
typename PlainObjectType,
typename Str
ideType>
403 Eigen::Ref<PlainObjectType, 0, StrideType>,
404 enable_if_t<is_eigen_dense_map<Eigen::Ref<PlainObjectType, 0, StrideType>>::value>
412 ((props::row_major ? props::inner_stride : props::outer_stride) == 1 ?
array::c_style :
413 (props::row_major ? props::outer_stride : props::inner_stride) == 1 ?
array::f_style : 0)>;
416 std::unique_ptr<MapType>
map;
417 std::unique_ptr<Type>
ref;
429 bool need_copy = !isinstance<Array>(src);
435 Array aref = reinterpret_borrow<Array>(src);
437 if (aref && (!need_writeable || aref.
writeable())) {
438 fits = props::conformable(aref);
439 if (!fits)
return false;
440 if (!fits.template stride_compatible<props>())
443 copy_or_ref = std::move(aref);
454 if (!convert || need_writeable)
return false;
457 if (!copy)
return false;
458 fits = props::conformable(copy);
459 if (!fits || !fits.template stride_compatible<props>())
461 copy_or_ref = std::move(copy);
466 map.reset(
new MapType(
data(copy_or_ref), fits.rows, fits.cols, make_stride(fits.stride.outer(), fits.stride.inner())));
467 ref.reset(
new Type(*map));
472 operator Type*() {
return ref.get(); }
473 operator Type&() {
return *
ref; }
474 template <
typename _T>
using cast_op_type = pybind11::detail::cast_op_type<_T>;
518 template <
typename Type>
530 static constexpr
auto name = props::descriptor;
536 operator Type() =
delete;
540 template<
typename Type>
545 static constexpr
bool rowMajor = Type::IsRowMajor;
551 auto obj = reinterpret_borrow<object>(src);
553 object matrix_type = sparse_module.attr(
554 rowMajor ?
"csr_matrix" :
"csc_matrix");
558 obj = matrix_type(obj);
567 auto shape = pybind11::tuple((pybind11::object) obj.attr(
"shape"));
568 auto nnz = obj.attr(
"nnz").
cast<Index>();
570 if (!
values || !innerIndices || !outerIndices)
574 shape[0].
cast<Index>(), shape[1].cast<Index>(), nnz,
584 rowMajor ?
"csr_matrix" :
"csc_matrix");
586 array data(src.nonZeros(), src.valuePtr());
587 array outerIndices((rowMajor ? src.rows() : src.cols()) + 1, src.outerIndexPtr());
588 array innerIndices(src.nonZeros(), src.innerIndexPtr());
592 std::make_pair(src.rows(), src.cols())
596 PYBIND11_TYPE_CASTER(Type,
_<(Type::IsRowMajor) != 0>(
"scipy.sparse.csr_matrix[",
"scipy.sparse.csc_matrix[")
603 #if defined(__GNUG__) || defined(__clang__) 604 # pragma GCC diagnostic pop 605 #elif defined(_MSC_VER) 606 # pragma warning(pop) std::unique_ptr< MapType > map
#define EIGEN_DEFAULT_DENSE_INDEX_TYPE
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
static handle cast(const Type &src, return_value_policy policy, handle parent)
static handle handle_of()
typename eigen_extract_stride< Type >::type StrideType
const ssize_t * shape() const
Dimensions of the array.
A matrix or vector expression mapping an existing array of data.
PyObject * ptr() const
Return the underlying PyObject * pointer.
all_of< is_template_base_of< Eigen::DenseBase, T >, std::is_base_of< Eigen::MapBase< T, Eigen::ReadOnlyAccessors >, T >> is_eigen_dense_map
static S make_stride(EigenIndex outer, EigenIndex)
Namespace containing all symbols from the Eigen library.
bool load(handle src, bool)
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 handle cast(const MapType &src, return_value_policy policy, handle parent)
static S make_stride(EigenIndex outer, EigenIndex inner)
PyExc_RuntimeError[[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
const ssize_t * strides() const
Strides of the array.
T * mutable_data(Ix...index)
handle eigen_encapsulate(Type *src)
movable_cast_op_type< T > cast_op_type
Tuple< Args... > make_tuple(Args...args)
Creates a tuple object, deducing the target type from the types of arguments.
static handle cast(Type *src, return_value_policy policy, handle parent)
#define EIGEN_VERSION_AT_LEAST(x, y, z)
std::unique_ptr< Type > ref
EIGEN_DEVICE_FUNC Index outer() const
std::integral_constant< EigenIndex, i==0?ifzero:i > if_zero
bool convert(const int &y)
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)
bool_constant< !stride_ctor_default< S >::value &&std::is_constructible< S, EigenIndex, EigenIndex >::value > stride_ctor_dual
typename std::remove_reference< T >::type remove_reference_t
bool load(handle src, bool convert)
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
std::is_base_of< Eigen::MapBase< T, Eigen::WriteAccessors >, T > is_eigen_mutable_map
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
bool_constant< S::InnerStrideAtCompileTime!=Eigen::Dynamic &&S::OuterStrideAtCompileTime!=Eigen::Dynamic &&std::is_default_constructible< S >::value > stride_ctor_default
static handle cast(const Type &src, return_value_policy, handle)
const Scalar * data(Array &a)
typename props::Scalar Scalar
Reference counting helper.
static S make_stride(EigenIndex, EigenIndex)
static handle cast(Type &&src, return_value_policy, handle parent)
all_of< negation< is_eigen_dense_map< T >>, is_template_base_of< Eigen::PlainObjectBase, T >> is_eigen_dense_plain
static handle cast(const Type &src, return_value_policy, handle)
A matrix or vector expression mapping an existing expression.
remove_reference_t< decltype(*std::declval< Type >).outerIndexPtr())> StorageIndex
static array ensure(handle h, int ExtraFlags=0)
EIGEN_DEFAULT_DENSE_INDEX_TYPE EigenIndex
Map< MatrixType > MapType
static module_ import(const char *name)
Import and return a module or throws error_already_set.
decltype(is_template_base_of_impl< Base >::check((intrinsic_t< T > *) nullptr)) is_template_base_of
handle eigen_array_cast(typename props::Type const &src, handle base=handle(), bool writeable=true)
static S make_stride(EigenIndex, EigenIndex inner)
std::is_same< bools< Ts::value..., true >, bools< true, Ts::value... >> all_of
ssize_t ndim() const
Number of dimensions.
is_template_base_of< Eigen::SparseMatrixBase, T > is_eigen_sparse
const T * data(Ix...index) const
typename Type::Scalar Scalar
bool writeable() const
If set, the array is writeable (otherwise the buffer is read-only)
pybind11::detail::cast_op_type< _T > cast_op_type
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
Annotation for function names.
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > Matrix
Annotation indicating that a class derives from another given type.
Container::iterator get(Container &c, Position position)
static handle cast_impl(CType *src, return_value_policy policy, handle parent)
void load(Archive &ar, Eigen::Matrix< Scalar_, Rows_, Cols_, Ops_, MaxRows_, MaxCols_ > &m, const unsigned int)
The matrix class, also used for vectors and row-vectors.
static EigenConformable< row_major > conformable(const array &a)
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
EIGEN_DEVICE_FUNC Index inner() const
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 handle cast(const Type *src, return_value_policy policy, handle parent)
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
#define PYBIND11_NAMESPACE_BEGIN(name)
PyArray_Proxy * array_proxy(void *ptr)
bool load(handle src, bool convert)