简体   繁体   English

犰狳C ++:数组的矩阵初始化

[英]armadillo C++: matrix initialization from array

I am new to using armadillo, and could not get the following in spite of trying / searching quite a bit. 我是不熟悉犰狳的新手,尽管尝试/搜索了很多内容,但还是无法获得以下内容。

There are two huge (dynamic) arrays (not vectors) that I need to perform correlation on. 我需要对两个巨大的(动态)数组(而不是向量)进行关联。 I resolved to use armadillo for this. 我决定为此使用犰狳。 I understand how to initialize arma::mat using vectors, but can I use arrays to do so? 我了解如何使用向量初始化arma :: mat,但可以使用数组来初始化吗? I understand not as I don't see any mention in the documentation . 我不了解,因为我没有在文档中看到任何提及。 I am trying to avoid the use of vectors for internal design reasons. 由于内部设计原因,我试图避免使用向量。 I tried manually initializing each element using sample arrays (as a dumb but starting point). 我尝试使用示例数组(作为愚蠢的起点)手动初始化每个元素。 Something like the following code wouldn't work. 类似于以下代码的代码将无法正常工作。

using namespace std;
using namespace arma;   

mat A(SIZE, 1), B(SIZE, 1);

for(int i = 0; i < SIZE; i++)
{
    A << v[i] << endr;
    B << c[i] << endr;
}

cout << "A: " << endl;
A.print();
cout << "B: " << endl;
B.print();

For the input arrays v = {1, 2, 0, -1, .9} and c = {0, .5, 1, -2, -5}. 对于输入数组v = {1,2,0,-1,.9}和c = {0,.5,1,-2,-5}。 The output will be: 输出将是:

A:
        0
B:
  -5.0000

which is understandable. 这是可以理解的。 Any work around for initializing arma::mat or arma::colvector with arrays? 用数组初始化arma :: mat或arma :: colvector的方法是否可行? Thanks in advance! 提前致谢!

Assuming that your arrays v and c are double arrays, you can just use the aux memory constructors: 假设数组v和c是双精度数组,则可以只使用aux内存构造函数:

From the armadillo doc : 犰狳doc中

  • mat(aux_mem*, n_rows, n_cols, copy_aux_mem = true, strict = true) mat(aux_mem *,n_rows,n_cols,copy_aux_mem = true,strict = true)

    Create a matrix using data from writeable auxiliary memory. 使用可写辅助存储器中的数据创建矩阵。 By default the matrix allocates its own memory and copies data from the auxiliary memory (for safety). 默认情况下,矩阵会分配自己的内存并从辅助内存中复制数据(出于安全考虑)。 However, if copy_aux_mem is set to false, the matrix will instead directly use the auxiliary memory (ie. no copying). 但是,如果将copy_aux_mem设置为false,则矩阵将直接使用辅助存储器(即不进行复制)。 This is faster, but can be dangerous unless you know what you're doing! 这样比较快,但是除非您知道自己在做什么,否则可能很危险!

The strict variable comes into effect only if copy_aux_mem is set to false (ie. the matrix is directly using auxiliary memory). 仅当copy_aux_mem设置为false时,严格变量才有效(即,矩阵直接使用辅助存储器)。 If strict is set to true, the matrix will be bound to the auxiliary memory for its lifetime; 如果strict设置为true,则矩阵将在其生命周期内绑定到辅助内存; the number of elements in the matrix can't be changed (directly or indirectly). 矩阵中元素的数量不能更改(直接或间接)。 If strict is set to false, the matrix will not be bound to the auxiliary memory for its lifetime, ie., the size of the matrix can be changed. 如果strict设置为false,则矩阵在其生命周期内将不会绑定到辅助存储器,即,可以更改矩阵的大小。 If the requested number of elements is different to the size of the auxiliary memory, new memory will be allocated and the auxiliary memory will no longer be used. 如果请求的元素数量与辅助存储器的大小不同,则将分配新的存储器,并且将不再使用辅助存储器。

  • mat(const aux_mem*, n_rows, n_cols) 垫(const aux_mem *,n_rows,n_cols)

Create a matrix by copying data from read-only auxiliary memory. 通过从只读辅助存储器中复制数据来创建矩阵。

Which means you can create your matrixes by copying your source data like this: 这意味着您可以通过复制源数据来创建矩阵,如下所示:

mat A_Copy(v, SIZE, 1);
mat B_Copy(c, SIZE, 1);

Or you can actually reuse the memory you already have allocated for your arrays to create read-only matrixes, like this: 或者,您实际上可以重用已经为数组分配的内存,以创建只读矩阵,如下所示:

mat A_InPlace(v, SIZE, 1, /*copy_aux_mem*/false, /*strict*/true);
mat B_InPlace(c, SIZE, 1, /*copy_aux_mem*/false, /*strict*/true);

This gets even simpler if you use vectors 如果使用向量,这将变得更加简单

vec A_Vec_Copy(v, SIZE);
vec B_Vec_Copy(c, SIZE);

Or: 要么:

vec A_Vec_InPlace(v, SIZE, false, true);
vec B_Vec_InPlace(c, SIZE, false, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM