armadillo C++: matrix initialization from array

寵の児 提交于 2019-11-29 05:56:26

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

From the armadillo doc:

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

Or:

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