简体   繁体   中英

How do I calculate the exponential of a complex matrix?

I'm having trouble trying to calculate the exponential of a complex matrix with the C++ Eigen library.

Below is an example code I try to make work.

#include <iostream>
#include "Dense"
#include <complex>
#include "unsupported/Eigen/src/MatrixFunctions/MatrixExponential.h"

int main()
{
    using namespace std::complex_literals;

    Eigen::MatrixXcd test(2,2);
    test(0,0)=1i+std::complex<double>(5);
    test(1,0)=1i*2.;
    test(0,1)=std::complex<double>(2);
    test(1,1)=3.*1i+std::complex<double>(3);

    std::cout << "The matrix exponential is:\n"
              << test.exp() << "\n\n";
}

When I run this program I get the error:

Implicit instantiation of undefined template 'Eigen::MatrixFunctionReturnValue<Eigen::Matrix<std::__1::complex<double>, -1, -1, 0, -1, -1> >'

I have tried to find an answer but I haven't found one yet.

Any help would be greatly appreciated.

Edit:

The standard matrix operations in Eigen work and the Eigen file/folder are located in my project folder. The only functions that don't seem to work are the matrix functions in the unsupported folder for complex matrixes (they do work for real ones).

You must not directly include headers from the Eigen/src or unsupported/Eigen/src subdirectories. Also, instead of #include "Dense" use #include <Eigen/Dense> (in many cases <Eigen/Core> is actually sufficient).

In your case you actually just need these includes, because all necessary dependencies are included by MatrixFunctions :

#include <iostream>
#include <unsupported/Eigen/MatrixFunctions>

Godbolt-Demo: https://godbolt.org/z/PmJWP3 (compilation may occasionally time out).

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