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?

2
  • Nothing wrong with iterating; but I am using boost, so I am hoping to get rid of tedious stuff like that.
    – Vahagn
    Feb 23, 2012 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, 2012 at 20:31

2 Answers 2

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;
}
1
  • Your matrix is 10 x 10. The point is to copy a vector of size N to a matrix of size 1 x N.
    – Vahagn
    Feb 23, 2012 at 20:57
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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.