Working with Complex Numbers (TCplx)

In C++, TCplx is a struct with public Re and Im fields and a public constructor, so TCplx(3.0, 4.0) and Cplx(3.0, 4.0) build the same value, and TCplx z{}; is 0 + 0i. Math387.h also declares the arithmetic operators +, -, *, / and the comparisons ==, !=, <, <=, >, >=, between two TCplx values and between a TCplx and a double. == treats two NaN parts as equal, and the ordering compares the magnitude first, then the phase.

Creating complex values:

Use the factory function Cplx(re, im) from Math387:

#include "Units.Math387.h"

using namespace Dew::Math;          // TCplx, TSCplx
using namespace Dew::Math::Units;

void ComplexExample()
{
    // Option 1: qualified call
    TCplx z1 = Math387::Cplx(3.0, 4.0);   // 3 + 4i

    // Option 2: with 'using namespace' for unqualified calls
    using namespace Dew::Math::Units::Math387;
    TCplx z2 = Cplx(3.0, 4.0);            // 3 + 4i

    // Option 3: TCplx has a public constructor in C++
    TCplx z3(3.0, 4.0);                   // 3 + 4i

    // Single-precision variant (returns TSCplx)
    TSCplx zs = CplxSingle(3.0f, 4.0f);
}

Accessing real and imaginary parts:

TCplx z = Cplx(3.0, 4.0);
double re = z.Re;   // 3.0
double im = z.Im;   // 4.0

Predefined constants (in Math387):

ConstantValueDescription
C_I0 + 1iImaginary unit
C_I_SINGLE0 + 1i (single)Single-precision imaginary unit
CINFcomplex infinityComplex version of INF
CNANcomplex NaNComplex version of NAN

Complex vector operations:

Vectors can hold complex data. Use ExtendToComplex() to convert a real vector to complex, or pass complex scalars to arithmetic methods:

Vector v;
v.Size(10, false, true);
v.SetVal(1.0);
v.ExtendToComplex();         // now complex: each element is 1+0i
v.Mul(Cplx(0, 1.0));         // multiply by i: each element becomes 0+1i
TCplx sum = Cplx(0, 0);
v.Sum(sum);                  // complex sum

Overload selection: Methods accepting scalar arguments (Add, Mul, Sub, etc.) have both Double and TCplx overloads. The compiler selects based on the argument type — pass Cplx(re, im) for complex operations, or a plain double for real operations.

In C++, hold complex values by value — TCplx z = Cplx(1, 2); — and do not write new TCplx(...), which allocates on the heap and returns a pointer.