Working with Complex Numbers (TCplx)

TCplx is a struct representing a double-precision complex number. It has no public constructor.

Creating complex values:

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

uses Math387;

var z: TCplx;
    zs: TSCplx;
begin
    z := Cplx(3.0, 4.0);          // 3 + 4i (Cplx is a free function)
    zs := CplxSingle(3.0, 4.0);   // single-precision variant
end;

Accessing real and imaginary parts:

var z: TCplx;
begin
    z := Cplx(3.0, 4.0);
    // z.Re = 3.0, z.Im = 4.0
end;

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:

var v: Vector;
    sum: TCplx;
begin
    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
    v.Sum(sum);                  // complex sum
end;

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.

Common mistake: Do not use new TCplx(...) or default(TCplx) — always use Cplx(re, im).