Search code examples
c++vectorunary-operator

MTL4 apply unary operator (e.g. abs()) elementwise to dense_vector<double>


I want to apply a unary operator, particualrly abs(), elementwise to a dense_vector< double>.

Is there a native variant to do this (rather than building manually a loop)? If so, I would need a brief explanation of the concept.

I browsed in the MTL4 Entire Manual and couldn't figure out, how to do it. It seems that I, somehow, have to define a corresponding functor. Is it mtl::sfunctor::abs? If so, how do I apply it to a vector?


Solution

  • After a quick glance through source of dense_vector, it seems like a standard conformant container. Therefore, this should work:

    std::transform(v.begin(), v.end(), v.begin(),
        static_cast<double(*)(double)>(&std::abs));