16 # pragma warning(disable: 4996) // C4996: std::unary_negation is deprecated 19 #include <Eigen/Cholesky> 21 using MatrixXdR = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
28 for (
int i = 0;
i < x.rows();
i++)
for (
int j = 0; j < x.cols(); j++)
29 x(
i, j) = 11 + 10*
i + j;
34 static Eigen::MatrixXd *
x;
36 x =
new Eigen::MatrixXd(3, 3);
57 double get_elem(Eigen::Ref<const Eigen::MatrixXd> m) {
return m(2, 1); };
62 template <
typename MatrixArgType> Eigen::MatrixXd
adjust_matrix(MatrixArgType m) {
63 Eigen::MatrixXd
ret(m);
64 for (
int c = 0; c < m.cols(); c++)
for (
int r = 0; r < m.rows(); r++)
65 ret(r, c) += 10*r + 100*c;
72 Eigen::Matrix4d
a = Eigen::Matrix4d::Zero();
73 Eigen::Matrix4d
b = Eigen::Matrix4d::Identity();
79 using FixedMatrixR = Eigen::Matrix<float, 5, 6, Eigen::RowMajor>;
80 using FixedMatrixC = Eigen::Matrix<float, 5, 6>;
81 using DenseMatrixR = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
82 using DenseMatrixC = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic>;
83 using FourRowMatrixC = Eigen::Matrix<float, 4, Eigen::Dynamic>;
84 using FourColMatrixC = Eigen::Matrix<float, Eigen::Dynamic, 4>;
85 using FourRowMatrixR = Eigen::Matrix<float, 4, Eigen::Dynamic>;
86 using FourColMatrixR = Eigen::Matrix<float, Eigen::Dynamic, 4>;
87 using SparseMatrixR = Eigen::SparseMatrix<float, Eigen::RowMajor>;
88 using SparseMatrixC = Eigen::SparseMatrix<float>;
90 m.attr(
"have_eigen") =
true;
93 m.def(
"double_col", [](
const Eigen::VectorXf &
x) -> Eigen::VectorXf {
return 2.0f *
x; });
94 m.def(
"double_row", [](
const Eigen::RowVectorXf &x) -> Eigen::RowVectorXf {
return 2.0f *
x; });
95 m.def(
"double_complex", [](
const Eigen::VectorXcf &x) -> Eigen::VectorXcf {
return 2.0f *
x; });
98 m.def(
"double_mat_cm", [](Eigen::MatrixXf x) -> Eigen::MatrixXf {
return 2.0f *
x; });
99 m.def(
"double_mat_rm", [](DenseMatrixR x) -> DenseMatrixR {
return 2.0f *
x; });
103 m.def(
"cholesky1", [](Eigen::Ref<MatrixXdR> x) -> Eigen::MatrixXd {
return x.llt().matrixL(); });
104 m.def(
"cholesky2", [](
const Eigen::Ref<const MatrixXdR> &x) -> Eigen::MatrixXd {
return x.llt().matrixL(); });
105 m.def(
"cholesky3", [](
const Eigen::Ref<MatrixXdR> &x) -> Eigen::MatrixXd {
return x.llt().matrixL(); });
106 m.def(
"cholesky4", [](Eigen::Ref<const MatrixXdR> x) -> Eigen::MatrixXd {
return x.llt().matrixL(); });
113 auto add_rm = [](Eigen::Ref<MatrixXdR>
x,
int r,
int c,
double v) {
x(r,c) += v; };
114 auto add_cm = [](Eigen::Ref<Eigen::MatrixXd>
x,
int r,
int c,
double v) {
x(r,c) += v; };
117 m.def(
"add_rm", add_rm);
118 m.def(
"add_cm", add_cm);
120 m.def(
"add1", add_rm);
121 m.def(
"add1", add_cm);
122 m.def(
"add2", add_cm);
123 m.def(
"add2", add_rm);
128 m.def(
"get_cm_ref", []() {
return Eigen::Ref<Eigen::MatrixXd>(
get_cm()); });
129 m.def(
"get_rm_ref", []() {
return Eigen::Ref<MatrixXdR>(
get_rm()); });
131 m.def(
"get_cm_const_ref", []() {
return Eigen::Ref<const Eigen::MatrixXd>(
get_cm()); });
132 m.def(
"get_rm_const_ref", []() {
return Eigen::Ref<const MatrixXdR>(
get_rm()); });
137 m.def(
"incr_matrix", [](Eigen::Ref<Eigen::MatrixXd> m,
double v) {
138 m += Eigen::MatrixXd::Constant(m.rows(), m.cols(), v);
144 m += Eigen::MatrixXd::Constant(m.rows(), m.cols(), v);
151 m.data(), (m.rows() + 1) / 2, m.cols(),
158 m.data(), m.rows(), (m.cols() + 1) / 2,
163 m.def(
"diagonal", [](
const Eigen::Ref<const Eigen::MatrixXd> &x) {
return x.diagonal(); });
164 m.def(
"diagonal_1", [](
const Eigen::Ref<const Eigen::MatrixXd> &x) {
return x.diagonal<1>(); });
165 m.def(
"diagonal_n", [](
const Eigen::Ref<const Eigen::MatrixXd> &x,
int index) {
return x.diagonal(index); });
168 m.def(
"block", [](
const Eigen::Ref<const Eigen::MatrixXd> &x,
int start_row,
int start_col,
int block_rows,
int block_cols) {
169 return x.block(start_row, start_col, block_rows, block_cols);
175 Eigen::MatrixXd mat = create();
179 static Eigen::MatrixXd create() {
return Eigen::MatrixXd::Ones(10, 10); }
180 static const Eigen::MatrixXd createConst() {
return Eigen::MatrixXd::Ones(10, 10); }
181 Eigen::MatrixXd &
get() {
return mat; }
182 Eigen::MatrixXd *getPtr() {
return &mat; }
183 const Eigen::MatrixXd &view() {
return mat; }
184 const Eigen::MatrixXd *viewPtr() {
return &mat; }
185 Eigen::Ref<Eigen::MatrixXd>
ref() {
return mat; }
186 Eigen::Ref<const Eigen::MatrixXd> refConst() {
return mat; }
187 Eigen::Block<Eigen::MatrixXd> block(
int r,
int c,
int nrow,
int ncol) {
return mat.block(r, c, nrow, ncol); }
188 Eigen::Block<const Eigen::MatrixXd> blockConst(
int r,
int c,
int nrow,
int ncol)
const {
return mat.block(r, c, nrow, ncol); }
190 py::EigenDStride(mat.outerStride() * (mat.outerSize()-1), mat.innerStride() * (mat.innerSize()-1))); }
192 py::EigenDStride(mat.outerStride() * (mat.outerSize()-1), mat.innerStride() * (mat.innerSize()-1))); }
197 .def_static(
"create", &ReturnTester::create)
198 .
def_static(
"create_const", &ReturnTester::createConst)
199 .
def(
"get", &ReturnTester::get, rvp::reference_internal)
200 .
def(
"get_ptr", &ReturnTester::getPtr, rvp::reference_internal)
201 .
def(
"view", &ReturnTester::view, rvp::reference_internal)
202 .
def(
"view_ptr", &ReturnTester::view, rvp::reference_internal)
203 .
def(
"copy_get", &ReturnTester::get)
204 .
def(
"copy_view", &ReturnTester::view)
206 .
def(
"ref_const", &ReturnTester::refConst)
208 .
def(
"ref_const_safe", &ReturnTester::refConst, rvp::reference_internal)
210 .
def(
"copy_ref_const", &ReturnTester::refConst, rvp::copy)
211 .
def(
"block", &ReturnTester::block)
212 .
def(
"block_safe", &ReturnTester::block, rvp::reference_internal)
213 .
def(
"block_const", &ReturnTester::blockConst, rvp::reference_internal)
214 .
def(
"copy_block", &ReturnTester::block, rvp::copy)
215 .
def(
"corners", &ReturnTester::corners, rvp::reference_internal)
216 .
def(
"corners_const", &ReturnTester::cornersConst, rvp::reference_internal)
221 m.
def(
"incr_diag", [](
int k) {
222 Eigen::DiagonalMatrix<int, Eigen::Dynamic> m(k);
223 for (
int i = 0;
i < k;
i++) m.diagonal()[
i] =
i+1;
228 m.
def(
"symmetric_lower", [](
const Eigen::MatrixXi &m) {
229 return m.selfadjointView<Eigen::Lower>();
232 m.def(
"symmetric_upper", [](
const Eigen::MatrixXi &m) {
233 return m.selfadjointView<Eigen::Upper>();
237 Eigen::MatrixXf mat(5, 6);
238 mat << 0, 3, 0, 0, 0, 11,
245 m.def(
"fixed_r", [mat]() -> FixedMatrixR {
return FixedMatrixR(mat); });
246 m.def(
"fixed_r_const", [mat]() ->
const FixedMatrixR {
return FixedMatrixR(mat); });
247 m.def(
"fixed_c", [mat]() -> FixedMatrixC {
return FixedMatrixC(mat); });
248 m.def(
"fixed_copy_r", [](
const FixedMatrixR &m) -> FixedMatrixR {
return m; });
249 m.def(
"fixed_copy_c", [](
const FixedMatrixC &m) -> FixedMatrixC {
return m; });
251 m.def(
"fixed_mutator_r", [](Eigen::Ref<FixedMatrixR>) {});
252 m.def(
"fixed_mutator_c", [](Eigen::Ref<FixedMatrixC>) {});
255 m.def(
"dense_r", [mat]() -> DenseMatrixR {
return DenseMatrixR(mat); });
256 m.def(
"dense_c", [mat]() -> DenseMatrixC {
return DenseMatrixC(mat); });
257 m.def(
"dense_copy_r", [](
const DenseMatrixR &m) -> DenseMatrixR {
return m; });
258 m.def(
"dense_copy_c", [](
const DenseMatrixC &m) -> DenseMatrixC {
return m; });
260 m.def(
"sparse_r", [mat]() -> SparseMatrixR {
return Eigen::SparseView<Eigen::MatrixXf>(mat); });
261 m.def(
"sparse_c", [mat]() -> SparseMatrixC {
return Eigen::SparseView<Eigen::MatrixXf>(mat); });
262 m.def(
"sparse_copy_r", [](
const SparseMatrixR &m) -> SparseMatrixR {
return m; });
263 m.def(
"sparse_copy_c", [](
const SparseMatrixC &m) -> SparseMatrixC {
return m; });
265 m.def(
"partial_copy_four_rm_r", [](
const FourRowMatrixR &m) -> FourRowMatrixR {
return m; });
266 m.def(
"partial_copy_four_rm_c", [](
const FourColMatrixR &m) -> FourColMatrixR {
return m; });
267 m.def(
"partial_copy_four_cm_r", [](
const FourRowMatrixC &m) -> FourRowMatrixC {
return m; });
268 m.def(
"partial_copy_four_cm_c", [](
const FourColMatrixC &m) -> FourColMatrixC {
return m; });
272 m.def(
"cpp_copy", [](
py::handle m) {
return m.
cast<Eigen::MatrixXd>()(1, 0); });
273 m.def(
"cpp_ref_c", [](
py::handle m) {
return m.
cast<Eigen::Ref<Eigen::MatrixXd>>()(1, 0); });
274 m.def(
"cpp_ref_r", [](
py::handle m) {
return m.
cast<Eigen::Ref<MatrixXdR>>()(1, 0); });
283 m.def(
"get_elem_nocopy", [](Eigen::Ref<const Eigen::MatrixXd> m) ->
double {
return get_elem(m); },
286 m.def(
"get_elem_rm_nocopy", [](Eigen::Ref<
const Eigen::Matrix<long, -1, -1, Eigen::RowMajor>> &m) ->
long {
return m(2, 1); },
293 m.def(
"iss738_f1", &
adjust_matrix<
const Eigen::Ref<const Eigen::MatrixXd> &>,
py::arg().noconvert());
294 m.def(
"iss738_f2", &
adjust_matrix<
const Eigen::Ref<
const Eigen::Matrix<double, -1, -1, Eigen::RowMajor>> &>,
py::arg().noconvert());
300 m.def(
"iss1105_col", [](Eigen::VectorXd) {
return true; });
301 m.def(
"iss1105_row", [](Eigen::RowVectorXd) {
return true; });
307 if (A.cols() != B.rows())
throw std::domain_error(
"Nonconformable matrices!");
321 m.
def(
"get_elem_direct", [](Eigen::Ref<const Eigen::VectorXd> v) {
322 py::module::import(
"numpy").attr(
"ones")(10);
325 m.
def(
"get_elem_indirect", [](std::vector<Eigen::Ref<const Eigen::VectorXd>> v) {
326 py::module::import(
"numpy").attr(
"ones")(10);
double get_elem(Eigen::Ref< const Eigen::MatrixXd > m)
Eigen::Stride< Eigen::Dynamic, Eigen::Dynamic > EigenDStride
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MatrixXdR
void print_destroyed(T *inst, Values &&...values)
test_initializer eigen("eigen", test_submodule_eigen)
Eigen::MatrixXd & get_cm()
Eigen::Ref< MatrixType, 0, EigenDStride > EigenDRef
size_t function_call handle ret
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
class_ & def_static(const char *name_, Func &&f, const Extra &... extra)
void print_created(T *inst, Values &&...values)
CustomOperatorNew()=default
arg & noconvert(bool flag=true)
Indicate that the type should not be converted in the type caster.
Eigen::MatrixXd adjust_matrix(MatrixArgType m)
class_ & def_readonly(const char *name, const D C::*pm, const Extra &...extra)
#define TEST_SUBMODULE(name, variable)
bool typename Extra class_ & def(const char *name_, Func &&f, const Extra &... extra)
Eigen::Map< MatrixType, 0, EigenDStride > EigenDMap