matrix.cpp 2.72 KB
Newer Older
Nicolas Mansard's avatar
Nicolas Mansard committed
1
/*
2
 * Copyright 2014-2019, CNRS
3
 * Copyright 2018-2020, INRIA
Nicolas Mansard's avatar
Nicolas Mansard committed
4
5
 */

6
#include "eigenpy/eigenpy.hpp"
Nicolas Mansard's avatar
Nicolas Mansard committed
7
#include <iostream>
Nicolas Mansard's avatar
Nicolas Mansard committed
8

9
10
11
12
13
14
15
16
17
18
19
20
21
22
template<typename Scalar>
Eigen::Matrix<Scalar,Eigen::Dynamic,1> vector1x1(const Scalar & value)
{
  typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1> ReturnType;
  return ReturnType::Constant(1,value);
} 

template<typename Scalar>
Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> matrix1x1(const Scalar & value)
{
  typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> ReturnType; 
  return ReturnType::Constant(1,1,value);
}

23
24
25
26
27
28
29
30
31
32
33
34
Eigen::VectorXd emptyVector()
{
  Eigen::VectorXd vec;
  vec.resize(0);
  return vec;
}

Eigen::MatrixXd emptyMatrix()
{
  return Eigen::MatrixXd(0,0);
}

Nicolas Mansard's avatar
Nicolas Mansard committed
35
Eigen::MatrixXd naturals(int R,int C,bool verbose)
Nicolas Mansard's avatar
Nicolas Mansard committed
36
{
Nicolas Mansard's avatar
Nicolas Mansard committed
37
38
39
40
41
42
43
  Eigen::MatrixXd mat(R,C);
  for(int r=0;r<R;++r)
    for(int c=0;c<C;++c)
      mat(r,c) = r*C+c;

  if(verbose)
    std::cout << "EigenMat = " << mat << std::endl;
Nicolas Mansard's avatar
Nicolas Mansard committed
44
45
  return mat;
}
Nicolas Mansard's avatar
Nicolas Mansard committed
46
47

Eigen::VectorXd naturals(int R,bool verbose)
Nicolas Mansard's avatar
Nicolas Mansard committed
48
{
Nicolas Mansard's avatar
Nicolas Mansard committed
49
50
51
52
53
54
  Eigen::VectorXd mat(R);
  for(int r=0;r<R;++r)
    mat[r] = r;

  if(verbose)
    std::cout << "EigenMat = " << mat << std::endl;
Nicolas Mansard's avatar
Nicolas Mansard committed
55
56
57
  return mat;
}

Nicolas Mansard's avatar
Nicolas Mansard committed
58
Eigen::Matrix3d naturals(bool verbose)
Nicolas Mansard's avatar
Nicolas Mansard committed
59
{
Nicolas Mansard's avatar
Nicolas Mansard committed
60
61
62
63
64
65
66
67
  Eigen::Matrix3d mat;
  for(int r=0;r<3;++r)
    for(int c=0;c<3;++c)
      mat(r,c) = r*3+c;

  if(verbose)
    std::cout << "EigenMat = " << mat << std::endl;
  return mat;
Nicolas Mansard's avatar
Nicolas Mansard committed
68
}
Nicolas Mansard's avatar
Nicolas Mansard committed
69
70

template<typename MatType>
71
Eigen::MatrixXd reflex(const MatType & M, bool verbose)
Nicolas Mansard's avatar
Nicolas Mansard committed
72
{
Nicolas Mansard's avatar
Nicolas Mansard committed
73
74
75
  if(verbose)
    std::cout << "EigenMat = " << M << std::endl;
  return Eigen::MatrixXd(M);
Nicolas Mansard's avatar
Nicolas Mansard committed
76
77
}

78
79
80
81
82
83
template<typename MatrixDerived>
MatrixDerived base(const Eigen::MatrixBase<MatrixDerived> & m)
{
  return m.derived();
}

84
85
86
87
88
89
90
template<typename Scalar>
Eigen::Matrix<Scalar,6,6> matrix6(const Scalar & value)
{
  typedef Eigen::Matrix<Scalar,6,6> ReturnType;
  return ReturnType::Constant(value);
}

Nicolas Mansard's avatar
Nicolas Mansard committed
91
BOOST_PYTHON_MODULE(matrix)
Nicolas Mansard's avatar
Nicolas Mansard committed
92
{
93
  using namespace Eigen;
Nicolas Mansard's avatar
Nicolas Mansard committed
94
95
96
  namespace bp = boost::python;
  eigenpy::enableEigenPy();

97
98
99
  typedef Eigen::Matrix<double,6,6> Matrix6;
  eigenpy::enableEigenPySpecific<Matrix6>();

Nicolas Mansard's avatar
Nicolas Mansard committed
100
101
102
103
  Eigen::MatrixXd (*naturalsXX)(int,int,bool) = naturals;
  Eigen::VectorXd (*naturalsX)(int,bool) = naturals;
  Eigen::Matrix3d (*naturals33)(bool) = naturals;

104
105
106
  bp::def("vector1x1", vector1x1<double>);
  bp::def("matrix1x1", matrix1x1<double>);

Nicolas Mansard's avatar
Nicolas Mansard committed
107
108
109
110
111
112
113
  bp::def("naturals", naturalsXX);
  bp::def("naturalsX", naturalsX);
  bp::def("naturals33", naturals33);

  bp::def("reflex", reflex<Eigen::MatrixXd>);
  bp::def("reflexV", reflex<Eigen::VectorXd>);
  bp::def("reflex33", reflex<Eigen::Matrix3d>);
114
  bp::def("reflex3", reflex<Eigen::Vector3d>);
115
116
117

  bp::def("emptyVector", emptyVector);
  bp::def("emptyMatrix", emptyMatrix);
118
119
120
  
  bp::def("base", base<VectorXd>);
  bp::def("base", base<MatrixXd>);
121
122

  bp::def("matrix6", matrix6<double>);
Nicolas Mansard's avatar
Nicolas Mansard committed
123
}