eigen: Subtracting a scalar from a vector

Luca picture Luca · Feb 28, 2016 · Viewed 12.8k times · Source

I am having an error when using the Eigen library and all I am trying to do is subtract a scalar from an Eigen::VectorXf. So, my code is something as follows:

#define VECTOR_TYPE Eigen::VectorXf
#define MATRIX_TYPE Eigen::MatrixXf

// myMat is of MATRIX_TYPE
JacobiSVD<MATRIX_TYPE> jacobi_svd(myMat,ComputeThinU | ComputeThinV); 

const float offset = 3.0f;
VECTOR_TYPE singular_values = jacobi_svd.singularValues();

VECTOR_TYPE test = singular_values - offset;

The last line results in a compilation error as:

error: invalid operands to binary expression ('Eigen::VectorXf' (aka 'Matrix') and 'float') VECTOR_TYPE test = singular_values - scale;

Eigen/src/Core/../plugins/CommonCwiseBinaryOps.h:19:28: note: candidate template ignored: could not match 'MatrixBase' against 'float' EIGEN_MAKE_CWISE_BINARY_OP(operator-,internal::scalar_difference_op)

Answer

ggael picture ggael · Feb 29, 2016

The simplest is to move to the so called "array" world:

VECTOR_TYPE test = singular_values.array() - offset;