Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eigen: Subtracting a scalar from a vector

Tags:

c++

eigen

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)

like image 768
Luca Avatar asked Feb 28 '16 21:02

Luca


People also ask

Can I subtract a scalar from a vector?

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

How do you subtract a scalar from a matrix?

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)).

What is Eigen column vector?

... 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.


1 Answers

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

VECTOR_TYPE test = singular_values.array() - offset;
like image 128
ggael Avatar answered Oct 19 '22 22:10

ggael