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)
It's mathematically invalid to subtract a scalar (which is just a one-dimensional vector) from a vector, so Eigen correctly throws an error.
If the first operand is a scalar, each entry in the second matrix is subtracted from that scalar. If the second operand is a scalar, that scalar is subtracted from each element in the first matrix. In order to subtract a scalar r from the diagonal elements of a matrix A, use A - r*eye(size(A)).
... in Eigen, vectors are just a special case of matrices, with either 1 row or 1 column. The case where they have 1 column is the most common; such vectors are called column-vectors, often abbreviated as just vectors. In the other case where they have 1 row, they are called row-vectors.
The simplest is to move to the so called "array" world:
VECTOR_TYPE test = singular_values.array() - offset;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With