The supported operations are:
#include <boost/multi_array.hpp> #include <cmath> #include <iostream> #include <iomanip> #include <functional> #include "multi_array_op.h" using namespace boost; using namespace mimas; using namespace std; template< typename T > struct _log: public unary_function< T, T > { T operator()( const T &x ) const { return log( x ); } }; ARRAY_OP( logarithmIt, logarithm, _log ) int main() { const int dim1 = 4, dim2 = 3, dim3 = 2; multi_array< double, 2 > x( extents[dim2][dim1] ); multi_array< double, 3 > y( extents[dim3][dim2][dim1] ); for ( int i=0; i<dim1; i++ ) for ( int j=0; j<dim2; j++ ) { for ( int k=0; k<dim3; k++ ) y[k][j][i] = i + j * dim1 + k * dim2 * dim1 + 1; x[j][i] = - i - j * dim1 - 1; }; cout << y.shape()[0] << " " << y.strides()[0] << endl; cout << y.shape()[1] << " " << y.strides()[1] << endl; cout << y.shape()[2] << " " << y.strides()[2] << endl; // Scalar // intern y /= 2.0; y[1] *= 2.0; // extern // left x / 2.0; y[1] * 2.0; // right 2.0 * x; 2.0 * y[1]; // Multiply subarray with scalar. typedef multi_array< double, 3 >::index_range range; multi_array< double, 3 >::array_view< 2 >::type view = y[ indices[ range(0,dim1) ][range(1,dim2-1)][1] ]; view *= -1.0; cout << view.shape()[0] << " " << view.strides()[0] << endl; cout << view.shape()[1] << " " << view.strides()[1] << endl; // External multiplication with subarray. 5.0 * view; ((const multi_array< double, 2 > &)x)[0] * ((const multi_array< double, 2 > &)x)[0]; // element-wise intern y[0] *= logarithm( absolute( y[1] ) + 1.0 ); y[0] *= ((const multi_array< double, 3 > &)y)[1]; y[0] *= ((const multi_array< double, 2 > &)x); x *= y[1]; x *= ((const multi_array< double, 3 > &)y)[1]; x *= ((const multi_array< double, 2 > &)x); // element-wise extern. y[0] * y[1]; y[0] * ((const multi_array< double, 3 > &)y)[1]; y[0] * ((const multi_array< double, 2 > &)x); y[1] * x; x * y[1]; x * ((const multi_array< double, 3 > &)y)[1]; x * ((const multi_array< double, 2 > &)x); x * x; for ( int k=0; k<dim3; k++ ) { for ( int j=0; j<dim2; j++ ) { for ( int i=0; i<dim1; i++ ) cout << setw( 12 ) << y[k][j][i]; cout << endl; } cout << endl; }; for ( int j=0; j<dim2; j++ ) { for ( int i=0; i<dim1; i++ ) cout << setw( 12 ) << x[j][i]; cout << endl; } }
Add documentation of all available array operators.