Though my question is similar to an earlier one posted here: Armadillo equivalent for MATLAB operations
It is slightly different and so it does not seem to work. I am solving a PDE and to iterate through time with a solution vector soln(Nr,Nt) where Nr is the spatial grid size and Nt, the number of time steps. I am using the following call
soln.col(n+1) = solve(A,B*soln.col(n));
Numerical error is introduced and we obtain small but not negligible negative numbers which are artificial. I don't want to use find(soln < 0) each pass because the matrix is Nr*Nt. I would prefer to just look at that particular column, but then the syntax find(soln.col(n+1))<0)
can not be used to adjust that columns elements. Of course I can just use a for loop each iteration and check element-wise, but I this seems like it would be slower (correct me if I am wrong).
Any help is appreciated.
The .clean() function may work for you here, available with Armadillo 9.600 or later.
soln.col(n+1).clean(datum::eps);
To clean an entire matrix:
soln.clean(datum::eps);
.clean() will zero out all elements with an absolute value <= datum::eps.
If you want to zero out all negative numbers, using the .transform() function is one possibility (requires a C++11 capable compiler):
vec tmp = solve(A,B*soln.col(n));
tmp.transform( [](double val) { return (val < 0.0) ? 0.0 : val; } );
soln.col(n+1) = tmp;
Processing each column directly may also work (haven't tested this):
soln.col(n+1).transform( [](double val) { return (val < 0.0) ? 0.0 : val; } );
The solve() function will probably take far longer to run than the post-processing anyway.