armadillo C++:从数组初始化矩阵

2022-01-19 00:00:00 matrix arrays initialization c++ armadillo

我是使用犰狳的新手,尽管尝试/搜索了很多,但无法获得以下内容.

I am new to using armadillo, and could not get the following in spite of trying / searching quite a bit.

我需要对两个巨大的(动态)数组(不是向量)执行关联.我决定为此使用犰狳.我了解如何使用向量初始化 arma::mat,但我可以使用数组来这样做吗?我不明白,因为我在 文档 中没有看到任何提及.出于内部设计原因,我试图避免使用向量.我尝试使用示例数组手动初始化每个元素(作为一个愚蠢但起点).类似下面的代码是行不通的.

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? 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();

对于输入数组 v = {1, 2, 0, -1, .9} 和 c = {0, .5, 1, -2, -5}.输出将是:

For the input arrays v = {1, 2, 0, -1, .9} and c = {0, .5, 1, -2, -5}. The output will be:

A:
        0
B:
  -5.0000

这是可以理解的.使用数组初始化 arma::mat 或 arma::colvector 的任何解决方法?提前致谢!

which is understandable. Any work around for initializing arma::mat or arma::colvector with arrays? Thanks in advance!

推荐答案

假设你的数组 v 和 c 是双数组,你可以只使用辅助内存构造函数:

Assuming that your arrays v and c are double arrays, you can just use the aux memory constructors:

来自犰狳文档:

  • 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)

使用可写辅助存储器中的数据创建矩阵.默认情况下,矩阵分配自己的内存并从辅助内存中复制数据(为了安全).但是,如果 copy_aux_mem 设置为 false,矩阵将改为直接使用辅助内存(即不复制).这更快,但除非您知道自己在做什么,否则可能很危险!

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). This is faster, but can be dangerous unless you know what you're doing!

仅当 copy_aux_mem 设置为 false(即矩阵直接使用辅助内存)时,严格变量才会生效.如果 strict 设置为 true,则矩阵将在其生命周期内绑定到辅助内存;矩阵中元素的数量不能(直接或间接)改变.如果 strict 设置为 false,则矩阵在其生命周期内不会绑定到辅助存储器,即可以更改矩阵的大小.如果请求的元素数量与辅助内存的大小不同,则会分配新的内存,辅助内存将不再使用.

The strict variable comes into effect only if copy_aux_mem is set to false (ie. the matrix is directly using auxiliary memory). If strict is set to true, the matrix will be bound to the auxiliary memory for its lifetime; 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. 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)

通过从只读辅助存储器复制数据来创建矩阵.

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);

或者:

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

相关文章