Boost uBLAS matrix/vector product

Budric picture Budric · Apr 4, 2011 · Viewed 14.1k times · Source

can someone please provide an example of how to use uBLAS product to multiply things? Or if there's a nicer C++ matrix library you can recommend I'd welcome that too. This is turning into one major headache.

Here's my code:

vector<double> myVec(scalar_vector<double>(3));
matrix<double> myMat(scalar_matrix<double>(3,3,1));
matrix<double> temp = prod(myVec, myMat);

Here's the error:

cannot convert from 'boost::numeric::ublas::matrix_vector_binary1<E1,E2,F>' to 'boost::numeric::ublas::matrix<T>'

I have exhausted my search. Stackoverflow has a question about this here. Boost documentation has an example here. I've copied the code from example, but it's of no use to me because the template magic that works for stdout is useless to me.

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    vector<double> v (3);
    for (unsigned i = 0; i < std::min (m.size1 (), v.size ()); ++ i) {
        for (unsigned j = 0; j < m.size2 (); ++ j)
            m (i, j) = 3 * i + j;
        v (i) = i;
    }

    std::cout << prod (m, v) << std::endl;
    std::cout << prod (v, m) << std::endl;
}

Answer

JCooper picture JCooper · Apr 4, 2011

The product of a vector and a matrix is a vector, not a matrix.