-
Notifications
You must be signed in to change notification settings - Fork 2
/
tetration.ts
61 lines (50 loc) · 1.46 KB
/
tetration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Tetration is repeated exponentiation. It is defined for any base `a` and any
// natural "exponent" `n` as the following:
//
// a^^0 = 1,
// a^^n = a^(a^^(n-1)).
//
// However, it can be extended to complex exponents using formulas. One of these
// such extensions is available on MathOverflow at https://mathoverflow.net/a/259371.
//
// This file implements the extension proposed there. I'd provide an accompanying
// Desmos graph, but Desmos doesn't understand tetration.
import D from "decimal.js"
import { gamma } from "./gamma.js"
const Decimal = D as typeof D.default
type Decimal = D.default
function factorial(x: Decimal): Decimal {
if (x.lessThan(0) && x.floor().equals(x)) {
return new Decimal(Infinity)
}
return gamma(x.plus(1))
}
function tetrate(base: Decimal, exponent: number): Decimal {
let result = new Decimal(1)
for (let i = 0; i < exponent; i++) {
result = base.pow(result)
}
return result
}
function tetrateAny(
a: Decimal | number,
z: Decimal | number,
approx: number,
): Decimal {
a = new Decimal(a)
z = new Decimal(z)
let result = new Decimal(0)
for (let n = 0; n <= approx; n++) {
for (let k = 0; k <= n; k++) {
result = result.plus(
factorial(z)
.dividedBy(factorial(new Decimal(k)))
.dividedBy(factorial(new Decimal(n - k)))
.dividedBy(factorial(z.minus(n)))
.times(tetrate(a, k))
.times((-1) ** (n - k)),
)
}
}
return result
}