-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMath.ark
More file actions
572 lines (503 loc) · 16.3 KB
/
Math.ark
File metadata and controls
572 lines (503 loc) · 16.3 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# @brief Calculate e^number
# @param value the Number
# =begin
# (math:exp 1) # 2.7182...
# =end
# @author https://github.com/SuperFola
(let exp (fun (_x) (builtin__math:exp _x)))
# @brief Calculate the logarithm of a number
# @param value the Number
# =begin
# (math:ln 1) # 0
# =end
# @author https://github.com/SuperFola
(let ln (fun (_x) (builtin__math:ln _x)))
# @brief Get the smallest possible integer greater than the number
# @param value the Number
# =begin
# (math:ceil 0.2) # 1
# =end
# @author https://github.com/SuperFola
(let ceil (fun (_x) (builtin__math:ceil _x)))
# @brief Get the smallest possible integer equal to the given number
# @param value the Number
# =begin
# (math:floor 1.7) # 1
# =end
# @author https://github.com/SuperFola
(let floor (fun (_x) (builtin__math:floor _x)))
# @brief Get the smallest possible integer equal to or greater than the given number
# @param value the Number
# =begin
# (math:round 0.2) # 0
# (math:round 0.6) # 1
# =end
# @author https://github.com/SuperFola
(let round (fun (_x) (builtin__math:round _x)))
# @brief Check if a Number is NaN
# @param value the Number
# =begin
# (math:NaN? 2) # false
# (math:NaN? math:NaN) # true
# =end
# @author https://github.com/SuperFola
(let NaN? (fun (_x) (builtin__math:NaN? _x)))
# @brief Check if a Number if Inf
# @param value the Number
# =begin
# (math:Inf? 1) # false
# (math:Inf? math:NaN) # false
# =end
# @author https://github.com/SuperFola
(let Inf? (fun (_x) (builtin__math:Inf? _x)))
# @brief Calculate the cosinus of a number
# @param value the Number (radians)
# =begin
# (math:cos 0) # 1
# (math:cos math:pi) # -1
# =end
# @author https://github.com/SuperFola
(let cos (fun (_x) (builtin__math:cos _x)))
# @brief Calculate the sinus of a number
# @param value the Number (radians)
# =begin
# (math:sin 0) # 0
# (math:cos (/ math:pi 2)) # 1
# =end
# @author https://github.com/SuperFola
(let sin (fun (_x) (builtin__math:sin _x)))
# @brief Calculate the tangent of a number
# @param value the Number (radians)
# =begin
# (math:tan 0) # 0
# (math:cos (/ math:pi 4)) # 1
# =end
# @author https://github.com/SuperFola
(let tan (fun (_x) (builtin__math:tan _x)))
# @brief Calculate the arc cosinus of a number
# @param value the Number
# =begin
# (math:arccos 1) # 0
# =end
# @author https://github.com/SuperFola
(let arccos (fun (_x) (builtin__math:arccos _x)))
# @brief Calculate the arc sinus of a number
# @param value the Number
# =begin
# (math:arcsin 1) # 1.570796326794897 (/ math:pi 2)
# =end
# @author https://github.com/SuperFola
(let arcsin (fun (_x) (builtin__math:arcsin _x)))
# @brief Calculate the arc tangent of a number
# @param value the Number
# =begin
# (math:arctan 0) # 0
# =end
# @author https://github.com/SuperFola
(let arctan (fun (_x) (builtin__math:arctan _x)))
# @brief Calculate the hyperbolic cosinus of a number
# @param value the Number
# @author https://github.com/Gryfenfer97
(let cosh (fun (_x) (builtin__math:cosh _x)))
# @brief Calculate the hyperbolic sinus of a number
# @param value the Number
# @author https://github.com/Gryfenfer97
(let sinh (fun (_x) (builtin__math:sinh _x)))
# @brief Calculate the hyperbolic tangent of a number
# @param value the Number
# @author https://github.com/Gryfenfer97
(let tanh (fun (_x) (builtin__math:tanh _x)))
# @brief Calculate the hyperbolic arc cosinus of a number
# @param value the Number
# @author https://github.com/Gryfenfer97
(let acosh (fun (_x) (builtin__math:acosh _x)))
# @brief Calculate the hyperbolic arc sinus of a number
# @param value the Number
# @author https://github.com/Gryfenfer97
(let asinh (fun (_x) (builtin__math:asinh _x)))
# @brief Calculate the hyperbolic arc tangent of a number
# @param value the Number
# @author https://github.com/Gryfenfer97
(let atanh (fun (_x) (builtin__math:atanh _x)))
# @brief Pi value (3.14159...)
# @author https://github.com/SuperFola
(let pi builtin__math:pi)
# @brief E value (2.7182...)
# @author https://github.com/SuperFola
(let e builtin__math:e)
# @brief Tau, the ratio of the circumference to the radius of a circle, which is equal to 2*pi (6.28318...)
# @author https://github.com/SuperFola
(let tau builtin__math:tau)
# @brief Float infinite value
# @author https://github.com/SuperFola
(let Inf builtin__math:Inf)
# @brief Float not-a-number value
# @author https://github.com/SuperFola
(let NaN builtin__math:NaN)
# @brief Return the absolute value of a number
# @param _x the number to get the absolute value of
# @author https://github.com/rstefanic
(let abs (fun (_x)
(if (< _x 0)
(* -1 _x)
_x)))
# @brief Return true if the number is even, false otherwise
# @param _n the number
# @author https://github.com/rstefanic
(let even? (fun (_n) (= 0 (mod _n 2))))
# @brief Return true if the number is even, false otherwise
# @deprecated Use `math:even?`
# @param _n the number
# @author https://github.com/rstefanic
(let even even?)
# @brief Return true if the number is odd, false otherwise
# @param _n the number
# @author https://github.com/rstefanic
(let odd? (fun (_n) (= 1 (abs (mod _n 2)))))
# @brief Return true if the number is odd, false otherwise
# @deprecated Use `math:odd?`
# @param _n the number
# @author https://github.com/rstefanic
(let odd odd?)
# @brief Get the minimum between two numbers
# @param _a the first number
# @param _b the second number
# @author https://github.com/rstefanic
(let min (fun (_a _b)
(if (< _a _b)
_a
_b)))
# @brief Get the maximum between two numbers
# @param _a the first number
# @param _b the second number
# @author https://github.com/rstefanic
(let max (fun (_a _b)
(if (> _a _b)
_a
_b)))
# @brief Increment a given number by 1
# @param _x number
# @author https://github.com/SuperFola
(let increment (fun (_x) (+ 1 _x)))
# @brief Decrement a given number by 1
# @param _x number
# @author https://github.com/SuperFola
(let decrement (fun (_x) (- _x 1)))
# @brief Multiply a number by -1, turning positive numbers negative and negative numbers positive
# @param _x number
# @author https://github.com/SuperFola
(let negate (fun (_x) (* -1 _x)))
# @brief Get a number to a given power
# @details Note that it's defined as exp(a * ln(x)), thus won't work for negative numbers
# @param _x the number to pow
# @param _a the exponent
# @author https://github.com/SuperFola
(let pow (fun (_x _a) (exp (* _a (ln _x)))))
# @brief Get the square root of a number
# @details Square roots can't be taken for negative numbers for obvious reasons.
# @param _x the number
# @author https://github.com/SuperFola
(let sqrt (fun (_x)
(if (= 0 _x)
0
(exp (* 0.5 (ln _x))))))
# @brief Compute the nth fibonacci term
# @param n the number
# @author https://github.com/SuperFola
(let fibo (fun (n) {
(let impl (fun (n p c)
(if (<= n 0)
0
(if (= n 1)
c
(impl (- n 1) c (+ p c))))))
(impl n 0 1) }))
# @brief Check if a given number is prime
# @param n the number
# @author https://github.com/SuperFola
(let prime? (fun (n)
(if (= 2 n)
true
(if (or (= 0 (mod n 2)) (= 1 n) (!= 0 (mod n 1)))
false
{
(let k (ceil (+ 1 (sqrt n))))
(mut i 3)
(mut continue true)
(while (and continue (< i k)) {
(if (= 0 (mod n i))
(set continue false))
(set i (+ 2 i)) })
continue }))))
# @brief Returns the list of a number's divisors
# @param n the number
# =begin
# (math:divs 6) # Returns [1 2 3 6]
# =end
# @author https://github.com/Wafelack
(let divs (fun (n) {
(assert (>= n 1) "divs: n must be greater or equal to 1")
(assert (= 0 (mod n 1)) "divs: n must be an integer")
(mut i 1)
(mut divisors [])
(let top (ceil (/ n 2)))
(while (and (<= i top) (!= top n)) {
(if (= (mod n i) 0) (append! divisors i))
(set i (+ i 1)) })
(append divisors n) }))
# @brief Returns the logarithm base n of a number
# @param x the number
# @param n the base
# =begin
# (math:log 81 3) # Returns 4
# =end
# @author https://github.com/Gryfenfer97
(let log (fun (x n) {
(assert (and (> x 0) (>= n 1)) "log: x must be greater than 0, and n must be greater or equal to 1")
(round (/ (ln x) (ln n))) }))
# @brief Returns the logarithm base 2 of a number
# @param x the number
# =begin
# (math:log2 128) # Returns 7
# =end
# @author https://github.com/Gryfenfer97
(let log2 (fun (x) (log x 2)))
# @brief Returns the logarithm base 10 of a number
# @param x the number
# =begin
# (math:log10 1000) # Returns 3
# =end
# @author https://github.com/Gryfenfer97
(let log10 (fun (x) (log x 10)))
# @brief Returns the quotient of the euclidean division of a and b
# @param a the dividend
# @param b the divisor
# =begin
# (math:floordiv 14 6) # Returns 2
# =end
# @author https://github.com/fabien-zoccola
(let floordiv (fun (a b) (floor (/ a b))))
# @brief Create a complex number
# @param real the real part of the complex number
# @param imag the imaginary value
# =begin
# (let c (math:complex 1 2))
# (print c.real " " c.imag) # 1 2
# =end
# @author https://github.com/SuperFola
(let complex (fun (real imag)
(fun (&real &imag) ())))
# @brief Compute the addition of two complex number
# @param _c0 the first complex number
# @param _c1 the second complex number
# =begin
# (let c (math:complex-add (math:complex 1 2) (math:complex 3 4)))
# (print c.real " " c.imag) # 4 6
# =end
# @author https://github.com/SuperFola
(let complex-add (fun (_c0 _c1) (complex (+ _c0.real _c1.real) (+ _c0.imag _c1.imag))))
# @brief Compute the subtraction of two complex number
# @param _c0 the first complex number
# @param _c1 the second complex number
# =begin
# (let c (math:complex-sub (math:complex 1 2) (math:complex 3 4)))
# (print c.real " " c.imag) # -2 -2
# =end
# @author https://github.com/SuperFola
(let complex-sub (fun (_c0 _c1) (complex (- _c0.real _c1.real) (- _c0.imag _c1.imag))))
# @brief Compute the multiplication of two complex number
# @param _c0 the first complex number
# @param _c1 the second complex number
# =begin
# (let c (math:complex-mul (math:complex 1 2) (math:complex 3 4)))
# (print c.real " " c.imag) # -5 10
# =end
# @author https://github.com/SuperFola
(let complex-mul (fun (_c0 _c1) (complex (+ (* _c0.real _c1.real) (- 0 (* _c0.imag _c1.imag))) (+ (* _c0.real _c1.imag) (* _c1.real _c0.imag)))))
# @brief Compute the conjugate of a complex number
# @param _c the complex number
# =begin
# (let c (math:complex-conjugate (math:complex 1 2)))
# (print c.real " " c.imag) # 1 -2
# =end
# @author https://github.com/SuperFola
(let complex-conjugate (fun (_c) (complex _c.real (- 0 _c.imag))))
# @brief Compute the module of a complex number
# @param _c the complex number
# =begin
# (let c (math:complex-module (math:complex 1 2)))
# (print c) # 2.2360679774997896964...
# =end
# @author https://github.com/SuperFola
(let complex-module (fun (_c) (sqrt (+ (* _c.real _c.real) (* _c.imag _c.imag)))))
# @brief Compute the division of two complex number
# @param _c0 the first complex number
# @param _c1 the second complex number
# =begin
# (let c (math:complex-div (math:complex 1 2) (math:complex 3 4)))
# (print c.real " " c.imag) # 0.44 0.08
# =end
# @author https://github.com/SuperFola
(let complex-div (fun (_c0 _c1) {
(let _conj (complex-conjugate _c1))
(let _top (complex-mul _c0 _conj))
(let _denom (+ (* _c1.real _c1.real) (* _c1.imag _c1.imag)))
(complex (/ _top.real _denom) (/ _top.imag _denom)) }))
# @brief Limit a given value to a range
# @param _x value to limit
# @param _min minimum
# @param _max maximum
# =begin
# (print (math:clamp 5 0 2)) # 2
# (print (math:clamp 6 0 10)) # 6
# =end
# @author https://github.com/SuperFola
(let clamp (fun (_x _min _max) (max _min (min _x _max))))
# @brief Linearly interpolate a value in [0; 1] between two bounds
# @param _x value to interpolate (must be between 0 and 1)
# @param _v0 lower bound
# @param _v1 upper bound
# =begin
# (print (math:lerp 0.22 15 132)) # 40.74
# =end
# @author https://github.com/SuperFola
(let lerp (fun (_x _v0 _v1) (+ (* (- 1 _x) _v0) (* _x _v1))))
# @brief Compute the dot product of two vectors
# @details The vectors must have the same length
# @param _v1 vector 1
# @param _v2 vector 2
# =begin
# (print (math:dotProduct [1 2 3] [4 5 6])) # 32
# =end
# @author https://github.com/SuperFola
(let dotProduct (fun (_v1 _v2) {
(assert (= (len _v1) (len _v2)) "vectors should have the same length")
(mut _result 0)
(mut _i 0)
(while (< _i (len _v1)) {
(set _result (+ _result (* (@ _v1 _i) (@ _v2 _i))))
(set _i (+ 1 _i)) })
_result }))
# @brief Compute a percentage of how much b is better than a
# @details Returns a positive number when _a is bigger than _b, negative otherwise
# @param _a the base
# @param _b new measure to compare
# =begin
# (let base 55) # something takes 55ms to run
# (let new 43) # now it takes 43ms
# (print (math:improvementRatioPercentage base new)) # 27.9069767442
# # 'new' is 27%~ faster than 'base'
# =end
# @author https://github.com/SuperFola
(let improvementRatioPercentage (fun (_a _b) (* 100 (- (/ _a _b) 1))))
# @brief Copy the sign of a given number
# @param _x number
# =begin
# (print (math:copySign 5)) # 1
# (print (math:copySign -3)) # -1
# =end
# @author https://github.com/SuperFola
(let copySign (fun (_x)
(if (< _x 0)
-1
1)))
# @brief Convert an angle in degrees to radians
# @param _degrees angle in degrees
# =begin
# (print (math:radians 90)) # pi/2
# =end
# @author https://github.com/SuperFola
(let radians (fun (_degrees) (/ (* _degrees pi) 180)))
# @brief Convert an angle in radians to degrees
# @param _radians angle in radians
# =begin
# (print (math:radians (/ math:pi 2))) # 90
# =end
# @author https://github.com/SuperFola
(let degrees (fun (_radians) (/ (* 180 _radians) pi)))
# @brief Check if a given number is an integer
# @param _x number
# =begin
# (print (math:integer 1)) # true
# (print (math:integer 1.000001)) # false
# =end
# @author https://github.com/SuperFola
(let integer? (fun (_x) (= (ceil _x) (floor _x))))
# @brief Compute the factorial of a number
# @param _n integer
# =begin
# (print (math:factorial 5)) # 120
# =end
# @author https://github.com/SuperFola
(let factorial (fun ((mut _n))
(if (< _n 0)
0
{
(mut _res 1)
(while (> _n 0) {
(set _res (* _res _n))
(set _n (- _n 1)) })
_res })))
# @brief Compute the binomial coefficient (n k)
# @details Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates to zero when k > n.
# @param _n total number of items
# @param _k number of items that will be picked
# @author https://github.com/SuperFola
(let binomialCoeff (fun (_n _k)
(if (<= _k _n)
(/ (factorial _n) (* (factorial _k) (factorial (- _n _k))))
0)))
# @brief Compute the number of ways to choose k items from n items without repetition and with order
# @details Evaluates to n! / (n - k)! when k <= n and evaluates to zero when k > n.
# @param _n total number of items
# @param _k number of items to pick
# @author https://github.com/SuperFola
(let permutations (fun (_n _k)
(if (<= _k _n)
(/ (factorial _n) (factorial (- _n _k)))
0)))
# @brief Compare two real numbers and return true if the first one is near the second one (1e-7 precision)
# @param _n first number
# @param _target target number
# @author https://github.com/SuperFola
(let close? (fun (_n _target) {
# todo: make epsilon configurable
(let _epsilon 1e-07)
(and (< (- _target _epsilon) _n) (< _n (+ _target _epsilon))) }))
# @brief Compute the euclidean distance between two vectors of the same size
# @param _a first vector, eg [1 4]
# @param _b second vector, eg [5 8]
# =begin
# (let v1 [1 2 3])
# (let v2 [7 9 8])
# (print (math:euclideanDistance v1 v2)) # 10.488088481701517
# =end
# @author https://github.com/SuperFola
(let euclideanDistance (fun (_a _b) {
(assert (and (= (len _a) (len _b)) (!= 0 (len _a))) "Expected non-empty vectors to have the same size")
(mut _dist 0)
(mut _i 0)
(while (< _i (len _a)) {
(let _diff (- (@ _a _i) (@ _b _i)))
(set _dist (+ _dist (* _diff _diff)))
(set _i (+ _i 1)) })
(sqrt _dist) }))
# @brief Compute the greatest common divisor of two numbers
# @param _a number
# @param _b number
# =begin
# (print (math:gcd 48 18)) # 6
# =end
# @author https://github.com/SuperFola
(let gcd (fun (_a _b)
(if (= 0 _b)
_a
(gcd _b (mod _a _b)))))
# @brief Compute the least common multiplier of two numbers
# @param _a number
# @param _b number
# =begin
# (print (math:lcm 4 6)) # 12
# =end
# @author https://github.com/SuperFola
(let lcm (fun (_a _b) (* (abs _a) (/ (abs _b) (gcd _a _b)))))