22 # pragma warning(disable : 4996) // C4996: std::unary_negation is deprecated
28 #include <Eigen/Cholesky>
36 for (
int i = 0;
i <
x.rows();
i++) {
37 for (
int j = 0;
j <
x.cols();
j++) {
38 x(
i,
j) = 11 + 10 *
i +
j;
45 static Eigen::MatrixXd *
x;
47 x =
new Eigen::MatrixXd(3, 3);
56 x =
new MatrixXdR(3, 3);
76 template <
typename MatrixArgType>
77 Eigen::MatrixXd adjust_matrix(MatrixArgType
m) {
78 Eigen::MatrixXd
ret(
m);
79 for (
int c = 0;
c <
m.cols();
c++) {
80 for (
int r = 0; r <
m.rows(); r++) {
81 ret(r,
c) += 10 * r + 100 *
c;
90 Eigen::Matrix4d
a = Eigen::Matrix4d::Zero();
91 Eigen::Matrix4d
b = Eigen::Matrix4d::Identity();
109 m.def(
"double_col", [](
const Eigen::VectorXf &
x) -> Eigen::VectorXf {
return 2.0f *
x; });
111 [](
const Eigen::RowVectorXf &
x) -> Eigen::RowVectorXf {
return 2.0f *
x; });
112 m.def(
"double_complex",
113 [](
const Eigen::VectorXcf &
x) -> Eigen::VectorXcf {
return 2.0f *
x; });
114 m.def(
"double_threec", [](py::EigenDRef<Eigen::Vector3f>
x) {
x *= 2; });
115 m.def(
"double_threer", [](py::EigenDRef<Eigen::RowVector3f>
x) {
x *= 2; });
116 m.def(
"double_mat_cm", [](
const Eigen::MatrixXf &
x) -> Eigen::MatrixXf {
return 2.0f *
x; });
117 m.def(
"double_mat_rm", [](
const DenseMatrixR &
x) -> DenseMatrixR {
return 2.0f *
x; });
124 return x.llt().matrixL();
129 return x.llt().matrixL();
141 m.def(
"add_rm", add_rm);
142 m.def(
"add_cm", add_cm);
144 m.def(
"add1", add_rm);
145 m.def(
"add1", add_cm);
146 m.def(
"add2", add_cm);
147 m.def(
"add2", add_rm);
150 [](py::EigenDRef<Eigen::MatrixXd>
x,
int r,
int c,
double v) {
x(r,
c) +=
v; });
165 m += Eigen::MatrixXd::Constant(
m.rows(),
m.cols(),
v);
168 py::return_value_policy::reference);
173 [](py::EigenDRef<Eigen::MatrixXd>
m,
double v) {
174 m += Eigen::MatrixXd::Constant(
m.rows(),
m.cols(),
v);
177 py::return_value_policy::reference);
182 [](py::EigenDRef<Eigen::MatrixXd>
m) {
183 return py::EigenDMap<Eigen::MatrixXd>(
187 py::EigenDStride(
m.outerStride(), 2 *
m.innerStride()));
189 py::return_value_policy::reference);
194 [](py::EigenDRef<Eigen::MatrixXd>
m) {
195 return py::EigenDMap<Eigen::MatrixXd>(
199 py::EigenDStride(2 *
m.outerStride(),
m.innerStride()));
201 py::return_value_policy::reference);
217 int block_cols) {
return x.block(start_row, start_col, block_rows, block_cols); });
219 [
m](
const py::object &x_obj,
224 return m.attr(
"_block")(x_obj, x_obj, start_row, start_col, block_rows, block_cols);
229 [](
const py::object &x_obj,
238 auto x0_orig = x_obj[*
i0].cast<
double>();
239 if (
x(0, 0) != x0_orig) {
240 throw std::runtime_error(
241 "Something in the type_caster for Eigen::Ref is terribly wrong.");
243 double x0_mod = x0_orig + 1;
245 auto copy_detected = (
x(0, 0) != x0_mod);
246 x_obj[*
i0] = x0_orig;
248 throw std::runtime_error(
"type_caster for Eigen::Ref made a copy.");
250 return x.block(start_row, start_col, block_rows, block_cols);
252 py::keep_alive<0, 1>());
263 static Eigen::MatrixXd
create() {
return Eigen::MatrixXd::Ones(10, 10); }
265 static const Eigen::MatrixXd createConst() {
return Eigen::MatrixXd::Ones(10, 10); }
266 Eigen::MatrixXd &
get() {
return mat; }
267 Eigen::MatrixXd *getPtr() {
return &
mat; }
268 const Eigen::MatrixXd &
view() {
return mat; }
269 const Eigen::MatrixXd *viewPtr() {
return &
mat; }
273 return mat.block(r,
c, nrow, ncol);
276 return mat.block(r,
c, nrow, ncol);
278 py::EigenDMap<Eigen::Matrix2d>
corners() {
279 return py::EigenDMap<Eigen::Matrix2d>(
281 py::EigenDStride(
mat.outerStride() * (
mat.outerSize() - 1),
282 mat.innerStride() * (
mat.innerSize() - 1)));
284 py::EigenDMap<const Eigen::Matrix2d> cornersConst()
const {
285 return py::EigenDMap<const Eigen::Matrix2d>(
287 py::EigenDStride(
mat.outerStride() * (
mat.outerSize() - 1),
288 mat.innerStride() * (
mat.innerSize() - 1)));
292 py::class_<ReturnTester>(
m,
"ReturnTester")
295 .def_static(
"create_const", &ReturnTester::createConst)
297 .def(
"get_ptr", &ReturnTester::getPtr, rvp::reference_internal)
303 .def(
"ref_const", &ReturnTester::refConst)
305 .def(
"ref_const_safe", &ReturnTester::refConst, rvp::reference_internal)
307 .def(
"copy_ref_const", &ReturnTester::refConst,
rvp::copy)
310 .def(
"block_const", &ReturnTester::blockConst, rvp::reference_internal)
313 .def(
"corners_const", &ReturnTester::cornersConst, rvp::reference_internal);
317 m.def(
"incr_diag", [](
int k) {
319 for (
int i = 0;
i < k;
i++) {
320 m.diagonal()[
i] =
i + 1;
326 m.def(
"symmetric_lower",
327 [](
const Eigen::MatrixXi &
m) {
return m.selfadjointView<
Eigen::Lower>(); });
329 m.def(
"symmetric_upper",
330 [](
const Eigen::MatrixXi &
m) {
return m.selfadjointView<
Eigen::Upper>(); });
333 Eigen::MatrixXf
mat(5, 6);
334 mat << 0, 3, 0, 0, 0, 11, 22, 0, 0, 0, 17, 11, 7, 5, 0, 1, 0, 11, 0, 0, 0, 0, 0, 11, 0, 0, 14,
338 m.def(
"fixed_r", [
mat]() -> FixedMatrixR {
return FixedMatrixR(
mat); });
342 m.def(
"fixed_r_const", [
mat]() ->
const FixedMatrixR {
return FixedMatrixR(
mat); });
343 m.def(
"fixed_c", [
mat]() -> FixedMatrixC {
return FixedMatrixC(
mat); });
344 m.def(
"fixed_copy_r", [](
const FixedMatrixR &
m) -> FixedMatrixR {
return m; });
345 m.def(
"fixed_copy_c", [](
const FixedMatrixC &
m) -> FixedMatrixC {
return m; });
349 m.def(
"fixed_mutator_a", [](
const py::EigenDRef<FixedMatrixC> &) {});
351 m.def(
"dense_r", [
mat]() -> DenseMatrixR {
return DenseMatrixR(
mat); });
352 m.def(
"dense_c", [
mat]() -> DenseMatrixC {
return DenseMatrixC(
mat); });
353 m.def(
"dense_copy_r", [](
const DenseMatrixR &
m) -> DenseMatrixR {
return m; });
354 m.def(
"dense_copy_c", [](
const DenseMatrixC &
m) -> DenseMatrixC {
return m; });
356 bool have_numpy =
true;
358 py::module_::import(
"numpy");
359 }
catch (
const py::error_already_set &) {
363 py::module_::import(
"numpy");
365 m.def(
"defaults_mat", [](
const Eigen::Matrix3d &) {},
py::arg(
"mat") = defaultMatrix);
367 Eigen::VectorXd defaultVector = Eigen::VectorXd::Ones(32);
368 m.def(
"defaults_vec", [](
const Eigen::VectorXd &) {},
py::arg(
"vec") = defaultMatrix);
371 m.def(
"sparse_r", [
mat]() -> SparseMatrixR {
377 m.def(
"sparse_copy_r", [](
const SparseMatrixR &
m) -> SparseMatrixR {
return m; });
378 m.def(
"sparse_copy_c", [](
const SparseMatrixC &
m) -> SparseMatrixC {
return m; });
380 m.def(
"partial_copy_four_rm_r", [](
const FourRowMatrixR &
m) -> FourRowMatrixR {
return m; });
381 m.def(
"partial_copy_four_rm_c", [](
const FourColMatrixR &
m) -> FourColMatrixR {
return m; });
382 m.def(
"partial_copy_four_cm_r", [](
const FourRowMatrixC &
m) -> FourRowMatrixC {
return m; });
383 m.def(
"partial_copy_four_cm_c", [](
const FourColMatrixC &
m) -> FourColMatrixC {
return m; });
387 m.def(
"cpp_copy", [](py::handle
m) {
return m.cast<Eigen::MatrixXd>()(1, 0); });
391 [](py::handle
m) {
return m.cast<py::EigenDRef<Eigen::MatrixXd>>()(1, 0); });
398 m.def(
"get_elem", &get_elem);
406 "get_elem_rm_nocopy",
430 m.def(
"iss1105_col", [](
const Eigen::VectorXd &) {
return true; });
431 m.def(
"iss1105_row", [](
const Eigen::RowVectorXd &) {
return true; });
437 [](
const py::EigenDRef<const Eigen::MatrixXd> &
A,
438 const py::EigenDRef<const Eigen::MatrixXd> &
B) -> Eigen::MatrixXd {
439 if (
A.cols() !=
B.rows()) {
440 throw std::domain_error(
"Nonconformable matrices!");
448 py::class_<CustomOperatorNew>(
m,
"CustomOperatorNew")
458 py::module_::import(
"numpy").attr(
"ones")(10);
462 py::module_::import(
"numpy").attr(
"ones")(10);