Applying Boost UBLAS blas-1 to Matrices
How do I apply level 1 blas on a boost::numeric::ublas matrix? For example I want t开发者_开发问答o compute the maximum entry or the sum of all entries. Using norm_1 or norm_inf on a matrix gives no compiler error but returns (as it seems to me) arbitrary results. I am using boost 1.42
norm_inf
on a matrix computes the matrix norm induced by the infinity-norm on the underlying vector space. This happens to be the maximum absolute row sum of the matrix.
If you look at hannes's example, the last row of the matrix (i=99, j=0...99) contains:
9900, 9901, 9902, ... , 9999
If you sum those entries, you get 994950
, which is exactly what norm_inf
produces.
A minimal example looks like this:
#include<iostream>
#include<boost/numeric/ublas/matrix.hpp>
using namespace boost::numeric::ublas;
int main(){
int l = 100;
matrix<double> m(l,l);
for (int i = 0; i < l; ++i) {
for (int j = 0; j < l; ++j) {
m(i,j)=i*l+j;
}
}
std::cout << norm_inf(m)<<std::endl;
return 0;
}
It should give 99, but yields 994950.
This is a one-liner that at least solves the given task:
float infnorm = accumulate(m.data().begin(),m.data().end(),0,std::max<double>);
精彩评论