-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtsg_python.cpp
More file actions
293 lines (257 loc) · 9.55 KB
/
Copy pathtsg_python.cpp
File metadata and controls
293 lines (257 loc) · 9.55 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//
// Copyright (c) 2014 Ronaldo Carpio
//
/*
This file is part of py_tsg.
py_tsg is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <TasmanianSparseGrids/TasmanianSparseGrid.hpp>
#include <Python.h>
#include <pyublas/numpy.hpp>
#include <tuple>
#include <string>
#include <vector>
using namespace TasGrid;
namespace bpl = boost::python;
using std::vector;
#define bpl_assert(exp, s) \
if (!(exp)) { \
PyErr_SetString(PyExc_ValueError, (s)); \
boost::python::throw_error_already_set(); \
}
typedef pyublas::numpy_vector<double> dPyArr;
typedef pyublas::numpy_vector<int> iPyArr;
typedef vector<double> dVec;
typedef vector<int> iVec;
typedef vector<dPyArr> dPyArrVector;
// convert any type with begin() and end() -> a Python list
template <class IterableT>
struct forward_iterable_to_list {
static PyObject* convert(IterableT const &cont) {
bpl::list result;
for (auto iter=cont.begin(); iter != cont.end(); iter++) {
result.append(*iter);
}
return boost::python::incref(result.ptr());
}
};
dPyArr double_carray_to_dPyArr(int n, const double* p) {
dPyArr result(n);
std::copy(p, p+n, result.begin());
return result;
}
class TSG_Wrap : public TasmanianSparseGrid {
public:
void make_global_grid(int dimensions, int outputs, int depth, TypeDepth type, TypeOneDRule oned, iPyArr const &a_weights, double alpha, double beta) {
double alpha_beta[2] = {alpha, beta};
iVec a_weights2;
const int* a_weights3 = NULL;
if (a_weights.size() > 0) {
bpl_assert(a_weights.size() == dimensions, "anisotropic weights must match dimensions");
a_weights2.resize(dimensions);
std::copy(a_weights.begin(), a_weights.end(), a_weights2.begin());
a_weights3 = &a_weights2[0];
}
this->makeGlobalGrid(dimensions, outputs, depth, type, oned, a_weights3, alpha_beta);
}
void make_full_tensor_grid(int dimensions, int outputs, iPyArr order, TypeOneDRule oned, double alpha, double beta) {
bpl_assert(dimensions == order.size(), "dimensions should equal len(order)");
double alpha_beta[2] = {alpha, beta};
vector<int> order2(order.begin(), order.end());
this->makeFullTensorGrid(dimensions, outputs, &order2[0], oned, alpha_beta);
}
void recycle_global_grid(int depth, TypeDepth type) {
this->recycleGlobalGrid(depth, type);
}
void recycle_full_tensor_grid(iPyArr order) {
vector<int> order2(order.begin(), order.end());
this->recycleFullTensorGrid(&order2[0]);
}
/*
std::string write_string() const {
std::ostringstream result;
this->write(result);
return result;
}
bool read_string(std::string const &s) {
std::istringstream source(s);
return this->read(source);
}
*/
void write_file(std::string const &filename) const {
this->write(filename.c_str());
}
bool read_file(std::string const &filename) {
return this->read(filename.c_str());
}
void set_transform_AB(dPyArr const &a_array, dPyArr const &b_array) {
int dims = this->getNumDimensions();
bpl_assert(dims == a_array.size() && dims == b_array.size(), "dimensions must match array length");
dVec a(a_array.begin(), a_array.end());
dVec b(b_array.begin(), b_array.end());
this->setTransformAB(&a[0], &b[0]);
}
vector<dPyArr> get_transform_AB() const {
vector<dPyArr> result(2);
int dims = this->getNumDimensions();
double *a=NULL, *b=NULL;
this->getTransformAB(a, b);
result[0] = double_carray_to_dPyArr(dims, a);
result[1] = double_carray_to_dPyArr(dims, b);
delete a;
delete b;
return result;
}
dPyArr get_points() const {
int dims = this->getNumDimensions();
int n_points = this->getNumPoints();
int array_dims[] = {n_points, dims};
double* output = NULL;
this->getPoints(output);
dPyArr result = double_carray_to_dPyArr(dims*n_points, output);
delete output;
result.reshape(2, array_dims);
return result;
}
dPyArr get_weights() const {
int n_points = this->getNumPoints();
double* output = NULL;
this->getWeights(output);
dPyArr result = double_carray_to_dPyArr(n_points, output);
delete output;
return result;
}
dPyArr get_interpolant_weights(dPyArr const &x) const {
int n_points = this->getNumPoints();
vector<double> x2(x.begin(), x.end());
double* output = NULL;
this->getInterpolantWeights(&x2[0], output);
dPyArr result = double_carray_to_dPyArr(n_points, output);
delete output;
return result;
}
dPyArr get_needed_points() const {
int dims = this->getNumDimensions();
int n_points = this->getNumNeededPoints();
int array_dims[] = {n_points, dims};
double* output = NULL;
this->getNeededPoints(output);
dPyArr result;
if (output != NULL) {
result = double_carray_to_dPyArr(dims*n_points, output);
delete output;
result.reshape(2, array_dims);
}
return result;
}
void load_needed_points(dPyArr const &vals) {
int outputs = this->getNumOutputs();
int n_points = this->getNumNeededPoints();
bpl_assert(vals.size() == outputs*n_points, "vals has wrong size");
vector<double> vals2(vals.begin(), vals.end());
this->loadNeededPoints(&vals2[0]);
}
dPyArr evaluate_wrap(dPyArr const &x) const {
vector<double> x2(x.begin(), x.end());
dPyArr result(this->getNumOutputs());
this->evaluate(&x2[0], &result[0]);
return result;
}
dPyArr integrate_wrap() const {
dPyArr result(this->getNumOutputs());
this->integrate(&result[0]);
return result;
}
};
BOOST_PYTHON_MODULE(_py_tsg)
{
bpl::to_python_converter<dPyArrVector, forward_iterable_to_list<dPyArrVector>>();
// functions that don't need a wrapper
// getVersion()
// getLicense()
// makeLocalPolynomialGrid
// makeWaveletGrid
// recycleLocalPolynomialGrid
// recycleWaveletGrid
// clearTransformAB
// getNumDimensions
// getNumOutputs
// getOneDRule
// getOneDRuleDescription
// getNumPoints
// getNumNeededPoints
// printStats
// setRefinement
bpl::class_<TSG_Wrap, boost::noncopyable>("TSG", bpl::init<>())
.def("get_version", &TSG_Wrap::getVersion)
.def("get_license", &TSG_Wrap::getLicense)
.def("make_global_grid", &TSG_Wrap::make_global_grid)
.def("make_local_polynomial_grid", &TSG_Wrap::makeLocalPolynomialGrid)
.def("make_wavelet_grid", &TSG_Wrap::makeWaveletGrid)
.def("make_full_tensor_grid", &TSG_Wrap::make_full_tensor_grid)
.def("recycle_global_grid", &TSG_Wrap::recycle_global_grid)
.def("recycle_local_polynomial_grid", &TSG_Wrap::recycleLocalPolynomialGrid)
.def("recycle_wavelet_grid", &TSG_Wrap::recycleWaveletGrid)
.def("recycle_full_tensor_grid", &TSG_Wrap::recycle_full_tensor_grid)
//.def("write_string", &TSG_Wrap::write_string)
//.def("read_string", &TSG_Wrap::read_string)
.def("write_file", &TSG_Wrap::write_file)
.def("read_file", &TSG_Wrap::read_file)
.def("set_transform_AB", &TSG_Wrap::set_transform_AB)
.def("clear_transform_AB", &TSG_Wrap::clearTransformAB)
.def("get_transform_AB", &TSG_Wrap::get_transform_AB)
.def("get_num_dimensions", &TSG_Wrap::getNumDimensions)
.def("get_num_outputs", &TSG_Wrap::getNumOutputs)
.def("get_oned_outputs", &TSG_Wrap::getOneDRule)
.def("get_oned_rule_description", &TSG_Wrap::getOneDRuleDescription)
.def("get_num_points", &TSG_Wrap::getNumPoints)
.def("get_points", &TSG_Wrap::get_points)
.def("get_weights", &TSG_Wrap::get_weights)
.def("get_interpolant_weights", &TSG_Wrap::get_interpolant_weights)
.def("get_num_needed_points", &TSG_Wrap::getNumNeededPoints)
.def("get_needed_points", &TSG_Wrap::get_needed_points)
.def("load_needed_points", &TSG_Wrap::load_needed_points)
.def("evaluate", &TSG_Wrap::evaluate_wrap)
.def("integrate", &TSG_Wrap::integrate_wrap)
.def("print_stats", &TSG_Wrap::printStats)
.def("set_refinement", &TSG_Wrap::setRefinement)
;
bpl::enum_<TypeOneDRule>("TypeOneDRule")
.value("rule_base", rule_base)
.value("rule_clenshawcurtis", rule_clenshawcurtis)
.value("rule_chebyshev", rule_chebyshev)
.value("rule_gausslegendre", rule_gausslegendre)
.value("rule_gausschebyshev1", rule_gausschebyshev1)
.value("rule_gausschebyshev2", rule_gausschebyshev2)
.value("rule_chebyshevN2P", rule_chebyshevN2P)
.value("rule_fejer2", rule_fejer2)
.value("rule_gaussgegenbauer", rule_gaussgegenbauer)
.value("rule_gaussjacobi", rule_gaussjacobi)
.value("rule_gausslaguerre", rule_gausslaguerre)
.value("rule_gausshermite", rule_gausshermite)
.value("rule_pwpolynomial", rule_pwpolynomial)
.value("rule_pwpolynomial0", rule_pwpolynomial0)
.value("rule_wavelet", rule_wavelet)
.value("rule_fulltensor", rule_fulltensor)
;
bpl::enum_<TypeDepth>("TypeDepth")
.value("type_level", type_level)
.value("type_basis", type_basis)
.value("type_hyperbolic", type_hyperbolic)
;
bpl::enum_<TypeRefinement>("TypeRefinement")
.value("refine_classic", refine_classic)
.value("refine_parents_first", refine_parents_first)
.value("refine_direction_selective", refine_direction_selective)
.value("refine_fds", refine_fds)
;
}