-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.affine
More file actions
377 lines (319 loc) · 8.69 KB
/
Copy pathmath.affine
File metadata and controls
377 lines (319 loc) · 8.69 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2025 hyperpolymath
//
// AffineScript Standard Library - Mathematics
//
// Builtin functions (implemented in interpreter runtime):
// sqrt(x: Float) -> Float
// cbrt(x: Float) -> Float
// pow_float(base: Float, exp: Float) -> Float
// floor(x: Float) -> Int
// ceil(x: Float) -> Int
// round(x: Float) -> Int
// trunc(x: Float) -> Int
// sin(x: Float) -> Float
// cos(x: Float) -> Float
// tan(x: Float) -> Float
// asin(x: Float) -> Float
// acos(x: Float) -> Float
// atan(x: Float) -> Float
// atan2(y: Float, x: Float) -> Float
// exp(x: Float) -> Float
// log(x: Float) -> Float (natural logarithm)
// log10(x: Float) -> Float
// log2(x: Float) -> Float
// ============================================================================
// Constants
// ============================================================================
/// Ratio of a circle's circumference to its diameter
const PI: Float = 3.141592653589793;
/// Euler's number, base of the natural logarithm
const E: Float = 2.718281828459045;
/// Full-turn constant (2 * PI)
const TAU: Float = 6.283185307179586;
/// Positive infinity sentinel (largest representable float)
const INFINITY: Float = 1.0 / 0.0;
/// Negative infinity sentinel
const NEG_INFINITY: Float = -1.0 / 0.0;
// ============================================================================
// Basic arithmetic
// ============================================================================
/// Absolute value of an integer
pub fn abs(x: Int) -> Int {
if x < 0 { -x } else { x }
}
/// Absolute value of a float
pub fn abs_float(x: Float) -> Float {
if x < 0.0 { -x } else { x }
}
/// Sign of an integer: -1, 0, or 1
pub fn sign(x: Int) -> Int {
if x > 0 { 1 } else if x < 0 { -1 } else { 0 }
}
/// Sign of a float: -1, 0, or 1
pub fn sign_float(x: Float) -> Int {
if x > 0.0 { 1 } else if x < 0.0 { -1 } else { 0 }
}
/// Copy the sign of `sign_source` onto the magnitude of `magnitude`.
/// Returns `+|magnitude|` when `sign_source >= 0.0`, else `-|magnitude|`.
/// Branchless replacement for the common pattern
/// `if cond { x } else { -x }` where `cond` is itself the sign of some value.
pub fn copysign(magnitude: Float, sign_source: Float) -> Float {
let m = if magnitude < 0.0 { -magnitude } else { magnitude };
if sign_source < 0.0 { -m } else { m }
}
// ============================================================================
// Power and roots
// ============================================================================
/// Integer exponentiation via repeated squaring
pub fn pow(base: Int, exp: Int) -> Int {
if exp == 0 {
return 1;
}
if exp == 1 {
return base;
}
let half = pow(base, exp / 2);
if exp % 2 == 0 {
half * half
} else {
base * half * half
}
}
/// Square of an integer
pub fn square(x: Int) -> Int {
x * x
}
/// Cube of an integer
pub fn cube(x: Int) -> Int {
x * x * x
}
// sqrt, cbrt, pow_float are builtins — see module header
// ============================================================================
// Rounding and truncation (builtins)
// ============================================================================
// floor, ceil, round, trunc are builtins — see module header
/// Convert an integer to a float
pub fn to_float(n: Int) -> Float {
float(n)
}
/// Fractional part of a float (x - trunc(x))
pub fn fract(x: Float) -> Float {
x - to_float(trunc(x))
}
// ============================================================================
// Trigonometry (builtins)
// ============================================================================
// sin, cos, tan, asin, acos, atan, atan2 are builtins — see module header
/// Convert degrees to radians
pub fn deg_to_rad(degrees: Float) -> Float {
degrees * PI / 180.0
}
/// Convert radians to degrees
pub fn rad_to_deg(radians: Float) -> Float {
radians * 180.0 / PI
}
/// Hyperbolic sine
pub fn sinh(x: Float) -> Float {
(exp(x) - exp(-x)) / 2.0
}
/// Hyperbolic cosine
pub fn cosh(x: Float) -> Float {
(exp(x) + exp(-x)) / 2.0
}
/// Hyperbolic tangent
pub fn tanh(x: Float) -> Float {
sinh(x) / cosh(x)
}
// ============================================================================
// Logarithms and exponentials (builtins)
// ============================================================================
// exp, log, log10, log2 are builtins — see module header
/// Logarithm with arbitrary base
pub fn log_base(base: Float, x: Float) -> Float {
log(x) / log(base)
}
// ============================================================================
// Comparison
// ============================================================================
/// Minimum of two integers
pub fn min_int(a: Int, b: Int) -> Int {
if a < b { a } else { b }
}
/// Maximum of two integers
pub fn max_int(a: Int, b: Int) -> Int {
if a > b { a } else { b }
}
/// Minimum of two floats
pub fn min_float(a: Float, b: Float) -> Float {
if a < b { a } else { b }
}
/// Maximum of two floats
pub fn max_float(a: Float, b: Float) -> Float {
if a > b { a } else { b }
}
/// Clamp an integer between min_val and max_val (inclusive)
pub fn clamp_int(value: Int, min_val: Int, max_val: Int) -> Int {
if value < min_val {
min_val
} else if value > max_val {
max_val
} else {
value
}
}
/// Clamp a float between min_val and max_val (inclusive)
pub fn clamp_float(value: Float, min_val: Float, max_val: Float) -> Float {
if value < min_val {
min_val
} else if value > max_val {
max_val
} else {
value
}
}
/// Linear interpolation between a and b by factor t (0.0 to 1.0)
pub fn lerp(a: Float, b: Float, t: Float) -> Float {
a + (b - a) * t
}
// ============================================================================
// Number theory
// ============================================================================
/// Greatest common divisor via Euclid's algorithm
pub fn gcd(a: Int, b: Int) -> Int {
let mut x = abs(a);
let mut y = abs(b);
while y != 0 {
let temp = y;
y = x % y;
x = temp;
}
x
}
/// Least common multiple
pub fn lcm(a: Int, b: Int) -> Int {
if a == 0 || b == 0 {
return 0;
}
abs(a * b) / gcd(a, b)
}
/// Check if n is even
pub fn is_even(n: Int) -> Bool {
n % 2 == 0
}
/// Check if n is odd
pub fn is_odd(n: Int) -> Bool {
n % 2 != 0
}
/// Check if n is a prime number (trial division)
pub fn is_prime(n: Int) -> Bool {
if n < 2 {
return false;
}
if n < 4 {
return true;
}
if n % 2 == 0 || n % 3 == 0 {
return false;
}
let mut i = 5;
while i * i <= n {
if n % i == 0 || n % (i + 2) == 0 {
return false;
}
i = i + 6;
}
true
}
/// Integer division rounding towards negative infinity (floor division)
pub fn div_floor(a: Int, b: Int) -> Int {
let q = a / b;
if (a % b != 0) && ((a < 0) != (b < 0)) {
q - 1
} else {
q
}
}
/// Modulo that always returns a non-negative result
pub fn mod_positive(a: Int, b: Int) -> Int {
let r = a % b;
if r < 0 {
r + abs(b)
} else {
r
}
}
// ============================================================================
// Sequences
// ============================================================================
/// Factorial of n (n!)
pub fn factorial(n: Int) -> Int {
if n <= 1 {
1
} else {
n * factorial(n - 1)
}
}
/// n-th Fibonacci number (iterative)
pub fn fibonacci(n: Int) -> Int {
if n <= 1 {
n
} else {
let mut a = 0;
let mut b = 1;
let mut i = 2;
while i <= n {
let temp = a + b;
a = b;
b = temp;
i = i + 1;
}
b
}
}
/// Sum of first n natural numbers: 1 + 2 + ... + n
pub fn sum_naturals(n: Int) -> Int {
n * (n + 1) / 2
}
/// Sum of first n squares: 1^2 + 2^2 + ... + n^2
pub fn sum_squares(n: Int) -> Int {
n * (n + 1) * (2 * n + 1) / 6
}
/// Binomial coefficient C(n, k) = n! / (k! * (n-k)!)
pub fn binomial(n: Int, k: Int) -> Int {
if k < 0 || k > n {
return 0;
}
// Use the smaller of k and n-k for efficiency
let k_eff = if k > n - k { n - k } else { k };
let mut result = 1;
let mut i = 0;
while i < k_eff {
result = result * (n - i) / (i + 1);
i = i + 1;
}
result
}
// ============================================================================
// Statistics helpers
// ============================================================================
/// Arithmetic mean of a list of floats
pub fn mean(values: [Float]) -> Float {
let n = len(values);
if n == 0 {
return 0.0;
}
let mut tot = 0.0;
for v in values {
tot = tot + v;
}
tot / to_float(n)
}
/// Sum of a list of floats
pub fn sum_float(values: [Float]) -> Float {
let mut tot = 0.0;
for v in values {
tot = tot + v;
}
tot
}