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 
135 template <typename Matrix>
136 Matrix copy_same(const Eigen::MatrixBase<Matrix>& mat) {
137  return mat;
138 }
139 
141  using namespace Eigen;
142  namespace bp = boost::python;
144 
145  // Square matrix
146  typedef Eigen::Matrix<double, 6, 6> Matrix6;
147  eigenpy::enableEigenPySpecific<Matrix6>();
148 
149  // Non-square matrix
150  typedef Eigen::Matrix<double, 4, 6> Matrix46;
151  eigenpy::enableEigenPySpecific<Matrix46>();
152 
153  Eigen::MatrixXd (*naturalsXX)(int, int, bool) = naturals;
154  Eigen::VectorXd (*naturalsX)(int, bool) = naturals;
155  Eigen::Matrix3d (*naturals33)(bool) = naturals;
156 
157  bp::def("vector1x1", vector1x1<double>);
158  bp::def("matrix1x1", matrix1x1<double>);
159  bp::def("matrix1x1", matrix1x1_input<double>);
160  bp::def("matrix1x1_int", matrix1x1_input<int>);
161 
162  bp::def("naturals", naturalsXX);
163  bp::def("naturalsX", naturalsX);
164  bp::def("naturals33", naturals33);
165 
166  bp::def("reflex", reflex<Eigen::MatrixXd>);
167  bp::def("reflexV", reflex<Eigen::VectorXd>);
168  bp::def("reflex33", reflex<Eigen::Matrix3d>);
169  bp::def("reflex3", reflex<Eigen::Vector3d>);
170 
171  bp::def("emptyVector", emptyVector);
172  bp::def("emptyMatrix", emptyMatrix);
173 
174  bp::def("base", base<VectorXd>);
175  bp::def("base", base<MatrixXd>);
176 
177  bp::def("plain", plain<VectorXd>);
178  bp::def("plain", plain<MatrixXd>);
179 
180  bp::def("matrix6", matrix6<double>);
181 
182  bp::def("generateRowMajorMatrix", generateRowMajorMatrix<double>);
183  bp::def("generateRowMajorVector", generateRowMajorVector<double>);
184 
185  bp::def("generateColMajorMatrix", generateColMajorMatrix<double>);
186  bp::def("generateColMajorVector", generateColMajorVector<double>);
187 
188  typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
189  RowMajorMatrixXd;
190  bp::def("asRowMajorFromColMajorMatrix",
191  copy<Eigen::MatrixXd, RowMajorMatrixXd>);
192  bp::def("asRowMajorFromColMajorVector",
193  copy<Eigen::VectorXd, Eigen::RowVectorXd>);
194  bp::def("asRowMajorFromRowMajorMatrix",
195  copy<RowMajorMatrixXd, RowMajorMatrixXd>);
196  bp::def("asRowMajorFromRowMajorVector",
197  copy<Eigen::RowVectorXd, Eigen::RowVectorXd>);
198 
199  bp::def("copyBoolToBool", copy_same<Eigen::Matrix<bool, -1, -1> >);
200 
201  bp::def("copyInt8ToInt8", copy_same<Eigen::Matrix<int8_t, -1, -1> >);
202  bp::def("copyCharToChar", copy_same<Eigen::Matrix<char, -1, -1> >);
203  bp::def("copyUCharToUChar", copy_same<Eigen::Matrix<unsigned char, -1, -1> >);
204 
205  bp::def("copyInt16ToInt16", copy_same<Eigen::Matrix<int16_t, -1, -1> >);
206  bp::def("copyUInt16ToUInt16", copy_same<Eigen::Matrix<uint16_t, -1, -1> >);
207 
208  bp::def("copyInt32ToInt32", copy_same<Eigen::Matrix<int32_t, -1, -1> >);
209  bp::def("copyUInt32ToUInt32", copy_same<Eigen::Matrix<uint32_t, -1, -1> >);
210 
211  bp::def("copyInt64ToInt64", copy_same<Eigen::Matrix<int64_t, -1, -1> >);
212  bp::def("copyUInt64ToUInt64", copy_same<Eigen::Matrix<uint64_t, -1, -1> >);
213 
214  bp::def("copyLongToLong", copy_same<Eigen::Matrix<long, -1, -1> >);
215  bp::def("copyULongToULong", copy_same<Eigen::Matrix<unsigned long, -1, -1> >);
216 
217  bp::def("copyLongLongToLongLong",
218  copy_same<Eigen::Matrix<long long, -1, -1> >);
219  bp::def("copyULongLongToULongLong",
220  copy_same<Eigen::Matrix<unsigned long long, -1, -1> >);
221 
222  bp::def("copyFloatToFloat", copy_same<Eigen::Matrix<float, -1, -1> >);
223  bp::def("copyDoubleToDouble", copy_same<Eigen::Matrix<double, -1, -1> >);
224  bp::def("copyLongDoubleToLongDouble",
225  copy_same<Eigen::Matrix<long double, -1, -1> >);
226 
227  bp::def("copyCFloatToCFloat",
228  copy_same<Eigen::Matrix<std::complex<float>, -1, -1> >);
229  bp::def("copyCDoubleToCDouble",
230  copy_same<Eigen::Matrix<std::complex<double>, -1, -1> >);
231  bp::def("copyCLongDoubleToCLongDouble",
232  copy_same<Eigen::Matrix<std::complex<long double>, -1, -1> >);
233 }
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:42
test_geometry.r
r
Definition: test_geometry.py:39
test_complex.rows
int rows
Definition: test_complex.py:4
test_matrix.vec
vec
Definition: test_matrix.py:180
test_geometry.R
R
Definition: test_geometry.py:81
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:140
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:8
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:137
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
copy_same
Matrix copy_same(const Eigen::MatrixBase< Matrix > &mat)
Definition: matrix.cpp:136
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_CholmodSimplicialLDLT.A
A
Definition: test_CholmodSimplicialLDLT.py:8
test_sparse_matrix.m
m
Definition: test_sparse_matrix.py:5
test_complex.cols
int cols
Definition: test_complex.py:5
test_geometry.verbose
bool verbose
Definition: test_geometry.py:12
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 Fri Apr 26 2024 02:17:35