-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy patheigen_bp_test_mod.cc
More file actions
177 lines (157 loc) · 7.08 KB
/
Copy patheigen_bp_test_mod.cc
File metadata and controls
177 lines (157 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// -*- c++ -*-
/*
* Copyright (c) 2010-2012, Jim Bosch
* All rights reserved.
*
* ndarray is distributed under a simple BSD-like license;
* see the LICENSE file that should be present in the root
* of the source distribution, or alternately available at:
* https://github.com/ndarray/ndarray
*/
#include "ndarray/eigen/bp/auto.h"
namespace bp = boost::python;
namespace bn = boost::numpy;
template <typename M>
bool acceptMatrix(M m) {
return (m(0,0) == 1) && (m(0,1) == 2) && (m(0,2) == 3)
&& (m(1,0) == 4) && (m(1,1) == 5) && (m(1,2) == 6);
}
template <typename M>
bool acceptVector(M m) {
return (m[0] == 1) && (m[1] == 2) && (m[2] == 3) && (m[3] == 4);
}
template <typename M>
void fillMatrix(M & m) {
m(0,0) = 1;
m(0,1) = 2;
m(0,2) = 3;
m(1,0) = 4;
m(1,1) = 5;
m(1,2) = 6;
}
template <typename M>
M returnEigenView() {
static typename boost::remove_const<typename boost::remove_reference<M>::type>::type m(
ndarray::allocate(2,3)
);
fillMatrix(m);
return m;
}
template <typename M>
M returnMatrix() {
static typename boost::remove_const<typename boost::remove_reference<M>::type>::type m(2,3);
fillMatrix(m);
return m;
}
template <typename M>
void fillVector(M & m) {
m[0] = 1;
m[1] = 2;
m[2] = 3;
m[3] = 4;
}
template <typename M>
M returnVector() {
static typename boost::remove_const<typename boost::remove_reference<M>::type>::type m(4);
fillVector(m);
return m;
}
template <typename M>
bp::object returnObject() {
static typename boost::remove_const<typename boost::remove_reference<M>::type>::type m(2,3);
fillMatrix(m);
bp::object o(m);
return o;
}
template <typename M>
class MatrixOwner {
M _matrix;
public:
MatrixOwner() : _matrix(2,3) { fillMatrix(_matrix); }
M const & getMatrix_cref() const { return _matrix; }
M & getMatrix_ref() { return _matrix; }
bool compareData(bn::matrix const & mp) const {
return _matrix.data() == reinterpret_cast<double const*>(mp.get_data());
}
EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
static void declare(char const * name) {
bp::class_< MatrixOwner >(name)
.def("getMatrix_cref", &MatrixOwner::getMatrix_cref,
ndarray::ReturnInternal<>())
.def("getMatrix_ref", &MatrixOwner::getMatrix_ref,
ndarray::ReturnInternal<>())
.def("compareData", &MatrixOwner::compareData)
;
}
};
static const int X = Eigen::Dynamic;
template <typename T, typename XprKind, int Rows, int Cols>
void wrapEigenView(std::string const & name) {
bp::def(("accept" + name + "_2p2").c_str(),
acceptMatrix<ndarray::EigenView<T,2,2,XprKind,Rows,Cols> const &>);
bp::def(("accept" + name + "_2p1").c_str(),
acceptMatrix<ndarray::EigenView<T,2,1,XprKind,Rows,Cols> const &>);
bp::def(("accept" + name + "_2p0").c_str(),
acceptMatrix<ndarray::EigenView<T,2,0,XprKind,Rows,Cols> const &>);
bp::def(("accept" + name + "_2m1").c_str(),
acceptMatrix<ndarray::EigenView<T,2,-1,XprKind,Rows,Cols> const &>);
bp::def(("accept" + name + "_2m2").c_str(),
acceptMatrix<ndarray::EigenView<T,2,-2,XprKind,Rows,Cols> const &>);
bp::def(("return" + name + "_2p2").c_str(),
returnEigenView<ndarray::EigenView<T,2,2,XprKind,Rows,Cols> >);
bp::def(("return" + name + "_2p1").c_str(),
returnEigenView<ndarray::EigenView<T,2,1,XprKind,Rows,Cols> >);
bp::def(("return" + name + "_2p0").c_str(),
returnEigenView<ndarray::EigenView<T,2,0,XprKind,Rows,Cols> >);
bp::def(("return" + name + "_2m1").c_str(),
returnEigenView<ndarray::EigenView<T,2,-1,XprKind,Rows,Cols> >);
bp::def(("return" + name + "_2m2").c_str(),
returnEigenView<ndarray::EigenView<T,2,-2,XprKind,Rows,Cols> >);
}
BOOST_PYTHON_MODULE(eigen_bp_test_mod) {
bn::initialize();
wrapEigenView<double,Eigen::MatrixXpr,2,3>("EigenView_M23d");
wrapEigenView<double,Eigen::MatrixXpr,X,3>("EigenView_MX3d");
wrapEigenView<double,Eigen::MatrixXpr,2,X>("EigenView_M2Xd");
wrapEigenView<double,Eigen::MatrixXpr,X,X>("EigenView_MXXd");
wrapEigenView<int,Eigen::MatrixXpr,2,3>("EigenView_M23i");
wrapEigenView<int,Eigen::MatrixXpr,X,3>("EigenView_MX3i");
wrapEigenView<int,Eigen::MatrixXpr,2,X>("EigenView_M2Xi");
wrapEigenView<int,Eigen::MatrixXpr,X,X>("EigenView_MXXi");
wrapEigenView<double,Eigen::ArrayXpr,2,3>("EigenView_A23d");
wrapEigenView<double,Eigen::ArrayXpr,X,3>("EigenView_AX3d");
wrapEigenView<double,Eigen::ArrayXpr,2,X>("EigenView_A2Xd");
wrapEigenView<double,Eigen::ArrayXpr,X,X>("EigenView_AXXd");
wrapEigenView<int,Eigen::ArrayXpr,2,3>("EigenView_A23i");
wrapEigenView<int,Eigen::ArrayXpr,X,3>("EigenView_AX3i");
wrapEigenView<int,Eigen::ArrayXpr,2,X>("EigenView_A2Xi");
wrapEigenView<int,Eigen::ArrayXpr,X,X>("EigenView_AXXi");
bp::def("acceptMatrix_23d_cref", acceptMatrix< Eigen::Matrix<double,2,3> const & >);
bp::def("acceptMatrix_X3d_cref", acceptMatrix< Eigen::Matrix<double,X,3> const & >);
bp::def("acceptMatrix_2Xd_cref", acceptMatrix< Eigen::Matrix<double,2,X> const & >);
bp::def("acceptMatrix_XXd_cref", acceptMatrix< Eigen::Matrix<double,X,X> const & >);
bp::def("acceptVector_41d_cref", acceptVector< Eigen::Matrix<double,4,1> const & >);
bp::def("acceptVector_X1d_cref", acceptVector< Eigen::Matrix<double,X,1> const & >);
bp::def("acceptVector_14d_cref", acceptVector< Eigen::Matrix<double,1,4> const & >);
bp::def("acceptVector_1Xd_cref", acceptVector< Eigen::Matrix<double,1,X> const & >);
bp::def("returnVector_41d", returnVector< Eigen::Matrix<double,4,1> >);
bp::def("returnVector_14d", returnVector< Eigen::Matrix<double,1,4> >);
bp::def("returnVector_X1d", returnVector< Eigen::Matrix<double,X,1> >);
bp::def("returnVector_1Xd", returnVector< Eigen::Matrix<double,1,X> >);
bp::def("returnMatrix_23d", returnMatrix< Eigen::Matrix<double,2,3> >);
bp::def("returnMatrix_X3d", returnMatrix< Eigen::Matrix<double,X,3> >);
bp::def("returnMatrix_2Xd", returnMatrix< Eigen::Matrix<double,2,X> >);
bp::def("returnMatrix_XXd", returnMatrix< Eigen::Matrix<double,X,X> >);
bp::def("returnMatrix_23d_c", returnMatrix< Eigen::Matrix<double,2,3> const>);
bp::def("returnMatrix_X3d_c", returnMatrix< Eigen::Matrix<double,X,3> const>);
bp::def("returnMatrix_2Xd_c", returnMatrix< Eigen::Matrix<double,2,X> const>);
bp::def("returnMatrix_XXd_c", returnMatrix< Eigen::Matrix<double,X,X> const>);
bp::def("returnObject_23d", returnObject< Eigen::Matrix<double,2,3> >);
bp::def("returnObject_X3d", returnObject< Eigen::Matrix<double,X,3> >);
bp::def("returnObject_2Xd", returnObject< Eigen::Matrix<double,2,X> >);
bp::def("returnObject_XXd", returnObject< Eigen::Matrix<double,X,X> >);
MatrixOwner< Eigen::Matrix<double,2,3> >::declare("MatrixOwner_23d");
MatrixOwner< Eigen::Matrix<double,X,3> >::declare("MatrixOwner_X3d");
MatrixOwner< Eigen::Matrix<double,2,X> >::declare("MatrixOwner_2Xd");
MatrixOwner< Eigen::Matrix<double,X,X> >::declare("MatrixOwner_XXd");
}