Boost logo

Ublas :

From: Gunter Winkler (guwi17_at_[hidden])
Date: 2007-06-12 01:17:26

Markus Weimer schrieb:
> Hi,
> I have some legacy code that computes the inner product between two
> row-major matrices by treating them as long vectors and applying a
> vector inner product to these vectors. I guess getting a vector-view
> onto a matrix is not possible in uBLAS. Thus, I want to compute the
> inner product between two matrices as follows:
> v = tr(A*B)
> However, I cannot find the trace operation in the documentation. Does
> that mean that there is no trace in uBLAS?
No, there is no trace operation implemented. However there are some
workarounds available:
element_prod(A,B); // compute c(i,j) = a(i,j)*b(i,j)
scalar_vector<double>(A.size1(), 1.0); // a vector of ones
sum(v); // sums element of a vector

Please try this:

sum( prod( scalar_vector<double>(A.size1(), 1.0), element_prod(A,B) ) );


PS: If you solution runs, please add it to the wiki.
PPS: If there are more people asking for a ones vector - I can add a
class ones_vector similar to zero_vector (g++ optimizes multiplications
with 1.0 away.)