2016-05-05 18:33:54 +00:00
|
|
|
/*
|
|
|
|
pybind11/eigen.h: Transparent conversion for dense and sparse Eigen matrices
|
|
|
|
|
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-08-17 23:49:39 +00:00
|
|
|
/* HINT: To suppress warnings originating from the Eigen headers, use -isystem.
|
|
|
|
See also:
|
|
|
|
https://stackoverflow.com/questions/2579576/i-dir-vs-isystem-dir
|
|
|
|
https://stackoverflow.com/questions/1741816/isystem-for-ms-visual-studio-c-compiler
|
|
|
|
*/
|
2016-05-29 11:40:40 +00:00
|
|
|
|
2021-08-17 23:49:39 +00:00
|
|
|
#include "numpy.h"
|
2016-05-05 18:33:54 +00:00
|
|
|
|
2021-10-11 20:13:01 +00:00
|
|
|
// The C4127 suppression was introduced for Eigen 3.4.0. In theory we could
|
|
|
|
// make it version specific, or even remove it later, but considering that
|
|
|
|
// 1. C4127 is generally far more distracting than useful for modern template code, and
|
|
|
|
// 2. we definitely want to ignore any MSVC warnings originating from Eigen code,
|
2022-02-16 15:07:53 +00:00
|
|
|
// it is probably best to keep this around indefinitely.
|
2021-10-11 20:13:01 +00:00
|
|
|
#if defined(_MSC_VER)
|
2022-02-10 20:17:07 +00:00
|
|
|
# pragma warning(push)
|
|
|
|
# pragma warning(disable : 4127) // C4127: conditional expression is constant
|
2022-02-16 15:07:53 +00:00
|
|
|
# pragma warning(disable : 5054) // https://github.com/pybind/pybind11/pull/3741
|
|
|
|
// C5054: operator '&': deprecated between enumerations of different types
|
2021-10-11 20:13:01 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-15 14:15:25 +00:00
|
|
|
#include <Eigen/Core>
|
|
|
|
#include <Eigen/SparseCore>
|
|
|
|
|
2021-10-11 20:13:01 +00:00
|
|
|
#if defined(_MSC_VER)
|
2022-02-10 20:17:07 +00:00
|
|
|
# pragma warning(pop)
|
2021-10-11 20:13:01 +00:00
|
|
|
#endif
|
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
// Eigen prior to 3.2.7 doesn't have proper move constructors--but worse, some classes get implicit
|
|
|
|
// move constructors that break things. We could detect this an explicitly copy, but an extra copy
|
|
|
|
// of matrices seems highly undesirable.
|
2022-02-10 20:17:07 +00:00
|
|
|
static_assert(EIGEN_VERSION_AT_LEAST(3, 2, 7),
|
|
|
|
"Eigen support in pybind11 requires Eigen >= 3.2.7");
|
2017-01-17 01:35:14 +00:00
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
// Provide a convenience alias for easier pass-by-ref usage with fully dynamic strides:
|
|
|
|
using EigenDStride = Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>;
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename MatrixType>
|
|
|
|
using EigenDRef = Eigen::Ref<MatrixType, 0, EigenDStride>;
|
|
|
|
template <typename MatrixType>
|
|
|
|
using EigenDMap = Eigen::Map<MatrixType, 0, EigenDStride>;
|
2017-01-17 01:35:14 +00:00
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_BEGIN(detail)
|
2016-05-05 18:33:54 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
#if EIGEN_VERSION_AT_LEAST(3, 3, 0)
|
2017-01-17 01:35:14 +00:00
|
|
|
using EigenIndex = Eigen::Index;
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Scalar, int Flags, typename StorageIndex>
|
2021-11-23 01:01:35 +00:00
|
|
|
using EigenMapSparseMatrix = Eigen::Map<Eigen::SparseMatrix<Scalar, Flags, StorageIndex>>;
|
2017-01-17 01:35:14 +00:00
|
|
|
#else
|
|
|
|
using EigenIndex = EIGEN_DEFAULT_DENSE_INDEX_TYPE;
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Scalar, int Flags, typename StorageIndex>
|
2021-11-23 01:01:35 +00:00
|
|
|
using EigenMapSparseMatrix = Eigen::MappedSparseMatrix<Scalar, Flags, StorageIndex>;
|
2017-01-17 01:35:14 +00:00
|
|
|
#endif
|
2016-05-05 18:33:54 +00:00
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
// Matches Eigen::Map, Eigen::Ref, blocks, etc:
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename T>
|
|
|
|
using is_eigen_dense_map = all_of<is_template_base_of<Eigen::DenseBase, T>,
|
|
|
|
std::is_base_of<Eigen::MapBase<T, Eigen::ReadOnlyAccessors>, T>>;
|
|
|
|
template <typename T>
|
|
|
|
using is_eigen_mutable_map = std::is_base_of<Eigen::MapBase<T, Eigen::WriteAccessors>, T>;
|
|
|
|
template <typename T>
|
|
|
|
using is_eigen_dense_plain
|
|
|
|
= all_of<negation<is_eigen_dense_map<T>>, is_template_base_of<Eigen::PlainObjectBase, T>>;
|
|
|
|
template <typename T>
|
|
|
|
using is_eigen_sparse = is_template_base_of<Eigen::SparseMatrixBase, T>;
|
2016-08-04 19:24:41 +00:00
|
|
|
// Test for objects inheriting from EigenBase<Derived> that aren't captured by the above. This
|
|
|
|
// basically covers anything that can be assigned to a dense matrix but that don't have a typical
|
|
|
|
// matrix data layout that can be copied from their .data(). For example, DiagonalMatrix and
|
|
|
|
// SelfAdjointView fall into this category.
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename T>
|
|
|
|
using is_eigen_other
|
|
|
|
= 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>>>>;
|
2016-08-04 19:24:41 +00:00
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
// Captures numpy/eigen conformability status (returned by EigenProps::conformable()):
|
2022-02-10 20:17:07 +00:00
|
|
|
template <bool EigenRowMajor>
|
|
|
|
struct EigenConformable {
|
2017-01-17 01:35:14 +00:00
|
|
|
bool conformable = false;
|
|
|
|
EigenIndex rows = 0, cols = 0;
|
2022-02-10 20:17:07 +00:00
|
|
|
EigenDStride stride{0, 0}; // Only valid if negativestrides is false!
|
|
|
|
bool negativestrides = false; // If true, do not use stride!
|
2017-01-17 01:35:14 +00:00
|
|
|
|
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy
* clang-tidy explicit attr.h (all automatic)
* clang-tidy explicit cast.h (all automatic)
* clang-tidy detail/init.h (1 NOLINT)
* clang-tidy detail/type_caster_base.h (2 NOLINT)
* clang-tidy pybind11.h (7 NOLINT)
* clang-tidy detail/common.h (3 NOLINT)
* clang-tidy detail/descr.h (2 NOLINT)
* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)
* clang-tidy eigen.h (7 NOLINT, 0 explicit)
* Adding 2 explicit in functional.h
* Adding 4 explicit in iostream.h
* clang-tidy numpy.h (1 NOLINT, 1 explicit)
* clang-tidy embed.h (0 NOLINT, 1 explicit)
* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)
* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)
* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)
* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)
* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)
* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)
* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/object.h (0 NOLINT, 2 explicit)
* clang-tidy batch of fully automatic fixes.
* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-09 01:53:38 +00:00
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
2017-01-17 01:35:14 +00:00
|
|
|
EigenConformable(bool fits = false) : conformable{fits} {}
|
|
|
|
// Matrix type:
|
2022-02-10 20:17:07 +00:00
|
|
|
EigenConformable(EigenIndex r, EigenIndex c, EigenIndex rstride, EigenIndex cstride)
|
|
|
|
: conformable{true}, rows{r}, cols{c},
|
|
|
|
// TODO: when Eigen bug #747 is fixed, remove the tests for non-negativity.
|
|
|
|
// http://eigen.tuxfamily.org/bz/show_bug.cgi?id=747
|
|
|
|
stride{EigenRowMajor ? (rstride > 0 ? rstride : 0)
|
|
|
|
: (cstride > 0 ? cstride : 0) /* outer stride */,
|
|
|
|
EigenRowMajor ? (cstride > 0 ? cstride : 0)
|
|
|
|
: (rstride > 0 ? rstride : 0) /* inner stride */},
|
|
|
|
negativestrides{rstride < 0 || cstride < 0} {}
|
2017-01-17 01:35:14 +00:00
|
|
|
// Vector type:
|
2017-03-17 17:51:52 +00:00
|
|
|
EigenConformable(EigenIndex r, EigenIndex c, EigenIndex stride)
|
2022-02-10 20:17:07 +00:00
|
|
|
: EigenConformable(r, c, r == 1 ? c * stride : stride, c == 1 ? r : r * stride) {}
|
2017-03-17 17:51:52 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename props>
|
|
|
|
bool stride_compatible() const {
|
2017-03-17 17:51:52 +00:00
|
|
|
// To have compatible strides, we need (on both dimensions) one of fully dynamic strides,
|
2022-02-10 20:17:07 +00:00
|
|
|
// matching strides, or a dimension size of 1 (in which case the stride value is
|
|
|
|
// irrelevant)
|
|
|
|
return !negativestrides
|
|
|
|
&& (props::inner_stride == Eigen::Dynamic || props::inner_stride == stride.inner()
|
|
|
|
|| (EigenRowMajor ? cols : rows) == 1)
|
|
|
|
&& (props::outer_stride == Eigen::Dynamic || props::outer_stride == stride.outer()
|
|
|
|
|| (EigenRowMajor ? rows : cols) == 1);
|
2017-01-17 01:35:14 +00:00
|
|
|
}
|
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy
* clang-tidy explicit attr.h (all automatic)
* clang-tidy explicit cast.h (all automatic)
* clang-tidy detail/init.h (1 NOLINT)
* clang-tidy detail/type_caster_base.h (2 NOLINT)
* clang-tidy pybind11.h (7 NOLINT)
* clang-tidy detail/common.h (3 NOLINT)
* clang-tidy detail/descr.h (2 NOLINT)
* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)
* clang-tidy eigen.h (7 NOLINT, 0 explicit)
* Adding 2 explicit in functional.h
* Adding 4 explicit in iostream.h
* clang-tidy numpy.h (1 NOLINT, 1 explicit)
* clang-tidy embed.h (0 NOLINT, 1 explicit)
* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)
* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)
* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)
* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)
* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)
* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)
* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/object.h (0 NOLINT, 2 explicit)
* clang-tidy batch of fully automatic fixes.
* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-09 01:53:38 +00:00
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
2017-01-17 01:35:14 +00:00
|
|
|
operator bool() const { return conformable; }
|
|
|
|
};
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Type>
|
|
|
|
struct eigen_extract_stride {
|
|
|
|
using type = Type;
|
|
|
|
};
|
2017-01-17 01:35:14 +00:00
|
|
|
template <typename PlainObjectType, int MapOptions, typename StrideType>
|
2022-02-10 20:17:07 +00:00
|
|
|
struct eigen_extract_stride<Eigen::Map<PlainObjectType, MapOptions, StrideType>> {
|
|
|
|
using type = StrideType;
|
|
|
|
};
|
2017-01-17 01:35:14 +00:00
|
|
|
template <typename PlainObjectType, int Options, typename StrideType>
|
2022-02-10 20:17:07 +00:00
|
|
|
struct eigen_extract_stride<Eigen::Ref<PlainObjectType, Options, StrideType>> {
|
|
|
|
using type = StrideType;
|
|
|
|
};
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
// Helper struct for extracting information from an Eigen type
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Type_>
|
|
|
|
struct EigenProps {
|
2017-01-17 01:35:14 +00:00
|
|
|
using Type = Type_;
|
|
|
|
using Scalar = typename Type::Scalar;
|
|
|
|
using StrideType = typename eigen_extract_stride<Type>::type;
|
2022-02-10 20:17:07 +00:00
|
|
|
static constexpr EigenIndex rows = Type::RowsAtCompileTime, cols = Type::ColsAtCompileTime,
|
|
|
|
size = Type::SizeAtCompileTime;
|
|
|
|
static constexpr bool row_major = Type::IsRowMajor,
|
|
|
|
vector
|
|
|
|
= Type::IsVectorAtCompileTime, // At least one dimension has fixed size 1
|
|
|
|
fixed_rows = rows != Eigen::Dynamic, fixed_cols = cols != Eigen::Dynamic,
|
|
|
|
fixed = size != Eigen::Dynamic, // Fully-fixed size
|
|
|
|
dynamic = !fixed_rows && !fixed_cols; // Fully-dynamic size
|
|
|
|
|
|
|
|
template <EigenIndex i, EigenIndex ifzero>
|
|
|
|
using if_zero = std::integral_constant<EigenIndex, i == 0 ? ifzero : i>;
|
|
|
|
static constexpr EigenIndex inner_stride
|
|
|
|
= if_zero<StrideType::InnerStrideAtCompileTime, 1>::value,
|
|
|
|
outer_stride = if_zero < StrideType::OuterStrideAtCompileTime,
|
|
|
|
vector ? size
|
|
|
|
: row_major ? cols
|
|
|
|
: rows > ::value;
|
|
|
|
static constexpr bool dynamic_stride
|
|
|
|
= inner_stride == Eigen::Dynamic && outer_stride == Eigen::Dynamic;
|
|
|
|
static constexpr bool requires_row_major
|
|
|
|
= !dynamic_stride && !vector && (row_major ? inner_stride : outer_stride) == 1;
|
|
|
|
static constexpr bool requires_col_major
|
|
|
|
= !dynamic_stride && !vector && (row_major ? outer_stride : inner_stride) == 1;
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
// Takes an input array and determines whether we can make it fit into the Eigen type. If
|
|
|
|
// the array is a vector, we attempt to fit it into either an Eigen 1xN or Nx1 vector
|
|
|
|
// (preferring the latter if it will fit in either, i.e. for a fully dynamic matrix type).
|
|
|
|
static EigenConformable<row_major> conformable(const array &a) {
|
|
|
|
const auto dims = a.ndim();
|
2022-02-08 00:23:20 +00:00
|
|
|
if (dims < 1 || dims > 2) {
|
2017-01-17 01:35:14 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
if (dims == 2) { // Matrix type: require exact match (or dynamic)
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
EigenIndex np_rows = a.shape(0), np_cols = a.shape(1),
|
|
|
|
np_rstride = a.strides(0) / static_cast<ssize_t>(sizeof(Scalar)),
|
|
|
|
np_cstride = a.strides(1) / static_cast<ssize_t>(sizeof(Scalar));
|
2022-02-08 00:23:20 +00:00
|
|
|
if ((PYBIND11_SILENCE_MSVC_C4127(fixed_rows) && np_rows != rows)
|
|
|
|
|| (PYBIND11_SILENCE_MSVC_C4127(fixed_cols) && np_cols != cols)) {
|
2017-01-17 01:35:14 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
return {np_rows, np_cols, np_rstride, np_cstride};
|
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
// Otherwise we're storing an n-vector. Only one of the strides will be used, but
|
|
|
|
// whichever is used, we want the (single) numpy stride value.
|
2017-01-17 01:35:14 +00:00
|
|
|
const EigenIndex n = a.shape(0),
|
2022-02-10 20:17:07 +00:00
|
|
|
stride = a.strides(0) / static_cast<ssize_t>(sizeof(Scalar));
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
if (vector) { // Eigen type is a compile-time vector
|
2022-02-08 00:23:20 +00:00
|
|
|
if (PYBIND11_SILENCE_MSVC_C4127(fixed) && size != n) {
|
2017-01-17 01:35:14 +00:00
|
|
|
return false; // Vector size mismatch
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
return {rows == 1 ? 1 : n, cols == 1 ? 1 : n, stride};
|
|
|
|
}
|
2021-07-09 13:45:53 +00:00
|
|
|
if (fixed) {
|
2017-01-17 01:35:14 +00:00
|
|
|
// The type has a fixed size, but is not a vector: abort
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-09 13:45:53 +00:00
|
|
|
if (fixed_cols) {
|
2017-01-17 01:35:14 +00:00
|
|
|
// Since this isn't a vector, cols must be != 1. We allow this only if it exactly
|
|
|
|
// equals the number of elements (rows is Dynamic, and so 1 row is allowed).
|
2022-02-08 00:23:20 +00:00
|
|
|
if (cols != n) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
return {1, n, stride};
|
2021-07-09 13:45:53 +00:00
|
|
|
} // Otherwise it's either fully dynamic, or column dynamic; both become a column vector
|
2022-02-08 00:23:20 +00:00
|
|
|
if (PYBIND11_SILENCE_MSVC_C4127(fixed_rows) && rows != n) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-10 20:17:07 +00:00
|
|
|
return {n, 1, stride};
|
2017-01-17 01:35:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
static constexpr bool show_writeable
|
|
|
|
= is_eigen_dense_map<Type>::value && is_eigen_mutable_map<Type>::value;
|
2017-07-02 09:48:56 +00:00
|
|
|
static constexpr bool show_order = is_eigen_dense_map<Type>::value;
|
|
|
|
static constexpr bool show_c_contiguous = show_order && requires_row_major;
|
2022-02-10 20:17:07 +00:00
|
|
|
static constexpr bool show_f_contiguous
|
|
|
|
= !show_c_contiguous && show_order && requires_col_major;
|
|
|
|
|
|
|
|
static constexpr auto descriptor
|
|
|
|
= const_name("numpy.ndarray[") + npy_format_descriptor<Scalar>::name + const_name("[")
|
|
|
|
+ const_name<fixed_rows>(const_name<(size_t) rows>(), const_name("m")) + const_name(", ")
|
|
|
|
+ const_name<fixed_cols>(const_name<(size_t) cols>(), const_name("n")) + const_name("]")
|
|
|
|
+
|
|
|
|
// For a reference type (e.g. Ref<MatrixXd>) we have other constraints that might need to
|
|
|
|
// be satisfied: writeable=True (for a mutable reference), and, depending on the map's
|
|
|
|
// stride options, possibly f_contiguous or c_contiguous. We include them in the
|
|
|
|
// descriptor output to provide some hint as to why a TypeError is occurring (otherwise
|
|
|
|
// it can be confusing to see that a function accepts a 'numpy.ndarray[float64[3,2]]' and
|
|
|
|
// an error message that you *gave* a numpy.ndarray of the right type and dimensions.
|
|
|
|
const_name<show_writeable>(", flags.writeable", "")
|
|
|
|
+ const_name<show_c_contiguous>(", flags.c_contiguous", "")
|
|
|
|
+ const_name<show_f_contiguous>(", flags.f_contiguous", "") + const_name("]");
|
2017-01-17 01:35:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Casts an Eigen type to numpy array. If given a base, the numpy array references the src data,
|
|
|
|
// otherwise it'll make a copy. writeable lets you turn off the writeable flag for the array.
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename props>
|
|
|
|
handle
|
|
|
|
eigen_array_cast(typename props::Type const &src, handle base = handle(), bool writeable = true) {
|
2017-04-14 20:33:44 +00:00
|
|
|
constexpr ssize_t elem_size = sizeof(typename props::Scalar);
|
2017-04-07 19:49:54 +00:00
|
|
|
array a;
|
2022-02-08 00:23:20 +00:00
|
|
|
if (props::vector) {
|
2022-02-10 20:17:07 +00:00
|
|
|
a = array({src.size()}, {elem_size * src.innerStride()}, src.data(), base);
|
2022-02-08 00:23:20 +00:00
|
|
|
} else {
|
|
|
|
a = array({src.rows(), src.cols()},
|
|
|
|
{elem_size * src.rowStride(), elem_size * src.colStride()},
|
|
|
|
src.data(),
|
|
|
|
base);
|
|
|
|
}
|
2017-04-07 19:49:54 +00:00
|
|
|
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!writeable) {
|
2017-01-17 01:35:14 +00:00
|
|
|
array_proxy(a.ptr())->flags &= ~detail::npy_api::NPY_ARRAY_WRITEABLE_;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
return a.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes an lvalue ref to some Eigen type and a (python) base object, creating a numpy array that
|
|
|
|
// reference the Eigen object's data with `base` as the python-registered base class (if omitted,
|
|
|
|
// the base will be set to None, and lifetime management is up to the caller). The numpy array is
|
|
|
|
// non-writeable if the given type is const.
|
|
|
|
template <typename props, typename Type>
|
|
|
|
handle eigen_ref_array(Type &src, handle parent = none()) {
|
|
|
|
// none here is to get past array's should-we-copy detection, which currently always
|
|
|
|
// copies when there is no base. Setting the base to None should be harmless.
|
|
|
|
return eigen_array_cast<props>(src, parent, !std::is_const<Type>::value);
|
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
// Takes a pointer to some dense, plain Eigen type, builds a capsule around it, then returns a
|
|
|
|
// numpy array that references the encapsulated data with a python-side reference to the capsule to
|
|
|
|
// tie its destruction to that of any dependent python objects. Const-ness is determined by
|
|
|
|
// whether or not the Type of the pointer given is const.
|
2017-01-17 01:35:14 +00:00
|
|
|
template <typename props, typename Type, typename = enable_if_t<is_eigen_dense_plain<Type>::value>>
|
|
|
|
handle eigen_encapsulate(Type *src) {
|
2017-03-22 21:04:00 +00:00
|
|
|
capsule base(src, [](void *o) { delete static_cast<Type *>(o); });
|
2017-01-17 01:35:14 +00:00
|
|
|
return eigen_ref_array<props>(*src, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type caster for regular, dense matrix types (e.g. MatrixXd), but not maps/refs/etc. of dense
|
|
|
|
// types.
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Type>
|
2017-01-17 01:35:14 +00:00
|
|
|
struct type_caster<Type, enable_if_t<is_eigen_dense_plain<Type>::value>> {
|
|
|
|
using Scalar = typename Type::Scalar;
|
|
|
|
using props = EigenProps<Type>;
|
2016-05-05 18:33:54 +00:00
|
|
|
|
2017-04-06 22:16:35 +00:00
|
|
|
bool load(handle src, bool convert) {
|
|
|
|
// If we're in no-convert mode, only load if given an array of the correct type
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!convert && !isinstance<array_t<Scalar>>(src)) {
|
2017-04-06 22:16:35 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-04-06 22:16:35 +00:00
|
|
|
|
|
|
|
// Coerce into an array, but don't do type conversion yet; the copy below handles it.
|
|
|
|
auto buf = array::ensure(src);
|
|
|
|
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!buf) {
|
2016-08-29 01:41:05 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-05-05 18:33:54 +00:00
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
auto dims = buf.ndim();
|
2022-02-08 00:23:20 +00:00
|
|
|
if (dims < 1 || dims > 2) {
|
2017-01-17 01:35:14 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-05-05 18:33:54 +00:00
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
auto fits = props::conformable(buf);
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!fits) {
|
2017-05-11 13:38:39 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-05-11 13:38:39 +00:00
|
|
|
|
2017-04-06 22:16:35 +00:00
|
|
|
// Allocate the new type, then build a numpy reference into it
|
|
|
|
value = Type(fits.rows, fits.cols);
|
|
|
|
auto ref = reinterpret_steal<array>(eigen_ref_array<props>(value));
|
2022-02-08 00:23:20 +00:00
|
|
|
if (dims == 1) {
|
|
|
|
ref = ref.squeeze();
|
|
|
|
} else if (ref.ndim() == 1) {
|
|
|
|
buf = buf.squeeze();
|
|
|
|
}
|
2016-05-05 18:33:54 +00:00
|
|
|
|
2017-04-06 22:16:35 +00:00
|
|
|
int result = detail::npy_api::get().PyArray_CopyInto_(ref.ptr(), buf.ptr());
|
2017-04-06 17:34:39 +00:00
|
|
|
|
2017-04-06 22:16:35 +00:00
|
|
|
if (result < 0) { // Copy failed!
|
|
|
|
PyErr_Clear();
|
|
|
|
return false;
|
2017-04-06 17:34:39 +00:00
|
|
|
}
|
2016-05-05 18:33:54 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
private:
|
|
|
|
// Cast implementation
|
|
|
|
template <typename CType>
|
|
|
|
static handle cast_impl(CType *src, return_value_policy policy, handle parent) {
|
|
|
|
switch (policy) {
|
|
|
|
case return_value_policy::take_ownership:
|
|
|
|
case return_value_policy::automatic:
|
|
|
|
return eigen_encapsulate<props>(src);
|
|
|
|
case return_value_policy::move:
|
|
|
|
return eigen_encapsulate<props>(new CType(std::move(*src)));
|
|
|
|
case return_value_policy::copy:
|
|
|
|
return eigen_array_cast<props>(*src);
|
|
|
|
case return_value_policy::reference:
|
|
|
|
case return_value_policy::automatic_reference:
|
|
|
|
return eigen_ref_array<props>(*src);
|
|
|
|
case return_value_policy::reference_internal:
|
|
|
|
return eigen_ref_array<props>(*src, parent);
|
|
|
|
default:
|
|
|
|
throw cast_error("unhandled return_value_policy: should not happen!");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Normal returned non-reference, non-const value:
|
|
|
|
static handle cast(Type &&src, return_value_policy /* policy */, handle parent) {
|
|
|
|
return cast_impl(&src, return_value_policy::move, parent);
|
|
|
|
}
|
|
|
|
// If you return a non-reference const, we mark the numpy array readonly:
|
|
|
|
static handle cast(const Type &&src, return_value_policy /* policy */, handle parent) {
|
|
|
|
return cast_impl(&src, return_value_policy::move, parent);
|
|
|
|
}
|
|
|
|
// lvalue reference return; default (automatic) becomes copy
|
|
|
|
static handle cast(Type &src, return_value_policy policy, handle parent) {
|
2022-02-08 00:23:20 +00:00
|
|
|
if (policy == return_value_policy::automatic
|
|
|
|
|| policy == return_value_policy::automatic_reference) {
|
2017-01-17 01:35:14 +00:00
|
|
|
policy = return_value_policy::copy;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
return cast_impl(&src, policy, parent);
|
|
|
|
}
|
|
|
|
// const lvalue reference return; default (automatic) becomes copy
|
|
|
|
static handle cast(const Type &src, return_value_policy policy, handle parent) {
|
2022-02-08 00:23:20 +00:00
|
|
|
if (policy == return_value_policy::automatic
|
|
|
|
|| policy == return_value_policy::automatic_reference) {
|
2017-01-17 01:35:14 +00:00
|
|
|
policy = return_value_policy::copy;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
return cast(&src, policy, parent);
|
|
|
|
}
|
|
|
|
// non-const pointer return
|
|
|
|
static handle cast(Type *src, return_value_policy policy, handle parent) {
|
|
|
|
return cast_impl(src, policy, parent);
|
|
|
|
}
|
|
|
|
// const pointer return
|
|
|
|
static handle cast(const Type *src, return_value_policy policy, handle parent) {
|
|
|
|
return cast_impl(src, policy, parent);
|
|
|
|
}
|
|
|
|
|
2017-07-02 09:48:56 +00:00
|
|
|
static constexpr auto name = props::descriptor;
|
2017-01-17 01:35:14 +00:00
|
|
|
|
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy
* clang-tidy explicit attr.h (all automatic)
* clang-tidy explicit cast.h (all automatic)
* clang-tidy detail/init.h (1 NOLINT)
* clang-tidy detail/type_caster_base.h (2 NOLINT)
* clang-tidy pybind11.h (7 NOLINT)
* clang-tidy detail/common.h (3 NOLINT)
* clang-tidy detail/descr.h (2 NOLINT)
* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)
* clang-tidy eigen.h (7 NOLINT, 0 explicit)
* Adding 2 explicit in functional.h
* Adding 4 explicit in iostream.h
* clang-tidy numpy.h (1 NOLINT, 1 explicit)
* clang-tidy embed.h (0 NOLINT, 1 explicit)
* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)
* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)
* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)
* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)
* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)
* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)
* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/object.h (0 NOLINT, 2 explicit)
* clang-tidy batch of fully automatic fixes.
* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-09 01:53:38 +00:00
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
2022-02-10 20:17:07 +00:00
|
|
|
operator Type *() { return &value; }
|
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy
* clang-tidy explicit attr.h (all automatic)
* clang-tidy explicit cast.h (all automatic)
* clang-tidy detail/init.h (1 NOLINT)
* clang-tidy detail/type_caster_base.h (2 NOLINT)
* clang-tidy pybind11.h (7 NOLINT)
* clang-tidy detail/common.h (3 NOLINT)
* clang-tidy detail/descr.h (2 NOLINT)
* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)
* clang-tidy eigen.h (7 NOLINT, 0 explicit)
* Adding 2 explicit in functional.h
* Adding 4 explicit in iostream.h
* clang-tidy numpy.h (1 NOLINT, 1 explicit)
* clang-tidy embed.h (0 NOLINT, 1 explicit)
* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)
* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)
* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)
* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)
* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)
* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)
* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/object.h (0 NOLINT, 2 explicit)
* clang-tidy batch of fully automatic fixes.
* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-09 01:53:38 +00:00
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
2022-02-10 20:17:07 +00:00
|
|
|
operator Type &() { return value; }
|
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy
* clang-tidy explicit attr.h (all automatic)
* clang-tidy explicit cast.h (all automatic)
* clang-tidy detail/init.h (1 NOLINT)
* clang-tidy detail/type_caster_base.h (2 NOLINT)
* clang-tidy pybind11.h (7 NOLINT)
* clang-tidy detail/common.h (3 NOLINT)
* clang-tidy detail/descr.h (2 NOLINT)
* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)
* clang-tidy eigen.h (7 NOLINT, 0 explicit)
* Adding 2 explicit in functional.h
* Adding 4 explicit in iostream.h
* clang-tidy numpy.h (1 NOLINT, 1 explicit)
* clang-tidy embed.h (0 NOLINT, 1 explicit)
* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)
* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)
* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)
* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)
* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)
* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)
* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/object.h (0 NOLINT, 2 explicit)
* clang-tidy batch of fully automatic fixes.
* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-09 01:53:38 +00:00
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
2022-02-10 20:17:07 +00:00
|
|
|
operator Type &&() && { return std::move(value); }
|
|
|
|
template <typename T>
|
|
|
|
using cast_op_type = movable_cast_op_type<T>;
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Type value;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Base class for casting reference/map/block/etc. objects back to python.
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename MapType>
|
|
|
|
struct eigen_map_caster {
|
2017-01-17 01:35:14 +00:00
|
|
|
private:
|
|
|
|
using props = EigenProps<MapType>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Directly referencing a ref/map's data is a bit dangerous (whatever the map/ref points to has
|
2022-02-10 20:17:07 +00:00
|
|
|
// to stay around), but we'll allow it under the assumption that you know what you're doing
|
|
|
|
// (and have an appropriate keep_alive in place). We return a numpy array pointing directly at
|
|
|
|
// the ref's data (The numpy array ends up read-only if the ref was to a const matrix type.)
|
|
|
|
// Note that this means you need to ensure you don't destroy the object in some other way (e.g.
|
|
|
|
// with an appropriate keep_alive, or with a reference to a statically allocated matrix).
|
2017-01-17 01:35:14 +00:00
|
|
|
static handle cast(const MapType &src, return_value_policy policy, handle parent) {
|
|
|
|
switch (policy) {
|
|
|
|
case return_value_policy::copy:
|
|
|
|
return eigen_array_cast<props>(src);
|
|
|
|
case return_value_policy::reference_internal:
|
|
|
|
return eigen_array_cast<props>(src, parent, is_eigen_mutable_map<MapType>::value);
|
|
|
|
case return_value_policy::reference:
|
|
|
|
case return_value_policy::automatic:
|
|
|
|
case return_value_policy::automatic_reference:
|
|
|
|
return eigen_array_cast<props>(src, none(), is_eigen_mutable_map<MapType>::value);
|
|
|
|
default:
|
|
|
|
// move, take_ownership don't make any sense for a ref/map:
|
|
|
|
pybind11_fail("Invalid return_value_policy for Eigen Map/Ref/Block type");
|
2016-05-20 10:00:56 +00:00
|
|
|
}
|
2016-05-05 18:33:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-02 09:48:56 +00:00
|
|
|
static constexpr auto name = props::descriptor;
|
2016-05-05 18:33:54 +00:00
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
// Explicitly delete these: support python -> C++ conversion on these (i.e. these can be return
|
|
|
|
// types but not bound arguments). We still provide them (with an explicitly delete) so that
|
|
|
|
// you end up here if you try anyway.
|
|
|
|
bool load(handle, bool) = delete;
|
|
|
|
operator MapType() = delete;
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename>
|
|
|
|
using cast_op_type = MapType;
|
2016-05-05 18:33:54 +00:00
|
|
|
};
|
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
// We can return any map-like object (but can only load Refs, specialized next):
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Type>
|
|
|
|
struct type_caster<Type, enable_if_t<is_eigen_dense_map<Type>::value>> : eigen_map_caster<Type> {};
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
// Loader for Ref<...> arguments. See the documentation for info on how to make this work without
|
|
|
|
// copying (it requires some extra effort in many cases).
|
|
|
|
template <typename PlainObjectType, typename StrideType>
|
|
|
|
struct type_caster<
|
|
|
|
Eigen::Ref<PlainObjectType, 0, StrideType>,
|
2022-02-10 20:17:07 +00:00
|
|
|
enable_if_t<is_eigen_dense_map<Eigen::Ref<PlainObjectType, 0, StrideType>>::value>>
|
|
|
|
: public eigen_map_caster<Eigen::Ref<PlainObjectType, 0, StrideType>> {
|
2017-01-17 01:35:14 +00:00
|
|
|
private:
|
|
|
|
using Type = Eigen::Ref<PlainObjectType, 0, StrideType>;
|
|
|
|
using props = EigenProps<Type>;
|
|
|
|
using Scalar = typename props::Scalar;
|
|
|
|
using MapType = Eigen::Map<PlainObjectType, 0, StrideType>;
|
2022-02-10 20:17:07 +00:00
|
|
|
using Array
|
|
|
|
= array_t<Scalar,
|
|
|
|
array::forcecast
|
|
|
|
| ((props::row_major ? props::inner_stride : props::outer_stride) == 1
|
|
|
|
? array::c_style
|
|
|
|
: (props::row_major ? props::outer_stride : props::inner_stride) == 1
|
|
|
|
? array::f_style
|
|
|
|
: 0)>;
|
2017-01-17 01:35:14 +00:00
|
|
|
static constexpr bool need_writeable = is_eigen_mutable_map<Type>::value;
|
|
|
|
// Delay construction (these have no default constructor)
|
|
|
|
std::unique_ptr<MapType> map;
|
|
|
|
std::unique_ptr<Type> ref;
|
|
|
|
// Our array. When possible, this is just a numpy array pointing to the source data, but
|
2022-02-10 20:17:07 +00:00
|
|
|
// sometimes we can't avoid copying (e.g. input is not a numpy array at all, has an
|
|
|
|
// incompatible layout, or is an array of a type that needs to be converted). Using a numpy
|
|
|
|
// temporary (rather than an Eigen temporary) saves an extra copy when we need both type
|
|
|
|
// conversion and storage order conversion. (Note that we refuse to use this temporary copy
|
|
|
|
// when loading an argument for a Ref<M> with M non-const, i.e. a read-write reference).
|
2017-01-17 01:35:14 +00:00
|
|
|
Array copy_or_ref;
|
2022-02-10 20:17:07 +00:00
|
|
|
|
2016-08-03 20:50:22 +00:00
|
|
|
public:
|
2017-01-17 01:35:14 +00:00
|
|
|
bool load(handle src, bool convert) {
|
2022-02-10 20:17:07 +00:00
|
|
|
// First check whether what we have is already an array of the right type. If not, we
|
|
|
|
// can't avoid a copy (because the copy is also going to do type conversion).
|
2017-01-17 01:35:14 +00:00
|
|
|
bool need_copy = !isinstance<Array>(src);
|
|
|
|
|
|
|
|
EigenConformable<props::row_major> fits;
|
|
|
|
if (!need_copy) {
|
|
|
|
// We don't need a converting copy, but we also need to check whether the strides are
|
|
|
|
// compatible with the Ref's stride requirements
|
2020-11-09 18:10:19 +00:00
|
|
|
auto aref = reinterpret_borrow<Array>(src);
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
if (aref && (!need_writeable || aref.writeable())) {
|
|
|
|
fits = props::conformable(aref);
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!fits) {
|
|
|
|
return false; // Incompatible dimensions
|
|
|
|
}
|
|
|
|
if (!fits.template stride_compatible<props>()) {
|
2017-01-17 01:35:14 +00:00
|
|
|
need_copy = true;
|
2022-02-08 00:23:20 +00:00
|
|
|
} else {
|
2017-01-17 01:35:14 +00:00
|
|
|
copy_or_ref = std::move(aref);
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2022-02-10 20:17:07 +00:00
|
|
|
} else {
|
2017-01-17 01:35:14 +00:00
|
|
|
need_copy = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_copy) {
|
|
|
|
// We need to copy: If we need a mutable reference, or we're not supposed to convert
|
|
|
|
// (either because we're in the no-convert overload pass, or because we're explicitly
|
|
|
|
// instructed not to copy (via `py::arg().noconvert()`) we have to fail loading.
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!convert || need_writeable) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
Array copy = Array::ensure(src);
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!copy) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
fits = props::conformable(copy);
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!fits || !fits.template stride_compatible<props>()) {
|
2017-01-17 01:35:14 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-03-13 15:49:10 +00:00
|
|
|
copy_or_ref = std::move(copy);
|
2017-06-26 21:20:39 +00:00
|
|
|
loader_life_support::add_patient(copy_or_ref);
|
2017-01-17 01:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ref.reset();
|
2022-02-10 20:17:07 +00:00
|
|
|
map.reset(new MapType(data(copy_or_ref),
|
|
|
|
fits.rows,
|
|
|
|
fits.cols,
|
|
|
|
make_stride(fits.stride.outer(), fits.stride.inner())));
|
2017-01-17 01:35:14 +00:00
|
|
|
ref.reset(new Type(*map));
|
2016-08-03 20:50:22 +00:00
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-08-03 20:50:22 +00:00
|
|
|
|
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy
* clang-tidy explicit attr.h (all automatic)
* clang-tidy explicit cast.h (all automatic)
* clang-tidy detail/init.h (1 NOLINT)
* clang-tidy detail/type_caster_base.h (2 NOLINT)
* clang-tidy pybind11.h (7 NOLINT)
* clang-tidy detail/common.h (3 NOLINT)
* clang-tidy detail/descr.h (2 NOLINT)
* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)
* clang-tidy eigen.h (7 NOLINT, 0 explicit)
* Adding 2 explicit in functional.h
* Adding 4 explicit in iostream.h
* clang-tidy numpy.h (1 NOLINT, 1 explicit)
* clang-tidy embed.h (0 NOLINT, 1 explicit)
* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)
* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)
* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)
* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)
* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)
* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)
* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/object.h (0 NOLINT, 2 explicit)
* clang-tidy batch of fully automatic fixes.
* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-09 01:53:38 +00:00
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
2022-02-10 20:17:07 +00:00
|
|
|
operator Type *() { return ref.get(); }
|
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy
* clang-tidy explicit attr.h (all automatic)
* clang-tidy explicit cast.h (all automatic)
* clang-tidy detail/init.h (1 NOLINT)
* clang-tidy detail/type_caster_base.h (2 NOLINT)
* clang-tidy pybind11.h (7 NOLINT)
* clang-tidy detail/common.h (3 NOLINT)
* clang-tidy detail/descr.h (2 NOLINT)
* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)
* clang-tidy eigen.h (7 NOLINT, 0 explicit)
* Adding 2 explicit in functional.h
* Adding 4 explicit in iostream.h
* clang-tidy numpy.h (1 NOLINT, 1 explicit)
* clang-tidy embed.h (0 NOLINT, 1 explicit)
* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)
* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)
* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)
* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)
* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)
* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)
* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/object.h (0 NOLINT, 2 explicit)
* clang-tidy batch of fully automatic fixes.
* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-09 01:53:38 +00:00
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
2022-02-10 20:17:07 +00:00
|
|
|
operator Type &() { return *ref; }
|
|
|
|
template <typename _T>
|
|
|
|
using cast_op_type = pybind11::detail::cast_op_type<_T>;
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T = Type, enable_if_t<is_eigen_mutable_map<T>::value, int> = 0>
|
2022-02-10 20:17:07 +00:00
|
|
|
Scalar *data(Array &a) {
|
|
|
|
return a.mutable_data();
|
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
template <typename T = Type, enable_if_t<!is_eigen_mutable_map<T>::value, int> = 0>
|
2022-02-10 20:17:07 +00:00
|
|
|
const Scalar *data(Array &a) {
|
|
|
|
return a.data();
|
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
// Attempt to figure out a constructor of `Stride` that will work.
|
|
|
|
// If both strides are fixed, use a default constructor:
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename S>
|
|
|
|
using stride_ctor_default = bool_constant<S::InnerStrideAtCompileTime != Eigen::Dynamic
|
|
|
|
&& S::OuterStrideAtCompileTime != Eigen::Dynamic
|
|
|
|
&& std::is_default_constructible<S>::value>;
|
2017-01-17 01:35:14 +00:00
|
|
|
// Otherwise, if there is a two-index constructor, assume it is (outer,inner) like
|
|
|
|
// Eigen::Stride, and use it:
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename S>
|
|
|
|
using stride_ctor_dual
|
|
|
|
= bool_constant<!stride_ctor_default<S>::value
|
|
|
|
&& std::is_constructible<S, EigenIndex, EigenIndex>::value>;
|
2017-01-17 01:35:14 +00:00
|
|
|
// Otherwise, if there is a one-index constructor, and just one of the strides is dynamic, use
|
|
|
|
// it (passing whichever stride is dynamic).
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename S>
|
|
|
|
using stride_ctor_outer
|
|
|
|
= 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>;
|
|
|
|
template <typename S>
|
|
|
|
using stride_ctor_inner
|
|
|
|
= 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>;
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
template <typename S = StrideType, enable_if_t<stride_ctor_default<S>::value, int> = 0>
|
2022-02-10 20:17:07 +00:00
|
|
|
static S make_stride(EigenIndex, EigenIndex) {
|
|
|
|
return S();
|
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
template <typename S = StrideType, enable_if_t<stride_ctor_dual<S>::value, int> = 0>
|
2022-02-10 20:17:07 +00:00
|
|
|
static S make_stride(EigenIndex outer, EigenIndex inner) {
|
|
|
|
return S(outer, inner);
|
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
template <typename S = StrideType, enable_if_t<stride_ctor_outer<S>::value, int> = 0>
|
2022-02-10 20:17:07 +00:00
|
|
|
static S make_stride(EigenIndex outer, EigenIndex) {
|
|
|
|
return S(outer);
|
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
template <typename S = StrideType, enable_if_t<stride_ctor_inner<S>::value, int> = 0>
|
2022-02-10 20:17:07 +00:00
|
|
|
static S make_stride(EigenIndex, EigenIndex inner) {
|
|
|
|
return S(inner);
|
|
|
|
}
|
2016-08-03 20:50:22 +00:00
|
|
|
};
|
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
// type_caster for special matrix types (e.g. DiagonalMatrix), which are EigenBase, but not
|
|
|
|
// EigenDense (i.e. they don't have a data(), at least not with the usual matrix layout).
|
|
|
|
// load() is not supported, but we can cast them into the python domain by first copying to a
|
|
|
|
// regular Eigen::Matrix, then casting that.
|
2016-08-04 19:24:41 +00:00
|
|
|
template <typename Type>
|
2017-01-17 01:35:14 +00:00
|
|
|
struct type_caster<Type, enable_if_t<is_eigen_other<Type>::value>> {
|
2016-08-04 19:24:41 +00:00
|
|
|
protected:
|
2022-02-10 20:17:07 +00:00
|
|
|
using Matrix
|
|
|
|
= Eigen::Matrix<typename Type::Scalar, Type::RowsAtCompileTime, Type::ColsAtCompileTime>;
|
2017-01-17 01:35:14 +00:00
|
|
|
using props = EigenProps<Matrix>;
|
2022-02-10 20:17:07 +00:00
|
|
|
|
2016-08-04 19:24:41 +00:00
|
|
|
public:
|
2017-01-17 01:35:14 +00:00
|
|
|
static handle cast(const Type &src, return_value_policy /* policy */, handle /* parent */) {
|
|
|
|
handle h = eigen_encapsulate<props>(new Matrix(src));
|
|
|
|
return h;
|
|
|
|
}
|
2022-02-10 20:17:07 +00:00
|
|
|
static handle cast(const Type *src, return_value_policy policy, handle parent) {
|
|
|
|
return cast(*src, policy, parent);
|
|
|
|
}
|
2016-08-04 19:24:41 +00:00
|
|
|
|
2017-07-02 09:48:56 +00:00
|
|
|
static constexpr auto name = props::descriptor;
|
2016-08-04 19:24:41 +00:00
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
// Explicitly delete these: support python -> C++ conversion on these (i.e. these can be return
|
|
|
|
// types but not bound arguments). We still provide them (with an explicitly delete) so that
|
|
|
|
// you end up here if you try anyway.
|
|
|
|
bool load(handle, bool) = delete;
|
|
|
|
operator Type() = delete;
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename>
|
|
|
|
using cast_op_type = Type;
|
2016-08-04 19:24:41 +00:00
|
|
|
};
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Type>
|
2016-09-12 15:36:43 +00:00
|
|
|
struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
|
2020-11-09 18:10:19 +00:00
|
|
|
using Scalar = typename Type::Scalar;
|
|
|
|
using StorageIndex = remove_reference_t<decltype(*std::declval<Type>().outerIndexPtr())>;
|
|
|
|
using Index = typename Type::Index;
|
Eigen: fix partially-fixed matrix conversion
Currently when we do a conversion between a numpy array and an Eigen
Vector, we allow the conversion only if the Eigen type is a
compile-time vector (i.e. at least one dimension is fixed at 1 at
compile time), or if the type is dynamic on *both* dimensions.
This means we can run into cases where MatrixXd allow things that
conforming, compile-time sizes does not: for example,
`Matrix<double,4,Dynamic>` is currently not allowed, even when assigning
from a 4-element vector, but it *is* allowed for a
`Matrix<double,Dynamic,Dynamic>`.
This commit also reverts the current behaviour of using the matrix's
storage order to determine the structure when the Matrix is fully
dynamic (i.e. in both dimensions). Currently we assign to an eigen row
if the storage order is row-major, and column otherwise: this seems
wrong (the storage order has nothing to do with the shape!). While
numpy doesn't distinguish between a row/column vector, Eigen does, but
it makes more sense to consistently choose one than to produce
something with a different shape based on the intended storage layout.
2017-01-13 00:50:33 +00:00
|
|
|
static constexpr bool rowMajor = Type::IsRowMajor;
|
2016-05-05 18:33:54 +00:00
|
|
|
|
|
|
|
bool load(handle src, bool) {
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!src) {
|
2016-05-10 14:59:01 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-05-10 14:59:01 +00:00
|
|
|
|
2016-10-28 01:08:15 +00:00
|
|
|
auto obj = reinterpret_borrow<object>(src);
|
2020-10-03 17:38:03 +00:00
|
|
|
object sparse_module = module_::import("scipy.sparse");
|
2022-02-10 20:17:07 +00:00
|
|
|
object matrix_type = sparse_module.attr(rowMajor ? "csr_matrix" : "csc_matrix");
|
2016-05-05 18:33:54 +00:00
|
|
|
|
2020-09-16 15:32:17 +00:00
|
|
|
if (!type::handle_of(obj).is(matrix_type)) {
|
2016-05-05 18:33:54 +00:00
|
|
|
try {
|
2016-05-08 12:34:09 +00:00
|
|
|
obj = matrix_type(obj);
|
2016-05-05 18:33:54 +00:00
|
|
|
} catch (const error_already_set &) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:41:05 +00:00
|
|
|
auto values = array_t<Scalar>((object) obj.attr("data"));
|
|
|
|
auto innerIndices = array_t<StorageIndex>((object) obj.attr("indices"));
|
|
|
|
auto outerIndices = array_t<StorageIndex>((object) obj.attr("indptr"));
|
2016-05-05 18:33:54 +00:00
|
|
|
auto shape = pybind11::tuple((pybind11::object) obj.attr("shape"));
|
|
|
|
auto nnz = obj.attr("nnz").cast<Index>();
|
|
|
|
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!values || !innerIndices || !outerIndices) {
|
2016-05-05 18:33:54 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-05-05 18:33:54 +00:00
|
|
|
|
2021-11-23 01:01:35 +00:00
|
|
|
value = EigenMapSparseMatrix<Scalar,
|
2022-02-10 20:17:07 +00:00
|
|
|
Type::Flags &(Eigen::RowMajor | Eigen::ColMajor),
|
|
|
|
StorageIndex>(shape[0].cast<Index>(),
|
|
|
|
shape[1].cast<Index>(),
|
2022-03-24 16:57:37 +00:00
|
|
|
std::move(nnz),
|
2022-02-10 20:17:07 +00:00
|
|
|
outerIndices.mutable_data(),
|
|
|
|
innerIndices.mutable_data(),
|
|
|
|
values.mutable_data());
|
2016-05-05 18:33:54 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static handle cast(const Type &src, return_value_policy /* policy */, handle /* parent */) {
|
2022-02-10 20:17:07 +00:00
|
|
|
const_cast<Type &>(src).makeCompressed();
|
2016-05-05 18:33:54 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
object matrix_type
|
|
|
|
= module_::import("scipy.sparse").attr(rowMajor ? "csr_matrix" : "csc_matrix");
|
2016-05-05 18:33:54 +00:00
|
|
|
|
2017-04-14 20:33:44 +00:00
|
|
|
array data(src.nonZeros(), src.valuePtr());
|
|
|
|
array outerIndices((rowMajor ? src.rows() : src.cols()) + 1, src.outerIndexPtr());
|
|
|
|
array innerIndices(src.nonZeros(), src.innerIndexPtr());
|
2016-05-05 18:33:54 +00:00
|
|
|
|
2022-03-24 16:57:37 +00:00
|
|
|
return matrix_type(std::make_tuple(
|
|
|
|
std::move(data), std::move(innerIndices), std::move(outerIndices)),
|
2022-02-10 20:17:07 +00:00
|
|
|
std::make_pair(src.rows(), src.cols()))
|
|
|
|
.release();
|
2016-05-05 18:33:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
PYBIND11_TYPE_CASTER(Type,
|
|
|
|
const_name<(Type::IsRowMajor) != 0>("scipy.sparse.csr_matrix[",
|
|
|
|
"scipy.sparse.csc_matrix[")
|
|
|
|
+ npy_format_descriptor<Scalar>::name + const_name("]"));
|
2016-05-05 18:33:54 +00:00
|
|
|
};
|
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_END(detail)
|
|
|
|
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|