I am new to C++ and am trying to translate the following MATLAB code into C++:
x = [1,2,3];
y = [4,5,7];
px = 0.5;
py = 0.5;
m = -1:1:1;
v = [1+i,2+i,3+i;1+2i,2+2i,3+2i;1+3i,2+3i,3+3i];
Tr5 = real(exp(2i*pi/px*x(1)*m)*v*exp(2i*pi/py*m'*y(1)));
fprintf('Tr5 = %f\n',Tr5);
which returns Tr5=18.
Here is my attempt in C++, where I have done as little as possible on each line to try and hunt down the bug:
#include <Eigen/Dense>
#include <unsupported/Eigen/MatrixFunctions>
#include <math.h>
#include <iterator>
#include <vector>
#include <algorithm>
#include <complex>
#include <iostream>
int main()
{
double x [3] = {1, 2, 3};
double y [3] = {4, 5, 7};
const std::complex<double> im(0.0, 1.0);
double px = 0.5;
double py = 0.5;
Eigen::MatrixXcd Tr1;
Eigen::MatrixXcd Tr2;
Eigen::MatrixXcd Tr3;
Eigen::MatrixXcd Tr4;
Eigen::MatrixXd m = Eigen::VectorXd::LinSpaced(2*1+1, -1, 1);
Eigen::MatrixXcd v4(3,3);
v4(0,0) = 1.0d + 1.0 * im;
v4(0,1) = 2.0d + 1.0 * im;
v4(0,2) = 3.0d + 1.0 * im;
v4(1,0) = 1.0d + 2.0 * im;
v4(1,1) = 2.0d + 2.0 * im;
v4(1,2) = 3.0d + 2.0 * im;
v4(2,0) = 1.0d + 3.0 * im;
v4(2,1) = 2.0d + 3.0 * im;
v4(2,2) = 3.0d + 3.0 * im;
Tr1 = 2.0*im*M_PI/px*x[1]*m;
Tr1 = Tr1.exp();
Tr2 = 2.0*im*M_PI/py*y[1]*m;
Tr2 = Tr2.exp();
Tr3 = Tr2.transpose();
Tr4 = Tr1 * v4 * Tr3;
Eigen::MatrixXcd Tr5 = Tr4.real(); // This should now be "18"
std::cout << "Tr5 = " << Tr5 << std::endl;
}
However, when I try to compile and run, I get the error:
/usr/local/include/unsupported/Eigen/src/MatrixFunctions/MatrixExponential.h:436: const Eigen::MatrixExponentialReturnValue<Derived> Eigen::MatrixBase<Derived>::exp() const [with Derived = Eigen::Matrix<std::complex<double>, -1, -1>]: Assertion `rows() == cols()' failed.
Could somebody please help me with the code?
Many thanks, Tom Waits