[英]Initializing boost matrix with a std::vector or array
I have a method that takes a std::vector as one of its parameters.我有一个将 std::vector 作为其参数之一的方法。 Is there a way I can initialize a matrix by assigning the std::vector to the matrix?
有没有办法通过将 std::vector 分配给矩阵来初始化矩阵? Here's what I tried to do below.
这是我在下面尝试做的。 Does anyone know how i can achieve assigning the vector (or even a pointer of doubles) to the matrix?
有谁知道我如何实现将向量(甚至是双精度指针)分配给矩阵? Thanks in advance.
提前致谢。 Mike
麦克风
void Foo(std::vector v)
{
matrix<double> m(m, n, v);
// work with matrix...
}
Here is yet another example of how this can be done:这是如何做到这一点的另一个例子:
#include <algorithm>
#include <vector>
#include <boost/numeric/ublas/storage.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
namespace ublas = boost::numeric::ublas;
template <typename T, typename F=ublas::row_major>
ublas::matrix<T, F> makeMatrix(std::size_t m, std::size_t n, const std::vector<T> & v)
{
if(m*n!=v.size()) {
; // Handle this case
}
ublas::unbounded_array<T> storage(m*n);
std::copy(v.begin(), v.end(), storage.begin());
return ublas::matrix<T>(m, n, storage);
}
int main () {;
std::vector<double> vec {1, 2, 3, 4, 5, 6};
ublas::matrix<double> mm = makeMatrix(3,2,vec);
std::cout << mm << std::endl;
}
A more convenient way is like this:更方便的方法是这样的:
matrix<double> m(m*n);
std::copy(v.begin(), v.end(), m.data().begin());
According to the boost matrix documentation , there are 3 constructors for the matrix class: empty, copy, and one taking two size_types for the number of rows and columns.根据boost matrix 文档,矩阵类有 3 个构造函数:empty、copy 和一个采用两个 size_types 表示行数和列数。 Since boost doesn't define it (probably because there are many ways to do it and not every class is gong to define a conversion into every other class) you are going to need to define the conversion.
由于 boost 没有定义它(可能是因为有很多方法可以做到这一点,并且不是每个类都将定义一个转换为每个其他类),因此您将需要定义转换。
Here's an approach that I would use, but since there are multiple ways to do this and the question doesn't specify how you want this done you may find a different approach more applicable to your situation.这是我会使用的一种方法,但由于有多种方法可以做到这一点,并且问题没有具体说明您希望如何完成这项工作,您可能会发现一种更适用于您的情况的不同方法。
void Foo(const std::vector<double> & v) {
size_t m = ... // you need to specify
size_t n = ... // you need to specify
if(v.size() < m * n) { // the vector size has to be bigger or equal than m * n
// handle this situation
}
matrix<double> mat(m, n);
for(size_t i=0; i<mat.size1(); i++) {
for(size_t j=0; j<mat.size2(); j++) {
mat(i,j) = v[i+j*mat.size1()];
}
}
}
A couple of notes about your provided code: std::vector needs a templated argument and you are declaring m
as a matrix and an input argument to it's constructor.关于您提供的代码的一些说明: std::vector 需要一个模板化参数,并且您将
m
声明为矩阵和它的构造函数的输入参数。
Simple answer, but not very apparent from the Boost documentation.简单的答案,但从 Boost 文档中不是很明显。
You may just use std::vector<> as type of storage array template paramerter instead of default unbounded_array<> for your matrix.您可以只使用 std::vector<> 作为存储数组模板参数的类型,而不是默认的 unbounded_array<> 矩阵。 (It's mentioned in footnote 2 of documentation on matrix<> class.)
(它在关于 matrix<> 类的文档的脚注 2 中提到。)
void Foo(const std::vector<double> &v, size_t n)
{
using namespace boost::numeric::ublas;
size_t m = v.size() / n;
matrix< double, row_major, std::vector<double> > M(m, n);
M.data() = v;
// work with matrix...
}
More variants of initialization can be found in your boost source: boost/libs/numeric/ublas/doc/samples/assignment_examples.cpp , as pointed out here: assign multiple values to boost::numeric::ublas::vector in c++更多的初始化变体可以在你的 boost 源中找到: boost/libs/numeric/ublas/doc/samples/assignment_examples.cpp ,正如这里所指出的: assign multiple values to boost::numeric::ublas::vector in c++
Or here: uBLAS examples , example 3, which is mentioned by related question: ublas: Wrap ublas::vector as ublas::matrix_expression或者在这里: uBLAS examples ,example 3,相关问题提到: ublas: Wrap ublas::vector as ublas::matrix_expression
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.