Armadillo, how to grow a vector and get his size?
In my last question, Ilmari Karonen suggested that I solve the Laplace equation in discrete mode. I use the grid and four nearest neighbors at each node to calculate the unknown height.
So the system that requires a solution can be written as a matrix:
A(matrix nxn) * U(vector n, unknown) = b(vector n, settled).
Last night I browsed online about C++ linear algebra. My choice is Armadillo
std::map<std::pair<float,float>,float> b;
std::map<std::pair<float,float>,float> U;
.
.
.
if(g/*grid*/->getNode(r,c)->status == TO_COMPUTE){
U.insert(std::make_pair(std::make_pair(r,c),g->getNode(r,c)->y));
/*right*/ if(g->getNode(r+1,c)->status == SETTLED) b.at(std::make_pair(r,c)) += g >getNode(r+1,c)->y;
/*left */ if(g->getNode(r-1,c)->status == SETTLED) b.at(std::make_pair(r,c)) += g->getNode(r-1,c)->y;
/*down */ if(g->getNode(r,c+1)->status == SETTLED) b.at(std::make_pair(r,c)) += g->getNode(r,c+1)->y;
/*up */ if(g->getNode(r,c-1)->status == SETTLED) b.at(std::make_pair(r,c)) += g->getNode(r,c-1)->y;
}
Then I guess I have to get the size of “U” to create arma::vec and use std::iterator
to parse the entire map to transfer the value in my arma::vec
. So I’m wondering how to grow
arma::vec
I’m looking for something like std::vector::p ush_back()
and then I’ll replace my std::map
. Also, How do I get the size of arma::vec
?
Note 1: The picture shows the outline and the point where I will calculate the value, the grid of unknown values is at y=0, the current outline is at y=-0.2 and the upper outline is at y=0.8
Note 2: WHEN MY SMALL (LINUX) PROGRAM IS READY, I WILL POST THE CODE ON MY BITBUCKET AS A VERY SMALL EXAMPLE OF USING THE DISCRETE LAPLACE OPERATOR
Update
November 26, 2013:
You can get a link to the code here
Solution
Check out Armadillo’s documentation X.n_elem gets the length of vector X. To resize the vector while preserving the data, use .resize(). .insert_rows() or .insert_cols() .
Note that resizing objects in performance-critical loops, whether Armadillo matrix/vector or std::vector, is inefficient. It is better to calculate the correct size beforehand.