aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/math/number/complex.lux
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--stdlib/source/library/lux/math/number/complex.lux103
1 files changed, 72 insertions, 31 deletions
diff --git a/stdlib/source/library/lux/math/number/complex.lux b/stdlib/source/library/lux/math/number/complex.lux
index c862a952e..518da2518 100644
--- a/stdlib/source/library/lux/math/number/complex.lux
+++ b/stdlib/source/library/lux/math/number/complex.lux
@@ -17,7 +17,9 @@
["[0]" code
["<[1]>" \\parser]]
[macro
- [syntax (.only syntax)]]]]])
+ [syntax (.only syntax)]]]]]
+ [///
+ [arithmetic (.only Arithmetic)]])
(type .public Complex
(Record
@@ -47,12 +49,14 @@
(..complex +0.0 +0.0))
(def .public (not_a_number? complex)
- (-> Complex Bit)
+ (-> Complex
+ Bit)
(or (f.not_a_number? (the #real complex))
(f.not_a_number? (the #imaginary complex))))
(def .public (= param input)
- (-> Complex Complex Bit)
+ (-> Complex Complex
+ Bit)
(and (f.= (the #real param)
(the #real input))
(f.= (the #imaginary param)
@@ -60,7 +64,8 @@
(with_template [<name> <op>]
[(def .public (<name> param input)
- (-> Complex Complex Complex)
+ (-> Complex Complex
+ Complex)
[#real (<op> (the #real param)
(the #real input))
#imaginary (<op> (the #imaginary param)
@@ -77,7 +82,8 @@
(with_template [<name> <transform>]
[(def .public <name>
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(|>> (revised #real <transform>)
(revised #imaginary <transform>)))]
@@ -86,18 +92,21 @@
)
(def .public conjugate
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(revised #imaginary f.opposite))
(def .public (*' param input)
- (-> Frac Complex Complex)
+ (-> Frac Complex
+ Complex)
[#real (f.* param
(the #real input))
#imaginary (f.* param
(the #imaginary input))])
(def .public (* param input)
- (-> Complex Complex Complex)
+ (-> Complex Complex
+ Complex)
[#real (f.- (f.* (the #imaginary param)
(the #imaginary input))
(f.* (the #real param)
@@ -108,7 +117,8 @@
(the #real input)))])
(def .public (/ param input)
- (-> Complex Complex Complex)
+ (-> Complex Complex
+ Complex)
(let [(open "[0]") param]
(if (f.< (f.abs #imaginary)
(f.abs #real))
@@ -122,13 +132,15 @@
..#imaginary (|> (the ..#imaginary input) (f.- (f.* quot (the ..#real input))) (f./ denom))]))))
(def .public (/' param subject)
- (-> Frac Complex Complex)
+ (-> Frac Complex
+ Complex)
(let [(open "[0]") subject]
[..#real (f./ param #real)
..#imaginary (f./ param #imaginary)]))
(def .public (% param input)
- (-> Complex Complex Complex)
+ (-> Complex Complex
+ Complex)
(let [scaled (/ param input)
quotient (|> scaled
(revised #real f.floor)
@@ -136,8 +148,18 @@
(- (* quotient param)
input)))
+(def .public arithmetic
+ (Arithmetic Complex)
+ (implementation
+ (def + ..+)
+ (def - ..-)
+ (def * ..*)
+ (def / ../)
+ (def % ..%)))
+
(def .public (cos subject)
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(let [(open "[0]") subject]
[..#real (f.* (f.cosh #imaginary)
(f.cos #real))
@@ -145,7 +167,8 @@
(f.sin #real)))]))
(def .public (cosh subject)
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(let [(open "[0]") subject]
[..#real (f.* (f.cos #imaginary)
(f.cosh #real))
@@ -153,7 +176,8 @@
(f.sinh #real))]))
(def .public (sin subject)
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(let [(open "[0]") subject]
[..#real (f.* (f.cosh #imaginary)
(f.sin #real))
@@ -161,7 +185,8 @@
(f.cos #real))]))
(def .public (sinh subject)
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(let [(open "[0]") subject]
[..#real (f.* (f.cos #imaginary)
(f.sinh #real))
@@ -169,7 +194,8 @@
(f.cosh #real))]))
(def .public (tan subject)
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(let [(open "[0]") subject
r2 (f.* +2.0 #real)
i2 (f.* +2.0 #imaginary)
@@ -178,7 +204,8 @@
..#imaginary (f./ d (f.sinh i2))]))
(def .public (tanh subject)
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(let [(open "[0]") subject
r2 (f.* +2.0 #real)
i2 (f.* +2.0 #imaginary)
@@ -187,7 +214,8 @@
..#imaginary (f./ d (f.sin i2))]))
(def .public (abs subject)
- (-> Complex Frac)
+ (-> Complex
+ Frac)
(let [(open "[0]") subject]
(if (f.< (f.abs #imaginary)
(f.abs #real))
@@ -203,21 +231,24 @@
(f.abs #real)))))))
(def .public (exp subject)
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(let [(open "[0]") subject
r_exp (f.exp #real)]
[..#real (f.* r_exp (f.cos #imaginary))
..#imaginary (f.* r_exp (f.sin #imaginary))]))
(def .public (log subject)
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(let [(open "[0]") subject]
[..#real (|> subject ..abs f.log)
..#imaginary (f.atan_2 #real #imaginary)]))
(with_template [<name> <type> <op>]
[(def .public (<name> param input)
- (-> <type> Complex Complex)
+ (-> <type> Complex
+ Complex)
(|> input log (<op> param) exp))]
[pow Complex ..*]
@@ -225,11 +256,13 @@
)
(def (with_sign sign magnitude)
- (-> Frac Frac Frac)
+ (-> Frac Frac
+ Frac)
(f.* (f.signum sign) magnitude))
(def .public (root_2 input)
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(let [(open "[0]") input
t (|> input ..abs (f.+ (f.abs #real)) (f./ +2.0) (f.pow +0.5))]
(if (f.< +0.0 #real)
@@ -241,11 +274,13 @@
#imaginary)])))
(def (root_2-1z input)
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(|> (complex +1.0) (- (* input input)) ..root_2))
(def .public (reciprocal (open "[0]"))
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(if (f.< (f.abs #imaginary)
(f.abs #real))
(let [q (f./ #imaginary #real)
@@ -260,14 +295,16 @@
..#imaginary (|> scale f.opposite (f.* q))])))
(def .public (acos input)
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(|> input
(..+ (|> input ..root_2-1z (..* ..i)))
..log
(..* (..opposite ..i))))
(def .public (asin input)
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(|> input
..root_2-1z
(..+ (..* ..i input))
@@ -275,7 +312,8 @@
(..* (..opposite ..i))))
(def .public (atan input)
- (-> Complex Complex)
+ (-> Complex
+ Complex)
(|> input
(..+ ..i)
(../ (..- input ..i))
@@ -283,11 +321,13 @@
(..* (../ (..complex +2.0) ..i))))
(def .public (argument (open "[0]"))
- (-> Complex Frac)
+ (-> Complex
+ Frac)
(f.atan_2 #real #imaginary))
(def .public (roots nth input)
- (-> Nat Complex (List Complex))
+ (-> Nat Complex
+ (List Complex))
(when nth
0 (list)
_ (let [r_nth (|> nth .int int.frac)
@@ -307,7 +347,8 @@
..#imaginary imaginary])))))))
(def .public (approximately? margin_of_error standard value)
- (-> Frac Complex Complex Bit)
+ (-> Frac Complex Complex
+ Bit)
(and (f.approximately? margin_of_error
(the ..#real standard)
(the ..#real value))