Pal
Pal

Reputation: 1039

Efficient copying/casting of large matrix std::vector<std::vector<double>> and armadillo arma::mat

Is there an efficient copying mechanism between

            std::vector<std::vector<double>> std_mat 
            arma::mat arma_mat

where arma::mat arma_mat refers to armadillo matrix/math library.

http://arma.sourceforge.net/

My project is dependent on two separate matrix/data acquisition libraries where matrix is defined as above respectively. However, at certain stage of the processing pipeline, I need to copy one to another to avoid breaking legacy code. I'm wondering if there is some kind of casting operator from one to the other (so we don't have to copy) or if not, an efficient copying mechanism (something similar to the vector.emplace_back concept). Right now I'm using a double for loop but I believe it can be more efficient.

Upvotes: 0

Views: 627

Answers (1)

dau_sama
dau_sama

Reputation: 4357

You can probably avoid the copy by changing a bit the way you use your vectors/memory.

If you look at the documentation

mat(ptr_aux_mem, n_rows, n_cols, copy_aux_mem = true, strict = false)

Create a matrix using data from writable auxiliary (external) memory, where ptr_aux_mem is a pointer to the 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 are doing!

If you know that your matrix has a fixed sized, you could construct a mat by providing a chunk of memory. Be careful that the documentation says that:

Elements are stored with column-major ordering (ie. column by column)

Upvotes: 1

Related Questions