1

I'm a very noob at Boost::uBLAS.

I have a function which take a ublas::matrix_expression<double> as input:

namespace ublas = boost::numeric::ublas;

void Func(const ublas::matrix_expression<double>& in,
                ublas::matrix_expression<double>& out);

A caller is holding a row vector as ublas::vector<double>, and I want it to be passed to Func.

Until now I have not found any way to do this.
What is the best way, preferably without any temporary allocation?

Thanks.

niboshi
  • 1,448
  • 3
  • 12
  • 20

2 Answers2

1

Well, there is an option to create a read-only adapter of a contiguous area of memory into a read-only matrix. Have a look at example 3. It is pretty straightforward to use:

#include "storage_adaptors.hpp"
#include <boost/numeric/ublas/assignment.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

ublas::vector<double> v(6);
v <<= 1, 2, 3, 4, 5, 6;
ublas::matrix<double> m = ublas::make_matrix_from_pointer(2, 3, &v(0));
std::cout << m << std::endl;

Possibly you could tweak that further to fit your needs/example.

Anonymous
  • 18,162
  • 2
  • 41
  • 64
  • 1
    `make_matrix_from_pointer()` does perform a copy from the source memory. Also, just clarifying to avoid confusion: It is not part of the boost library. – NoahR Feb 21 '13 at 23:54
0

You can avoid allocating if you're ready to sacrifice some multiplication, use

outer_prod(scalar_vector<double>(1, 1), vec)

to transform vector into matrix expression. Also, your function probably should be

template<class C>
void Func(const matrix_expression<C>& in...

matrix_expression itself doesn't model matrix expression concept, it's just the base class.

panda-34
  • 4,089
  • 20
  • 25