aboutsummaryrefslogtreecommitdiffhomepage
path: root/std/Math/Real.bruijn
diff options
context:
space:
mode:
authorMarvin Borner2024-04-25 16:18:21 +0200
committerMarvin Borner2024-04-25 16:18:21 +0200
commit3fb29797cb2251e868bbd16918117b68962faaa8 (patch)
tree36e5c51c97da028e521bc4a0ce69bf4d1b19a9a4 /std/Math/Real.bruijn
parent2ac808e5ac77a84166973691b2a1e4e0afa9a308 (diff)
Various experiments
Diffstat (limited to 'std/Math/Real.bruijn')
-rw-r--r--std/Math/Real.bruijn38
1 files changed, 28 insertions, 10 deletions
diff --git a/std/Math/Real.bruijn b/std/Math/Real.bruijn
index 437a947..59a32e3 100644
--- a/std/Math/Real.bruijn
+++ b/std/Math/Real.bruijn
@@ -79,39 +79,57 @@ compress b Q.compress ⧗ Real → Real
# ---
-:import std/List .
+:import std/List L
# ∑(1/n^2)
# converges to 1.6449...
-frac-1/n² [^(0 &[[(Q.add 1 op) : N.++0]] start)] ⧗ Real
+unary-1/n² [0 &[[(Q.add 1 op) : N.++0]] start [[0]]] ⧗ Real
op (+1) : N.--(N.mul 0 0)
start (+0.0f) : (+1)
# ∑(x^n/n!) = e^x
# inefficient Taylor expansion
-frac-slow-exp [[^(0 &[[(Q.add 1 op) : N.++0]] start)]] ⧗ Number → Real
+unary-slow-exp [[0 &[[(Q.add 1 op) : N.++0]] start [[0]]]] ⧗ Number → Real
op (N.pow 3 0) : N.--(N.fac 0)
start (+0.0f) : (+1)
# ∑(x^n/n!) = e^x
# more efficient Taylor expansion
-frac-exp [[(0 &[[[[[[[2 1 0 (Q.add 4 op) N.++3]] pow fac]]]]] start) [[[[1]]]]]] ⧗ Number → Real
+unary-exp [[0 &[[[[[[[2 1 0 (Q.add 4 op) N.++3]] pow fac]]]]] start [[[[1]]]]]] ⧗ Number → Real
pow N.mul 6 4
fac N.mul 1 3
op 1 : N.--0
start [0 (+1) (+1) (+1.0f) (+1)]
+# equivalent to unary-exp but with ternary index using infinite list iteration
+iter-exp [0 [[[[1]]]]] ∘∘ [L.…!!… (L.iterate &[[[[op]]]] start)] ⧗ Number → Real
+ start [0 (+1) (+1) (+1.0f) (+1)]
+ op [[[2 1 0 (Q.add 4 (1 : N.--0)) N.++3]] pow fac]
+ pow N.mul 5 4
+ fac N.mul 1 3
+
# real^number
-pow-n […!!… (iterate (mul 0) (+1.0r))] ⧗ Real → Number → Real
+pow-n [L.…!!… (L.iterate (mul 0) (+1.0r))] ⧗ Real → Number → Real
+
+lim-exp [[pow-n [(N.add 2 1) : N.--1] 0]]
+
+iter-slow-ln [0 [[[[1]]]]] ∘∘ [L.…!!… (L.iterate &[[[[op]]]] start)] ⧗ Number → Real
+ start [0 (+1) (+1) (+0.0f) (+0)]
+ op [[[2 1 0 (Q.add 4 go) N.++3]] 4 3]
+ go Q.mul l r
+ l (+2) : (N.mul (+2) 3)
+ r Q.pow-n (N.--7 : 7) N.++(N.mul (+2) 3)
-exp [y [[[[rec]]]] (+1) (+1.0r) (+1.0r)]
- rec (1 / 0) + (3 N.++2 (1 ⋅ 4) (0 ⋅ (number→real 2)))
+iter-ln [0 [[[1]]]] ∘∘ [L.…!!… (L.iterate &[[[op]]] start)] ⧗ Number → Real
+ start [0 (N.--1 : 1) (+0.0f) (+0)]
+ op [0 pow (Q.add 2 go) N.++1]
+ pow Q.mul 3 (Q.pow-n (N.--4 : 4) (+2))
+ go Q.mul ((+2) : (N.mul (+2) 1)) 3
-ln [y [[[rec]]] (+1) 0]
- rec (N.=²?1 -‣ [0] (0 / (number→real 1))) + (2 N.++1 (3 ⋅ 0))
+derive [[[[((3 (0 + 1)) - (3 0)) / 1]] [(+1) : 1] 0]] ⧗ (Real → Real) → (Real → Real)
# power function
-pow [[exp (0 ⋅ (ln 1))]] ⧗ Real → Real → Real
+pow [[iter-exp (0 ⋅ (iter-ln 1))]] ⧗ Real → Real → Real
…**… pow