diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/lux/data/number/complex.lux | 173 |
1 files changed, 87 insertions, 86 deletions
diff --git a/stdlib/source/lux/data/number/complex.lux b/stdlib/source/lux/data/number/complex.lux index 8b9be426f..ec4b27326 100644 --- a/stdlib/source/lux/data/number/complex.lux +++ b/stdlib/source/lux/data/number/complex.lux @@ -11,7 +11,8 @@ [data ["." maybe] [number - ["." frac]] + ["." int] + ["f" frac]] ["." text ("#;." monoid)] [collection ["." list ("#;." functor)]]] @@ -39,14 +40,14 @@ (def: #export zero Complex (complex +0.0 +0.0)) (def: #export (not-a-number? complex) - (or (frac.not-a-number? (get@ #real complex)) - (frac.not-a-number? (get@ #imaginary complex)))) + (or (f.not-a-number? (get@ #real complex)) + (f.not-a-number? (get@ #imaginary complex)))) (def: #export (= param input) (-> Complex Complex Bit) - (and (f/= (get@ #real param) + (and (f.= (get@ #real param) (get@ #real input)) - (f/= (get@ #imaginary param) + (f.= (get@ #imaginary param) (get@ #imaginary input)))) (template [<name> <op>] @@ -57,8 +58,8 @@ #imaginary (<op> (get@ #imaginary param) (get@ #imaginary input))})] - [+ f/+] - [- f/-] + [+ f.+] + [- f.-] ) (structure: #export equivalence (Equivalence Complex) @@ -70,51 +71,51 @@ (|>> (update@ #real <transform>) (update@ #imaginary <transform>)))] - [negate frac.negate] - [signum frac.signum] + [negate f.negate] + [signum f.signum] ) (def: #export conjugate (-> Complex Complex) - (update@ #imaginary frac.negate)) + (update@ #imaginary f.negate)) (def: #export (*' param input) (-> Frac Complex Complex) - {#real (f/* param + {#real (f.* param (get@ #real input)) - #imaginary (f/* param + #imaginary (f.* param (get@ #imaginary input))}) (def: #export (* param input) (-> Complex Complex Complex) - {#real (f/- (f/* (get@ #imaginary param) + {#real (f.- (f.* (get@ #imaginary param) (get@ #imaginary input)) - (f/* (get@ #real param) + (f.* (get@ #real param) (get@ #real input))) - #imaginary (f/+ (f/* (get@ #real param) + #imaginary (f.+ (f.* (get@ #real param) (get@ #imaginary input)) - (f/* (get@ #imaginary param) + (f.* (get@ #imaginary param) (get@ #real input)))}) (def: #export (/ param input) (-> Complex Complex Complex) (let [(^slots [#real #imaginary]) param] - (if (f/< (frac.abs imaginary) - (frac.abs real)) - (let [quot (f// imaginary real) - denom (|> real (f/* quot) (f/+ imaginary))] - {#real (|> (get@ #real input) (f/* quot) (f/+ (get@ #imaginary input)) (f// denom)) - #imaginary (|> (get@ #imaginary input) (f/* quot) (f/- (get@ #real input)) (f// denom))}) - (let [quot (f// real imaginary) - denom (|> imaginary (f/* quot) (f/+ real))] - {#real (|> (get@ #imaginary input) (f/* quot) (f/+ (get@ #real input)) (f// denom)) - #imaginary (|> (get@ #imaginary input) (f/- (f/* quot (get@ #real input))) (f// denom))})))) + (if (f.< (f.abs imaginary) + (f.abs real)) + (let [quot (f./ imaginary real) + denom (|> real (f.* quot) (f.+ imaginary))] + {#real (|> (get@ #real input) (f.* quot) (f.+ (get@ #imaginary input)) (f./ denom)) + #imaginary (|> (get@ #imaginary input) (f.* quot) (f.- (get@ #real input)) (f./ denom))}) + (let [quot (f./ real imaginary) + denom (|> imaginary (f.* quot) (f.+ real))] + {#real (|> (get@ #imaginary input) (f.* quot) (f.+ (get@ #real input)) (f./ denom)) + #imaginary (|> (get@ #imaginary input) (f.- (f.* quot (get@ #real input))) (f./ denom))})))) (def: #export (/' param subject) (-> Frac Complex Complex) (let [(^slots [#real #imaginary]) subject] - {#real (f// param real) - #imaginary (f// param imaginary)})) + {#real (f./ param real) + #imaginary (f./ param imaginary)})) (def: #export (% param input) (-> Complex Complex Complex) @@ -128,76 +129,76 @@ (def: #export (cos subject) (-> Complex Complex) (let [(^slots [#real #imaginary]) subject] - {#real (f/* (math.cosh imaginary) + {#real (f.* (math.cosh imaginary) (math.cos real)) - #imaginary (frac.negate (f/* (math.sinh imaginary) - (math.sin real)))})) + #imaginary (f.negate (f.* (math.sinh imaginary) + (math.sin real)))})) (def: #export (cosh subject) (-> Complex Complex) (let [(^slots [#real #imaginary]) subject] - {#real (f/* (math.cos imaginary) + {#real (f.* (math.cos imaginary) (math.cosh real)) - #imaginary (f/* (math.sin imaginary) + #imaginary (f.* (math.sin imaginary) (math.sinh real))})) (def: #export (sin subject) (-> Complex Complex) (let [(^slots [#real #imaginary]) subject] - {#real (f/* (math.cosh imaginary) + {#real (f.* (math.cosh imaginary) (math.sin real)) - #imaginary (f/* (math.sinh imaginary) + #imaginary (f.* (math.sinh imaginary) (math.cos real))})) (def: #export (sinh subject) (-> Complex Complex) (let [(^slots [#real #imaginary]) subject] - {#real (f/* (math.cos imaginary) + {#real (f.* (math.cos imaginary) (math.sinh real)) - #imaginary (f/* (math.sin imaginary) + #imaginary (f.* (math.sin imaginary) (math.cosh real))})) (def: #export (tan subject) (-> Complex Complex) (let [(^slots [#real #imaginary]) subject - r2 (f/* +2.0 real) - i2 (f/* +2.0 imaginary) - d (f/+ (math.cos r2) (math.cosh i2))] - {#real (f// d (math.sin r2)) - #imaginary (f// d (math.sinh i2))})) + r2 (f.* +2.0 real) + i2 (f.* +2.0 imaginary) + d (f.+ (math.cos r2) (math.cosh i2))] + {#real (f./ d (math.sin r2)) + #imaginary (f./ d (math.sinh i2))})) (def: #export (tanh subject) (-> Complex Complex) (let [(^slots [#real #imaginary]) subject - r2 (f/* +2.0 real) - i2 (f/* +2.0 imaginary) - d (f/+ (math.cosh r2) (math.cos i2))] - {#real (f// d (math.sinh r2)) - #imaginary (f// d (math.sin i2))})) + r2 (f.* +2.0 real) + i2 (f.* +2.0 imaginary) + d (f.+ (math.cosh r2) (math.cos i2))] + {#real (f./ d (math.sinh r2)) + #imaginary (f./ d (math.sin i2))})) (def: #export (abs subject) (-> Complex Complex) (let [(^slots [#real #imaginary]) subject] - (complex (if (f/< (frac.abs imaginary) - (frac.abs real)) - (if (f/= +0.0 imaginary) - (frac.abs real) - (let [q (f// imaginary real)] - (f/* (math.pow +0.5 (f/+ +1.0 (f/* q q))) - (frac.abs imaginary)))) - (if (f/= +0.0 real) - (frac.abs imaginary) - (let [q (f// real imaginary)] - (f/* (math.pow +0.5 (f/+ +1.0 (f/* q q))) - (frac.abs real)))) + (complex (if (f.< (f.abs imaginary) + (f.abs real)) + (if (f.= +0.0 imaginary) + (f.abs real) + (let [q (f./ imaginary real)] + (f.* (math.pow +0.5 (f.+ +1.0 (f.* q q))) + (f.abs imaginary)))) + (if (f.= +0.0 real) + (f.abs imaginary) + (let [q (f./ real imaginary)] + (f.* (math.pow +0.5 (f.+ +1.0 (f.* q q))) + (f.abs real)))) )))) (def: #export (exp subject) (-> Complex Complex) (let [(^slots [#real #imaginary]) subject r-exp (math.exp real)] - {#real (f/* r-exp (math.cos imaginary)) - #imaginary (f/* r-exp (math.sin imaginary))})) + {#real (f.* r-exp (math.cos imaginary)) + #imaginary (f.* r-exp (math.sin imaginary))})) (def: #export (log subject) (-> Complex Complex) @@ -216,18 +217,18 @@ (def: (copy-sign sign magnitude) (-> Frac Frac Frac) - (f/* (frac.signum sign) magnitude)) + (f.* (f.signum sign) magnitude)) (def: #export (root2 (^@ input (^slots [#real #imaginary]))) (-> Complex Complex) - (let [t (|> input ..abs (get@ #real) (f/+ (frac.abs real)) (f// +2.0) (math.pow +0.5))] - (if (f/>= +0.0 real) + (let [t (|> input ..abs (get@ #real) (f.+ (f.abs real)) (f./ +2.0) (math.pow +0.5))] + (if (f.>= +0.0 real) {#real t - #imaginary (f// (f/* +2.0 t) + #imaginary (f./ (f.* +2.0 t) imaginary)} - {#real (f// (f/* +2.0 t) - (frac.abs imaginary)) - #imaginary (f/* t (copy-sign imaginary +1.0))}))) + {#real (f./ (f.* +2.0 t) + (f.abs imaginary)) + #imaginary (f.* t (copy-sign imaginary +1.0))}))) (def: #export (root2-1z input) (-> Complex Complex) @@ -235,18 +236,18 @@ (def: #export (reciprocal (^slots [#real #imaginary])) (-> Complex Complex) - (if (f/< (frac.abs imaginary) - (frac.abs real)) - (let [q (f// imaginary real) - scale (f// (|> real (f/* q) (f/+ imaginary)) + (if (f.< (f.abs imaginary) + (f.abs real)) + (let [q (f./ imaginary real) + scale (f./ (|> real (f.* q) (f.+ imaginary)) +1.0)] - {#real (f/* q scale) - #imaginary (frac.negate scale)}) - (let [q (f// real imaginary) - scale (f// (|> imaginary (f/* q) (f/+ real)) + {#real (f.* q scale) + #imaginary (f.negate scale)}) + (let [q (f./ real imaginary) + scale (f./ (|> imaginary (f.* q) (f.+ real)) +1.0)] {#real scale - #imaginary (|> scale frac.negate (f/* q))}))) + #imaginary (|> scale f.negate (f.* q))}))) (def: #export (acos input) (-> Complex Complex) @@ -279,18 +280,18 @@ (-> Nat Complex (List Complex)) (if (n/= 0 nth) (list) - (let [r-nth (|> nth .int int-to-frac) - nth-root-of-abs (|> input abs (get@ #real) (math.pow (f// r-nth +1.0))) - nth-phi (|> input argument (f// r-nth)) - slice (|> math.pi (f/* +2.0) (f// r-nth))] + (let [r-nth (|> nth .int int.frac) + nth-root-of-abs (|> input abs (get@ #real) (math.pow (f./ r-nth +1.0))) + nth-phi (|> input argument (f./ r-nth)) + slice (|> math.pi (f.* +2.0) (f./ r-nth))] (|> (list.indices nth) (list;map (function (_ nth') - (let [inner (|> nth' .int int-to-frac - (f/* slice) - (f/+ nth-phi)) - real (f/* nth-root-of-abs + (let [inner (|> nth' .int int.frac + (f.* slice) + (f.+ nth-phi)) + real (f.* nth-root-of-abs (math.cos inner)) - imaginary (f/* nth-root-of-abs + imaginary (f.* nth-root-of-abs (math.sin inner))] {#real real #imaginary imaginary}))))))) |