Boost vector and matrix multiplication:

This is a sample program for multiplying matrix to vector using boost.Am not able to understand why i am getting this error.Can anyone help me understand what is wrong in this program.
Thanks in advance

int main()
{
boost::numeric::ublas::matrix<double,column_major> m(100, 100);
boost::numeric::ublas::vector<double> n(100);
boost::numeric::ublas::vector<double> mn(100);

for (unsigned i = 0; i < m.size1(); ++i)
for (unsigned j = 0; j < m.size2(); ++j)
m(i, j) = 3 * i + j;

for (unsigned i = 0; i < n.size(); ++i)
n(i) = 1.25;

mn = block_prod<vector<double>, 64>(m, n);

for (unsigned i = 0; i < mn.size(); ++i)
std::cout << mn(i)<<" ";
return 0;
}


I get this compile error >d:\boost_1_72_0\boost\numeric\ublas\operation_blocked.hpp(64): error C2672: 'project': no matching overloaded function found
Since you didn't post your whole program I can't say for certain, but probably you're missing a file:
#include <boost/numeric/ublas/vector_proxy.hpp>
The following "works" in cpp.sh anyway.

For the boost version in cpp.sh, block_prod appears to work properly on matrix * matrix only, so I have faked that by making your vector a 1-column matrix.
https://www.boost.org/doc/libs/1_72_0/libs/numeric/ublas/doc/operations_overview.html

I would be VERY surprised if block_prod() had any advantages for this application. Use just prod() or axpy_prod() if you want matrix * vector.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/operation.hpp>
#include <boost/numeric/ublas/operation_blocked.hpp>
using namespace boost::numeric::ublas;

int main()
{
   matrix<double> m(100, 100);
// vector<double> n(100);
// vector<double> mn(100);
   matrix<double> n(100,1);
   matrix<double> mn(100,1);

   for (unsigned i = 0; i < m.size1(); ++i)
      for (unsigned j = 0; j < m.size2(); ++j)
         m(i, j) = 3 * i + j;
   
// for (unsigned i = 0; i < n.size(); ++i)
//    n(i) = 1.25;
   for (unsigned i = 0; i < n.size1(); ++i)
      n(i,0) = 1.25;

   mn = block_prod<matrix<double>, 64>(m, n);

   for (unsigned i = 0; i < mn.size1(); ++i)
   std::cout << mn(i,0) <<" ";
}




If you want a normal matrix * vector operation then you can use simply
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
using namespace boost::numeric::ublas;

int main()
{
   matrix<double> m(100, 100);
   vector<double> n(100);

   for (unsigned i = 0; i < m.size1(); ++i)
      for (unsigned j = 0; j < m.size2(); ++j)
         m(i, j) = 3 * i + j;
   
   for (unsigned i = 0; i < n.size(); ++i)
     n(i) = 1.25;

   auto mn = prod(m, n);

   for (unsigned i = 0; i < mn.size(); ++i)
      std::cout << mn(i) <<" ";
}
Last edited on
Topic archived. No new replies allowed.