简体   繁体   中英

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?

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 .

PS MatrixLogarithmReturnValue is part of the unsupported modules for matrix functions.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM