Complex scalar (TorchLean.Complex α) #
TorchLean is scalar-polymorphic, and some model components (e.g. FFT/FNO-style blocks) want a complex-valued scalar type.
Mathlib’s ℂ is specialized to ℝ and intentionally has no order instance; TorchLean’s generic
Context includes order-like operations (LT/LE, max/min) for ReLU/argmax-style code paths.
To avoid changing mathlib’s global behavior (and to support runtime-friendly backends like
ExecFloat.Binary 8 23), we provide a small parametric complex scalar:
TorchLean.Complex α := α × α with fields re and im.
Complex square roots use the principal branch. Complex logarithms retain the polar angle through
Atan2 α; a backend must supply that real-coordinate operation to obtain the complex Context.
Arithmetic operations inherit the rounding and exceptional-value behavior of the component type.
The Context instance supports explicit complex programs. For real-valued losses, the application
API autograd.complex.grad differentiates both real coordinates by forward-mode seeding; its
gradients work with nn.sgdStep, and Checkpoint.State preserves both components. The ordinary
supervised trainer still has a real-valued data and result boundary. Complex-linear reverse rules
must not be substituted for real-coordinate differentiation of a nonholomorphic loss.
Parametric complex numbers $a+ib$ over a scalar type α.
- re : α
Real part.
- im : α
Imaginary part.
Instances For
Instances For
Embed a real scalar as a complex scalar with zero imaginary part.
Instances For
Basic algebraic structure #
Division uses the standard formula $$ \frac{a+bi}{c+di}=\frac{(ac+bd)+i(bc-ad)}{c^2+d^2}. $$
Order is only used in TorchLean for branchy ops like ReLU/max/min. Complex numbers do not have a
canonical order, so we pick a simple real-part order: compare re and ignore im.
This instance is local to TorchLean’s branchy tensor operations and does not change mathlib’s ℂ.
Numeric literals and constants #
Transcendentals #
Real magnitude evaluated with scaling to avoid unnecessary overflow from squaring.
This is mathematically sqrt(re² + im²) for real coordinates; floating-point evaluation can differ
in the last bits from the unscaled expression. No IEEE complex special-value contract is asserted.