Skip to content

Commit 3ad5111

Browse files
authored
Merge pull request #75 from DanexCodr/copilot/fix-redundancies-in-standard-library
Refactor std math library: remove redundancy, merge MathCore, and make Fibonacci lambda-based
2 parents 2da98d1 + e8961ab commit 3ad5111

8 files changed

Lines changed: 271 additions & 265 deletions

File tree

source_.jar

-228 Bytes
Binary file not shown.

src/main/cod/std/math/Math.cod

Lines changed: 223 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,235 @@
11
unit math
22

33
use {
4-
math.MathCore,
5-
math.MathTrig,
6-
math.MathNumberTheory,
7-
math.MathRandom,
8-
math.MathLinearAlgebra,
9-
math.MathStats
4+
math.Trig,
5+
math.NumberTheory,
6+
math.Random,
7+
math.LinearAlgebra,
8+
math.Stats
109
}
1110

1211
PI: float = 3.141592653589793
1312
E: float = 2.718281828459045
1413
TAU: float = 6.283185307179586
1514
PHI: float = 1.618033988749895
1615

17-
fibonacci: [int] = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
16+
share fibonacci(n: int) :: int|float {
17+
if n < 0 { ~> 0 }
18+
fibTailRecursive := [result]:\(k: int, a: int|float, b: int|float) :: result: int|float {
19+
if k == 0 { ~> result: a }
20+
if k == 1 { ~> result: b }
21+
~> result: <~(k - 1, b, a + b)
22+
}
23+
~> fibTailRecursive(n, 0, 1)
24+
}
1825
catalan: [int] = [1, 1, 2, 5, 14, 42, 132, 429]
1926

