aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEduardo Julian2017-01-29 11:42:16 -0400
committerEduardo Julian2017-01-29 11:42:16 -0400
commit9ff1c394dbf65f98adb6e183e576dee739f3d596 (patch)
tree99e72b372f69bf0e5bc62891b705217513c922ce
parent97329ec45dd93dc1008d3778c6173fdfbfbd7ab8 (diff)
- Some refactorings and small expansions to lux/math.
Diffstat (limited to '')
-rw-r--r--stdlib/source/lux/math.lux25
-rw-r--r--stdlib/source/lux/math/complex.lux16
-rw-r--r--stdlib/source/lux/math/logic/fuzzy.lux4
-rw-r--r--stdlib/test/test/lux/math.lux4
-rw-r--r--stdlib/test/test/lux/math/complex.lux6
5 files changed, 34 insertions, 21 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)]
diff --git a/stdlib/test/test/lux/math.lux b/stdlib/test/test/lux/math.lux
index 7ed31bdb4..18cb1545c 100644
--- a/stdlib/test/test/lux/math.lux
+++ b/stdlib/test/test/lux/math.lux
@@ -48,10 +48,10 @@
base (|> R;real (:: @ map (r.* factor)))]
($_ seq
(assert "Square-root is inverse of square."
- (|> base (&;pow 2.0) &;sqrt (r.= base)))
+ (|> base (&;pow 2.0) &;root2 (r.= base)))
(assert "Cubic-root is inverse of cube."
- (|> base (&;pow 3.0) &;cbrt (r.= base)))
+ (|> base (&;pow 3.0) &;root3 (r.= base)))
))
(test: "Rounding"
diff --git a/stdlib/test/test/lux/math/complex.lux b/stdlib/test/test/lux/math/complex.lux
index 9fba68dc9..04ebcb3c0 100644
--- a/stdlib/test/test/lux/math/complex.lux
+++ b/stdlib/test/test/lux/math/complex.lux
@@ -138,11 +138,11 @@
(assert "x*(x^-1) = 1"
(|> x (&;c.* (&;reciprocal x)) (within? margin-of-error &;one)))
- (assert "Absolute value of signum is always sqrt(2), 1 or 0."
+ (assert "Absolute value of signum is always root2(2), 1 or 0."
(let [signum-abs (|> x &;c.signum &;c.abs (get@ #&;real))]
(or (r.= 0.0 signum-abs)
(r.= 1.0 signum-abs)
- (r.= (math;sqrt 2.0) signum-abs))))
+ (r.= (math;root2 2.0) signum-abs))))
(assert "Negation is its own inverse."
(let [there (&;c.negate x)
@@ -173,7 +173,7 @@
[x gen-complex]
($_ seq
(assert "Square root is inverse of power 2.0"
- (|> x (&;pow' 2.0) &;sqrt (within? margin-of-error x)))
+ (|> x (&;pow' 2.0) &;root2 (within? margin-of-error x)))
(assert "Logarithm is inverse of exponentiation."
(|> x &;log &;exp (within? margin-of-error x)))