eigen: Subtracting a scalar from a vector
Asked Answered
S

3

13

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)

Standoff answered 28/2, 2016 at 21:58 Comment(0)
F
27

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

VECTOR_TYPE test = singular_values.array() - offset;
Fugazy answered 29/2, 2016 at 6:50 Comment(1)
This should be the accepted answer. Creating a vector of ones has a runtime cost, while the conversion to array doesn't.Reiterate
C
13

It's mathematically invalid to subtract a scalar (which is just a one-dimensional vector) from a vector, so Eigen correctly throws an error.

Instead, you should write

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

Moreover, you can have a look at the array() functionality which provides element-wise transformations.

Chandos answered 28/2, 2016 at 22:6 Comment(1)
You should make your answer only about the conversion to an array. Creating a vector of ones and multiplying is unnecessarily complex and has a runtime cost, while the conversion to array doesn't: eigen.tuxfamily.org/dox/group__TutorialArrayClass.html#title6Reiterate
T
-1

If I am not mistaken you can also use a broadcasting operation:

VECTOR_TYPE test = singular_values.rowwise() - offset;
Tarttan answered 31/1, 2019 at 9:15 Comment(1)
I agree with raaj. You cannot broadcast between a vector and scalar like that. @Fugazy has the best answer, using array().Bentham

© 2022 - 2024 — McMap. All rights reserved.