diff options
author | Eduardo Julian | 2017-01-29 11:42:16 -0400 |
---|---|---|
committer | Eduardo Julian | 2017-01-29 11:42:16 -0400 |
commit | 9ff1c394dbf65f98adb6e183e576dee739f3d596 (patch) | |
tree | 99e72b372f69bf0e5bc62891b705217513c922ce /stdlib/source | |
parent | 97329ec45dd93dc1008d3778c6173fdfbfbd7ab8 (diff) |
- Some refactorings and small expansions to lux/math.
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/lux/math.lux | 25 | ||||
-rw-r--r-- | stdlib/source/lux/math/complex.lux | 16 | ||||
-rw-r--r-- | stdlib/source/lux/math/logic/fuzzy.lux | 4 |
3 files changed, 29 insertions, 16 deletions
diff --git a/stdlib/source/lux/math.lux b/stdlib/source/lux/math.lux index 9369ae44f..e87bb1b1b 100644 --- a/stdlib/source/lux/math.lux +++ b/stdlib/source/lux/math.lux @@ -45,17 +45,13 @@ [exp "invokestatic:java.lang.Math:exp:double"] [log "invokestatic:java.lang.Math:log:double"] - [cbrt "invokestatic:java.lang.Math:cbrt:double"] - [sqrt "invokestatic:java.lang.Math:sqrt:double"] + [root2 "invokestatic:java.lang.Math:sqrt:double"] + [root3 "invokestatic:java.lang.Math:cbrt:double"] [degrees "invokestatic:java.lang.Math:toDegrees:double"] [radians "invokestatic:java.lang.Math:toRadians:double"] ) -(def: #export (square n) - (-> Real Real) - (r.* n n)) - (do-template [<name> <method>] [(def: #export (<name> n) (-> Real Real) @@ -78,6 +74,23 @@ [pow "invokestatic:java.lang.Math:pow:double,double"] ) +(def: #export (log' base input) + (r./ (log base) + (log input))) + +(def: #export (factorial n) + (-> Nat Nat) + (loop [acc +1 + n n] + (if (n.<= +1 n) + acc + (recur (n.* n acc) (n.dec n))))) + +(def: #export (hypotenuse catA catB) + (-> Real Real Real) + (root2 (r.+ (pow 2.0 catA) + (pow 2.0 catB)))) + (def: #export (gcd a b) {#;doc "Greatest Common Divisor."} (-> Nat Nat Nat) diff --git a/stdlib/source/lux/math/complex.lux b/stdlib/source/lux/math/complex.lux index 252e31d51..eae4fbe55 100644 --- a/stdlib/source/lux/math/complex.lux +++ b/stdlib/source/lux/math/complex.lux @@ -183,12 +183,12 @@ (if (r.= 0.0 imaginary) (r/abs real) (let [q (r./ imaginary real)] - (r.* (math;sqrt (r.+ 1.0 (r.* q q))) + (r.* (math;root2 (r.+ 1.0 (r.* q q))) (r/abs imaginary)))) (if (r.= 0.0 real) (r/abs imaginary) (let [q (r./ real imaginary)] - (r.* (math;sqrt (r.+ 1.0 (r.* q q))) + (r.* (math;root2 (r.+ 1.0 (r.* q q))) (r/abs real)))) )))) @@ -234,9 +234,9 @@ (-> Real Real Real) (r.* (r/signum sign) magnitude)) -(def: #export (sqrt (^@ input (^slots [#real #imaginary]))) +(def: #export (root2 (^@ input (^slots [#real #imaginary]))) (-> Complex Complex) - (let [t (|> input c.abs (get@ #real) (r.+ (r/abs real)) (r./ 2.0) math;sqrt)] + (let [t (|> input c.abs (get@ #real) (r.+ (r/abs real)) (r./ 2.0) math;root2)] (if (r.>= 0.0 real) {#real t #imaginary (r./ (r.* 2.0 t) @@ -245,9 +245,9 @@ (r/abs imaginary)) #imaginary (r.* t (copy-sign imaginary 1.0))}))) -(def: #export (sqrt-1z input) +(def: #export (root2-1z input) (-> Complex Complex) - (|> (complex 1.0) (c.- (c.* input input)) sqrt)) + (|> (complex 1.0) (c.- (c.* input input)) root2)) (def: #export (reciprocal (^slots [#real #imaginary])) (-> Complex Complex) @@ -267,14 +267,14 @@ (def: #export (acos input) (-> Complex Complex) (|> input - (c.+ (|> input sqrt-1z (c.* i))) + (c.+ (|> input root2-1z (c.* i))) log (c.* (c.negate i)))) (def: #export (asin input) (-> Complex Complex) (|> input - sqrt-1z + root2-1z (c.+ (c.* i input)) log (c.* (c.negate i)))) diff --git a/stdlib/source/lux/math/logic/fuzzy.lux b/stdlib/source/lux/math/logic/fuzzy.lux index 12d8b9dc3..ea3a795ff 100644 --- a/stdlib/source/lux/math/logic/fuzzy.lux +++ b/stdlib/source/lux/math/logic/fuzzy.lux @@ -113,10 +113,10 @@ (def: #export (gaussian deviation center) (-> Real Real (Fuzzy Real)) (lambda [elem] - (let [scale (|> deviation math;square (r.* 2.0)) + (let [scale (|> deviation (math;pow 2.0) (r.* 2.0)) membership (|> elem (r.- center) - math;square + (math;pow 2.0) (r.* -1.0) (r./ scale) math;exp)] |