Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I work with complex numbers in Eigen (C++)?

I am successfully working with Eigen and I'm trying to understand a few details with complex numbers.

  1. How do I multiply a Matrix or Vector by a complex constant? Multiplying to complex matrices is easy; likewise with inner products of complex vectors and matrices.
  2. How do I assign a complex value to a matrix element? I've tried:

This works fine (Visual Studio)

kx.real()(0, 0) = 1.0;

This throws a compiler error

kz_r.imag()(0, ii) =1.0

The error I get is:

Severity Code Description Project File Line Suppression State Error C2440 'return': cannot convert from 'double' to 'double &' \eigen\src\core\mathfunctions.h 919

like image 674
user3533030 Avatar asked Sep 17 '25 11:09

user3533030


1 Answers

Just use std::complex<double> (or float):

std::complex<double> c(1,1);
Eigen::MatrixXd R1; R1.setRandom(2,2);
Eigen::MatrixXcd C1 = c*R1; // multiply complex*real
Eigen::MatrixXcd C2 = c*C1; // complex scalar times complex matrix
C1(0,0) = c; // assign complex value.
like image 170
chtz Avatar answered Sep 20 '25 03:09

chtz