Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

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? 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}. The output will be:

A:
        0
B:
  -5.0000

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...