ATTENTION!
The following answer is incorrect. See the Editing Section below. I left the original answer so as to give context and credit to those who pointed out the error.
I am not particularly familiar with boost libraries, so there may be a more standard way to do this, but I think you can do what you want with iterators and STL transform function templates. An introduction to the documentation of the uBLAS library says its classes are designed to be compatible with the same iterator behavior that is used in STL. Formatting templates and vector templates have iterators that can be used to access individual elements. The vector has begin() and end() , and the matrix has begin1() , end1() , begin2() and end2() . Varieties 1 are iterators over columns, and varieties 2 are iterators over a series of rows. See the VectorExpression Acceleration Documentation and Matrix Expression for a bit more information.
Using the STL transform algorithm, you can apply a function to each element of an iterative sequence and assign the result to another iterative sequence of the same length or the same sequence. Therefore, to use this on the uBLAS accelerator, you can do this:
using namespace boost::numeric::ublas; // Create a 30 element vector of doubles vector<double> vec(30); // Assign 8.0 to each element. std::fill(vec.begin(), vec.end(), 8.0); // Perform the "Gamma" function on each element and assign the result back // to the original element in the vector. std::transform(vec.begin(), vec.end(), vec.begin(), boost::math::tgamma);
For a matrix, this will be basically the same; you would use either iterators 1 or 2 . Which one you want to use depends on whether your matrix layout has the size of a row or column. A quick look at the uBLAS documentation leads me to think that this may be one, so you will need to study the code and determine which one is used to select the most efficient iteration order.
matrix<double> mat(30, 30); . . . std::transform(mat.begin1(), mat.end1(), mat.begin1(), boost::math::tgamma);
The function that you pass as the last argument can be a function that takes a single double argument and returns a double value. It can also be a functor .
This is not exactly the same as the vectorization example you gave, but it looks like it should be close to what you want.
EDIT
Looks like I should have checked my recommendation before doing this. As pointed out by others, iterators "1" and "2" only repeat along the same row / column of the matrix. The review documentation in Boost is seriously misleading. He claims that begin1() "Returns iterator1 pointing to the beginning of the matrix," and end1() "Returns iterator1 pointing to the end of the matrix." Would it kill them to say “matrix column” instead of “matrix”? I assumed that iterator1 was a columnar iterator that would iterate over the whole matrix. For the correct way to do this, see Answer to Lantern Rouge .