1#include "boost/multi_array.hpp"
2#include "boost/array.hpp"
3#include "CustomLibraries/np.hpp"
10 typedef boost::multi_array<double, 4>::index index;
11 boost::multi_array<double, 4> A(boost::extents[3][4][2][2]);
15 for (index i = 0; i != 3; ++i)
16 for (index j = 0; j != 4; ++j)
17 for (index k = 0; k != 2; ++k)
18 for (index l = 0; l != 2; ++l)
19 A[i][j][k][l] = values++;
23 for (index i = 0; i != 3; ++i)
24 for (index j = 0; j != 4; ++j)
25 for (index k = 0; k != 2; ++k)
26 for (index l = 0; l != 2; ++l)
27 assert(A[i][j][k][l] == verify++);
29 double dx, dy, dz, dt;
34 std::vector<boost::multi_array<double, 4>> my_arrays =
np::gradient(A, {dx, dy, dz, dt});
37 std::vector<boost::multi_array<double, 1>> gradf =
np::gradient(x, {1.0});
38 for (
int i = 0; i < 5; i++)
40 std::cout << gradf[0][i] <<
",";
52 const boost::multi_array<double, 1> axis[4] = {x, y, z, t};
53 std::vector<boost::multi_array<double, 4>> my_arrays =
np::meshgrid(axis,
false, np::xy);
57 boost::multi_array<double, 1> x2 =
np::linspace(0, 1, nx);
58 boost::multi_array<double, 1> y2 =
np::linspace(0, 1, ny);
59 const boost::multi_array<double, 1> axis2[2] = {x2, y2};
60 std::vector<boost::multi_array<double, 2>> my_arrays2 =
np::meshgrid(axis2,
false, np::xy);
62 for (
int i = 0; i < ny; i++)
64 for (
int j = 0; j < nx; j++)
66 std::cout << my_arrays2[0][i][j] <<
" ";
71 for (
int i = 0; i < ny; i++)
73 for (
int j = 0; j < nx; j++)
75 std::cout << my_arrays2[1][i][j] <<
" ";
81void test_complex_operations()
85 boost::multi_array<double, 1> x =
np::linspace(0, 1, nx);
86 boost::multi_array<double, 1> y =
np::linspace(0, 1, ny);
87 const boost::multi_array<double, 1> axis[2] = {x, y};
88 std::vector<boost::multi_array<double, 2>> my_arrays =
np::meshgrid(axis,
false, np::xy);
89 boost::multi_array<double, 2> A =
np::sqrt(my_arrays[0]);
90 std::cout <<
"sqrt\n";
91 for (
int i = 0; i < ny; i++)
93 for (
int j = 0; j < nx; j++)
95 std::cout << A[i][j] <<
" ";
102 std::cout <<
"sqrt of " << a <<
" is " << sqa <<
"\n";
103 std::cout <<
"exp\n";
104 boost::multi_array<double, 2> B =
np::exp(my_arrays[0]);
105 for (
int i = 0; i < ny; i++)
107 for (
int j = 0; j < nx; j++)
109 std::cout << B[i][j] <<
" ";
114 std::cout <<
"Power\n";
115 boost::multi_array<double, 1> x2 =
np::linspace(1, 3, nx);
116 boost::multi_array<double, 1> y2 =
np::linspace(1, 3, ny);
117 const boost::multi_array<double, 1> axis2[2] = {x2, y2};
118 std::vector<boost::multi_array<double, 2>> my_arrays2 =
np::meshgrid(axis2,
false, np::xy);
119 boost::multi_array<double, 2> C =
np::pow(my_arrays2[1], 2.0);
120 for (
int i = 0; i < ny; i++)
122 for (
int j = 0; j < nx; j++)
124 std::cout << C[i][j] <<
" ";
132 boost::multi_array<double, 1> x =
np::linspace(0, 1, 5);
133 boost::multi_array<double, 1> y =
np::linspace(0, 1, 5);
134 boost::multi_array<double, 1> z =
np::linspace(0, 1, 5);
135 boost::multi_array<double, 1> t =
np::linspace(0, 1, 5);
136 const boost::multi_array<double, 1> axis[4] = {x, y, z, t};
137 std::vector<boost::multi_array<double, 4>> my_arrays =
np::meshgrid(axis,
false, np::xy);
138 boost::multi_array<double, 1> x2 =
np::linspace(0, 1, 5);
139 boost::multi_array<double, 1> y2 =
np::linspace(0, 1, 5);
140 boost::multi_array<double, 1> z2 =
np::linspace(0, 1, 5);
141 boost::multi_array<double, 1> t2 =
np::linspace(0, 1, 5);
142 const boost::multi_array<double, 1> axis2[4] = {x2, y2, z2, t2};
143 std::vector<boost::multi_array<double, 4>> my_arrays2 =
np::meshgrid(axis2,
false, np::xy);
144 std::cout <<
"equality test:\n";
145 std::cout << (bool)(my_arrays == my_arrays2) <<
"\n";
147void test_basic_operations()
151 boost::multi_array<double, 1> x =
np::linspace(0, 1, nx);
152 boost::multi_array<double, 1> y =
np::linspace(0, 1, ny);
153 const boost::multi_array<double, 1> axis[2] = {x, y};
154 std::vector<boost::multi_array<double, 2>> my_arrays =
np::meshgrid(axis,
false, np::xy);
156 std::cout <<
"basic operations:\n";
158 std::cout <<
"addition:\n";
159 boost::multi_array<double, 2> A = my_arrays[0] + my_arrays[1];
161 for (
int i = 0; i < ny; i++)
163 for (
int j = 0; j < nx; j++)
165 std::cout << A[i][j] <<
" ";
170 std::cout <<
"multiplication:\n";
171 boost::multi_array<double, 2> B = my_arrays[0] * my_arrays[1];
173 for (
int i = 0; i < ny; i++)
175 for (
int j = 0; j < nx; j++)
177 std::cout << B[i][j] <<
" ";
182 boost::multi_array<double, 1> t =
np::linspace(0, 1, nx);
183 boost::multi_array<double, 1> t_time_3 = coeff * t;
184 boost::multi_array<double, 1> t_time_2 = 2.0 * t;
185 std::cout <<
"t_time_3: ";
186 for (
int j = 0; j < nx; j++)
188 std::cout << t_time_3[j] <<
" ";
191 std::cout <<
"t_time_2: ";
192 for (
int j = 0; j < nx; j++)
194 std::cout << t_time_2[j] <<
" ";
203 int dimensions[] = {ny, nx};
204 boost::multi_array<double, 2> A = np::zeros<double>(dimensions);
205 std::cout <<
"zeros:\n";
206 for (
int i = 0; i < ny; i++)
208 for (
int j = 0; j < nx; j++)
210 std::cout << A[i][j] <<
" ";
220 boost::multi_array<double, 1> x =
np::linspace(0, 10, nx);
221 boost::multi_array<double, 1> y =
np::linspace(-1, 1, ny);
222 const boost::multi_array<double, 1> axis[2] = {x, y};
223 std::vector<boost::multi_array<double, 2>> my_array =
np::meshgrid(axis,
false, np::xy);
224 std::cout <<
"min: " <<
np::min(my_array[0]) <<
"\n";
225 std::cout <<
"max: " <<
np::max(my_array[1]) <<
"\n";
226 std::cout <<
"max simple: " <<
np::max(1.0, 2.0, 3.0, 4.0, 5.0) <<
"\n";
227 std::cout <<
"min simple: " <<
np::min(1, -2, 3, -4, 5) <<
"\n";
230void test_toy_problem()
232 boost::multi_array<double, 1> x =
np::linspace(0, 1, 100);
233 boost::multi_array<double, 1> y =
np::linspace(0, 1, 100);
237 const boost::multi_array<double, 1> axis[2] = {x, y};
238 std::vector<boost::multi_array<double, 2>> XcY =
np::meshgrid(axis,
false, np::xy);
244 boost::multi_array<double, 2> f =
np::pow(XcY[0], 2.0) + XcY[0] *
np::pow(XcY[1], 1.0);
248 std::vector<boost::multi_array<double, 2>> gradf =
np::gradient(f, {dx, dy});
254 std::cout <<
"df/dx of f(x,y) = x^2 + xy at x = " << x[i] <<
" and y = " << y[j] <<
" is equal to " << gradf[0][i][j];
263 boost::multi_array<double, 1> x =
np::linspace(-1, 1, nx);
264 boost::multi_array<double, 1> y =
np::linspace(-1, 1, ny);
265 const boost::multi_array<double, 1> axis[2] = {x, y};
266 std::vector<boost::multi_array<double, 2>> XcY =
np::meshgrid(axis,
false, np::xy);
267 boost::multi_array<double, 2> abs_f =
np::abs(XcY[0]);
268 std::cout <<
"abs_f: \n";
269 for (
int i = 0; i < ny; i++)
271 for (
int j = 0; j < nx; j++)
273 std::cout << abs_f[i][j] <<
" ";
283 boost::multi_array<double, 1> x =
np::linspace(-1, 1, nx);
284 boost::multi_array<double, 1> y =
np::linspace(-1, 1, ny);
285 const boost::multi_array<double, 1> axis[2] = {x, y};
286 std::vector<boost::multi_array<double, 2>> XcY =
np::meshgrid(axis,
false, np::xy);
287 boost::multi_array<double, 2> f =
np::pow(XcY[0], 2.0) + XcY[0] *
np::pow(XcY[1], 1.0);
288 std::cout <<
"f: \n";
289 for (
int i = 0; i < ny; i++)
291 for (
int j = 0; j < nx; j++)
293 std::cout << f[i][j] <<
" ";
297 std::cout <<
"f[0]: \n";
298 boost::multi_array<double, 1> f_slice =
np::slice(f, 0);
299 for (
int i = 0; i < nx; i++)
301 std::cout << f_slice[i] <<
" ";
305 std::cout <<
"f[1]: \n";
307 for (
int i = 0; i < ny; i++)
309 std::cout << f_slice[i] <<
" ";
318 test_complex_operations();
320 test_basic_operations();
::value constexpr boost::multi_array< T, ND > pow(const boost::multi_array< T, ND > &input_array, const T exponent)
Implements the numpy pow function on multi arrays.
::value constexpr std::vector< boost::multi_array< T, ND > > gradient(boost::multi_array< T, ND > inArray, std::initializer_list< T > args)
::value constexpr std::vector< boost::multi_array< T, ND > > meshgrid(const boost::multi_array< T, 1 >(&cinput)[ND], bool sparsing=false, indexing indexing_type=xy)
::value constexpr boost::multi_array< T, ND > sqrt(const boost::multi_array< T, ND > &input_array)
Implements the numpy sqrt function on multi arrays.
::value constexpr boost::multi_array< T, ND > exp(const boost::multi_array< T, ND > &input_array)
Implements the numpy exp function on multi arrays.
::value constexpr T abs(T input)
Implements the numpy abs function for a scalar.
boost::multi_array< double, 1 > linspace(double start, double stop, long unsigned int num)
Implements the numpy linspace function.
::value constexpr T min(boost::multi_array< T, ND > const &input_array)
Implements the numpy min function for an n-dimensionl multi array.
::value constexpr boost::multi_array< T, ND - 1 > slice(boost::multi_array< T, ND > const &input_array, std::size_t slice_index)
Slices the array through one dimension and returns a ND - 1 dimensional array.
::value constexpr T max(boost::multi_array< T, ND > const &input_array)
Implements the numpy max function for an n-dimensionl multi array.