问题
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.
回答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.
回答2:
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.
来源:https://stackoverflow.com/questions/9224941/ublas-wrap-ublasvector-as-ublasmatrix-expression