matrix.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2022 CNRS INRIA
3  */
4 
5 #include <iostream>
6 
7 #include "eigenpy/eigenpy.hpp"
8 
9 template <typename Scalar>
10 Eigen::Matrix<Scalar, Eigen::Dynamic, 1> vector1x1(const Scalar& value) {
11  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1> ReturnType;
12  return ReturnType::Constant(1, value);
13 }
14 
15 template <typename Scalar>
16 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> matrix1x1(
17  const Scalar& value) {
18  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> ReturnType;
19  return ReturnType::Constant(1, 1, value);
20 }
21 
22 template <typename Scalar>
23 void matrix1x1_input(const Eigen::Matrix<Scalar, 1, 1>& mat) {
24  std::cout << mat << std::endl;
25 }
26 
27 Eigen::VectorXd emptyVector() {
28  Eigen::VectorXd vec;
29  vec.resize(0);
30  return vec;
31 }
32 
33 Eigen::MatrixXd emptyMatrix() { return Eigen::MatrixXd(0, 0); }
34 
35 Eigen::MatrixXd naturals(int R, int C, bool verbose) {
36  Eigen::MatrixXd mat(R, C);
37  for (int r = 0; r < R; ++r)
38  for (int c = 0; c < C; ++c) mat(r, c) = r * C + c;
39 
40  if (verbose) std::cout << "EigenMat = " << mat << std::endl;
41  return mat;
42 }
43 
44 Eigen::VectorXd naturals(int R, bool verbose) {
45  Eigen::VectorXd mat(R);
46  for (int r = 0; r < R; ++r) mat[r] = r;
47 
48  if (verbose) std::cout << "EigenMat = " << mat << std::endl;
49  return mat;
50 }
51 
52 Eigen::Matrix3d naturals(bool verbose) {
53  Eigen::Matrix3d mat;
54  for (int r = 0; r < 3; ++r)
55  for (int c = 0; c < 3; ++c) mat(r, c) = r * 3 + c;
56 
57  if (verbose) std::cout << "EigenMat = " << mat << std::endl;
58  return mat;
59 }
60 
61 template <typename MatType>
62 Eigen::MatrixXd reflex(const MatType& M, bool verbose) {
63  if (verbose) std::cout << "EigenMat = " << M << std::endl;
64  return Eigen::MatrixXd(M);
65 }
66 
67 template <typename MatrixDerived>
68 MatrixDerived base(const Eigen::MatrixBase<MatrixDerived>& m) {
69  return m.derived();
70 }
71 
72 template <typename MatrixDerived>
73 MatrixDerived plain(const Eigen::PlainObjectBase<MatrixDerived>& m) {
74  return m.derived();
75 }
76 
77 template <typename Scalar>
78 Eigen::Matrix<Scalar, 6, 6> matrix6(const Scalar& value) {
79  typedef Eigen::Matrix<Scalar, 6, 6> ReturnType;
80  return ReturnType::Constant(value);
81 }
82 
83 template <typename Scalar>
84 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
85 generateRowMajorMatrix(const Eigen::DenseIndex rows,
86  const Eigen::DenseIndex cols) {
87  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
88  RowMajorMatrix;
89  RowMajorMatrix A(rows, cols);
90  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1> Vector;
91  Eigen::Map<Vector>(A.data(), A.size()) =
92  Vector::LinSpaced(A.size(), 1, static_cast<Scalar>(A.size()));
93  std::cout << "Matrix values:\n" << A << std::endl;
94  return A;
95 }
96 
97 template <typename Scalar>
98 Eigen::Matrix<Scalar, 1, Eigen::Dynamic, Eigen::RowMajor>
99 generateRowMajorVector(const Eigen::DenseIndex size) {
100  typedef Eigen::Matrix<Scalar, 1, Eigen::Dynamic, Eigen::RowMajor>
101  RowMajorVector;
102  RowMajorVector A(size);
103  A.setLinSpaced(size, 1, static_cast<Scalar>(size));
104  std::cout << "Vector values: " << A.transpose() << std::endl;
105  return A;
106 }
107 
108 template <typename Scalar>
109 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> generateColMajorMatrix(
110  const Eigen::DenseIndex rows, const Eigen::DenseIndex cols) {
111  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> ColMajorMatrix;
112  ColMajorMatrix A(rows, cols);
113  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1> Vector;
114  Eigen::Map<Vector>(A.data(), A.size()) =
115  Vector::LinSpaced(A.size(), 1, static_cast<Scalar>(A.size()));
116  std::cout << "Matrix values:\n" << A << std::endl;
117  return A;
118 }
119 
120 template <typename Scalar>
121 Eigen::Matrix<Scalar, 1, Eigen::Dynamic> generateColMajorVector(
122  const Eigen::DenseIndex size) {
123  typedef Eigen::Matrix<Scalar, 1, Eigen::Dynamic> ColMajorVector;
124  ColMajorVector A(size);
125  A.setLinSpaced(size, 1, static_cast<Scalar>(size));
126  std::cout << "Vector values: " << A.transpose() << std::endl;
127  return A;
128 }
129 
130 template <typename Matrix, typename ReturnMatrix>
131 ReturnMatrix copy(const Eigen::MatrixBase<Matrix>& mat) {
132  return mat;
133 }
134 
136  using namespace Eigen;
137  namespace bp = boost::python;
139 
140  // Square matrix
141  typedef Eigen::Matrix<double, 6, 6> Matrix6;
142  eigenpy::enableEigenPySpecific<Matrix6>();
143 
144  // Non-square matrix
145  typedef Eigen::Matrix<double, 4, 6> Matrix46;
146  eigenpy::enableEigenPySpecific<Matrix46>();
147 
148  Eigen::MatrixXd (*naturalsXX)(int, int, bool) = naturals;
149  Eigen::VectorXd (*naturalsX)(int, bool) = naturals;
150  Eigen::Matrix3d (*naturals33)(bool) = naturals;
151 
152  bp::def("vector1x1", vector1x1<double>);
153  bp::def("matrix1x1", matrix1x1<double>);
154  bp::def("matrix1x1", matrix1x1_input<double>);
155  bp::def("matrix1x1_int", matrix1x1_input<int>);
156 
157  bp::def("naturals", naturalsXX);
158  bp::def("naturalsX", naturalsX);
159  bp::def("naturals33", naturals33);
160 
161  bp::def("reflex", reflex<Eigen::MatrixXd>);
162  bp::def("reflexV", reflex<Eigen::VectorXd>);
163  bp::def("reflex33", reflex<Eigen::Matrix3d>);
164  bp::def("reflex3", reflex<Eigen::Vector3d>);
165 
166  bp::def("emptyVector", emptyVector);
167  bp::def("emptyMatrix", emptyMatrix);
168 
169  bp::def("base", base<VectorXd>);
170  bp::def("base", base<MatrixXd>);
171 
172  bp::def("plain", plain<VectorXd>);
173  bp::def("plain", plain<MatrixXd>);
174 
175  bp::def("matrix6", matrix6<double>);
176 
177  bp::def("generateRowMajorMatrix", generateRowMajorMatrix<double>);
178  bp::def("generateRowMajorVector", generateRowMajorVector<double>);
179 
180  bp::def("generateColMajorMatrix", generateColMajorMatrix<double>);
181  bp::def("generateColMajorVector", generateColMajorVector<double>);
182 
183  typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
184  RowMajorMatrixXd;
185  bp::def("asRowMajorFromColMajorMatrix",
186  copy<Eigen::MatrixXd, RowMajorMatrixXd>);
187  bp::def("asRowMajorFromColMajorVector",
188  copy<Eigen::VectorXd, Eigen::RowVectorXd>);
189  bp::def("asRowMajorFromRowMajorMatrix",
190  copy<RowMajorMatrixXd, RowMajorMatrixXd>);
191  bp::def("asRowMajorFromRowMajorVector",
192  copy<Eigen::RowVectorXd, Eigen::RowVectorXd>);
193 }
naturals
Eigen::MatrixXd naturals(int R, int C, bool verbose)
Definition: matrix.cpp:35
boost::python
Definition: alignment.hpp:49
Eigen
Definition: complex.cpp:7
vector1x1
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > vector1x1(const Scalar &value)
Definition: matrix.cpp:10
generateRowMajorVector
Eigen::Matrix< Scalar, 1, Eigen::Dynamic, Eigen::RowMajor > generateRowMajorVector(const Eigen::DenseIndex size)
Definition: matrix.cpp:99
eigenpy::enableEigenPy
void EIGENPY_DLLAPI enableEigenPy()
Definition: eigenpy.cpp:29
test_geometry.r
r
Definition: test_geometry.py:41
test_complex.rows
int rows
Definition: test_complex.py:6
test_matrix.vec
vec
Definition: test_matrix.py:180
test_geometry.R
R
Definition: test_geometry.py:83
generateColMajorVector
Eigen::Matrix< Scalar, 1, Eigen::Dynamic > generateColMajorVector(const Eigen::DenseIndex size)
Definition: matrix.cpp:121
BOOST_PYTHON_MODULE
BOOST_PYTHON_MODULE(matrix)
Definition: matrix.cpp:135
emptyMatrix
Eigen::MatrixXd emptyMatrix()
Definition: matrix.cpp:33
plain
MatrixDerived plain(const Eigen::PlainObjectBase< MatrixDerived > &m)
Definition: matrix.cpp:73
emptyVector
Eigen::VectorXd emptyVector()
Definition: matrix.cpp:27
copy
ReturnMatrix copy(const Eigen::MatrixBase< Matrix > &mat)
Definition: matrix.cpp:131
test_tensor.c
c
Definition: test_tensor.py:10
matrix1x1_input
void matrix1x1_input(const Eigen::Matrix< Scalar, 1, 1 > &mat)
Definition: matrix.cpp:23
test_eigen_ref.mat
mat
Definition: test_eigen_ref.py:140
test_matrix.value
float value
Definition: test_matrix.py:161
generateRowMajorMatrix
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > generateRowMajorMatrix(const Eigen::DenseIndex rows, const Eigen::DenseIndex cols)
Definition: matrix.cpp:85
matrix6
Eigen::Matrix< Scalar, 6, 6 > matrix6(const Scalar &value)
Definition: matrix.cpp:78
test_matrix.M
M
Definition: test_matrix.py:10
reflex
Eigen::MatrixXd reflex(const MatType &M, bool verbose)
Definition: matrix.cpp:62
eigenpy.hpp
test_complex.cols
int cols
Definition: test_complex.py:7
test_geometry.verbose
bool verbose
Definition: test_geometry.py:14
test_eigen_solver.A
A
Definition: test_eigen_solver.py:5
base
MatrixDerived base(const Eigen::MatrixBase< MatrixDerived > &m)
Definition: matrix.cpp:68
generateColMajorMatrix
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > generateColMajorMatrix(const Eigen::DenseIndex rows, const Eigen::DenseIndex cols)
Definition: matrix.cpp:109
matrix1x1
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > matrix1x1(const Scalar &value)
Definition: matrix.cpp:16


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Tue Jan 23 2024 03:15:01