Skip to content

Commit 7fd4d74

Browse files
authored
Merge pull request #26 from NicklasXYZ/main
Update the combinatorics and sequences module
2 parents 7fd917f + 15f4c0b commit 7fd4d74

12 files changed

+1642
-726
lines changed

README.md

+26-20
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,49 @@ The library supports both targets: Erlang and JavaScript.
1010
## Quickstart
1111

1212
```gleam
13+
import gleam/float
14+
import gleam/iterator
15+
import gleam/option.{Some}
1316
import gleam_community/maths/arithmetics
14-
import gleam_community/maths/combinatorics
17+
import gleam_community/maths/combinatorics.{WithoutRepetitions}
1518
import gleam_community/maths/elementary
1619
import gleam_community/maths/piecewise
1720
import gleam_community/maths/predicates
18-
import gleam/float
19-
import gleam/int
21+
import gleeunit/should
2022
21-
pub fn main() {
23+
pub fn example() {
2224
// Evaluate the sine function
23-
elementary.sin(elementary.pi())
24-
// Returns Float: 0.0
25+
let result = elementary.sin(elementary.pi())
26+
27+
// Set the relative and absolute tolerance
28+
let assert Ok(absolute_tol) = elementary.power(10.0, -6.0)
29+
let relative_tol = 0.0
30+
31+
// Check that the value is very close to 0.0
32+
// That is, if 'result' is within +/- 10^(-6)
33+
predicates.is_close(result, 0.0, relative_tol, absolute_tol)
34+
|> should.be_true()
2535
2636
// Find the greatest common divisor
2737
arithmetics.gcd(54, 24)
28-
// Returns Int: 6
38+
|> should.equal(6)
2939
3040
// Find the minimum and maximum of a list
3141
piecewise.extrema([10.0, 3.0, 50.0, 20.0, 3.0], float.compare)
32-
// Returns Tuple: Ok(#(3.0, 50.0))
33-
34-
// Find the list indices of the smallest value
35-
piecewise.arg_minimum([10, 3, 50, 20, 3], int.compare)
36-
// Returns List: Ok([1, 4])
42+
|> should.equal(Ok(#(3.0, 50.0)))
3743
3844
// Determine if a number is fractional
3945
predicates.is_fractional(0.3333)
40-
// Returns Bool: True
41-
42-
// Determine if 28 is a power of 3
43-
predicates.is_power(28, 3)
44-
// Returns Bool: False
46+
|> should.equal(True)
4547
46-
// Generate all k = 1 combinations of [1, 2]
47-
combinatorics.list_combination([1, 2], 1)
48-
// Returns List: Ok([[1], [2]])
48+
// Generate all k = 2 combinations of [1, 2, 3]
49+
let assert Ok(combinations) =
50+
combinatorics.list_combination([1, 2, 3], 2, Some(WithoutRepetitions))
51+
combinations
52+
|> iterator.to_list()
53+
|> should.equal([[1, 2], [1, 3], [2, 3]])
4954
}
55+
5056
```
5157

5258
## Installation

src/gleam_community/maths/arithmetics.gleam

