1

What am I doing wrong here?

// file main.cpp

#include <iostream>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>

namespace ublas = boost::numeric::ublas;

int main()
{
    ublas::vector<double> const v( 10 );
    ublas::matrix<double> m( 1, v.size() );
    ublas::matrix_row<ublas::matrix<double> > r( m, 1 );
    r = v;
    return 0;
}

This fails with message:

Check failed in file /usr/local/include/boost/numeric/ublas/functional.hpp at line 1370:
i < size_i
terminate called after throwing an instance of 'boost::numeric::ublas::bad_index'
  what():  bad index
Aborted

However, is there more laconic way to v into m at main.cpp?

Vahagn
  • 4,670
  • 9
  • 43
  • 72
  • Nothing wrong with iterating; but I am using boost, so I am hoping to get rid of tedious stuff like that. – Vahagn Feb 23 '12 at 20:29
  • Fair enough. I worked quite a bit with boost ublas in my last semester at school, and don't recall any easy function to do what you're doing. Doesn't mean it's not there, I just never encountered it. – Sam DeHaan Feb 23 '12 at 20:31

2 Answers2

3

Did this not work?

std::copy(v.begin(), v.end(), m.begin1());

this will occupy the first v.size() elements of m with the value of v.

The following code compiles and runs on my system (boost 1.48 and g++ 4.62)

#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <algorithm>

int main()
{
    boost::numeric::ublas::vector<int> v(10);
    boost::numeric::ublas::matrix<int> m(10,10);  //using v.size() also works
    std::copy(v.begin(), v.end(), m.begin1());
    return 0;
}
111111
  • 15,686
  • 6
  • 47
  • 62
1

Of course, you're trying to access 1st row which just isn't there for 1 x v,size() matrix. You should write:

ublas::matrix_row<ublas::matrix<double> > r( m, 0 );

though you'd be better with

row(m, 0) = v;
panda-34
  • 4,089
  • 20
  • 25