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

邮差的信 提交于 2020-01-25 02:57:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!