ClonedOne
ClonedOne

Reputation: 599

Error with Eigen vector logarithm invalid use of incomplete type

I am trying to compute the element-wise 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 compiling 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 is it that I'm missing?

Upvotes: 3

Views: 2406

Answers (2)

chtz
chtz

Reputation: 18827

VectorXd::log() is MatrixBase<...>::log() which computes the matrix logarithm of a square matrix. If you want the element-wise logarithm you need to use the array functionality:

kld = kld.array().log();
// or:
kld = log(kld.array());

If all your operations are element-wise, consider using ArrayXd instead of VectorXd:

void function(const Eigen::ArrayXd& p, const Eigen::ArrayXd& q) {
    Eigen::ArrayXd kld = log(p/q);
    std::cout << kld << std::endl;
}

Upvotes: 5

Avi Ginsburg
Avi Ginsburg

Reputation: 10596

To do element-wise operations on Eigen objects (Matrix or Vector), you need to specify that. This is done by by adding .array() to the Matrix/Vector object like so:

kld = kld.array().log();

See this tutorial.

P.S. MatrixLogarithmReturnValue is part of the unsupported modules for matrix functions.

Upvotes: 4

Related Questions