Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost uBLAS matrix/vector product

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;
}
like image 851
Budric Avatar asked Apr 04 '11 16:04

Budric


People also ask

Can you dot product a matrix and a vector?

The first component of the matrix-vector product is the dot product of x with the first row of A, etc. In fact, if A has only one row, the matrix-vector product is really a dot product in disguise.

Is matrix vector multiplication linear?

And this is a important takeaway. Matrix multiplication or matrix products with vectors is always a linear transformation.


1 Answers

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

like image 153
JCooper Avatar answered Oct 21 '22 20:10

JCooper