Sign in

    luna-complex

    Generic complex-number types and algebraic operations for LunaFlow, with floating analytic backend functions in the float_backend subpackage.

    complex-numbers
    math
    Download zip
    Author
    Version
    0.3.1
    License
    Apache-2.0
    Last updated
    6 hours ago
    Downloads
    44

    #luna-complex

    img img img

    luna-complex provides complex numbers for Luna Flow. The generic Complex[T] implements complex arithmetic and the luna-generic structure traits over any scalar type, and the float_backend package adds the analytic functions of Complex[Double] (roots, logarithms, powers, trigonometric and hyperbolic functions and their inverses) with numerically stable formulas.

    #Install

    moon add Luna-Flow/luna-complex@0.3.1

    // moon.pkg
    import {
    "Luna-Flow/luna-complex" @complex,
    "Luna-Flow/luna-complex/float_backend" @fb,
    }

    #Example

    fn main {
    let z = @complex.Complex::new(-3.0, 4.0)
    let w = @complex.Complex::new(1.0, 2.0)
    println("z * w = \{z * w}")
    println("|z| = \{@fb.abs(z)}")
    println("sqrt(z) = \{@fb.sqrt(z)}")
    }

    z * w = -11 + -2i |z| = 5 sqrt(z) = 1 + 2i

    #Packages

    PackagePurpose
    Luna-Flow/luna-complexgeneric Complex[T]: construction, in-place setters, arithmetic, conjugation, Zero … Field instances
    Luna-Flow/luna-complex/float_backendComplex[Double] analytic functions as free functions (abs, arg, div, sqrt, exp, log, pow, trigonometric and hyperbolic functions and inverses); capability traits FloatingAnalyticScalar, FloatingSpecialValues, FloatingBackendScalar

    The analytic functions are free functions such as @fb.log(z), because a package cannot add methods to a type defined in another package.

    #Known issues

    The float_backend functions have documented defects that are not fixed yet: arg returns on the negative real axis (so log(-1) is and non-integer powers of negative reals are wrong), acos and acosh are off by for some negative inputs, reciprocal functions abort near zero, and div returns NaN for subnormal divisors. The Field instance of Complex[Complex[Double]] breaks the field laws. The float_backend design and the core design list the details.

    #Toolchain

    MoonBit moonc 0.10 or newer, with moon.mod / moon.pkg manifests. Tests run on wasm-gc, wasm, js and native.

    #Documentation

    The manual, with API, tutorial and design pages for both packages and Chinese and Japanese translations, is published at https://lunaflow.cn/en/luna-complex/. Its English source is doc/manual/index.md. Changes between versions are listed in CHANGELOG.md.

    #Contributing

    See the contribution guide. Run ./ready_to_pr.sh before opening a pull request.

    #License

    Apache-2.0. See LICENSE.

    Complex

    pub(all) struct Complex[T] {
    re : T
    im : T
    } derive(Eq,
    Debug
    )

    Represents a complex number with mutable real and imaginary parts.
    impl AddGroup for Complex[T]
    impl AddMonoid for Complex[T]
    impl Conjugate for Complex[T]
    impl Field for Complex[T]
    impl MulGroup for Complex[T]
    impl MulMonoid for Complex[T]
    impl Ring for Complex[T]
    impl Semiring for Complex[T]
    impl Add for Complex[T]
    impl Mul for Complex[T]
    impl Neg for Complex[T]
    impl Show for Complex[T]
    impl Sub for Complex[T]

    Complex::add

    fn[T : Add] Complex::add(self : Complex[T], other : Complex[T]) -> Complex[T]

    Complex::conjugate

    fn[T : Neg] Complex::conjugate(self : Complex[T]) -> Complex[T]

    Complex::equal

    fn[T : Eq] Complex::equal(Complex[T], Complex[T]) -> Bool

    Complex::mul

    fn[T : Mul + Add + Sub] Complex::mul(self : Complex[T], other : Complex[T]) -> Complex[T]

    Complex::neg

    fn[T : Neg] Complex::neg(self : Complex[T]) -> Complex[T]

    Complex::new

    fn[T] Complex::new(re : T, im : T) -> Complex[T]

    Complex::set

    fn[T] Complex::set(self : Complex[T], re : T, im : T) -> Unit

    Complex::set_im

    fn[T] Complex::set_im(self : Complex[T], im : T) -> Unit

    Complex::set_re

    fn[T] Complex::set_re(self : Complex[T], re : T) -> Unit

    Complex::sub

    fn[T : Sub] Complex::sub(self : Complex[T], other : Complex[T]) -> Complex[T]

    Complex::to_string

    fn[T : Show] Complex::to_string(self : Complex[T]) -> String

    Complex::zero