Apply user-defined scalar function elementwise to Armadillo array

23 Views Asked by At

As the title says, take y = f(x) where x is an array and f is a scalar function. I tried transform() and for_each(), but they don't work for 1D arrays.

double foo(double x) { /* expression with arithmetic operators, pow(), and sqrt() */ }
template <typename T> T foo(const T& x) { /* gives same error as above */ }

...

const double pi = 4.0*atan(1.0);
int n = 2000;
auto i = arma::linspace(0, n + 1, n);
auto x = 8*pi*i/(n - 1) - 4*pi;
auto y = sin(x); // no error because sin is defined by armadillo
auto y = foo(x); // error: cannot convert 'arma::eOp<arma::eOp<arma::... to 'double'
auto y = x;
y.transform(foo); // error: transform() not defined

Or can anyone recommend a c++ library that is better for easily converting from matlab/numpy/python code?

0

There are 0 best solutions below