+40-40
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
////<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.10/dist/katex.min.css" integrity="sha384-wcIxkf4k558AjM3Yz3BBFQUbk/zgIYC2R0QpeeYb+TwlBVMrlgLqwRjRtGZiK7ww" crossorigin="anonymous">
2-
////<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.10/dist/katex.min.js" integrity="sha384-hIoBPJpTUs74ddyc4bFZSM1TVlQDA60VBbJS0oA934VSz82sBx1X7kSx2ATBDIyd" crossorigin="anonymous"></script>
3-
////<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.10/dist/contrib/auto-render.min.js" integrity="sha384-43gviWU0YVjaDtb/GhzOouOXtZMP/7XUzwPTstBeZFe/+rCMvRwr4yROQP43s0Xk" crossorigin="anonymous"></script>
1+
////<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css" integrity="sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+" crossorigin="anonymous">
2+
////<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js" integrity="sha384-7zkQWkzuo3B5mTepMUcHkMB5jZaolc2xDwL6VFqjFALcbeS9Ggm/Yr2r3Dy4lfFg" crossorigin="anonymous"></script>
3+
////<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/auto-render.min.js" integrity="sha384-43gviWU0YVjaDtb/GhzOouOXtZMP/7XUzwPTstBeZFe/+rCMvRwr4yROQP43s0Xk" crossorigin="anonymous"></script>
44
////<script>
55
//// document.addEventListener("DOMContentLoaded", function() {
66
//// renderMathInElement(document.body, {
77
//// // customised options
88
//// // • auto-render specific keys, e.g.:
99
//// delimiters: [
1010
//// {left: '$$', right: '$$', display: false},
11-
//// // {left: '$', right: '$', display: false},
12-
//// // {left: '\\(', right: '\\)', display: false},
11+
//// {left: '$', right: '$', display: false},
12+
//// {left: '\\(', right: '\\)', display: false},
1313
//// {left: '\\[', right: '\\]', display: true}
1414
//// ],
1515
//// // • rendering keys, e.g.:
16-
//// throwOnError : false
16+
//// throwOnError : true
1717
//// });
1818
//// });
1919
////</script>
@@ -58,8 +58,8 @@ import gleam_community/maths/piecewise
5858
/// </div>
5959
///
6060
/// The function calculates the greatest common divisor of two integers
61-
/// $$x, y \in \mathbb{Z}$$. The greatest common divisor is the largest positive
62-
/// integer that is divisible by both $$x$$ and $$y$$.
61+
/// \\(x, y \in \mathbb{Z}\\). The greatest common divisor is the largest positive
62+
/// integer that is divisible by both \\(x\\) and \\(y\\).
6363
///
6464
/// <details>
6565
/// <summary>Example:</summary>
@@ -108,15 +108,15 @@ fn do_gcd(x: Int, y: Int) -> Int {
108108
/// </div>
109109
///
110110
///
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
113-
/// division of $$x$$ by $$y$$, such that:
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
113+
/// division of \\(x\\) by \\(y\\), such that:
114114
///
115115
/// \\[
116116
/// x = q \cdot y + r \quad \text{and} \quad 0 \leq r < |y|,
117117
/// \\]
118118
///
119-
/// where $$q$$ is an integer that represents the quotient of the division.
119+
/// where \\(q\\) is an integer that represents the quotient of the division.
120120
///
121121
/// The Euclidean modulo function of two numbers, is the remainder operation most
122122
/// commonly utilized in mathematics. This differs from the standard truncating
@@ -169,8 +169,8 @@ pub fn int_euclidean_modulo(x: Int, y: Int) -> Int {
169169
/// </div>
170170
///
171171
/// The function calculates the least common multiple of two integers
172-
/// $$x, y \in \mathbb{Z}$$. The least common multiple is the smallest positive
173-
/// integer that has both $$x$$ and $$y$$ as factors.
172+
/// \\(x, y \in \mathbb{Z}\\). The least common multiple is the smallest positive
173+
/// integer that has both \\(x\\) and \\(y\\) as factors.
174174
///
175175
/// <details>
176176
/// <summary>Example:</summary>
@@ -305,9 +305,9 @@ pub fn proper_divisors(n: Int) -> List(Int) {
305305
/// \sum_{i=1}^n w_i x_i
306306
/// \\]
307307
///
308-
/// In the formula, $$n$$ is the length of the list and $$x_i \in \mathbb{R}$$ is
309-
/// the value in the input list indexed by $$i$$, while the $$w_i \in \mathbb{R}$$
310-
/// are corresponding weights ($$w_i = 1.0\\;\forall i=1...n$$ by default).
308+
/// In the formula, \\(n\\) is the length of the list and \\(x_i \in \mathbb{R}\\) is
309+
/// the value in the input list indexed by \\(i\\), while the \\(w_i \in \mathbb{R}\\)
310+
/// are corresponding weights (\\(w_i = 1.0\\;\forall i=1...n\\) by default).
311311
///
312312
/// <details>
313313
/// <summary>Example:</summary>
@@ -362,8 +362,8 @@ pub fn float_sum(arr: List(Float), weights: option.Option(List(Float))) -> Float
362362
/// \sum_{i=1}^n x_i
363363
/// \\]
364364
///
365-
/// In the formula, $$n$$ is the length of the list and $$x_i \in \mathbb{Z}$$ is
366-
/// the value in the input list indexed by $$i$$.
365+
/// In the formula, \\(n\\) is the length of the list and \\(x_i \in \mathbb{Z}\\) is
366+
/// the value in the input list indexed by \\(i\\).
367367
///
368368
/// <details>
369369
/// <summary>Example:</summary>
@@ -411,9 +411,9 @@ pub fn int_sum(arr: List(Int)) -> Int {
411411
/// \prod_{i=1}^n x_i^{w_i}
412412
/// \\]
413413
///
414-
/// In the formula, $$n$$ is the length of the list and $$x_i \in \mathbb{R}$$ is
415-
/// the value in the input list indexed by $$i$$, while the $$w_i \in \mathbb{R}$$
416-
/// are corresponding weights ($$w_i = 1.0\\;\forall i=1...n$$ by default).
414+
/// In the formula, \\(n\\) is the length of the list and \\(x_i \in \mathbb{R}\\) is
415+
/// the value in the input list indexed by \\(i\\), while the \\(w_i \in \mathbb{R}\\)
416+
/// are corresponding weights (\\(w_i = 1.0\\;\forall i=1...n\\) by default).
417417
///
418418
/// <details>
419419
/// <summary>Example:</summary>
@@ -486,8 +486,8 @@ pub fn float_product(
486486
/// \prod_{i=1}^n x_i
487487
/// \\]
488488
///
489-
/// In the formula, $$n$$ is the length of the list and $$x_i \in \mathbb{Z}$$ is
490-
/// the value in the input list indexed by $$i$$.
489+
/// In the formula, \\(n\\) is the length of the list and \\(x_i \in \mathbb{Z}\\) is
490+
/// the value in the input list indexed by \\(i\\).
491491
///
492492
/// <details>
493493
/// <summary>Example:</summary>
@@ -535,10 +535,10 @@ pub fn int_product(arr: List(Int)) -> Int {
535535
/// v_j = \sum_{i=1}^j x_i \\;\\; \forall j = 1,\dots, n
536536
/// \\]
537537
///
538-
/// 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}$$
540-
/// is the value in the input list indexed by $$i$$. The value $$v_j$$ is thus the
541-
/// sum of the $$1$$ to $$j$$ first elements in the given list.
538+
/// 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}\\)
540+
/// is the value in the input list indexed by \\(i\\). The value \\(v_j\\) is thus the
541+
/// sum of the \\(1\\) to \\(j\\) first elements in the given list.
542542
///
543543
/// <details>
544544
/// <summary>Example:</summary>
@@ -585,10 +585,10 @@ pub fn float_cumulative_sum(arr: List(Float)) -> List(Float) {
585585
/// v_j = \sum_{i=1}^j x_i \\;\\; \forall j = 1,\dots, n
586586
/// \\]
587587
///
588-
/// 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}$$
590-
/// is the value in the input list indexed by $$i$$. The value $$v_j$$ is thus the
591-
/// sum of the $$1$$ to $$j$$ first elements in the given list.
588+
/// 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}\\)
590+
/// is the value in the input list indexed by \\(i\\). The value \\(v_j\\) is thus the
591+
/// sum of the \\(1\\) to \\(j\\) first elements in the given list.
592592
///
593593
/// <details>
594594
/// <summary>Example:</summary>
@@ -635,10 +635,10 @@ pub fn int_cumulative_sum(arr: List(Int)) -> List(Int) {
635635
/// v_j = \prod_{i=1}^j x_i \\;\\; \forall j = 1,\dots, n
636636
/// \\]
637637
///
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
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
642642
/// given list.
643643
///
644644
/// <details>
@@ -687,10 +687,10 @@ pub fn float_cumulative_product(arr: List(Float)) -> List(Float) {
687687
/// v_j = \prod_{i=1}^j x_i \\;\\; \forall j = 1,\dots, n
688688
/// \\]
689689
///
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
693-
/// value $$v_j$$ is thus the product of the $$1$$ to $$j$$ first elements in the
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
693+
/// value \\(v_j\\) is thus the product of the \\(1\\) to \\(j\\) first elements in the
694694
/// given list.
695695
///
696696
/// <details>

0 commit comments

Comments
 (0)