Components of Complex Matrices
The real and imaginary parts of complex matrices are also easy to access. The member functions real() and imag() return a matrix containing the real or imaginary part. For example, consider the following code:
 
#include <rw/lapack/hermmat.h>
#include <rw/lapack/symmat.h>
#include <rw/lapack/skewmat.h>
 
int main()
{
RWHermMat<DComplex> H(5, 5); // 1
RWSymMat<double> Hreal = real(H); // 2
RWSkewMat<double> Himag = imag(H); // 3
 
return 0;
}
//1 In //1 the matrix H is defined to be a 5 x 5 Hermitian matrix. A Hermitian matrix is equal to its complex conjugate transpose; this implies that its real part is symmetric, and its imaginary part is skew symmetric.
//2 Here we extract the real part of H. Note that the global function real() is an overloaded function. The compiler selects the function with prototype:
RWSymMat<double> real(const RWHermMat<DComplex>&);
Note that this function returns a symmetric matrix.
//3 Now the imaginary part of H is extracted. The function imag() has prototype:
RWSkewMat<double> imag(const RWHermMat<DComplex>&);
and returns a skew symmetric matrix, just as the mathematics implies it must.
In this example, the matrices Hreal and Himag actually refer to the data of H; that is, they present different views of the same data as H. You can use this fact to conveniently modify the real and imaginary parts of a complex matrix in the following example:
 
#include <rw/cgenmat.h>
#include <rw/dgenmat.h>
 
int main()
{
RWGenMat<DComplex> A(5,5, rwUninitialized); // 1
real(A) = 1; // 2
imag(A) = -1; // 3
 
return 0;
}
//1 This line defines a 5 x 5 general complex matrix with double precision.
//2 The real parts of the matrix are set to 1.
//3 The imaginary parts of the matrix are set to –1.