niboshi
niboshi

Reputation: 1498

ublas: Wrap ublas::vector as ublas::matrix_expression

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.

Upvotes: 1

Views: 702

Answers (2)

panda-34
panda-34

Reputation: 4209

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.

Upvotes: 0

Anonymous
Anonymous

Reputation: 18631

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.

Upvotes: 1

Related Questions