# Complex Numbers

`Complex<T>` groups real and imaginary components. The prelude representation
uses the public fields `real` and `imag`; choose an explicit numeric component
type such as `f64` for numerical work.

## Construction and components

Construct the value with a struct literal:

```vex
let point = Complex<f64> { real: 3.0, imag: 4.0 };
let real_part = point.real;
let imaginary_part = point.imag;
```

Zero and purely real or imaginary values use the same form. Do not assume
constructors such as `Complex.new`, `fromPolar`, or `fromImaginary` are present
merely because another complex-number library offers them.

## Addition, subtraction, and multiplication

The prelude defines component-wise addition and subtraction and the usual
complex product `(a + bi)(c + di) = (ac - bd) + (ad + bc)i`:

```vex [complex-product.vx]
fn main(): i32 {
    let left = Complex<f64> { real: 1.0, imag: 2.0 };
    let right = Complex<f64> { real: 3.0, imag: 4.0 };
    let sum = left + right;
    let product = left * right;
    if sum.real != 4.0 || sum.imag != 6.0 { return 1; }
    if product.real != -5.0 || product.imag != 10.0 { return 2; }
    $println("complex arithmetic ok");
    return 0;
}
```

Expected stdout is `complex arithmetic ok`, exit status 0. These particular
values have exactly representable results; general floating-point calculations
need a tolerance and an explicit numerical error policy.

Scalar addition/subtraction updates the real component while preserving the
imaginary component. The prelude also defines scalar-left multiplication for
its floating component types. Keep the scalar type compatible with the
component type:

```vex
let value = Complex<f64> { real: 3.0, imag: 4.0 };
let shifted = value + 10.0; // 13 + 4i
let scaled = 2.0 * value;   // 6 + 8i
```

## Derived operations

A small helper can express an operation whose method is not part of the
prelude surface. For moderate finite values, the squared magnitude is:

```vex
fn normSquared(value: &Complex<f64>): f64 {
    return value.real * value.real + value.imag * value.imag;
}

fn conjugate(value: &Complex<f64>): Complex<f64> {
    return Complex<f64> { real: value.real, imag: -value.imag };
}
```

The first formula can overflow or underflow for extreme components. Use a
numerically scaled algorithm when that range matters. A formula illustrating
complex arithmetic is not a substitute for a numerical library's error and
exception contracts.

Division, reciprocal, transcendental functions, polar conversion, and
precision-conversion convenience methods are not provided by the inspected
prelude declaration. This page therefore does not present `exp`, `ln`, `pow`,
`sin`, `cos`, `tan`, `as_f32`, or `as_f16` as methods you can copy into a
program. Build the required operation from validated component arithmetic or
use a library that defines it explicitly.

## Example: bounded Mandelbrot iteration

This helper uses only the component fields and the multiplication/addition
operations above:

```vex [mandelbrot.vx]
fn mandelbrotIterations(c: Complex<f64>, maxIterations: i32): i32 {
    let! z = Complex<f64> { real: 0.0, imag: 0.0 };
    for iteration in 0..maxIterations {
        let squared = z.real * z.real + z.imag * z.imag;
        if squared > 4.0 { return iteration; }
        z = z * z + c;
    }
    return maxIterations;
}
fn main(): i32 {
    let inside = Complex<f64> { real: 0.0, imag: 0.0 };
    let outside = Complex<f64> { real: 2.0, imag: 2.0 };
    if mandelbrotIterations(inside, 10) != 10 { return 1; }
    if mandelbrotIterations(outside, 10) != 1 { return 2; }
    $println("bounded iteration ok");
    return 0;
}
```

Expected stdout is `bounded iteration ok`, exit status 0. The helper assumes
finite components and a nonnegative iteration limit. Reaching the limit means
that escape was not observed within that many iterations; it is not a proof
of membership for every input.

## Ownership and arrays

`Complex<T>` follows the structural ownership of its two components. Numeric
components can copy; an owning component does not become Copy merely because
the declaration carries the Copy marker.

An array of complex values is a fixed-shape collection. Do not infer support
for every tensor operator, GPU backend, or vectorized transcendental function
from that fact. Start with scalar correctness and verify the exact operation
and target before relying on accelerated execution.

## Memory layout and FFI

`Complex<f64>`, `[f64; 2]`, and C `_Complex double` are not interchangeable
foreign-call contracts. In-memory fields and aggregate argument/return
conventions are separate. A matching byte size does not establish ABI
compatibility; use a target-specific C shim and validate both sides.

## Related pages

- [Primitive types](primitives.md)
- [Arrays](arrays.md)
- [Operator methods](../advanced/operators.md)
- [FFI](../ffi.md)

<a id="accessing-components"></a>
<a id="arithmetic-operations"></a>
<a id="scalar-operations"></a>
<a id="magnitude-and-phase"></a>
<a id="conjugate-and-reciprocal"></a>
<a id="equality-and-comparison"></a>
<a id="common-functions"></a>
<a id="conversion"></a>
<a id="use-cases"></a>
<a id="tensor-integration"></a>
<a id="memory-layout"></a>
