Eigen: subtracting a scalar from a vector

I have an error when using the Eigen library, and all I am trying to do is subtract the scalar from Eigen :: VectorXf. So my code is 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 leads to a compilation error like:

error: invalid operands in 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 is ignored: could not match 'MatrixBase' with 'float' EIGEN_MAKE_CWISE_BINARY_OP (operator-, internal :: scalar_difference_op)

+5
source share
3 answers

, ( ), Eigen .

auto n = singular_values.size();
VECTOR_TYPE test = singular_values - offset * VECTOR_TYPE::Ones(n);

, array(), .

+11

"array" :

VECTOR_TYPE test = singular_values.array() - offset;
+14

If I'm not mistaken, you can also use the broadcast operation:

VECTOR_TYPE test = singular_values.rowwise() - offset;
+1
source

All Articles