uBLAS matrix_column proxy

When I wrote a function that calculates the matrix L1 norm, the resulting code would have been more readable if I had based it on the vector L1 norm functionality previously defined. But to do that, we need a class that acts like a proxy for the underlying matrix, making available one of its rows. uBLAS implements matrix_column just for cases like this.

Let's refactor my::norm_1() to use matrix_column:
// ...
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp> // 1
namespace ublas = boost::numeric::ublas;
namespace my
{
    double norm_1(ublas::matrix<double>& m)
    {
        double norm = 0.0;
        for(unsigned int j = 0; j < m.size2(); ++j)
        {
            ublas::matrix_column<ublas::matrix<double> > mc(m, j); // 2
            double current = my::norm_1(mc); // 3
            if(current > norm)
                norm = current;
        }
        return norm;
    }
}
1. Here are the matrix proxy definitions, remember to include this header file.
2. Declaring a matrix_column based on a matrix of doubles, passing to it the actual matrix and the index of the column we are interested in.
3. The matrix_column could be treated as a normal uBLAS vector, here we are passing it to our norm_1() functionality.

No comments:

Post a Comment