How to calculate the absolute value of a vector in Eigen?

How to calculate the absolute value of a vector in Eigen? Since the obvious way

Eigen::VectorXf v(-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0); v.abs(); // Compute abs value. 

does not work.

+6
source share
1 answer

For Eigen 3.2.1 using p.abs(); just as you would use p.normalize , there is a compiler error in the lines

error: no member named 'abs' in 'Eigen :: Matrix' p.abs (); ~ ^

therefore, a vector in Eigen is nothing more than a matrix type. To calculate the absolute values โ€‹โ€‹of the matrix in Eigen, you can use the p.cwiseAbs() or p.array().abs(); array p.array().abs(); . Both of these absolute functions return a value, not the variable itself.

So the right way to do this is to do

 p = p.cwiseAbs(); 

or

 p = p.array().abs(); 
+13
source

Source: https://habr.com/ru/post/973898/


All Articles