2016-05-05 18:33:54 +00:00
|
|
|
|
/*
|
2016-08-12 11:50:00 +00:00
|
|
|
|
tests/eigen.cpp -- automatic conversion of Eigen types
|
2016-05-05 18:33:54 +00:00
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
First draft of Eigen::Tensor support (#4201)
* First draft of Eigen::Tensor support
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix build errors
* Weird allocator stuff?
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove unused + additional allocator junk
* Disable warning
* Use constexpr
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* clang tidy fixes
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve comments
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove auto constexpr function
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Try again for older C++
* Oops forgot constexpr
* Move to new files as suggested
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix weird tests
* Fix nits
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Oops, forgot import
* Fix clang 3.6 bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* More comprehensive test suite
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Refactor allocators to make things more clear
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Switch to std::copy
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Switch to DSizes instead of array
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Address feedback
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix python + dummy c++ change to trigger build
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Alignment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add include guard
* Forgot inline
* Fix compiler warning
* Remove bad test
* Better type signatures
* Add guards to make compiler requirements more explicit
* style: pre-commit fixes
* Force rerun of tests due to flake
* style: pre-commit fixes
* Keep pragmas & all related comments together, add PLEASE KEEP IN SYNC
* Move headers out of detail
* style: pre-commit fixes
* Fix cmake
* Improve casting
* style: pre-commit fixes
* Add a ton more tests + refactor
* Improve names
* style: pre-commit fixes
* Update include/pybind11/eigen/tensor.h
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
* Fix tests
* style: pre-commit fixes
* Update
* Add a test to verify that strange numpy arrays work
* Fix dumb compiler warning
* Better tests
* Better tests
* Fix tests
* style: pre-commit fixes
* More test fixes
* style: pre-commit fixes
* A ton more test coverage
* Fix tests
* style: pre-commit fixes
* style: pre-commit fixes
* Add back constexpr
* Another test
* style: pre-commit fixes
* Improve tests
* Whoops
* Less magic numbers
* Update tests/test_eigen_tensor.py
Co-authored-by: Sergiu Deitsch <sergiud@users.noreply.github.com>
* Update tests/test_eigen_tensor.py
Co-authored-by: Sergiu Deitsch <sergiud@users.noreply.github.com>
* style: pre-commit fixes
* Fix tests
* style: pre-commit fixes
* Fix memory leak
* style: pre-commit fixes
* Fix order
* style: pre-commit fixes
* Add test to make sure unsafe casts fail
* Minor bug fix to work on 32 bit machines
* Implement convert flag
* style: pre-commit fixes
* Switch to correct TensorMap const use
* style: pre-commit fixes
* Support older versions of eigen
* Weird c++ compilers
* Fix Eigen bug
* Fix another eigen bug
* Yet another eigen bug
* Potential flakes?
* style: pre-commit fixes
* Rerun tests with dummy exception to find out what is going on
* Another dummy test run
* Ablate more
* Found the broken test?
* One step closer
* one step further
* Double check
* one thing at a time
* Give up and disable the test
* Clang lies about being gcc
* Oops, fix matrix test
* style: pre-commit fixes
* Add tests to verify scalar conversions
* style: pre-commit fixes
* Fix nits
* Support no_array
* Fix tests
* style: pre-commit fixes
* Silence compiler warning
* Improve build system for ancient compilers
* Make clang happy
* Make gcc happy
* Implement Skylion's suggestions
* Fix warning
* Inline const pointer check
* Implement suggestions
* style: pre-commit fixes
* Improve tests
* Typo
* style: pre-commit fixes
* Support Google's build environment
* style: pre-commit fixes
* Update include/pybind11/eigen/tensor.h
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
* style: pre-commit fixes
* Test cleanup per Skylion
* Switch to remvove_cv_t
* Cleaner test
* style: pre-commit fixes
* Remove tensor from eigen.h, update tests
* style: pre-commit fixes
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: Sergiu Deitsch <sergiud@users.noreply.github.com>
2022-10-18 23:54:16 +00:00
|
|
|
|
#include <pybind11/eigen/matrix.h>
|
2017-06-26 21:20:39 +00:00
|
|
|
|
#include <pybind11/stl.h>
|
2017-12-15 14:15:25 +00:00
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
|
#include "constructor_stats.h"
|
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
|
2017-12-15 14:15:25 +00:00
|
|
|
|
#if defined(_MSC_VER)
|
2022-02-10 20:17:07 +00:00
|
|
|
|
# pragma warning(disable : 4996) // C4996: std::unary_negation is deprecated
|
2017-12-15 14:15:25 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2016-08-03 20:50:22 +00:00
|
|
|
|
#include <Eigen/Cholesky>
|
2016-05-05 18:33:54 +00:00
|
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
|
using MatrixXdR = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
|
2016-07-05 19:01:11 +00:00
|
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
|
// Sets/resets a testing reference matrix to have values of 10*r + c, where r and c are the
|
|
|
|
|
// (1-based) row/column number.
|
2022-02-10 20:17:07 +00:00
|
|
|
|
template <typename M>
|
|
|
|
|
void reset_ref(M &x) {
|
2022-02-08 00:23:20 +00:00
|
|
|
|
for (int i = 0; i < x.rows(); i++) {
|
|
|
|
|
for (int j = 0; j < x.cols(); j++) {
|
|
|
|
|
x(i, j) = 11 + 10 * i + j;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
|
}
|
2016-08-03 20:50:22 +00:00
|
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
|
// Returns a static, column-major matrix
|
|
|
|
|
Eigen::MatrixXd &get_cm() {
|
|
|
|
|
static Eigen::MatrixXd *x;
|
|
|
|
|
if (!x) {
|
|
|
|
|
x = new Eigen::MatrixXd(3, 3);
|
|
|
|
|
reset_ref(*x);
|
|
|
|
|
}
|
|
|
|
|
return *x;
|
|
|
|
|
}
|
|
|
|
|
// Likewise, but row-major
|
|
|
|
|
MatrixXdR &get_rm() {
|
|
|
|
|
static MatrixXdR *x;
|
|
|
|
|
if (!x) {
|
|
|
|
|
x = new MatrixXdR(3, 3);
|
|
|
|
|
reset_ref(*x);
|
|
|
|
|
}
|
|
|
|
|
return *x;
|
|
|
|
|
}
|
|
|
|
|
// Resets the values of the static matrices returned by get_cm()/get_rm()
|
|
|
|
|
void reset_refs() {
|
|
|
|
|
reset_ref(get_cm());
|
|
|
|
|
reset_ref(get_rm());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns element 2,1 from a matrix (used to test copy/nocopy)
|
2021-06-22 16:11:54 +00:00
|
|
|
|
double get_elem(const Eigen::Ref<const Eigen::MatrixXd> &m) { return m(2, 1); };
|
2017-03-17 17:51:52 +00:00
|
|
|
|
|
|
|
|
|
// Returns a matrix with 10*r + 100*c added to each matrix element (to help test that the matrix
|
|
|
|
|
// reference is referencing rows/columns correctly).
|
2022-02-10 20:17:07 +00:00
|
|
|
|
template <typename MatrixArgType>
|
|
|
|
|
Eigen::MatrixXd adjust_matrix(MatrixArgType m) {
|
2017-03-17 17:51:52 +00:00
|
|
|
|
Eigen::MatrixXd ret(m);
|
2022-02-08 00:23:20 +00:00
|
|
|
|
for (int c = 0; c < m.cols(); c++) {
|
|
|
|
|
for (int r = 0; r < m.rows(); r++) {
|
|
|
|
|
ret(r, c) += 10 * r + 100 * c; // NOLINT(clang-analyzer-core.uninitialized.Assign)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-17 17:51:52 +00:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-21 00:15:20 +00:00
|
|
|
|
struct CustomOperatorNew {
|
|
|
|
|
CustomOperatorNew() = default;
|
|
|
|
|
|
|
|
|
|
Eigen::Matrix4d a = Eigen::Matrix4d::Zero();
|
|
|
|
|
Eigen::Matrix4d b = Eigen::Matrix4d::Identity();
|
|
|
|
|
|
|
|
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
|
|
|
|
|
};
|
|
|
|
|
|
First draft of Eigen::Tensor support (#4201)
* First draft of Eigen::Tensor support
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix build errors
* Weird allocator stuff?
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove unused + additional allocator junk
* Disable warning
* Use constexpr
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* clang tidy fixes
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve comments
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove auto constexpr function
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Try again for older C++
* Oops forgot constexpr
* Move to new files as suggested
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix weird tests
* Fix nits
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Oops, forgot import
* Fix clang 3.6 bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* More comprehensive test suite
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Refactor allocators to make things more clear
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Switch to std::copy
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Switch to DSizes instead of array
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Address feedback
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix python + dummy c++ change to trigger build
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Alignment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add include guard
* Forgot inline
* Fix compiler warning
* Remove bad test
* Better type signatures
* Add guards to make compiler requirements more explicit
* style: pre-commit fixes
* Force rerun of tests due to flake
* style: pre-commit fixes
* Keep pragmas & all related comments together, add PLEASE KEEP IN SYNC
* Move headers out of detail
* style: pre-commit fixes
* Fix cmake
* Improve casting
* style: pre-commit fixes
* Add a ton more tests + refactor
* Improve names
* style: pre-commit fixes
* Update include/pybind11/eigen/tensor.h
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
* Fix tests
* style: pre-commit fixes
* Update
* Add a test to verify that strange numpy arrays work
* Fix dumb compiler warning
* Better tests
* Better tests
* Fix tests
* style: pre-commit fixes
* More test fixes
* style: pre-commit fixes
* A ton more test coverage
* Fix tests
* style: pre-commit fixes
* style: pre-commit fixes
* Add back constexpr
* Another test
* style: pre-commit fixes
* Improve tests
* Whoops
* Less magic numbers
* Update tests/test_eigen_tensor.py
Co-authored-by: Sergiu Deitsch <sergiud@users.noreply.github.com>
* Update tests/test_eigen_tensor.py
Co-authored-by: Sergiu Deitsch <sergiud@users.noreply.github.com>
* style: pre-commit fixes
* Fix tests
* style: pre-commit fixes
* Fix memory leak
* style: pre-commit fixes
* Fix order
* style: pre-commit fixes
* Add test to make sure unsafe casts fail
* Minor bug fix to work on 32 bit machines
* Implement convert flag
* style: pre-commit fixes
* Switch to correct TensorMap const use
* style: pre-commit fixes
* Support older versions of eigen
* Weird c++ compilers
* Fix Eigen bug
* Fix another eigen bug
* Yet another eigen bug
* Potential flakes?
* style: pre-commit fixes
* Rerun tests with dummy exception to find out what is going on
* Another dummy test run
* Ablate more
* Found the broken test?
* One step closer
* one step further
* Double check
* one thing at a time
* Give up and disable the test
* Clang lies about being gcc
* Oops, fix matrix test
* style: pre-commit fixes
* Add tests to verify scalar conversions
* style: pre-commit fixes
* Fix nits
* Support no_array
* Fix tests
* style: pre-commit fixes
* Silence compiler warning
* Improve build system for ancient compilers
* Make clang happy
* Make gcc happy
* Implement Skylion's suggestions
* Fix warning
* Inline const pointer check
* Implement suggestions
* style: pre-commit fixes
* Improve tests
* Typo
* style: pre-commit fixes
* Support Google's build environment
* style: pre-commit fixes
* Update include/pybind11/eigen/tensor.h
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
* style: pre-commit fixes
* Test cleanup per Skylion
* Switch to remvove_cv_t
* Cleaner test
* style: pre-commit fixes
* Remove tensor from eigen.h, update tests
* style: pre-commit fixes
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: Sergiu Deitsch <sergiud@users.noreply.github.com>
2022-10-18 23:54:16 +00:00
|
|
|
|
TEST_SUBMODULE(eigen_matrix, m) {
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
using FixedMatrixR = Eigen::Matrix<float, 5, 6, Eigen::RowMajor>;
|
|
|
|
|
using FixedMatrixC = Eigen::Matrix<float, 5, 6>;
|
|
|
|
|
using DenseMatrixR = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
|
|
|
|
|
using DenseMatrixC = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic>;
|
|
|
|
|
using FourRowMatrixC = Eigen::Matrix<float, 4, Eigen::Dynamic>;
|
|
|
|
|
using FourColMatrixC = Eigen::Matrix<float, Eigen::Dynamic, 4>;
|
|
|
|
|
using FourRowMatrixR = Eigen::Matrix<float, 4, Eigen::Dynamic>;
|
|
|
|
|
using FourColMatrixR = Eigen::Matrix<float, Eigen::Dynamic, 4>;
|
|
|
|
|
using SparseMatrixR = Eigen::SparseMatrix<float, Eigen::RowMajor>;
|
|
|
|
|
using SparseMatrixC = Eigen::SparseMatrix<float>;
|
2016-05-05 18:33:54 +00:00
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
// various tests
|
2017-01-17 01:35:14 +00:00
|
|
|
|
m.def("double_col", [](const Eigen::VectorXf &x) -> Eigen::VectorXf { return 2.0f * x; });
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("double_row",
|
|
|
|
|
[](const Eigen::RowVectorXf &x) -> Eigen::RowVectorXf { return 2.0f * x; });
|
|
|
|
|
m.def("double_complex",
|
|
|
|
|
[](const Eigen::VectorXcf &x) -> Eigen::VectorXcf { return 2.0f * x; });
|
2017-01-17 01:35:14 +00:00
|
|
|
|
m.def("double_threec", [](py::EigenDRef<Eigen::Vector3f> x) { x *= 2; });
|
|
|
|
|
m.def("double_threer", [](py::EigenDRef<Eigen::RowVector3f> x) { x *= 2; });
|
2021-06-22 16:11:54 +00:00
|
|
|
|
m.def("double_mat_cm", [](const Eigen::MatrixXf &x) -> Eigen::MatrixXf { return 2.0f * x; });
|
|
|
|
|
m.def("double_mat_rm", [](const DenseMatrixR &x) -> DenseMatrixR { return 2.0f * x; });
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
// test_eigen_ref_to_python
|
2017-01-17 01:35:14 +00:00
|
|
|
|
// Different ways of passing via Eigen::Ref; the first and second are the Eigen-recommended
|
2021-07-12 20:10:28 +00:00
|
|
|
|
m.def("cholesky1",
|
|
|
|
|
[](const Eigen::Ref<MatrixXdR> &x) -> Eigen::MatrixXd { return x.llt().matrixL(); });
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("cholesky2", [](const Eigen::Ref<const MatrixXdR> &x) -> Eigen::MatrixXd {
|
|
|
|
|
return x.llt().matrixL();
|
|
|
|
|
});
|
|
|
|
|
m.def("cholesky3",
|
|
|
|
|
[](const Eigen::Ref<MatrixXdR> &x) -> Eigen::MatrixXd { return x.llt().matrixL(); });
|
2021-07-12 20:10:28 +00:00
|
|
|
|
m.def("cholesky4", [](const Eigen::Ref<const MatrixXdR> &x) -> Eigen::MatrixXd {
|
|
|
|
|
return x.llt().matrixL();
|
|
|
|
|
});
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
// test_eigen_ref_mutators
|
2022-02-10 20:17:07 +00:00
|
|
|
|
// Mutators: these add some value to the given element using Eigen, but Eigen should be mapping
|
|
|
|
|
// into the numpy array data and so the result should show up there. There are three versions:
|
|
|
|
|
// one that works on a contiguous-row matrix (numpy's default), one for a contiguous-column
|
|
|
|
|
// matrix, and one for any matrix.
|
|
|
|
|
auto add_rm = [](Eigen::Ref<MatrixXdR> x, int r, int c, double v) { x(r, c) += v; };
|
|
|
|
|
auto add_cm = [](Eigen::Ref<Eigen::MatrixXd> x, int r, int c, double v) { x(r, c) += v; };
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
|
|
// Mutators (Eigen maps into numpy variables):
|
|
|
|
|
m.def("add_rm", add_rm); // Only takes row-contiguous
|
|
|
|
|
m.def("add_cm", add_cm); // Only takes column-contiguous
|
|
|
|
|
// Overloaded versions that will accept either row or column contiguous:
|
|
|
|
|
m.def("add1", add_rm);
|
|
|
|
|
m.def("add1", add_cm);
|
|
|
|
|
m.def("add2", add_cm);
|
|
|
|
|
m.def("add2", add_rm);
|
|
|
|
|
// This one accepts a matrix of any stride:
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("add_any",
|
|
|
|
|
[](py::EigenDRef<Eigen::MatrixXd> x, int r, int c, double v) { x(r, c) += v; });
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
2018-02-21 18:28:55 +00:00
|
|
|
|
// Return mutable references (numpy maps into eigen variables)
|
2017-01-17 01:35:14 +00:00
|
|
|
|
m.def("get_cm_ref", []() { return Eigen::Ref<Eigen::MatrixXd>(get_cm()); });
|
|
|
|
|
m.def("get_rm_ref", []() { return Eigen::Ref<MatrixXdR>(get_rm()); });
|
|
|
|
|
// The same references, but non-mutable (numpy maps into eigen variables, but is !writeable)
|
|
|
|
|
m.def("get_cm_const_ref", []() { return Eigen::Ref<const Eigen::MatrixXd>(get_cm()); });
|
|
|
|
|
m.def("get_rm_const_ref", []() { return Eigen::Ref<const MatrixXdR>(get_rm()); });
|
|
|
|
|
|
|
|
|
|
m.def("reset_refs", reset_refs); // Restores get_{cm,rm}_ref to original values
|
|
|
|
|
|
|
|
|
|
// Increments and returns ref to (same) matrix
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def(
|
|
|
|
|
"incr_matrix",
|
|
|
|
|
[](Eigen::Ref<Eigen::MatrixXd> m, double v) {
|
|
|
|
|
m += Eigen::MatrixXd::Constant(m.rows(), m.cols(), v);
|
|
|
|
|
return m;
|
|
|
|
|
},
|
|
|
|
|
py::return_value_policy::reference);
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
|
|
// Same, but accepts a matrix of any strides
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def(
|
|
|
|
|
"incr_matrix_any",
|
|
|
|
|
[](py::EigenDRef<Eigen::MatrixXd> m, double v) {
|
|
|
|
|
m += Eigen::MatrixXd::Constant(m.rows(), m.cols(), v);
|
|
|
|
|
return m;
|
|
|
|
|
},
|
|
|
|
|
py::return_value_policy::reference);
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
|
|
// Returns an eigen slice of even rows
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def(
|
|
|
|
|
"even_rows",
|
|
|
|
|
[](py::EigenDRef<Eigen::MatrixXd> m) {
|
|
|
|
|
return py::EigenDMap<Eigen::MatrixXd>(
|
|
|
|
|
m.data(),
|
|
|
|
|
(m.rows() + 1) / 2,
|
|
|
|
|
m.cols(),
|
2017-01-17 01:35:14 +00:00
|
|
|
|
py::EigenDStride(m.outerStride(), 2 * m.innerStride()));
|
2022-02-10 20:17:07 +00:00
|
|
|
|
},
|
|
|
|
|
py::return_value_policy::reference);
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
|
|
|
|
// Returns an eigen slice of even columns
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def(
|
|
|
|
|
"even_cols",
|
|
|
|
|
[](py::EigenDRef<Eigen::MatrixXd> m) {
|
|
|
|
|
return py::EigenDMap<Eigen::MatrixXd>(
|
|
|
|
|
m.data(),
|
|
|
|
|
m.rows(),
|
|
|
|
|
(m.cols() + 1) / 2,
|
2017-01-17 01:35:14 +00:00
|
|
|
|
py::EigenDStride(2 * m.outerStride(), m.innerStride()));
|
2022-02-10 20:17:07 +00:00
|
|
|
|
},
|
|
|
|
|
py::return_value_policy::reference);
|
2016-07-05 19:01:11 +00:00
|
|
|
|
|
2016-08-04 17:21:39 +00:00
|
|
|
|
// Returns diagonals: a vector-like object with an inner stride != 1
|
|
|
|
|
m.def("diagonal", [](const Eigen::Ref<const Eigen::MatrixXd> &x) { return x.diagonal(); });
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("diagonal_1",
|
|
|
|
|
[](const Eigen::Ref<const Eigen::MatrixXd> &x) { return x.diagonal<1>(); });
|
|
|
|
|
m.def("diagonal_n",
|
|
|
|
|
[](const Eigen::Ref<const Eigen::MatrixXd> &x, int index) { return x.diagonal(index); });
|
2016-08-04 17:21:39 +00:00
|
|
|
|
|
|
|
|
|
// Return a block of a matrix (gives non-standard strides)
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("block",
|
2022-10-07 16:20:38 +00:00
|
|
|
|
[m](const py::object &x_obj,
|
|
|
|
|
int start_row,
|
|
|
|
|
int start_col,
|
|
|
|
|
int block_rows,
|
|
|
|
|
int block_cols) {
|
|
|
|
|
return m.attr("_block")(x_obj, x_obj, start_row, start_col, block_rows, block_cols);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
m.def(
|
|
|
|
|
"_block",
|
|
|
|
|
[](const py::object &x_obj,
|
|
|
|
|
const Eigen::Ref<const Eigen::MatrixXd> &x,
|
|
|
|
|
int start_row,
|
|
|
|
|
int start_col,
|
|
|
|
|
int block_rows,
|
|
|
|
|
int block_cols) {
|
|
|
|
|
// See PR #4217 for background. This test is a bit over the top, but might be useful
|
|
|
|
|
// as a concrete example to point to when explaining the dangling reference trap.
|
|
|
|
|
auto i0 = py::make_tuple(0, 0);
|
|
|
|
|
auto x0_orig = x_obj[*i0].cast<double>();
|
|
|
|
|
if (x(0, 0) != x0_orig) {
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
"Something in the type_caster for Eigen::Ref is terribly wrong.");
|
|
|
|
|
}
|
|
|
|
|
double x0_mod = x0_orig + 1;
|
|
|
|
|
x_obj[*i0] = x0_mod;
|
|
|
|
|
auto copy_detected = (x(0, 0) != x0_mod);
|
|
|
|
|
x_obj[*i0] = x0_orig;
|
|
|
|
|
if (copy_detected) {
|
|
|
|
|
throw std::runtime_error("type_caster for Eigen::Ref made a copy.");
|
|
|
|
|
}
|
|
|
|
|
return x.block(start_row, start_col, block_rows, block_cols);
|
|
|
|
|
},
|
|
|
|
|
py::keep_alive<0, 1>());
|
2016-08-04 17:21:39 +00:00
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
// test_eigen_return_references, test_eigen_keepalive
|
2017-01-17 01:35:14 +00:00
|
|
|
|
// return value referencing/copying tests:
|
|
|
|
|
class ReturnTester {
|
|
|
|
|
Eigen::MatrixXd mat = create();
|
2022-02-10 20:17:07 +00:00
|
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
|
public:
|
|
|
|
|
ReturnTester() { print_created(this); }
|
|
|
|
|
~ReturnTester() { print_destroyed(this); }
|
|
|
|
|
static Eigen::MatrixXd create() { return Eigen::MatrixXd::Ones(10, 10); }
|
2021-09-10 04:27:36 +00:00
|
|
|
|
// NOLINTNEXTLINE(readability-const-return-type)
|
2017-01-17 01:35:14 +00:00
|
|
|
|
static const Eigen::MatrixXd createConst() { return Eigen::MatrixXd::Ones(10, 10); }
|
|
|
|
|
Eigen::MatrixXd &get() { return mat; }
|
|
|
|
|
Eigen::MatrixXd *getPtr() { return &mat; }
|
|
|
|
|
const Eigen::MatrixXd &view() { return mat; }
|
|
|
|
|
const Eigen::MatrixXd *viewPtr() { return &mat; }
|
|
|
|
|
Eigen::Ref<Eigen::MatrixXd> ref() { return mat; }
|
|
|
|
|
Eigen::Ref<const Eigen::MatrixXd> refConst() { return mat; }
|
2022-02-10 20:17:07 +00:00
|
|
|
|
Eigen::Block<Eigen::MatrixXd> block(int r, int c, int nrow, int ncol) {
|
|
|
|
|
return mat.block(r, c, nrow, ncol);
|
|
|
|
|
}
|
|
|
|
|
Eigen::Block<const Eigen::MatrixXd> blockConst(int r, int c, int nrow, int ncol) const {
|
|
|
|
|
return mat.block(r, c, nrow, ncol);
|
|
|
|
|
}
|
|
|
|
|
py::EigenDMap<Eigen::Matrix2d> corners() {
|
|
|
|
|
return py::EigenDMap<Eigen::Matrix2d>(
|
|
|
|
|
mat.data(),
|
|
|
|
|
py::EigenDStride(mat.outerStride() * (mat.outerSize() - 1),
|
|
|
|
|
mat.innerStride() * (mat.innerSize() - 1)));
|
|
|
|
|
}
|
|
|
|
|
py::EigenDMap<const Eigen::Matrix2d> cornersConst() const {
|
|
|
|
|
return py::EigenDMap<const Eigen::Matrix2d>(
|
|
|
|
|
mat.data(),
|
|
|
|
|
py::EigenDStride(mat.outerStride() * (mat.outerSize() - 1),
|
|
|
|
|
mat.innerStride() * (mat.innerSize() - 1)));
|
|
|
|
|
}
|
2017-01-17 01:35:14 +00:00
|
|
|
|
};
|
|
|
|
|
using rvp = py::return_value_policy;
|
|
|
|
|
py::class_<ReturnTester>(m, "ReturnTester")
|
|
|
|
|
.def(py::init<>())
|
|
|
|
|
.def_static("create", &ReturnTester::create)
|
|
|
|
|
.def_static("create_const", &ReturnTester::createConst)
|
|
|
|
|
.def("get", &ReturnTester::get, rvp::reference_internal)
|
|
|
|
|
.def("get_ptr", &ReturnTester::getPtr, rvp::reference_internal)
|
|
|
|
|
.def("view", &ReturnTester::view, rvp::reference_internal)
|
|
|
|
|
.def("view_ptr", &ReturnTester::view, rvp::reference_internal)
|
2022-02-10 20:17:07 +00:00
|
|
|
|
.def("copy_get", &ReturnTester::get) // Default rvp: copy
|
|
|
|
|
.def("copy_view", &ReturnTester::view) // "
|
|
|
|
|
.def("ref", &ReturnTester::ref) // Default for Ref is to reference
|
2017-01-17 01:35:14 +00:00
|
|
|
|
.def("ref_const", &ReturnTester::refConst) // Likewise, but const
|
|
|
|
|
.def("ref_safe", &ReturnTester::ref, rvp::reference_internal)
|
|
|
|
|
.def("ref_const_safe", &ReturnTester::refConst, rvp::reference_internal)
|
|
|
|
|
.def("copy_ref", &ReturnTester::ref, rvp::copy)
|
|
|
|
|
.def("copy_ref_const", &ReturnTester::refConst, rvp::copy)
|
|
|
|
|
.def("block", &ReturnTester::block)
|
|
|
|
|
.def("block_safe", &ReturnTester::block, rvp::reference_internal)
|
|
|
|
|
.def("block_const", &ReturnTester::blockConst, rvp::reference_internal)
|
|
|
|
|
.def("copy_block", &ReturnTester::block, rvp::copy)
|
|
|
|
|
.def("corners", &ReturnTester::corners, rvp::reference_internal)
|
2022-02-10 20:17:07 +00:00
|
|
|
|
.def("corners_const", &ReturnTester::cornersConst, rvp::reference_internal);
|
2017-01-17 01:35:14 +00:00
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
// test_special_matrix_objects
|
2016-08-04 19:24:41 +00:00
|
|
|
|
// Returns a DiagonalMatrix with diagonal (1,2,3,...)
|
|
|
|
|
m.def("incr_diag", [](int k) {
|
|
|
|
|
Eigen::DiagonalMatrix<int, Eigen::Dynamic> m(k);
|
2022-02-08 00:23:20 +00:00
|
|
|
|
for (int i = 0; i < k; i++) {
|
|
|
|
|
m.diagonal()[i] = i + 1;
|
|
|
|
|
}
|
2016-08-04 19:24:41 +00:00
|
|
|
|
return m;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Returns a SelfAdjointView referencing the lower triangle of m
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("symmetric_lower",
|
|
|
|
|
[](const Eigen::MatrixXi &m) { return m.selfadjointView<Eigen::Lower>(); });
|
2016-08-04 19:24:41 +00:00
|
|
|
|
// Returns a SelfAdjointView referencing the lower triangle of m
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("symmetric_upper",
|
|
|
|
|
[](const Eigen::MatrixXi &m) { return m.selfadjointView<Eigen::Upper>(); });
|
2016-08-04 19:24:41 +00:00
|
|
|
|
|
2017-01-17 01:35:14 +00:00
|
|
|
|
// Test matrix for various functions below.
|
|
|
|
|
Eigen::MatrixXf mat(5, 6);
|
2022-02-10 20:17:07 +00:00
|
|
|
|
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,
|
|
|
|
|
0, 8, 11;
|
2016-05-05 18:33:54 +00:00
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
// test_fixed, and various other tests
|
2017-01-17 01:35:14 +00:00
|
|
|
|
m.def("fixed_r", [mat]() -> FixedMatrixR { return FixedMatrixR(mat); });
|
2021-09-10 04:27:36 +00:00
|
|
|
|
// Our Eigen does a hack which respects constness through the numpy writeable flag.
|
|
|
|
|
// Therefore, the const return actually affects this type despite being an rvalue.
|
|
|
|
|
// NOLINTNEXTLINE(readability-const-return-type)
|
2017-01-17 01:35:14 +00:00
|
|
|
|
m.def("fixed_r_const", [mat]() -> const FixedMatrixR { return FixedMatrixR(mat); });
|
|
|
|
|
m.def("fixed_c", [mat]() -> FixedMatrixC { return FixedMatrixC(mat); });
|
|
|
|
|
m.def("fixed_copy_r", [](const FixedMatrixR &m) -> FixedMatrixR { return m; });
|
|
|
|
|
m.def("fixed_copy_c", [](const FixedMatrixC &m) -> FixedMatrixC { return m; });
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
// test_mutator_descriptors
|
2021-07-12 20:10:28 +00:00
|
|
|
|
m.def("fixed_mutator_r", [](const Eigen::Ref<FixedMatrixR> &) {});
|
|
|
|
|
m.def("fixed_mutator_c", [](const Eigen::Ref<FixedMatrixC> &) {});
|
|
|
|
|
m.def("fixed_mutator_a", [](const py::EigenDRef<FixedMatrixC> &) {});
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
// test_dense
|
2017-01-17 01:35:14 +00:00
|
|
|
|
m.def("dense_r", [mat]() -> DenseMatrixR { return DenseMatrixR(mat); });
|
|
|
|
|
m.def("dense_c", [mat]() -> DenseMatrixC { return DenseMatrixC(mat); });
|
|
|
|
|
m.def("dense_copy_r", [](const DenseMatrixR &m) -> DenseMatrixR { return m; });
|
|
|
|
|
m.def("dense_copy_c", [](const DenseMatrixC &m) -> DenseMatrixC { return m; });
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
// test_sparse, test_sparse_signature
|
2021-07-14 01:14:58 +00:00
|
|
|
|
m.def("sparse_r", [mat]() -> SparseMatrixR {
|
|
|
|
|
// NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn)
|
|
|
|
|
return Eigen::SparseView<Eigen::MatrixXf>(mat);
|
|
|
|
|
});
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("sparse_c",
|
|
|
|
|
[mat]() -> SparseMatrixC { return Eigen::SparseView<Eigen::MatrixXf>(mat); });
|
2017-01-17 01:35:14 +00:00
|
|
|
|
m.def("sparse_copy_r", [](const SparseMatrixR &m) -> SparseMatrixR { return m; });
|
|
|
|
|
m.def("sparse_copy_c", [](const SparseMatrixC &m) -> SparseMatrixC { return m; });
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
// test_partially_fixed
|
2017-01-17 01:35:14 +00:00
|
|
|
|
m.def("partial_copy_four_rm_r", [](const FourRowMatrixR &m) -> FourRowMatrixR { return m; });
|
|
|
|
|
m.def("partial_copy_four_rm_c", [](const FourColMatrixR &m) -> FourColMatrixR { return m; });
|
|
|
|
|
m.def("partial_copy_four_cm_r", [](const FourRowMatrixC &m) -> FourRowMatrixC { return m; });
|
|
|
|
|
m.def("partial_copy_four_cm_c", [](const FourColMatrixC &m) -> FourColMatrixC { return m; });
|
2016-05-05 18:33:54 +00:00
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
// test_cpp_casting
|
2017-01-17 01:35:14 +00:00
|
|
|
|
// Test that we can cast a numpy object to a Eigen::MatrixXd explicitly
|
|
|
|
|
m.def("cpp_copy", [](py::handle m) { return m.cast<Eigen::MatrixXd>()(1, 0); });
|
|
|
|
|
m.def("cpp_ref_c", [](py::handle m) { return m.cast<Eigen::Ref<Eigen::MatrixXd>>()(1, 0); });
|
|
|
|
|
m.def("cpp_ref_r", [](py::handle m) { return m.cast<Eigen::Ref<MatrixXdR>>()(1, 0); });
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("cpp_ref_any",
|
|
|
|
|
[](py::handle m) { return m.cast<py::EigenDRef<Eigen::MatrixXd>>()(1, 0); });
|
2016-05-05 18:33:54 +00:00
|
|
|
|
|
2021-01-15 20:59:47 +00:00
|
|
|
|
// [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works.
|
2016-05-05 18:33:54 +00:00
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
// test_nocopy_wrapper
|
2017-01-17 01:35:14 +00:00
|
|
|
|
// Test that we can prevent copying into an argument that would normally copy: First a version
|
|
|
|
|
// that would allow copying (if types or strides don't match) for comparison:
|
|
|
|
|
m.def("get_elem", &get_elem);
|
|
|
|
|
// Now this alternative that calls the tells pybind to fail rather than copy:
|
2021-07-12 20:10:28 +00:00
|
|
|
|
m.def(
|
|
|
|
|
"get_elem_nocopy",
|
|
|
|
|
[](const Eigen::Ref<const Eigen::MatrixXd> &m) -> double { return get_elem(m); },
|
|
|
|
|
py::arg{}.noconvert());
|
2017-01-17 01:35:14 +00:00
|
|
|
|
// Also test a row-major-only no-copy const ref:
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def(
|
|
|
|
|
"get_elem_rm_nocopy",
|
|
|
|
|
[](Eigen::Ref<const Eigen::Matrix<long, -1, -1, Eigen::RowMajor>> &m) -> long {
|
|
|
|
|
return m(2, 1);
|
|
|
|
|
},
|
|
|
|
|
py::arg{}.noconvert());
|
2017-03-17 17:51:52 +00:00
|
|
|
|
|
2022-06-29 18:37:16 +00:00
|
|
|
|
// test_issue738, test_zero_length
|
|
|
|
|
// Issue #738: 1×N or N×1 2D matrices were neither accepted nor properly copied with an
|
2017-03-17 17:51:52 +00:00
|
|
|
|
// incompatible stride value on the length-1 dimension--but that should be allowed (without
|
|
|
|
|
// requiring a copy!) because the stride value can be safely ignored on a size-1 dimension.
|
2022-06-29 18:37:16 +00:00
|
|
|
|
// Similarly, 0×N or N×0 matrices were not accepted--again, these should be allowed since
|
|
|
|
|
// they contain no data. This particularly affects numpy ≥ 1.23, which sets the strides to
|
|
|
|
|
// 0 if any dimension size is 0.
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("iss738_f1",
|
|
|
|
|
&adjust_matrix<const Eigen::Ref<const Eigen::MatrixXd> &>,
|
|
|
|
|
py::arg{}.noconvert());
|
|
|
|
|
m.def("iss738_f2",
|
|
|
|
|
&adjust_matrix<const Eigen::Ref<const Eigen::Matrix<double, -1, -1, Eigen::RowMajor>> &>,
|
|
|
|
|
py::arg{}.noconvert());
|
2017-03-21 00:15:20 +00:00
|
|
|
|
|
2017-09-21 22:09:39 +00:00
|
|
|
|
// test_issue1105
|
|
|
|
|
// Issue #1105: when converting from a numpy two-dimensional (Nx1) or (1xN) value into a dense
|
2021-06-22 16:11:54 +00:00
|
|
|
|
// eigen Vector or RowVector, the argument would fail to load because the numpy copy would
|
2021-07-12 20:10:28 +00:00
|
|
|
|
// fail: numpy won't broadcast a Nx1 into a 1-dimensional vector.
|
|
|
|
|
m.def("iss1105_col", [](const Eigen::VectorXd &) { return true; });
|
|
|
|
|
m.def("iss1105_row", [](const Eigen::RowVectorXd &) { return true; });
|
2017-09-21 22:09:39 +00:00
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
// test_named_arguments
|
2017-04-08 23:26:42 +00:00
|
|
|
|
// Make sure named arguments are working properly:
|
2021-07-12 20:10:28 +00:00
|
|
|
|
m.def(
|
|
|
|
|
"matrix_multiply",
|
|
|
|
|
[](const py::EigenDRef<const Eigen::MatrixXd> &A,
|
|
|
|
|
const py::EigenDRef<const Eigen::MatrixXd> &B) -> Eigen::MatrixXd {
|
2022-02-08 00:23:20 +00:00
|
|
|
|
if (A.cols() != B.rows()) {
|
2021-07-12 20:10:28 +00:00
|
|
|
|
throw std::domain_error("Nonconformable matrices!");
|
2022-02-08 00:23:20 +00:00
|
|
|
|
}
|
2021-07-12 20:10:28 +00:00
|
|
|
|
return A * B;
|
|
|
|
|
},
|
|
|
|
|
py::arg("A"),
|
|
|
|
|
py::arg("B"));
|
2017-04-08 23:26:42 +00:00
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
// test_custom_operator_new
|
2017-03-21 00:15:20 +00:00
|
|
|
|
py::class_<CustomOperatorNew>(m, "CustomOperatorNew")
|
|
|
|
|
.def(py::init<>())
|
|
|
|
|
.def_readonly("a", &CustomOperatorNew::a)
|
|
|
|
|
.def_readonly("b", &CustomOperatorNew::b);
|
2017-06-26 21:20:39 +00:00
|
|
|
|
|
|
|
|
|
// test_eigen_ref_life_support
|
|
|
|
|
// In case of a failure (the caster's temp array does not live long enough), creating
|
|
|
|
|
// a new array (np.ones(10)) increases the chances that the temp array will be garbage
|
|
|
|
|
// collected and/or that its memory will be overridden with different values.
|
2021-07-12 20:10:28 +00:00
|
|
|
|
m.def("get_elem_direct", [](const Eigen::Ref<const Eigen::VectorXd> &v) {
|
2020-10-03 17:38:03 +00:00
|
|
|
|
py::module_::import("numpy").attr("ones")(10);
|
2017-06-26 21:20:39 +00:00
|
|
|
|
return v(5);
|
|
|
|
|
});
|
|
|
|
|
m.def("get_elem_indirect", [](std::vector<Eigen::Ref<const Eigen::VectorXd>> v) {
|
2020-10-03 17:38:03 +00:00
|
|
|
|
py::module_::import("numpy").attr("ones")(10);
|
2017-06-26 21:20:39 +00:00
|
|
|
|
return v[0](5);
|
|
|
|
|
});
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
}
|