20
20
////<style>
21
21
//// .katex { font-size: 1.1em; }
22
22
////</style>
23
- ////
23
+ ////
24
24
//// ---
25
- ////
25
+ ////
26
26
//// Arithmetics: A module containing a collection of fundamental mathematical functions relating to simple arithmetics (addition, subtraction, multiplication, etc.), but also number theory.
27
- ////
27
+ ////
28
28
//// * **Division functions**
29
29
//// * [`gcd`](#gcd)
30
30
//// * [`lcm`](#lcm)
40
40
//// * [`int_cumulative_sum`](#int_cumulative_sum)
41
41
//// * [`float_cumulative_product`](#float_cumulative_product)
42
42
//// * [`int_cumulative_product`](#int_cumulative_product)
43
- ////
43
+ ////
44
44
45
45
import gleam/int
46
46
import gleam/list
@@ -57,7 +57,7 @@ import gleam_community/maths/piecewise
57
57
/// </a>
58
58
/// </div>
59
59
///
60
- /// The function calculates the greatest common divisor of two integers
60
+ /// The function calculates the greatest common divisor of two integers
61
61
/// \\(x, y \in \mathbb{Z}\\). The greatest common divisor is the largest positive
62
62
/// integer that is divisible by both \\(x\\) and \\(y\\).
63
63
///
@@ -70,7 +70,7 @@ import gleam_community/maths/piecewise
70
70
/// pub fn example() {
71
71
/// arithmetics.gcd(1, 1)
72
72
/// |> should.equal(1)
73
- ///
73
+ ///
74
74
/// arithmetics.gcd(100, 10)
75
75
/// |> should.equal(10)
76
76
///
@@ -86,8 +86,8 @@ import gleam_community/maths/piecewise
86
86
/// </div>
87
87
///
88
88
pub fn gcd ( x : Int , y : Int ) -> Int {
89
- let absx : Int = piecewise . int_absolute_value ( x )
90
- let absy : Int = piecewise . int_absolute_value ( y )
89
+ let absx = piecewise . int_absolute_value ( x )
90
+ let absy = piecewise . int_absolute_value ( y )
91
91
do_gcd ( absx , absy )
92
92
}
93
93
@@ -107,24 +107,24 @@ fn do_gcd(x: Int, y: Int) -> Int {
107
107
/// </a>
108
108
/// </div>
109
109
///
110
- ///
110
+ ///
111
111
/// Given two integers, \\(x\\) (dividend) and \\(y\\) (divisor), the Euclidean modulo
112
- /// of \\(x\\) by \\(y\\), denoted as \\(x \mod y\\), is the remainder \\(r\\) of the
112
+ /// of \\(x\\) by \\(y\\), denoted as \\(x \mod y\\), is the remainder \\(r\\) of the
113
113
/// division of \\(x\\) by \\(y\\), such that:
114
- ///
114
+ ///
115
115
/// \\[
116
116
/// x = q \cdot y + r \quad \text{and} \quad 0 \leq r < |y|,
117
117
/// \\]
118
- ///
118
+ ///
119
119
/// where \\(q\\) is an integer that represents the quotient of the division.
120
120
///
121
- /// The Euclidean modulo function of two numbers, is the remainder operation most
122
- /// commonly utilized in mathematics. This differs from the standard truncating
123
- /// modulo operation frequently employed in programming via the `%` operator.
124
- /// Unlike the `%` operator, which may return negative results depending on the
125
- /// divisor's sign, the Euclidean modulo function is designed to always yield a
121
+ /// The Euclidean modulo function of two numbers, is the remainder operation most
122
+ /// commonly utilized in mathematics. This differs from the standard truncating
123
+ /// modulo operation frequently employed in programming via the `%` operator.
124
+ /// Unlike the `%` operator, which may return negative results depending on the
125
+ /// divisor's sign, the Euclidean modulo function is designed to always yield a
126
126
/// positive outcome, ensuring consistency with mathematical conventions.
127
- ///
127
+ ///
128
128
/// Note that like the Gleam division operator `/` this will return `0` if one of
129
129
/// the arguments is `0`.
130
130
///
@@ -138,7 +138,7 @@ fn do_gcd(x: Int, y: Int) -> Int {
138
138
/// pub fn example() {
139
139
/// arithmetics.euclidean_modulo(15, 4)
140
140
/// |> should.equal(3)
141
- ///
141
+ ///
142
142
/// arithmetics.euclidean_modulo(-3, -2)
143
143
/// |> should.equal(1)
144
144
///
@@ -168,7 +168,7 @@ pub fn int_euclidean_modulo(x: Int, y: Int) -> Int {
168
168
/// </a>
169
169
/// </div>
170
170
///
171
- /// The function calculates the least common multiple of two integers
171
+ /// The function calculates the least common multiple of two integers
172
172
/// \\(x, y \in \mathbb{Z}\\). The least common multiple is the smallest positive
173
173
/// integer that has both \\(x\\) and \\(y\\) as factors.
174
174
///
@@ -181,7 +181,7 @@ pub fn int_euclidean_modulo(x: Int, y: Int) -> Int {
181
181
/// pub fn example() {
182
182
/// arithmetics.lcm(1, 1)
183
183
/// |> should.equal(1)
184
- ///
184
+ ///
185
185
/// arithmetics.lcm(100, 10)
186
186
/// |> should.equal(100)
187
187
///
@@ -197,8 +197,8 @@ pub fn int_euclidean_modulo(x: Int, y: Int) -> Int {
197
197
/// </div>
198
198
///
199
199
pub fn lcm ( x : Int , y : Int ) -> Int {
200
- let absx : Int = piecewise . int_absolute_value ( x )
201
- let absy : Int = piecewise . int_absolute_value ( y )
200
+ let absx = piecewise . int_absolute_value ( x )
201
+ let absy = piecewise . int_absolute_value ( y )
202
202
absx * absy / do_gcd ( absx , absy )
203
203
}
204
204
@@ -208,7 +208,7 @@ pub fn lcm(x: Int, y: Int) -> Int {
208
208
/// </a>
209
209
/// </div>
210
210
///
211
- /// The function returns all the positive divisors of an integer, including the
211
+ /// The function returns all the positive divisors of an integer, including the
212
212
/// number itself.
213
213
///
214
214
/// <details>
@@ -240,11 +240,11 @@ pub fn divisors(n: Int) -> List(Int) {
240
240
}
241
241
242
242
fn find_divisors ( n : Int ) -> List ( Int ) {
243
- let nabs : Float = piecewise . float_absolute_value ( conversion . int_to_float ( n ) )
243
+ let nabs = piecewise . float_absolute_value ( conversion . int_to_float ( n ) )
244
244
let assert Ok ( sqrt_result ) = elementary . square_root ( nabs )
245
- let max : Int = conversion . float_to_int ( sqrt_result ) + 1
245
+ let max = conversion . float_to_int ( sqrt_result ) + 1
246
246
list . range ( 2 , max )
247
- |> list . fold ( [ 1 , n ] , fn ( acc : List ( Int ) , i : Int ) -> List ( Int ) {
247
+ |> list . fold ( [ 1 , n ] , fn ( acc , i ) {
248
248
case n % i == 0 {
249
249
True -> [ i , n / i , .. acc ]
250
250
False -> acc
@@ -260,7 +260,7 @@ fn find_divisors(n: Int) -> List(Int) {
260
260
/// </a>
261
261
/// </div>
262
262
///
263
- /// The function returns all the positive divisors of an integer, excluding the
263
+ /// The function returns all the positive divisors of an integer, excluding the
264
264
/// number iteself.
265
265
///
266
266
/// <details>
@@ -288,7 +288,7 @@ fn find_divisors(n: Int) -> List(Int) {
288
288
/// </div>
289
289
///
290
290
pub fn proper_divisors ( n : Int ) -> List ( Int ) {
291
- let divisors : List ( Int ) = find_divisors ( n )
291
+ let divisors = find_divisors ( n )
292
292
divisors
293
293
|> list . take ( list . length ( divisors ) - 1 )
294
294
}
@@ -340,12 +340,10 @@ pub fn float_sum(arr: List(Float), weights: option.Option(List(Float))) -> Float
340
340
[ ] , _ -> 0.0
341
341
_ , option . None ->
342
342
arr
343
- |> list . fold ( 0.0 , fn ( acc : Float , a : Float ) -> Float { a +. acc } )
343
+ |> list . fold ( 0.0 , fn ( acc , a ) { a +. acc } )
344
344
_ , option . Some ( warr ) -> {
345
345
list . zip ( arr , warr )
346
- |> list . fold ( 0.0 , fn ( acc : Float , a : # ( Float , Float ) ) -> Float {
347
- pair . first ( a ) *. pair . second ( a ) +. acc
348
- } )
346
+ |> list . fold ( 0.0 , fn ( acc , a ) { pair . first ( a ) *. pair . second ( a ) +. acc } )
349
347
}
350
348
}
351
349
}
@@ -362,7 +360,7 @@ pub fn float_sum(arr: List(Float), weights: option.Option(List(Float))) -> Float
362
360
/// \sum_{i=1}^n x_i
363
361
/// \\]
364
362
///
365
- /// In the formula, \\(n\\) is the length of the list and \\(x_i \in \mathbb{Z}\\) is
363
+ /// In the formula, \\(n\\) is the length of the list and \\(x_i \in \mathbb{Z}\\) is
366
364
/// the value in the input list indexed by \\(i\\).
367
365
///
368
366
/// <details>
@@ -395,7 +393,7 @@ pub fn int_sum(arr: List(Int)) -> Int {
395
393
[ ] -> 0
396
394
_ ->
397
395
arr
398
- |> list . fold ( 0 , fn ( acc : Int , a : Int ) -> Int { a + acc } )
396
+ |> list . fold ( 0 , fn ( acc , a ) { a + acc } )
399
397
}
400
398
}
401
399
@@ -414,7 +412,7 @@ pub fn int_sum(arr: List(Int)) -> Int {
414
412
/// In the formula, \\(n\\) is the length of the list and \\(x_i \in \mathbb{R}\\) is
415
413
/// the value in the input list indexed by \\(i\\), while the \\(w_i \in \mathbb{R}\\)
416
414
/// are corresponding weights (\\(w_i = 1.0\\;\forall i=1...n\\) by default).
417
- ///
415
+ ///
418
416
/// <details>
419
417
/// <summary>Example:</summary>
420
418
///
@@ -451,20 +449,20 @@ pub fn float_product(
451
449
|> Ok
452
450
_ , option . None ->
453
451
arr
454
- |> list . fold ( 1.0 , fn ( acc : Float , a : Float ) -> Float { a *. acc } )
452
+ |> list . fold ( 1.0 , fn ( acc , a ) { a *. acc } )
455
453
|> Ok
456
454
_ , option . Some ( warr ) -> {
457
455
let results =
458
456
list . zip ( arr , warr )
459
- |> list . map ( fn ( a : # ( Float , Float ) ) -> Result ( Float , String ) {
457
+ |> list . map ( fn ( a ) {
460
458
pair . first ( a )
461
459
|> elementary . power ( pair . second ( a ) )
462
460
} )
463
461
|> result . all
464
462
case results {
465
463
Ok ( prods ) ->
466
464
prods
467
- |> list . fold ( 1.0 , fn ( acc : Float , a : Float ) -> Float { a *. acc } )
465
+ |> list . fold ( 1.0 , fn ( acc , a ) { a *. acc } )
468
466
|> Ok
469
467
Error ( msg ) ->
470
468
msg
@@ -486,7 +484,7 @@ pub fn float_product(
486
484
/// \prod_{i=1}^n x_i
487
485
/// \\]
488
486
///
489
- /// In the formula, \\(n\\) is the length of the list and \\(x_i \in \mathbb{Z}\\) is
487
+ /// In the formula, \\(n\\) is the length of the list and \\(x_i \in \mathbb{Z}\\) is
490
488
/// the value in the input list indexed by \\(i\\).
491
489
///
492
490
/// <details>
@@ -519,7 +517,7 @@ pub fn int_product(arr: List(Int)) -> Int {
519
517
[ ] -> 1
520
518
_ ->
521
519
arr
522
- |> list . fold ( 1 , fn ( acc : Int , a : Int ) -> Int { a * acc } )
520
+ |> list . fold ( 1 , fn ( acc , a ) { a * acc } )
523
521
}
524
522
}
525
523
@@ -536,7 +534,7 @@ pub fn int_product(arr: List(Int)) -> Int {
536
534
/// \\]
537
535
///
538
536
/// In the formula, \\(v_j\\) is the \\(j\\)'th element in the cumulative sum of \\(n\\)
539
- /// elements. That is, \\(n\\) is the length of the list and \\(x_i \in \mathbb{R}\\)
537
+ /// elements. That is, \\(n\\) is the length of the list and \\(x_i \in \mathbb{R}\\)
540
538
/// is the value in the input list indexed by \\(i\\). The value \\(v_j\\) is thus the
541
539
/// sum of the \\(1\\) to \\(j\\) first elements in the given list.
542
540
///
@@ -569,7 +567,7 @@ pub fn float_cumulative_sum(arr: List(Float)) -> List(Float) {
569
567
[ ] -> [ ]
570
568
_ ->
571
569
arr
572
- |> list . scan ( 0.0 , fn ( acc : Float , a : Float ) -> Float { a +. acc } )
570
+ |> list . scan ( 0.0 , fn ( acc , a ) { a +. acc } )
573
571
}
574
572
}
575
573
@@ -586,7 +584,7 @@ pub fn float_cumulative_sum(arr: List(Float)) -> List(Float) {
586
584
/// \\]
587
585
///
588
586
/// In the formula, \\(v_j\\) is the \\(j\\)'th element in the cumulative sum of \\(n\\)
589
- /// elements. That is, \\(n\\) is the length of the list and \\(x_i \in \mathbb{Z}\\)
587
+ /// elements. That is, \\(n\\) is the length of the list and \\(x_i \in \mathbb{Z}\\)
590
588
/// is the value in the input list indexed by \\(i\\). The value \\(v_j\\) is thus the
591
589
/// sum of the \\(1\\) to \\(j\\) first elements in the given list.
592
590
///
@@ -619,7 +617,7 @@ pub fn int_cumulative_sum(arr: List(Int)) -> List(Int) {
619
617
[ ] -> [ ]
620
618
_ ->
621
619
arr
622
- |> list . scan ( 0 , fn ( acc : Int , a : Int ) -> Int { a + acc } )
620
+ |> list . scan ( 0 , fn ( acc , a ) { a + acc } )
623
621
}
624
622
}
625
623
@@ -635,10 +633,10 @@ pub fn int_cumulative_sum(arr: List(Int)) -> List(Int) {
635
633
/// v_j = \prod_{i=1}^j x_i \\;\\; \forall j = 1,\dots, n
636
634
/// \\]
637
635
///
638
- /// In the formula, \\(v_j\\) is the \\(j\\)'th element in the cumulative product of
639
- /// \\(n\\) elements. That is, \\(n\\) is the length of the list and
640
- /// \\(x_i \in \mathbb{R}\\) is the value in the input list indexed by \\(i\\). The
641
- /// value \\(v_j\\) is thus the sum of the \\(1\\) to \\(j\\) first elements in the
636
+ /// In the formula, \\(v_j\\) is the \\(j\\)'th element in the cumulative product of
637
+ /// \\(n\\) elements. That is, \\(n\\) is the length of the list and
638
+ /// \\(x_i \in \mathbb{R}\\) is the value in the input list indexed by \\(i\\). The
639
+ /// value \\(v_j\\) is thus the sum of the \\(1\\) to \\(j\\) first elements in the
642
640
/// given list.
643
641
///
644
642
/// <details>
@@ -671,7 +669,7 @@ pub fn float_cumulative_product(arr: List(Float)) -> List(Float) {
671
669
[ ] -> [ ]
672
670
_ ->
673
671
arr
674
- |> list . scan ( 1.0 , fn ( acc : Float , a : Float ) -> Float { a *. acc } )
672
+ |> list . scan ( 1.0 , fn ( acc , a ) { a *. acc } )
675
673
}
676
674
}
677
675
@@ -687,9 +685,9 @@ pub fn float_cumulative_product(arr: List(Float)) -> List(Float) {
687
685
/// v_j = \prod_{i=1}^j x_i \\;\\; \forall j = 1,\dots, n
688
686
/// \\]
689
687
///
690
- /// In the formula, \\(v_j\\) is the \\(j\\)'th element in the cumulative product of
691
- /// \\(n\\) elements. That is, \\(n\\) is the length of the list and
692
- /// \\(x_i \in \mathbb{Z}\\) is the value in the input list indexed by \\(i\\). The
688
+ /// In the formula, \\(v_j\\) is the \\(j\\)'th element in the cumulative product of
689
+ /// \\(n\\) elements. That is, \\(n\\) is the length of the list and
690
+ /// \\(x_i \in \mathbb{Z}\\) is the value in the input list indexed by \\(i\\). The
693
691
/// value \\(v_j\\) is thus the product of the \\(1\\) to \\(j\\) first elements in the
694
692
/// given list.
695
693
///
@@ -723,6 +721,6 @@ pub fn int_cumulative_product(arr: List(Int)) -> List(Int) {
723
721
[ ] -> [ ]
724
722
_ ->
725
723
arr
726
- |> list . scan ( 1 , fn ( acc : Int , a : Int ) -> Int { a * acc } )
724
+ |> list . scan ( 1 , fn ( acc , a ) { a * acc } )
727
725
}
728
726
}
0 commit comments