Error with logarithm of Eigen vector invalid using incomplete type
I am trying to calculate the natural natural logarithm of a vector using the Eigen library, here's my code:
#include <Eigen/Core>
#include <Eigen/Dense>
void function(VectorXd p, VectorXd q) {
VectorXd kld = p.cwiseQuotient(q);
kld = kld.log();
std::cout << kld << std::endl;
}
However, when compiled with
g++ -I eigen_lib -std=c++11 -march=native test_eigen.cpp -o test_eigen
I get
test_eigen.cpp:15:23: error: invalid use of incomplete type ‘const class Eigen::MatrixLogarithmReturnValue<Eigen::Matrix<double, -1, 1> >’ kld = kld.log();
What am I missing?
source to share
VectorXd::log()
is that MatrixBase<...>::log()
which calculates the matrix logarithm of a square matrix. If you want the element logarithm, you need to use the array functionality:
kld = kld.array().log();
// or:
kld = log(kld.array());
If all your operations are elementary, use ArrayXd
instead VectorXd
:
void function(const Eigen::ArrayXd& p, const Eigen::ArrayXd& q) {
Eigen::ArrayXd kld = log(p/q);
std::cout << kld << std::endl;
}
source to share
To perform rudimentary operations on your own objects ( Matrix
or Vector
), you need to specify this. This is done by adding / .array()
to the object like this:Matrix
Vector
kld = kld.array().log();
See the tutorial .
PS MatrixLogarithmReturnValue
is part of unsupported modules for matrix functions.
source to share