TorchLean API

NN.Spec.Core.Complex

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 IEEE32Exec), we provide a small parametric complex scalar:

TorchLean.Complex α := α × α with fields re and im.

The provided Context instance is designed to be:

This is not meant to be a full replacement for complex analysis; for deep analytic theorems, use mathlib’s directly.

structure TorchLean.Complex (α : Type) :

Parametric complex numbers a + i·b over a scalar type α.

  • re : α

    Real part.

  • im : α

    Imaginary part.

Instances For
    @[implicit_reducible]
    instance TorchLean.instReprComplex {α✝ : Type} [Repr α✝] :
    Repr (Complex α✝)
    def TorchLean.instReprComplex.repr {α✝ : Type} [Repr α✝] :
    Complex α✝Std.Format
    Instances For
      def TorchLean.Complex.ofReal {α : Type} [Zero α] (a : α) :

      Embed a real scalar as a complex scalar with zero imaginary part.

      Instances For
        @[simp]
        theorem TorchLean.Complex.re_ofReal {α : Type} [Zero α] (a : α) :
        (ofReal a).re = a
        @[simp]
        theorem TorchLean.Complex.im_ofReal {α : Type} [Zero α] (a : α) :
        (ofReal a).im = 0
        def TorchLean.Complex.I {α : Type} [Zero α] [One α] :

        Imaginary unit i.

        Instances For
          @[simp]
          theorem TorchLean.Complex.re_I {α : Type} [Zero α] [One α] :
          I.re = 0
          @[simp]
          theorem TorchLean.Complex.im_I {α : Type} [Zero α] [One α] :
          I.im = 1

          Basic algebraic structure #

          @[implicit_reducible]
          @[implicit_reducible]
          instance TorchLean.Complex.instZero {α : Type} [Zero α] :
          @[implicit_reducible]
          instance TorchLean.Complex.instOneOfZero {α : Type} [One α] [Zero α] :
          @[implicit_reducible]
          instance TorchLean.Complex.instNeg {α : Type} [Neg α] :
          @[implicit_reducible]
          instance TorchLean.Complex.instAdd {α : Type} [Add α] :
          @[implicit_reducible]
          instance TorchLean.Complex.instSub {α : Type} [Sub α] :
          @[implicit_reducible]
          instance TorchLean.Complex.instMulOfAddOfSub {α : Type} [Mul α] [Add α] [Sub α] :

          Complex multiplication.

          Division uses the standard formula (a+bi)/(c+di) = ((ac+bd) + i(bc-ad)) / (c^2 + d^2).

          @[implicit_reducible]
          instance TorchLean.Complex.instDivOfMulOfAddOfSub {α : Type} [Mul α] [Add α] [Sub α] [Div α] :
          @[implicit_reducible]
          instance TorchLean.Complex.instBEq {α : Type} [BEq α] :

          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 keeps the instance lightweight and avoids polluting mathlib’s with ad-hoc orderings.

          @[implicit_reducible]
          instance TorchLean.Complex.instLT {α : Type} [LT α] :
          LT (Complex α)
          @[implicit_reducible]
          instance TorchLean.Complex.instLE {α : Type} [LE α] :
          LE (Complex α)
          @[implicit_reducible]

          Numeric literals and constants #

          @[implicit_reducible]
          @[implicit_reducible]

          Transcendentals #

          def TorchLean.Complex.Internal.normSq {α : Type} [Mul α] [Add α] (z : Complex α) :
          α

          Squared magnitude re^2 + im^2 (helper for abs/log).

          Instances For

            Real magnitude sqrt(normSq z).

            Instances For

              max/min and Context #

              @[implicit_reducible]
              @[implicit_reducible]
              @[implicit_reducible]
              @[implicit_reducible]

              Lift a scalar Context to TorchLean complex scalars.