7

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;
}
Community
  • 1
  • 1
Budric
  • 3,599
  • 8
  • 35
  • 38

2 Answers2

10

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

JCooper
  • 6,395
  • 1
  • 25
  • 31
  • Yeah that works. Although I don't see why it can't work for a 1x3 matrix as well. – Budric Apr 04 '11 at 19:29
  • It would, but you'd have to declare it as a matrix. I think the problem is in the flexibility that Boost tries to provide while still using only header files. It makes it too hard to use in some cases and the template intricacies start to drive you crazy. – JCooper Apr 04 '11 at 19:40
  • That's what I find about Boost. I think Eigen library (also headers only) managed to strike a nice balance between templates, speed and usability. So i'm going with that solution rather than using Boost uBLAS. – Budric Apr 04 '11 at 21:12
  • This will be true only when M.V^T=(V.M)^T where M is matrix and V is row vector. – Nikolay Frick Aug 30 '13 at 15:23
3

I haven't looked that much at Boost uBLAS, but Eigen sure is nice, and has good performance as well.

janneb
  • 36,249
  • 2
  • 81
  • 97
  • Thanks, I'll have a look. Wow. I'm really liking this so far. I spent some time googling for matrix library, but this didn't come up. Usually you get LAPACK++ MTL in the results and others. Sometimes they aren't open licensed. Sometimes they aren't elegant. – Budric Apr 04 '11 at 18:36