Boost logo

Ublas :

From: Russ (c.r.coggrave_at_[hidden])
Date: 2006-04-24 18:03:04


Neal Becker <ndbecker2 <at> gmail.com> writes:

>
> I'm stumped - any hints?
>
> #include <boost/numeric/ublas/vector.hpp>
> #include <boost/numeric/ublas/matrix.hpp>
>
> int main() {
> namespace ublas = boost::numeric::ublas;
>
> ublas::vector<int> a (4);
> ublas::matrix<int> b (4,4);
> ublas::matrix<int> c = ublas::prod (a, b);
> }
>

Try the following...

ublas::vector<int> a (4);
// Fill
for (ublas::vector<int>::size_type i = 0; i < a.size(); i++)
  a(i) = i;
ublas::matrix<int> b (4,4);
// Fill
for (ublas::matrix<int>::size_type i = 0; i < b.size1(); i++)
  for (ublas::matrix<int>::size_type j = 0; j < b.size2(); j++)
    b(i,j) = j + i*b.size2();
// Product
ublas::vector<int> c = ublas::prod (a, b);
// Output
std::cout << "a = " << a << std::endl;
std::cout << "b = " << b << std::endl;
std::cout << "c = " << c << std::endl;

Generates the following output...

a = [4](0,1,2,3)
b = [4,4]((0,1,2,3),(4,5,6,7),(8,9,10,11),(12,13,14,15))
c = [4](56,62,68,74)

The main difference to your orginal code is that c must be a vector.

Regards,

Russ