-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathbp_test_mod.cc
More file actions
190 lines (177 loc) · 9.04 KB
/
Copy pathbp_test_mod.cc
File metadata and controls
190 lines (177 loc) · 9.04 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
178
179
180
181
182
183
184
185
186
187
188
189
190
// -*- 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/bp/auto.h"
#include "boost/random/mersenne_twister.hpp"
#include "boost/random/uniform_int.hpp"
static boost::mt19937 engine;
static boost::uniform_int<> random_int(2, 5);
template <typename T, int N, int C>
ndarray::Array<T,N,C> makeArray(ndarray::Vector<ndarray::Size,N> const & shape) {
ndarray::Array<typename boost::remove_const<T>::type,N,N> a = ndarray::allocate(shape);
ndarray::Array<typename boost::remove_const<T>::type,1,1> flat = ndarray::flatten<1>(a);
for (int n=0; n < flat.template getSize<0>(); ++n) {
flat[n] = n;
}
return a;
}
template <int N>
ndarray::Vector<ndarray::Size,N> makeShape() {
ndarray::Vector<ndarray::Size,N> shape;
for (int n=0; n<N; ++n) {
shape[n] = random_int(engine);
}
return shape;
}
template <typename T, int N, int C>
ndarray::Array<T,N,C> returnArray() {
ndarray::Array<typename boost::remove_const<T>::type,N,N> a = ndarray::allocate(makeShape<N>());
ndarray::Array<typename boost::remove_const<T>::type,1,1> flat = ndarray::flatten<1>(a);
for (int n=0; n < flat.template getSize<0>(); ++n) {
flat[n] = n;
}
return a;
}
template <typename T, int N, int C>
bool acceptArray(ndarray::Array<T,N,C> const & p) {
ndarray::Array<typename boost::remove_const<T>::type,N,N> a = ndarray::allocate(p.getShape());
a.deep() = p;
ndarray::Array<typename boost::remove_const<T>::type,1,1> flat = ndarray::flatten<1>(a);
for (int n=0; n < flat.template getSize<0>(); ++n) {
if (flat[n] != n) return false;
}
return true;
}
template <typename T, int N, int C>
bool acceptArrayVal(ndarray::Array<T,N,C> p) {
ndarray::Array<typename boost::remove_const<T>::type,N,N> a = ndarray::allocate(p.getShape());
a.deep() = p;
ndarray::Array<typename boost::remove_const<T>::type,1,1> flat = ndarray::flatten<1>(a);
for (int n=0; n < flat.template getSize<0>(); ++n) {
if (flat[n] != n) return false;
}
return true;
}
template <typename T, int N, int C>
bool extractArray(boost::python::object const & obj) {
ndarray::Array<T,N,C> p = boost::python::extract< ndarray::Array<T,N,C> >(obj);
ndarray::Array<typename boost::remove_const<T>::type,N,N> a = ndarray::allocate(p.getShape());
a.deep() = p;
ndarray::Array<typename boost::remove_const<T>::type,1,1> flat = ndarray::flatten<1>(a);
for (int n=0; n < flat.template getSize<0>(); ++n) {
if (flat[n] != n) return false;
}
return true;
}
template <typename T, int N>
ndarray::Vector<T,N> returnVector() {
ndarray::Vector<T,N> r;
for (int n=0; n<N; ++n) {
r[n] = n;
}
return r;
}
template <typename T, int N>
bool acceptVector(ndarray::Vector<T,N> const & p) {
for (int n=0; n<N; ++n) {
if (p[n] != T(n)) return false;
}
return true;
}
template <typename T, int N>
bool extractVector(boost::python::object const & obj) {
ndarray::Vector<T,N> p = boost::python::extract< ndarray::Vector<T,N> >(obj);
for (int n=0; n<N; ++n) {
if (p[n] != T(n)) return false;
}
return true;
}
BOOST_PYTHON_MODULE(bp_test_mod) {
boost::numpy::initialize();
boost::python::def("makeArray_d33", makeArray<double,3,3>);
boost::python::def("returnArray_d11", returnArray<double,1,1>);
boost::python::def("returnArray_d10", returnArray<double,1,0>);
boost::python::def("returnArray_d22", returnArray<double,2,2>);
boost::python::def("returnArray_d21", returnArray<double,2,1>);
boost::python::def("returnArray_d20", returnArray<double,2,0>);
boost::python::def("returnArray_d33", returnArray<double,3,3>);
boost::python::def("returnArray_d32", returnArray<double,3,2>);
boost::python::def("returnArray_d31", returnArray<double,3,1>);
boost::python::def("returnArray_d30", returnArray<double,3,0>);
boost::python::def("returnArray_dc11", returnArray<double const,1,1>);
boost::python::def("returnArray_dc10", returnArray<double const,1,0>);
boost::python::def("returnArray_dc22", returnArray<double const,2,2>);
boost::python::def("returnArray_dc21", returnArray<double const,2,1>);
boost::python::def("returnArray_dc20", returnArray<double const,2,0>);
boost::python::def("returnArray_dc33", returnArray<double const,3,3>);
boost::python::def("returnArray_dc32", returnArray<double const,3,2>);
boost::python::def("returnArray_dc31", returnArray<double const,3,1>);
boost::python::def("returnArray_dc30", returnArray<double const,3,0>);
boost::python::def("acceptArray_d11", acceptArray<double,1,1>);
boost::python::def("acceptArray_d10", acceptArray<double,1,0>);
boost::python::def("acceptArray_d22", acceptArray<double,2,2>);
boost::python::def("acceptArray_d21", acceptArray<double,2,1>);
boost::python::def("acceptArray_d20", acceptArray<double,2,0>);
boost::python::def("acceptArray_d33", acceptArray<double,3,3>);
boost::python::def("acceptArray_d32", acceptArray<double,3,2>);
boost::python::def("acceptArray_d31", acceptArray<double,3,1>);
boost::python::def("acceptArray_d30", acceptArray<double,3,0>);
boost::python::def("acceptArray_dc11", acceptArray<double const,1,1>);
boost::python::def("acceptArray_dc10", acceptArray<double const,1,0>);
boost::python::def("acceptArray_dc22", acceptArray<double const,2,2>);
boost::python::def("acceptArray_dc21", acceptArray<double const,2,1>);
boost::python::def("acceptArray_dc20", acceptArray<double const,2,0>);
boost::python::def("acceptArray_dc33", acceptArray<double const,3,3>);
boost::python::def("acceptArray_dc32", acceptArray<double const,3,2>);
boost::python::def("acceptArray_dc31", acceptArray<double const,3,1>);
boost::python::def("acceptArray_dc30", acceptArray<double const,3,0>);
boost::python::def("acceptArrayVal_d11", acceptArrayVal<double,1,1>);
boost::python::def("acceptArrayVal_d10", acceptArrayVal<double,1,0>);
boost::python::def("acceptArrayVal_d22", acceptArrayVal<double,2,2>);
boost::python::def("acceptArrayVal_d21", acceptArrayVal<double,2,1>);
boost::python::def("acceptArrayVal_d20", acceptArrayVal<double,2,0>);
boost::python::def("acceptArrayVal_d33", acceptArrayVal<double,3,3>);
boost::python::def("acceptArrayVal_d32", acceptArrayVal<double,3,2>);
boost::python::def("acceptArrayVal_d31", acceptArrayVal<double,3,1>);
boost::python::def("acceptArrayVal_d30", acceptArrayVal<double,3,0>);
boost::python::def("acceptArrayVal_dc11", acceptArrayVal<double const,1,1>);
boost::python::def("acceptArrayVal_dc10", acceptArrayVal<double const,1,0>);
boost::python::def("acceptArrayVal_dc22", acceptArrayVal<double const,2,2>);
boost::python::def("acceptArrayVal_dc21", acceptArrayVal<double const,2,1>);
boost::python::def("acceptArrayVal_dc20", acceptArrayVal<double const,2,0>);
boost::python::def("acceptArrayVal_dc33", acceptArrayVal<double const,3,3>);
boost::python::def("acceptArrayVal_dc32", acceptArrayVal<double const,3,2>);
boost::python::def("acceptArrayVal_dc31", acceptArrayVal<double const,3,1>);
boost::python::def("acceptArrayVal_dc30", acceptArrayVal<double const,3,0>);
boost::python::def("extractArray_d11", extractArray<double,1,1>);
boost::python::def("extractArray_d10", extractArray<double,1,0>);
boost::python::def("extractArray_d22", extractArray<double,2,2>);
boost::python::def("extractArray_d21", extractArray<double,2,1>);
boost::python::def("extractArray_d20", extractArray<double,2,0>);
boost::python::def("extractArray_d33", extractArray<double,3,3>);
boost::python::def("extractArray_d32", extractArray<double,3,2>);
boost::python::def("extractArray_d31", extractArray<double,3,1>);
boost::python::def("extractArray_d30", extractArray<double,3,0>);
boost::python::def("extractArray_dc11", extractArray<double const,1,1>);
boost::python::def("extractArray_dc10", extractArray<double const,1,0>);
boost::python::def("extractArray_dc22", extractArray<double const,2,2>);
boost::python::def("extractArray_dc21", extractArray<double const,2,1>);
boost::python::def("extractArray_dc20", extractArray<double const,2,0>);
boost::python::def("extractArray_dc33", extractArray<double const,3,3>);
boost::python::def("extractArray_dc32", extractArray<double const,3,2>);
boost::python::def("extractArray_dc31", extractArray<double const,3,1>);
boost::python::def("extractArray_dc30", extractArray<double const,3,0>);
boost::python::def("returnVector_d3", returnVector<double,3>);
boost::python::def("returnVector_d2", returnVector<double,2>);
boost::python::def("acceptVector_d3", acceptVector<double,3>);
boost::python::def("acceptVector_d2", acceptVector<double,2>);
boost::python::def("extractVector_d3", extractVector<double,3>);
boost::python::def("extractVector_d2", extractVector<double,2>);
}