20-
share abs(x: int|float) :: int|float { ~> MathCore.abs(x) }
21-
share sign(x: int|float) :: int|float { ~> MathCore.sign(x) }
22-
share min(a: int|float, b: int|float) :: int|float { ~> MathCore.min(a, b) }
23-
share max(a: int|float, b: int|float) :: int|float { ~> MathCore.max(a, b) }
24-
share lerp(a: int|float, b: int|float, t: int|float) :: int|float { ~> MathCore.lerp(a, b, t) }
25-
share floor(x: int|float) :: int|float { ~> MathCore.floor(x) }
26-
share ceil(x: int|float) :: int|float { ~> MathCore.ceil(x) }
27-
share round(x: int|float) :: int|float { ~> MathCore.round(x) }
28-
share trunc(x: int|float) :: int|float { ~> MathCore.trunc(x) }
29-
share frac(x: int|float) :: int|float { ~> MathCore.frac(x) }
30-
share pow(base: int|float, exponent: int) :: int|float { ~> MathCore.pow(base, exponent) }
31-
share sqrt(x: int|float) :: int|float { ~> MathCore.sqrt(x) }
32-
share cbrt(x: int|float) :: int|float { ~> MathCore.cbrt(x) }
33-
share hypot(a: int|float, b: int|float) :: int|float { ~> MathCore.hypot(a, b) }
34-
share square(x: int|float) :: int|float { ~> MathCore.square(x) }
35-
share cube(x: int|float) :: int|float { ~> MathCore.cube(x) }
36-
share exp(x: int|float) :: int|float { ~> MathCore.exp(x) }
37-
share expm1(x: int|float) :: int|float { ~> MathCore.expm1(x) }
38-
share log(x: int|float) :: int|float { ~> MathCore.log(x) }
39-
share log10(x: int|float) :: int|float { ~> MathCore.log10(x) }
40-
share log2(x: int|float) :: int|float { ~> MathCore.log2(x) }
41-
share log1(x: int|float) :: int|float { ~> MathCore.log1(x) }
42-
share log1p(x: int|float) :: int|float { ~> MathCore.log1p(x) }
43-
44-
share sin(x: int|float) :: int|float { ~> MathTrig.sin(x) }
45-
share cos(x: int|float) :: int|float { ~> MathTrig.cos(x) }
46-
share tan(x: int|float) :: int|float { ~> MathTrig.tan(x) }
47-
share asin(x: int|float) :: int|float { ~> MathTrig.asin(x) }
48-
share acos(x: int|float) :: int|float { ~> MathTrig.acos(x) }
49-
share atan(x: int|float) :: int|float { ~> MathTrig.atan(x) }
50-
share atan2(y: int|float, x: int|float) :: int|float { ~> MathTrig.atan2(y, x) }
51-
share sinh(x: int|float) :: int|float { ~> MathTrig.sinh(x) }
52-
share cosh(x: int|float) :: int|float { ~> MathTrig.cosh(x) }
53-
share tanh(x: int|float) :: int|float { ~> MathTrig.tanh(x) }
54-
share asinh(x: int|float) :: int|float { ~> MathTrig.asinh(x) }
55-
share acosh(x: int|float) :: int|float { ~> MathTrig.acosh(x) }
56-
share atanh(x: int|float) :: int|float { ~> MathTrig.atanh(x) }
57-
share toRadians(x: int|float) :: int|float { ~> MathCore.toRadians(x) }
58-
share toDegrees(x: int|float) :: int|float { ~> MathCore.toDegrees(x) }
59-
60-
share gcd(a: int, b: int) :: int|float { ~> MathNumberTheory.gcd(a, b) }
61-
share lcd(a: int, b: int) :: int|float { ~> MathNumberTheory.lcd(a, b) }
62-
share lcm(a: int, b: int) :: int|float { ~> MathNumberTheory.lcm(a, b) }
63-
share isPrime(n: int) :: int|float { ~> MathNumberTheory.isPrime(n) }
64-
share nextPrime(n: int) :: int|float { ~> MathNumberTheory.nextPrime(n) }
65-
share prevPrime(n: int) :: int|float { ~> MathNumberTheory.prevPrime(n) }
66-
share factorize(n: int) :: [] { ~> MathNumberTheory.factorize(n) }
67-
share factorial(n: int) :: int|float { ~> MathNumberTheory.factorial(n) }
68-
share binomial(n: int, k: int) :: int|float { ~> MathNumberTheory.binomial(n, k) }
69-
share permutations(n: int, k: int) :: int|float { ~> MathNumberTheory.permutations(n, k) }
70-
share combinations(n: int, k: int) :: int|float { ~> MathNumberTheory.combinations(n, k) }
71-
72-
share random() :: int|float { ~> MathRandom.random() }
73-
share random(max: int) :: int|float { ~> MathRandom.random(max) }
74-
share random(min: int, max: int) :: int|float { ~> MathRandom.random(min, max) }
75-
share randomGaussian() :: int|float { ~> MathRandom.randomGaussian() }
76-
share randomSeed(seed: int) { MathRandom.randomSeed(seed) }
77-
78-
share dot(a: [], b: []) :: int|float { ~> MathLinearAlgebra.dot(a, b) }
79-
share cross(a: [], b: []) :: [] { ~> MathLinearAlgebra.cross(a, b) }
80-
share magnitude(a: []) :: int|float { ~> MathLinearAlgebra.magnitude(a) }
81-
share normalize(a: []) :: [] { ~> MathLinearAlgebra.normalize(a) }
82-
share matmul(a: [], b: []) :: [] { ~> MathLinearAlgebra.matmul(a, b) }
83-
share transpose(a: []) :: [] { ~> MathLinearAlgebra.transpose(a) }
84-
share identity(n: int) :: [] { ~> MathLinearAlgebra.identity(n) }
85-
share determinant(a: []) :: int|float { ~> MathLinearAlgebra.determinant(a) }
86-
87-
share mean(values: []) :: int|float { ~> MathStats.mean(values) }
88-
share median(values: []) :: int|float { ~> MathStats.median(values) }
89-
share mode(values: []) :: int|float { ~> MathStats.mode(values) }
90-
share variance(values: []) :: int|float { ~> MathStats.variance(values) }
91-
share stddev(values: []) :: int|float { ~> MathStats.stddev(values) }
92-
share runningMean(values: []) :: [] { ~> MathStats.runningMean(values) }
93-
share runningStddev(values: []) :: [] { ~> MathStats.runningStddev(values) }
27+
share Core {
28+
PI: float = 3.141592653589793
29+
E: float = 2.718281828459045
30+
31+
share abs(x: int|float) :: int|float {
32+
if x < 0 {
33+
~> -x
34+
}
35+
~> x
36+
}
37+
38+
share sign(x: int|float) :: int|float {
39+
if x > 0 { ~> 1 }
40+
if x < 0 { ~> -1 }
41+
~> 0
42+
}
43+
44+
share min(a: int|float, b: int|float) :: int|float {
45+
if a < b { ~> a }
46+
~> b
47+
}
48+
49+
share max(a: int|float, b: int|float) :: int|float {
50+
if a > b { ~> a }
51+
~> b
52+
}
53+
54+
share lerp(a: int|float, b: int|float, t: int|float) :: int|float {
55+
~> a + (b - a) * t
56+
}
57+
58+
share trunc(x: int|float) :: int|float {
59+
~> x - (x % 1)
60+
}
61+
62+
share floor(x: int|float) :: int|float {
63+
t := Core.trunc(x)
64+
if x < t {
65+
~> t - 1
66+
}
67+
~> t
68+
}
69+
70+
share ceil(x: int|float) :: int|float {
71+
t := Core.trunc(x)
72+
if x > t {
73+
~> t + 1
74+
}
75+
~> t
76+
}
77+
78+
share round(x: int|float) :: int|float {
79+
if x >= 0 {
80+
~> Core.floor(x + 0.5)
81+
}
82+
~> Core.ceil(x - 0.5)
83+
}
84+
85+
share frac(x: int|float) :: int|float {
86+
~> x - Core.floor(x)
87+
}
88+
89+
share square(x: int|float) :: int|float { ~> x * x }
90+
share cube(x: int|float) :: int|float { ~> x * x * x }
91+
92+
share pow(base: int|float, exponent: int) :: int|float {
93+
if exponent == 0 { ~> 1 }
94+
if exponent < 0 {
95+
~> 1 / Core.pow(base, -exponent)
96+
}
97+
outValue: int|float = 1
98+
for i of 1 to exponent {
99+
outValue = outValue * base
100+
}
101+
~> outValue
102+
}
103+
104+
share sqrt(x: int|float) :: int|float {
105+
if x <= 0 { ~> 0 }
106+
guess: int|float = x
107+
for i of 1 to 16 {
108+
guess = (guess + x / guess) / 2
109+
}
110+
~> guess
111+
}
112+
113+
share cbrt(x: int|float) :: int|float {
114+
if x == 0 { ~> 0 }
115+
guess: int|float = x
116+
for i of 1 to 16 {
117+
guess = (2 * guess + (x / (guess * guess))) / 3
118+
}
119+
~> guess
120+
}
121+
122+
share hypot(a: int|float, b: int|float) :: int|float {
123+
~> Core.sqrt(a * a + b * b)
124+
}
125+
126+
share exp(x: int|float) :: int|float {
127+
term: int|float = 1
128+
sum: int|float = 1
129+
for i of 1 to 20 {
130+
term = term * x / i
131+
sum = sum + term
132+
}
133+
~> sum
134+
}
135+
136+
share expm1(x: int|float) :: int|float {
137+
~> Core.exp(x) - 1
138+
}
139+
140+
share log(x: int|float) :: int|float {
141+
if x <= 0 { ~> 0 }
142+
y: int|float = x - 1
143+
for i of 1 to 20 {
144+
eY := Core.exp(y)
145+
if eY == 0 {
146+
~> y
147+
}
148+
y = y - (eY - x) / eY
149+
}
150+
~> y
151+
}
152+
153+
share log10(x: int|float) :: int|float { ~> Core.log(x) / Core.log(10) }
154+
share log2(x: int|float) :: int|float { ~> Core.log(x) / Core.log(2) }
155+
share log1(x: int|float) :: int|float { ~> Core.log1p(x) }
156+
share log1p(x: int|float) :: int|float { ~> Core.log(1 + x) }
157+
158+
share toRadians(x: int|float) :: int|float { ~> x * PI / 180 }
159+
share toDegrees(x: int|float) :: int|float { ~> x * 180 / PI }
160+
}
161+
162+
share abs(x: int|float) :: int|float { ~> Core.abs(x) }
163+
share sign(x: int|float) :: int|float { ~> Core.sign(x) }
164+
share min(a: int|float, b: int|float) :: int|float { ~> Core.min(a, b) }
165+
share max(a: int|float, b: int|float) :: int|float { ~> Core.max(a, b) }
166+
share lerp(a: int|float, b: int|float, t: int|float) :: int|float { ~> Core.lerp(a, b, t) }
167+
share floor(x: int|float) :: int|float { ~> Core.floor(x) }
168+
share ceil(x: int|float) :: int|float { ~> Core.ceil(x) }
169+
share round(x: int|float) :: int|float { ~> Core.round(x) }
170+
share trunc(x: int|float) :: int|float { ~> Core.trunc(x) }
171+
share frac(x: int|float) :: int|float { ~> Core.frac(x) }
172+
share pow(base: int|float, exponent: int) :: int|float { ~> Core.pow(base, exponent) }
173+
share sqrt(x: int|float) :: int|float { ~> Core.sqrt(x) }
174+
share cbrt(x: int|float) :: int|float { ~> Core.cbrt(x) }
175+
share hypot(a: int|float, b: int|float) :: int|float { ~> Core.hypot(a, b) }
176+
share square(x: int|float) :: int|float { ~> Core.square(x) }
177+
share cube(x: int|float) :: int|float { ~> Core.cube(x) }
178+
share exp(x: int|float) :: int|float { ~> Core.exp(x) }
179+
share expm1(x: int|float) :: int|float { ~> Core.expm1(x) }
180+
share log(x: int|float) :: int|float { ~> Core.log(x) }
181+
share log10(x: int|float) :: int|float { ~> Core.log10(x) }
182+
share log2(x: int|float) :: int|float { ~> Core.log2(x) }
183+
share log1(x: int|float) :: int|float { ~> Core.log1(x) }
184+
share log1p(x: int|float) :: int|float { ~> Core.log1p(x) }
185+
186+
share sin(x: int|float) :: int|float { ~> Trig.sin(x) }
187+
share cos(x: int|float) :: int|float { ~> Trig.cos(x) }
188+
share tan(x: int|float) :: int|float { ~> Trig.tan(x) }
189+
share asin(x: int|float) :: int|float { ~> Trig.asin(x) }
190+
share acos(x: int|float) :: int|float { ~> Trig.acos(x) }
191+
share atan(x: int|float) :: int|float { ~> Trig.atan(x) }
192+
share atan2(y: int|float, x: int|float) :: int|float { ~> Trig.atan2(y, x) }
193+
share sinh(x: int|float) :: int|float { ~> Trig.sinh(x) }
194+
share cosh(x: int|float) :: int|float { ~> Trig.cosh(x) }
195+
share tanh(x: int|float) :: int|float { ~> Trig.tanh(x) }
196+
share asinh(x: int|float) :: int|float { ~> Trig.asinh(x) }
197+
share acosh(x: int|float) :: int|float { ~> Trig.acosh(x) }
198+
share atanh(x: int|float) :: int|float { ~> Trig.atanh(x) }
199+
share toRadians(x: int|float) :: int|float { ~> Core.toRadians(x) }
200+
share toDegrees(x: int|float) :: int|float { ~> Core.toDegrees(x) }
201+
202+
share gcd(a: int, b: int) :: int|float { ~> NumberTheory.gcd(a, b) }
203+
share lcd(a: int, b: int) :: int|float { ~> NumberTheory.lcd(a, b) }
204+
share lcm(a: int, b: int) :: int|float { ~> NumberTheory.lcm(a, b) }
205+
share isPrime(n: int) :: int|float { ~> NumberTheory.isPrime(n) }
206+
share nextPrime(n: int) :: int|float { ~> NumberTheory.nextPrime(n) }
207+
share prevPrime(n: int) :: int|float { ~> NumberTheory.prevPrime(n) }
208+
share factorize(n: int) :: [] { ~> NumberTheory.factorize(n) }
209+
share factorial(n: int) :: int|float { ~> NumberTheory.factorial(n) }
210+
share binomial(n: int, k: int) :: int|float { ~> NumberTheory.binomial(n, k) }
211+
share permutations(n: int, k: int) :: int|float { ~> NumberTheory.permutations(n, k) }
212+
share combinations(n: int, k: int) :: int|float { ~> NumberTheory.combinations(n, k) }
213+
214+
share random() :: int|float { ~> Random.random() }
215+
share random(max: int) :: int|float { ~> Random.random(max) }
216+
share random(min: int, max: int) :: int|float { ~> Random.random(min, max) }
217+
share randomGaussian() :: int|float { ~> Random.randomGaussian() }
218+
share randomSeed(seed: int) { Random.randomSeed(seed) }
219+
220+
share dot(a: [], b: []) :: int|float { ~> LinearAlgebra.dot(a, b) }
221+
share cross(a: [], b: []) :: [] { ~> LinearAlgebra.cross(a, b) }
222+
share magnitude(a: []) :: int|float { ~> LinearAlgebra.magnitude(a) }
223+
share normalize(a: []) :: [] { ~> LinearAlgebra.normalize(a) }
224+
share matmul(a: [], b: []) :: [] { ~> LinearAlgebra.matmul(a, b) }
225+
share transpose(a: []) :: [] { ~> LinearAlgebra.transpose(a) }
226+
share identity(n: int) :: [] { ~> LinearAlgebra.identity(n) }
227+
share determinant(a: []) :: int|float { ~> LinearAlgebra.determinant(a) }
228+
229+
share mean(values: []) :: int|float { ~> Stats.mean(values) }
230+
share median(values: []) :: int|float { ~> Stats.median(values) }
231+
share mode(values: []) :: int|float { ~> Stats.mode(values) }
232+
share variance(values: []) :: int|float { ~> Stats.variance(values) }
233+
share stddev(values: []) :: int|float { ~> Stats.stddev(values) }
234+
share runningMean(values: []) :: [] { ~> Stats.runningMean(values) }
235+
share runningStddev(values: []) :: [] { ~> Stats.runningStddev(values) }

0 commit comments

Comments
 (0)