Skip to content

Commit

Permalink
Better random generation for Decimal.
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoejp committed Nov 5, 2023
1 parent 4dacca8 commit 036b400
Show file tree
Hide file tree
Showing 52 changed files with 427 additions and 336 deletions.
4 changes: 2 additions & 2 deletions stdlib/source/documentation/lux/math/random.lux
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
($.definition /.decimal)
($.definition /.character)

($.definition /.safe_decimal
"A number in the interval [0.0,1.0]."))
($.definition /.unit_decimal
"A number in the interval [0.0, 1.0]."))
text (list ($.definition /.unicode)
($.definition /.ascii)
($.definition /.alphabetic)
Expand Down
37 changes: 20 additions & 17 deletions stdlib/source/library/lux/math/number/complex.lux
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@
(-> Complex Complex
Complex)
(let [(open "/[0]") param]
(if (d.< (d.abs /#imaginary)
(d.abs /#real))
(if (d.< (d.absolute /#imaginary)
(d.absolute /#real))
(let [quot (d./ /#imaginary /#real)
denom (|> /#real (d.* quot) (d.+ /#imaginary))]
[..#real (|> (its ..#real input) (d.* quot) (d.+ (its ..#imaginary input)) (d./ denom))
Expand Down Expand Up @@ -226,22 +226,25 @@
[..#real (d./ d (hyperbola.sine r2))
..#imaginary (d./ d (circle.sin i2))]))

(the .public (abs subject)
(the .public (absolute subject)
(-> Complex
Decimal)
(let [(open "/[0]") subject]
(if (d.< (d.abs /#imaginary)
(d.abs /#real))
(if (d.< (d.absolute /#imaginary)
(d.absolute /#real))
(if (d.= +0.0 /#imaginary)
(d.abs /#real)
(d.absolute /#real)
(let [q (d./ /#imaginary /#real)]
(d.* (d.pow +0.5 (d.+ +1.0 (d.* q q)))
(d.abs /#imaginary))))
(d.absolute /#imaginary))))
(if (d.= +0.0 /#real)
(d.abs /#imaginary)
(d.absolute /#imaginary)
(let [q (d./ /#real /#imaginary)]
(d.* (d.pow +0.5 (d.+ +1.0 (d.* q q)))
(d.abs /#real)))))))
(d.absolute /#real)))))))

(alias [abs]
..absolute)

(the .public (exp subject)
(-> Complex
Expand All @@ -255,7 +258,7 @@
(-> Complex
Complex)
(let [(open "/[0]") subject]
[..#real (|> subject ..abs d.log)
[..#real (|> subject ..absolute d.log)
..#imaginary (circle.atan_2 /#real /#imaginary)]))

(template.with [<name> <type> <op>]
Expand All @@ -277,10 +280,10 @@
(-> Complex
Complex)
(let [(open "/[0]") input
t (|> input ..abs (d.+ (d.abs /#real)) (d./ +2.0) (d.pow +0.5))]
t (|> input ..absolute (d.+ (d.absolute /#real)) (d./ +2.0) (d.pow +0.5))]
(if (d.< +0.0 /#real)
[..#real (d./ (d.* +2.0 t)
(d.abs /#imaginary))
(d.absolute /#imaginary))
..#imaginary (d.* t (..with_sign /#imaginary +1.0))]
[..#real t
..#imaginary (d./ (d.* +2.0 t)
Expand All @@ -294,8 +297,8 @@
(the .public (reciprocal (open "/[0]"))
(-> Complex
Complex)
(if (d.< (d.abs /#imaginary)
(d.abs /#real))
(if (d.< (d.absolute /#imaginary)
(d.absolute /#real))
(let [q (d./ /#imaginary /#real)
scale (d./ (|> /#real (d.* q) (d.+ /#imaginary))
+1.0)]
Expand Down Expand Up @@ -344,17 +347,17 @@
(when nth
0 (list)
_ (let [r_nth (|> nth .integer integer.decimal)
nth_root_of_abs (|> input ..abs (d.pow (d./ r_nth +1.0)))
nth_root_of_absolute (|> input ..absolute (d.pow (d./ r_nth +1.0)))
nth_phi (|> input ..argument (d./ r_nth))
slice (d./ r_nth circle.tau)]
(|> (list.indices nth)
(list#each (function (_ nth')
(let [inner (|> nth' .integer integer.decimal
(d.* slice)
(d.+ nth_phi))
real (d.* nth_root_of_abs
real (d.* nth_root_of_absolute
(circle.cos inner))
imaginary (d.* nth_root_of_abs
imaginary (d.* nth_root_of_absolute
(circle.sin inner))]
[..#real real
..#imaginary imaginary])))))))
Expand Down
11 changes: 7 additions & 4 deletions stdlib/source/library/lux/math/number/decimal.lux
Original file line number Diff line number Diff line change
Expand Up @@ -361,18 +361,21 @@
(..% param subject)])

... https://en.wikipedia.org/wiki/Additive_inverse
(the .public opposite
(the .public (opposite it)
(-> It
It)
(..* -1.0))
(..- it +0.0))

(the .public (abs it)
(the .public (absolute it)
(-> It
It)
(if (..< +0.0 it)
(..* -1.0 it)
(..opposite it)
it))

(alias [abs]
..absolute)

(the .public (signum it)
(-> It
It)
Expand Down
Loading

0 comments on commit 036b400

Please sign in to comment.