mirror of
https://github.com/sigmasternchen/gleam-community-maths
synced 2025-03-15 07:59:01 +00:00
♻️ Remove type annotations from lambdas.
This commit is contained in:
parent
4806e56baa
commit
00ad62800f
8 changed files with 101 additions and 119 deletions
|
@ -244,7 +244,7 @@ fn find_divisors(n: Int) -> List(Int) {
|
||||||
let assert Ok(sqrt_result) = elementary.square_root(nabs)
|
let assert Ok(sqrt_result) = elementary.square_root(nabs)
|
||||||
let max = conversion.float_to_int(sqrt_result) + 1
|
let max = conversion.float_to_int(sqrt_result) + 1
|
||||||
list.range(2, max)
|
list.range(2, max)
|
||||||
|> list.fold([1, n], fn(acc: List(Int), i: Int) -> List(Int) {
|
|> list.fold([1, n], fn(acc, i) {
|
||||||
case n % i == 0 {
|
case n % i == 0 {
|
||||||
True -> [i, n / i, ..acc]
|
True -> [i, n / i, ..acc]
|
||||||
False -> acc
|
False -> acc
|
||||||
|
@ -340,10 +340,10 @@ pub fn float_sum(arr: List(Float), weights: option.Option(List(Float))) -> Float
|
||||||
[], _ -> 0.0
|
[], _ -> 0.0
|
||||||
_, option.None ->
|
_, option.None ->
|
||||||
arr
|
arr
|
||||||
|> list.fold(0.0, fn(acc: Float, a: Float) -> Float { a +. acc })
|
|> list.fold(0.0, fn(acc, a) { a +. acc })
|
||||||
_, option.Some(warr) -> {
|
_, option.Some(warr) -> {
|
||||||
list.zip(arr, warr)
|
list.zip(arr, warr)
|
||||||
|> list.fold(0.0, fn(acc: Float, a: #(Float, Float)) -> Float {
|
|> list.fold(0.0, fn(acc: Float, a) {
|
||||||
pair.first(a) *. pair.second(a) +. acc
|
pair.first(a) *. pair.second(a) +. acc
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -395,7 +395,7 @@ pub fn int_sum(arr: List(Int)) -> Int {
|
||||||
[] -> 0
|
[] -> 0
|
||||||
_ ->
|
_ ->
|
||||||
arr
|
arr
|
||||||
|> list.fold(0, fn(acc: Int, a: Int) -> Int { a + acc })
|
|> list.fold(0, fn(acc, a) { a + acc })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -451,12 +451,12 @@ pub fn float_product(
|
||||||
|> Ok
|
|> Ok
|
||||||
_, option.None ->
|
_, option.None ->
|
||||||
arr
|
arr
|
||||||
|> list.fold(1.0, fn(acc: Float, a: Float) -> Float { a *. acc })
|
|> list.fold(1.0, fn(acc, a) { a *. acc })
|
||||||
|> Ok
|
|> Ok
|
||||||
_, option.Some(warr) -> {
|
_, option.Some(warr) -> {
|
||||||
let results =
|
let results =
|
||||||
list.zip(arr, warr)
|
list.zip(arr, warr)
|
||||||
|> list.map(fn(a: #(Float, Float)) -> Result(Float, String) {
|
|> list.map(fn(a) {
|
||||||
pair.first(a)
|
pair.first(a)
|
||||||
|> elementary.power(pair.second(a))
|
|> elementary.power(pair.second(a))
|
||||||
})
|
})
|
||||||
|
@ -464,7 +464,7 @@ pub fn float_product(
|
||||||
case results {
|
case results {
|
||||||
Ok(prods) ->
|
Ok(prods) ->
|
||||||
prods
|
prods
|
||||||
|> list.fold(1.0, fn(acc: Float, a: Float) -> Float { a *. acc })
|
|> list.fold(1.0, fn(acc, a) { a *. acc })
|
||||||
|> Ok
|
|> Ok
|
||||||
Error(msg) ->
|
Error(msg) ->
|
||||||
msg
|
msg
|
||||||
|
@ -519,7 +519,7 @@ pub fn int_product(arr: List(Int)) -> Int {
|
||||||
[] -> 1
|
[] -> 1
|
||||||
_ ->
|
_ ->
|
||||||
arr
|
arr
|
||||||
|> list.fold(1, fn(acc: Int, a: Int) -> Int { a * acc })
|
|> list.fold(1, fn(acc, a) { a * acc })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -569,7 +569,7 @@ pub fn float_cumulative_sum(arr: List(Float)) -> List(Float) {
|
||||||
[] -> []
|
[] -> []
|
||||||
_ ->
|
_ ->
|
||||||
arr
|
arr
|
||||||
|> list.scan(0.0, fn(acc: Float, a: Float) -> Float { a +. acc })
|
|> list.scan(0.0, fn(acc, a) { a +. acc })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -619,7 +619,7 @@ pub fn int_cumulative_sum(arr: List(Int)) -> List(Int) {
|
||||||
[] -> []
|
[] -> []
|
||||||
_ ->
|
_ ->
|
||||||
arr
|
arr
|
||||||
|> list.scan(0, fn(acc: Int, a: Int) -> Int { a + acc })
|
|> list.scan(0, fn(acc, a) { a + acc })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -671,7 +671,7 @@ pub fn float_cumulative_product(arr: List(Float)) -> List(Float) {
|
||||||
[] -> []
|
[] -> []
|
||||||
_ ->
|
_ ->
|
||||||
arr
|
arr
|
||||||
|> list.scan(1.0, fn(acc: Float, a: Float) -> Float { a *. acc })
|
|> list.scan(1.0, fn(acc, a) { a *. acc })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -723,6 +723,6 @@ pub fn int_cumulative_product(arr: List(Int)) -> List(Int) {
|
||||||
[] -> []
|
[] -> []
|
||||||
_ ->
|
_ ->
|
||||||
arr
|
arr
|
||||||
|> list.scan(1, fn(acc: Int, a: Int) -> Int { a * acc })
|
|> list.scan(1, fn(acc, a) { a * acc })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,12 +20,12 @@
|
||||||
////<style>
|
////<style>
|
||||||
//// .katex { font-size: 1.1em; }
|
//// .katex { font-size: 1.1em; }
|
||||||
////</style>
|
////</style>
|
||||||
////
|
////
|
||||||
//// ---
|
//// ---
|
||||||
////
|
////
|
||||||
//// Combinatorics: A module that offers mathematical functions related to counting, arrangements,
|
//// Combinatorics: A module that offers mathematical functions related to counting, arrangements,
|
||||||
//// and permutations/combinations.
|
//// and permutations/combinations.
|
||||||
////
|
////
|
||||||
//// * **Combinatorial functions**
|
//// * **Combinatorial functions**
|
||||||
//// * [`combination`](#combination)
|
//// * [`combination`](#combination)
|
||||||
//// * [`factorial`](#factorial)
|
//// * [`factorial`](#factorial)
|
||||||
|
@ -33,7 +33,7 @@
|
||||||
//// * [`list_combination`](#list_combination)
|
//// * [`list_combination`](#list_combination)
|
||||||
//// * [`list_permutation`](#list_permutation)
|
//// * [`list_permutation`](#list_permutation)
|
||||||
//// * [`cartesian_product`](#cartesian_product)
|
//// * [`cartesian_product`](#cartesian_product)
|
||||||
////
|
////
|
||||||
|
|
||||||
import gleam/iterator
|
import gleam/iterator
|
||||||
import gleam/list
|
import gleam/list
|
||||||
|
@ -70,26 +70,26 @@ pub type CombinatoricsMode {
|
||||||
/// Also known as the "stars and bars" problem in combinatorics.
|
/// Also known as the "stars and bars" problem in combinatorics.
|
||||||
///
|
///
|
||||||
/// The implementation uses an efficient iterative multiplicative formula for computing the result.
|
/// The implementation uses an efficient iterative multiplicative formula for computing the result.
|
||||||
///
|
///
|
||||||
/// <details>
|
/// <details>
|
||||||
/// <summary>Details</summary>
|
/// <summary>Details</summary>
|
||||||
///
|
///
|
||||||
/// A \\(k\\)-combination is a sequence of \\(k\\) elements selected from \\(n\\) elements where
|
/// A \\(k\\)-combination is a sequence of \\(k\\) elements selected from \\(n\\) elements where
|
||||||
/// the order of selection does not matter. For example, consider selecting 2 elements from a list
|
/// the order of selection does not matter. For example, consider selecting 2 elements from a list
|
||||||
/// of 3 elements: `["A", "B", "C"]`:
|
/// of 3 elements: `["A", "B", "C"]`:
|
||||||
///
|
///
|
||||||
/// - For \\(k\\)-combinations (without repetitions), where order does not matter, the possible
|
/// - For \\(k\\)-combinations (without repetitions), where order does not matter, the possible
|
||||||
/// selections are:
|
/// selections are:
|
||||||
/// - `["A", "B"]`
|
/// - `["A", "B"]`
|
||||||
/// - `["A", "C"]`
|
/// - `["A", "C"]`
|
||||||
/// - `["B", "C"]`
|
/// - `["B", "C"]`
|
||||||
///
|
///
|
||||||
/// - For \\(k\\)-combinations (with repetitions), where order does not matter but elements can
|
/// - For \\(k\\)-combinations (with repetitions), where order does not matter but elements can
|
||||||
/// repeat, the possible selections are:
|
/// repeat, the possible selections are:
|
||||||
/// - `["A", "A"], ["A", "B"], ["A", "C"]`
|
/// - `["A", "A"], ["A", "B"], ["A", "C"]`
|
||||||
/// - `["B", "B"], ["B", "C"], ["C", "C"]`
|
/// - `["B", "B"], ["B", "C"], ["C", "C"]`
|
||||||
///
|
///
|
||||||
/// - On the contrary, for \\(k\\)-permutations (without repetitions), the order matters, so the
|
/// - On the contrary, for \\(k\\)-permutations (without repetitions), the order matters, so the
|
||||||
/// possible selections are:
|
/// possible selections are:
|
||||||
/// - `["A", "B"], ["B", "A"]`
|
/// - `["A", "B"], ["B", "A"]`
|
||||||
/// - `["A", "C"], ["C", "A"]`
|
/// - `["A", "C"], ["C", "A"]`
|
||||||
|
@ -106,15 +106,15 @@ pub type CombinatoricsMode {
|
||||||
/// // Invalid input gives an error
|
/// // Invalid input gives an error
|
||||||
/// combinatorics.combination(-1, 1, option.None)
|
/// combinatorics.combination(-1, 1, option.None)
|
||||||
/// |> should.be_error()
|
/// |> should.be_error()
|
||||||
///
|
///
|
||||||
/// // Valid input: n = 4 and k = 0
|
/// // Valid input: n = 4 and k = 0
|
||||||
/// combinatorics.combination(4, 0, option.Some(combinatorics.WithoutRepetitions))
|
/// combinatorics.combination(4, 0, option.Some(combinatorics.WithoutRepetitions))
|
||||||
/// |> should.equal(Ok(1))
|
/// |> should.equal(Ok(1))
|
||||||
///
|
///
|
||||||
/// // Valid input: k = n (n = 4, k = 4)
|
/// // Valid input: k = n (n = 4, k = 4)
|
||||||
/// combinatorics.combination(4, 4, option.Some(combinatorics.WithoutRepetitions))
|
/// combinatorics.combination(4, 4, option.Some(combinatorics.WithoutRepetitions))
|
||||||
/// |> should.equal(Ok(1))
|
/// |> should.equal(Ok(1))
|
||||||
///
|
///
|
||||||
/// // Valid input: combinations with repetition (n = 2, k = 3)
|
/// // Valid input: combinations with repetition (n = 2, k = 3)
|
||||||
/// combinatorics.combination(2, 3, option.Some(combinatorics.WithRepetitions))
|
/// combinatorics.combination(2, 3, option.Some(combinatorics.WithRepetitions))
|
||||||
/// |> should.equal(Ok(4))
|
/// |> should.equal(Ok(4))
|
||||||
|
@ -125,7 +125,7 @@ pub type CombinatoricsMode {
|
||||||
/// <small>Back to top ↑</small>
|
/// <small>Back to top ↑</small>
|
||||||
/// </a>
|
/// </a>
|
||||||
/// </div>
|
/// </div>
|
||||||
///
|
///
|
||||||
pub fn combination(
|
pub fn combination(
|
||||||
n: Int,
|
n: Int,
|
||||||
k: Int,
|
k: Int,
|
||||||
|
@ -161,7 +161,7 @@ fn combination_without_repetitions(n: Int, k: Int) -> Result(Int, String) {
|
||||||
False -> n - k
|
False -> n - k
|
||||||
}
|
}
|
||||||
list.range(1, min)
|
list.range(1, min)
|
||||||
|> list.fold(1, fn(acc: Int, x: Int) -> Int { acc * { n + 1 - x } / x })
|
|> list.fold(1, fn(acc, x) { acc * { n + 1 - x } / x })
|
||||||
|> Ok
|
|> Ok
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -215,7 +215,7 @@ pub fn factorial(n) -> Result(Int, String) {
|
||||||
|> Ok
|
|> Ok
|
||||||
_ ->
|
_ ->
|
||||||
list.range(1, n)
|
list.range(1, n)
|
||||||
|> list.fold(1, fn(acc: Int, x: Int) -> Int { acc * x })
|
|> list.fold(1, fn(acc, x) { acc * x })
|
||||||
|> Ok
|
|> Ok
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -227,50 +227,50 @@ pub fn factorial(n) -> Result(Int, String) {
|
||||||
/// </div>
|
/// </div>
|
||||||
///
|
///
|
||||||
/// A combinatorial function for computing the number of \\(k\\)-permutations.
|
/// A combinatorial function for computing the number of \\(k\\)-permutations.
|
||||||
///
|
///
|
||||||
/// **Without** repetitions:
|
/// **Without** repetitions:
|
||||||
///
|
///
|
||||||
/// \\[
|
/// \\[
|
||||||
/// P(n, k) = \binom{n}{k} \cdot k! = \frac{n!}{(n - k)!}
|
/// P(n, k) = \binom{n}{k} \cdot k! = \frac{n!}{(n - k)!}
|
||||||
/// \\]
|
/// \\]
|
||||||
///
|
///
|
||||||
/// **With** repetitions:
|
/// **With** repetitions:
|
||||||
///
|
///
|
||||||
/// \\[
|
/// \\[
|
||||||
/// P^*(n, k) = n^k
|
/// P^*(n, k) = n^k
|
||||||
/// \\]
|
/// \\]
|
||||||
///
|
///
|
||||||
/// The implementation uses an efficient iterative multiplicative formula for computing the result.
|
/// The implementation uses an efficient iterative multiplicative formula for computing the result.
|
||||||
///
|
///
|
||||||
/// <details>
|
/// <details>
|
||||||
/// <summary>Details</summary>
|
/// <summary>Details</summary>
|
||||||
///
|
///
|
||||||
/// A \\(k\\)-permutation (without repetitions) is a sequence of \\(k\\) elements selected from \
|
/// A \\(k\\)-permutation (without repetitions) is a sequence of \\(k\\) elements selected from \
|
||||||
/// \\(n\\) elements where the order of selection matters. For example, consider selecting 2
|
/// \\(n\\) elements where the order of selection matters. For example, consider selecting 2
|
||||||
/// elements from a list of 3 elements: `["A", "B", "C"]`:
|
/// elements from a list of 3 elements: `["A", "B", "C"]`:
|
||||||
///
|
///
|
||||||
/// - For \\(k\\)-permutations (without repetitions), the order matters, so the possible selections
|
/// - For \\(k\\)-permutations (without repetitions), the order matters, so the possible selections
|
||||||
/// are:
|
/// are:
|
||||||
/// - `["A", "B"], ["B", "A"]`
|
/// - `["A", "B"], ["B", "A"]`
|
||||||
/// - `["A", "C"], ["C", "A"]`
|
/// - `["A", "C"], ["C", "A"]`
|
||||||
/// - `["B", "C"], ["C", "B"]`
|
/// - `["B", "C"], ["C", "B"]`
|
||||||
///
|
///
|
||||||
/// - For \\(k\\)-permutations (with repetitions), the order also matters, but we have repeated
|
/// - For \\(k\\)-permutations (with repetitions), the order also matters, but we have repeated
|
||||||
/// selections:
|
/// selections:
|
||||||
/// - `["A", "A"], ["A", "B"], ["A", "C"]`
|
/// - `["A", "A"], ["A", "B"], ["A", "C"]`
|
||||||
/// - `["B", "A"], ["B", "B"], ["B", "C"]`
|
/// - `["B", "A"], ["B", "B"], ["B", "C"]`
|
||||||
/// - `["C", "A"], ["C", "B"], ["C", "C"]`
|
/// - `["C", "A"], ["C", "B"], ["C", "C"]`
|
||||||
///
|
///
|
||||||
/// - On the contrary, for \\(k\\)-combinations (without repetitions), where order does not matter,
|
/// - On the contrary, for \\(k\\)-combinations (without repetitions), where order does not matter,
|
||||||
/// the possible selections are:
|
/// the possible selections are:
|
||||||
/// - `["A", "B"]`
|
/// - `["A", "B"]`
|
||||||
/// - `["A", "C"]`
|
/// - `["A", "C"]`
|
||||||
/// - `["B", "C"]`
|
/// - `["B", "C"]`
|
||||||
/// </details>
|
/// </details>
|
||||||
///
|
///
|
||||||
/// <details>
|
/// <details>
|
||||||
/// <summary>Example:</summary>
|
/// <summary>Example:</summary>
|
||||||
///
|
///
|
||||||
/// import gleam/option
|
/// import gleam/option
|
||||||
/// import gleeunit/should
|
/// import gleeunit/should
|
||||||
/// import gleam_community/maths/combinatorics
|
/// import gleam_community/maths/combinatorics
|
||||||
|
@ -325,7 +325,7 @@ fn permutation_without_repetitions(n: Int, k: Int) -> Result(Int, String) {
|
||||||
}
|
}
|
||||||
_, _ ->
|
_, _ ->
|
||||||
list.range(0, k - 1)
|
list.range(0, k - 1)
|
||||||
|> list.fold(1, fn(acc: Int, x: Int) -> Int { acc * { n - x } })
|
|> list.fold(1, fn(acc, x) { acc * { n - x } })
|
||||||
|> Ok
|
|> Ok
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -346,11 +346,11 @@ fn permutation_with_repetitions(n: Int, k: Int) -> Result(Int, String) {
|
||||||
/// </a>
|
/// </a>
|
||||||
/// </div>
|
/// </div>
|
||||||
///
|
///
|
||||||
/// Generates all possible combinations of \\(k\\) elements selected from a given list of size
|
/// Generates all possible combinations of \\(k\\) elements selected from a given list of size
|
||||||
/// \\(n\\).
|
/// \\(n\\).
|
||||||
///
|
///
|
||||||
/// The function can handle cases with and without repetitions
|
/// The function can handle cases with and without repetitions
|
||||||
/// (see more details [here](#combination)). Also, note that repeated elements are treated as
|
/// (see more details [here](#combination)). Also, note that repeated elements are treated as
|
||||||
/// distinct.
|
/// distinct.
|
||||||
///
|
///
|
||||||
/// <details>
|
/// <details>
|
||||||
|
@ -370,7 +370,7 @@ fn permutation_with_repetitions(n: Int, k: Int) -> Result(Int, String) {
|
||||||
/// 3,
|
/// 3,
|
||||||
/// option.Some(combinatorics.WithoutRepetitions),
|
/// option.Some(combinatorics.WithoutRepetitions),
|
||||||
/// )
|
/// )
|
||||||
///
|
///
|
||||||
/// result
|
/// result
|
||||||
/// |> iterator.to_list()
|
/// |> iterator.to_list()
|
||||||
/// |> set.from_list()
|
/// |> set.from_list()
|
||||||
|
@ -476,11 +476,11 @@ fn do_list_combination_with_repetitions(
|
||||||
/// </a>
|
/// </a>
|
||||||
/// </div>
|
/// </div>
|
||||||
///
|
///
|
||||||
/// Generates all possible permutations of \\(k\\) elements selected from a given list of size
|
/// Generates all possible permutations of \\(k\\) elements selected from a given list of size
|
||||||
/// \\(n\\).
|
/// \\(n\\).
|
||||||
///
|
///
|
||||||
/// The function can handle cases with and without repetitions
|
/// The function can handle cases with and without repetitions
|
||||||
/// (see more details [here](#permutation)). Also, note that repeated elements are treated as
|
/// (see more details [here](#permutation)). Also, note that repeated elements are treated as
|
||||||
/// distinct.
|
/// distinct.
|
||||||
///
|
///
|
||||||
/// <details>
|
/// <details>
|
||||||
|
@ -500,7 +500,7 @@ fn do_list_combination_with_repetitions(
|
||||||
/// 3,
|
/// 3,
|
||||||
/// option.Some(combinatorics.WithoutRepetitions),
|
/// option.Some(combinatorics.WithoutRepetitions),
|
||||||
/// )
|
/// )
|
||||||
///
|
///
|
||||||
/// result
|
/// result
|
||||||
/// |> iterator.to_list()
|
/// |> iterator.to_list()
|
||||||
/// |> set.from_list()
|
/// |> set.from_list()
|
||||||
|
@ -523,7 +523,7 @@ fn do_list_combination_with_repetitions(
|
||||||
/// </a>
|
/// </a>
|
||||||
/// </div>
|
/// </div>
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
pub fn list_permutation(
|
pub fn list_permutation(
|
||||||
arr: List(a),
|
arr: List(a),
|
||||||
k: Int,
|
k: Int,
|
||||||
|
@ -636,7 +636,7 @@ fn do_list_permutation_with_repetitions(
|
||||||
/// set.from_list([])
|
/// set.from_list([])
|
||||||
/// |> combinatorics.cartesian_product(set.from_list([]))
|
/// |> combinatorics.cartesian_product(set.from_list([]))
|
||||||
/// |> should.equal(set.from_list([]))
|
/// |> should.equal(set.from_list([]))
|
||||||
///
|
///
|
||||||
/// // Cartesian product of two sets with numeric values
|
/// // Cartesian product of two sets with numeric values
|
||||||
/// set.from_list([1.0, 10.0])
|
/// set.from_list([1.0, 10.0])
|
||||||
/// |> combinatorics.cartesian_product(set.from_list([1.0, 2.0]))
|
/// |> combinatorics.cartesian_product(set.from_list([1.0, 2.0]))
|
||||||
|
@ -654,16 +654,9 @@ fn do_list_permutation_with_repetitions(
|
||||||
///
|
///
|
||||||
pub fn cartesian_product(xset: set.Set(a), yset: set.Set(a)) -> set.Set(#(a, a)) {
|
pub fn cartesian_product(xset: set.Set(a), yset: set.Set(a)) -> set.Set(#(a, a)) {
|
||||||
xset
|
xset
|
||||||
|> set.fold(
|
|> set.fold(set.new(), fn(accumulator0: set.Set(#(a, a)), member0: a) {
|
||||||
set.new(),
|
set.fold(yset, accumulator0, fn(accumulator1: set.Set(#(a, a)), member1: a) {
|
||||||
fn(accumulator0: set.Set(#(a, a)), member0: a) -> set.Set(#(a, a)) {
|
set.insert(accumulator1, #(member0, member1))
|
||||||
set.fold(
|
})
|
||||||
yset,
|
})
|
||||||
accumulator0,
|
|
||||||
fn(accumulator1: set.Set(#(a, a)), member1: a) -> set.Set(#(a, a)) {
|
|
||||||
set.insert(accumulator1, #(member0, member1))
|
|
||||||
},
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -182,7 +182,7 @@ pub fn norm(
|
||||||
_, option.None -> {
|
_, option.None -> {
|
||||||
let aggregate =
|
let aggregate =
|
||||||
arr
|
arr
|
||||||
|> list.fold(0.0, fn(accumulator: Float, element: Float) -> Float {
|
|> list.fold(0.0, fn(accumulator, element) {
|
||||||
let assert Ok(result) =
|
let assert Ok(result) =
|
||||||
piecewise.float_absolute_value(element)
|
piecewise.float_absolute_value(element)
|
||||||
|> elementary.power(p)
|
|> elementary.power(p)
|
||||||
|
@ -202,19 +202,16 @@ pub fn norm(
|
||||||
let tuples = list.zip(arr, warr)
|
let tuples = list.zip(arr, warr)
|
||||||
let aggregate =
|
let aggregate =
|
||||||
tuples
|
tuples
|
||||||
|> list.fold(
|
|> list.fold(0.0, fn(accumulator, tuple) {
|
||||||
0.0,
|
let first_element = pair.first(tuple)
|
||||||
fn(accumulator: Float, tuple: #(Float, Float)) -> Float {
|
let second_element = pair.second(tuple)
|
||||||
let first_element = pair.first(tuple)
|
let assert Ok(result) =
|
||||||
let second_element = pair.second(tuple)
|
elementary.power(
|
||||||
let assert Ok(result) =
|
piecewise.float_absolute_value(first_element),
|
||||||
elementary.power(
|
p,
|
||||||
piecewise.float_absolute_value(first_element),
|
)
|
||||||
p,
|
second_element *. result +. accumulator
|
||||||
)
|
})
|
||||||
second_element *. result +. accumulator
|
|
||||||
},
|
|
||||||
)
|
|
||||||
let assert Ok(result) = elementary.power(aggregate, 1.0 /. p)
|
let assert Ok(result) = elementary.power(aggregate, 1.0 /. p)
|
||||||
result
|
result
|
||||||
|> Ok
|
|> Ok
|
||||||
|
@ -370,9 +367,7 @@ pub fn minkowski_distance(
|
||||||
False -> {
|
False -> {
|
||||||
let differences =
|
let differences =
|
||||||
list.zip(xarr, yarr)
|
list.zip(xarr, yarr)
|
||||||
|> list.map(fn(tuple: #(Float, Float)) -> Float {
|
|> list.map(fn(tuple) { pair.first(tuple) -. pair.second(tuple) })
|
||||||
pair.first(tuple) -. pair.second(tuple)
|
|
||||||
})
|
|
||||||
|
|
||||||
let assert Ok(result) = norm(differences, p, weights)
|
let assert Ok(result) = norm(differences, p, weights)
|
||||||
result
|
result
|
||||||
|
@ -496,7 +491,7 @@ pub fn chebyshev_distance(
|
||||||
|> Error
|
|> Error
|
||||||
Ok(_) -> {
|
Ok(_) -> {
|
||||||
list.zip(xarr, yarr)
|
list.zip(xarr, yarr)
|
||||||
|> list.map(fn(tuple: #(Float, Float)) -> Float {
|
|> list.map(fn(tuple) {
|
||||||
{ pair.first(tuple) -. pair.second(tuple) }
|
{ pair.first(tuple) -. pair.second(tuple) }
|
||||||
|> piecewise.float_absolute_value()
|
|> piecewise.float_absolute_value()
|
||||||
})
|
})
|
||||||
|
@ -553,9 +548,7 @@ pub fn mean(arr: List(Float)) -> Result(Float, String) {
|
||||||
_ ->
|
_ ->
|
||||||
arr
|
arr
|
||||||
|> arithmetics.float_sum(option.None)
|
|> arithmetics.float_sum(option.None)
|
||||||
|> fn(a: Float) -> Float {
|
|> fn(a) { a /. conversion.int_to_float(list.length(arr)) }
|
||||||
a /. conversion.int_to_float(list.length(arr))
|
|
||||||
}
|
|
||||||
|> Ok
|
|> Ok
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -684,12 +677,12 @@ pub fn variance(arr: List(Float), ddof: Int) -> Result(Float, String) {
|
||||||
False -> {
|
False -> {
|
||||||
let assert Ok(mean) = mean(arr)
|
let assert Ok(mean) = mean(arr)
|
||||||
arr
|
arr
|
||||||
|> list.map(fn(a: Float) -> Float {
|
|> list.map(fn(a) {
|
||||||
let assert Ok(result) = elementary.power(a -. mean, 2.0)
|
let assert Ok(result) = elementary.power(a -. mean, 2.0)
|
||||||
result
|
result
|
||||||
})
|
})
|
||||||
|> arithmetics.float_sum(option.None)
|
|> arithmetics.float_sum(option.None)
|
||||||
|> fn(a: Float) -> Float {
|
|> fn(a) {
|
||||||
a
|
a
|
||||||
/. {
|
/. {
|
||||||
conversion.int_to_float(list.length(arr))
|
conversion.int_to_float(list.length(arr))
|
||||||
|
@ -1094,9 +1087,7 @@ pub fn cosine_similarity(
|
||||||
|
|
||||||
let numerator_elements =
|
let numerator_elements =
|
||||||
zipped_arr
|
zipped_arr
|
||||||
|> list.map(fn(tuple: #(Float, Float)) -> Float {
|
|> list.map(fn(tuple) { pair.first(tuple) *. pair.second(tuple) })
|
||||||
pair.first(tuple) *. pair.second(tuple)
|
|
||||||
})
|
|
||||||
|
|
||||||
case weights {
|
case weights {
|
||||||
option.None -> {
|
option.None -> {
|
||||||
|
@ -1284,14 +1275,14 @@ pub fn braycurtis_distance(
|
||||||
let zipped_arr = list.zip(xarr, yarr)
|
let zipped_arr = list.zip(xarr, yarr)
|
||||||
let numerator_elements =
|
let numerator_elements =
|
||||||
zipped_arr
|
zipped_arr
|
||||||
|> list.map(fn(tuple: #(Float, Float)) -> Float {
|
|> list.map(fn(tuple) {
|
||||||
piecewise.float_absolute_value({
|
piecewise.float_absolute_value({
|
||||||
pair.first(tuple) -. pair.second(tuple)
|
pair.first(tuple) -. pair.second(tuple)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
let denominator_elements =
|
let denominator_elements =
|
||||||
zipped_arr
|
zipped_arr
|
||||||
|> list.map(fn(tuple: #(Float, Float)) -> Float {
|
|> list.map(fn(tuple) {
|
||||||
piecewise.float_absolute_value({
|
piecewise.float_absolute_value({
|
||||||
pair.first(tuple) +. pair.second(tuple)
|
pair.first(tuple) +. pair.second(tuple)
|
||||||
})
|
})
|
||||||
|
|
|
@ -823,7 +823,7 @@ pub fn int_flip_sign(x: Int) -> Int {
|
||||||
/// </a>
|
/// </a>
|
||||||
/// </div>
|
/// </div>
|
||||||
///
|
///
|
||||||
pub fn minimum(x: a, y: a, compare: fn(a, a) -> order.Order) -> a {
|
pub fn minimum(x: a, y: a, compare: fn(a, a) -> order.Order) {
|
||||||
case compare(x, y) {
|
case compare(x, y) {
|
||||||
order.Lt -> x
|
order.Lt -> x
|
||||||
order.Eq -> x
|
order.Eq -> x
|
||||||
|
@ -869,7 +869,7 @@ pub fn minimum(x: a, y: a, compare: fn(a, a) -> order.Order) -> a {
|
||||||
/// </a>
|
/// </a>
|
||||||
/// </div>
|
/// </div>
|
||||||
///
|
///
|
||||||
pub fn maximum(x: a, y: a, compare: fn(a, a) -> order.Order) -> a {
|
pub fn maximum(x: a, y: a, compare: fn(a, a) -> order.Order) {
|
||||||
case compare(x, y) {
|
case compare(x, y) {
|
||||||
order.Lt -> y
|
order.Lt -> y
|
||||||
order.Eq -> y
|
order.Eq -> y
|
||||||
|
@ -909,7 +909,7 @@ pub fn maximum(x: a, y: a, compare: fn(a, a) -> order.Order) -> a {
|
||||||
/// </a>
|
/// </a>
|
||||||
/// </div>
|
/// </div>
|
||||||
///
|
///
|
||||||
pub fn minmax(x: a, y: a, compare: fn(a, a) -> order.Order) -> #(a, a) {
|
pub fn minmax(x: a, y: a, compare: fn(a, a) -> order.Order) {
|
||||||
#(minimum(x, y, compare), maximum(x, y, compare))
|
#(minimum(x, y, compare), maximum(x, y, compare))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -956,7 +956,7 @@ pub fn list_minimum(
|
||||||
|> Error
|
|> Error
|
||||||
[x, ..rest] ->
|
[x, ..rest] ->
|
||||||
Ok(
|
Ok(
|
||||||
list.fold(rest, x, fn(acc: a, element: a) {
|
list.fold(rest, x, fn(acc, element) {
|
||||||
case compare(element, acc) {
|
case compare(element, acc) {
|
||||||
order.Lt -> element
|
order.Lt -> element
|
||||||
_ -> acc
|
_ -> acc
|
||||||
|
@ -1010,7 +1010,7 @@ pub fn list_maximum(
|
||||||
|> Error
|
|> Error
|
||||||
[x, ..rest] ->
|
[x, ..rest] ->
|
||||||
Ok(
|
Ok(
|
||||||
list.fold(rest, x, fn(acc: a, element: a) {
|
list.fold(rest, x, fn(acc, element) {
|
||||||
case compare(acc, element) {
|
case compare(acc, element) {
|
||||||
order.Lt -> element
|
order.Lt -> element
|
||||||
_ -> acc
|
_ -> acc
|
||||||
|
@ -1073,13 +1073,13 @@ pub fn arg_minimum(
|
||||||
arr
|
arr
|
||||||
|> list_minimum(compare)
|
|> list_minimum(compare)
|
||||||
arr
|
arr
|
||||||
|> list.index_map(fn(element: a, index: Int) -> Int {
|
|> list.index_map(fn(element, index) {
|
||||||
case compare(element, min) {
|
case compare(element, min) {
|
||||||
order.Eq -> index
|
order.Eq -> index
|
||||||
_ -> -1
|
_ -> -1
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|> list.filter(fn(index: Int) -> Bool {
|
|> list.filter(fn(index) {
|
||||||
case index {
|
case index {
|
||||||
-1 -> False
|
-1 -> False
|
||||||
_ -> True
|
_ -> True
|
||||||
|
@ -1143,13 +1143,13 @@ pub fn arg_maximum(
|
||||||
arr
|
arr
|
||||||
|> list_maximum(compare)
|
|> list_maximum(compare)
|
||||||
arr
|
arr
|
||||||
|> list.index_map(fn(element: a, index: Int) -> Int {
|
|> list.index_map(fn(element, index) {
|
||||||
case compare(element, max) {
|
case compare(element, max) {
|
||||||
order.Eq -> index
|
order.Eq -> index
|
||||||
_ -> -1
|
_ -> -1
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|> list.filter(fn(index: Int) -> Bool {
|
|> list.filter(fn(index) {
|
||||||
case index {
|
case index {
|
||||||
-1 -> False
|
-1 -> False
|
||||||
_ -> True
|
_ -> True
|
||||||
|
@ -1210,7 +1210,7 @@ pub fn extrema(
|
||||||
|> Error
|
|> Error
|
||||||
[x, ..rest] ->
|
[x, ..rest] ->
|
||||||
Ok(
|
Ok(
|
||||||
list.fold(rest, #(x, x), fn(acc: #(a, a), element: a) {
|
list.fold(rest, #(x, x), fn(acc, element) {
|
||||||
let first = pair.first(acc)
|
let first = pair.first(acc)
|
||||||
let second = pair.second(acc)
|
let second = pair.second(acc)
|
||||||
case compare(element, first), compare(second, element) {
|
case compare(element, first), compare(second, element) {
|
||||||
|
|
|
@ -135,11 +135,11 @@ fn float_absolute_difference(a: Float, b: Float) -> Float {
|
||||||
/// let rtol = 0.01
|
/// let rtol = 0.01
|
||||||
/// let atol = 0.10
|
/// let atol = 0.10
|
||||||
/// predicates.all_close(xarr, yarr, rtol, atol)
|
/// predicates.all_close(xarr, yarr, rtol, atol)
|
||||||
/// |> fn(zarr: Result(List(Bool), String)) -> Result(Bool, Nil) {
|
/// |> fn(zarr), String)) {
|
||||||
/// case zarr {
|
/// case zarr {
|
||||||
/// Ok(arr) ->
|
/// Ok(arr) ->
|
||||||
/// arr
|
/// arr
|
||||||
/// |> list.all(fn(a: Bool) -> Bool { a })
|
/// |> list.all(fn(a) { a })
|
||||||
/// |> Ok
|
/// |> Ok
|
||||||
/// _ -> Nil |> Error
|
/// _ -> Nil |> Error
|
||||||
/// }
|
/// }
|
||||||
|
@ -168,9 +168,7 @@ pub fn all_close(
|
||||||
|> Error
|
|> Error
|
||||||
True ->
|
True ->
|
||||||
list.zip(xarr, yarr)
|
list.zip(xarr, yarr)
|
||||||
|> list.map(fn(z: #(Float, Float)) -> Bool {
|
|> list.map(fn(z) { is_close(pair.first(z), pair.second(z), rtol, atol) })
|
||||||
is_close(pair.first(z), pair.second(z), rtol, atol)
|
|
||||||
})
|
|
||||||
|> Ok
|
|> Ok
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -304,7 +302,7 @@ fn do_sum(arr: List(Int)) -> Int {
|
||||||
[] -> 0
|
[] -> 0
|
||||||
_ ->
|
_ ->
|
||||||
arr
|
arr
|
||||||
|> list.fold(0, fn(acc: Int, a: Int) -> Int { a + acc })
|
|> list.fold(0, fn(acc, a) { a + acc })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -102,7 +102,7 @@ pub fn arange(
|
||||||
|> conversion.float_to_int()
|
|> conversion.float_to_int()
|
||||||
|
|
||||||
iterator.range(0, num - 1)
|
iterator.range(0, num - 1)
|
||||||
|> iterator.map(fn(i: Int) {
|
|> iterator.map(fn(i) {
|
||||||
start +. conversion.int_to_float(i) *. step_abs *. direction
|
start +. conversion.int_to_float(i) *. step_abs *. direction
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -179,7 +179,7 @@ pub fn linear_space(
|
||||||
case num > 0 {
|
case num > 0 {
|
||||||
True -> {
|
True -> {
|
||||||
iterator.range(0, num - 1)
|
iterator.range(0, num - 1)
|
||||||
|> iterator.map(fn(i: Int) -> Float {
|
|> iterator.map(fn(i) {
|
||||||
start +. conversion.int_to_float(i) *. increment *. direction
|
start +. conversion.int_to_float(i) *. increment *. direction
|
||||||
})
|
})
|
||||||
|> Ok
|
|> Ok
|
||||||
|
@ -246,7 +246,7 @@ pub fn logarithmic_space(
|
||||||
True -> {
|
True -> {
|
||||||
let assert Ok(linspace) = linear_space(start, stop, num, endpoint)
|
let assert Ok(linspace) = linear_space(start, stop, num, endpoint)
|
||||||
linspace
|
linspace
|
||||||
|> iterator.map(fn(i: Float) -> Float {
|
|> iterator.map(fn(i) {
|
||||||
let assert Ok(result) = elementary.power(base, i)
|
let assert Ok(result) = elementary.power(base, i)
|
||||||
result
|
result
|
||||||
})
|
})
|
||||||
|
|
|
@ -132,7 +132,7 @@ fn gamma_lanczos(x: Float) -> Float {
|
||||||
False -> {
|
False -> {
|
||||||
let z = x -. 1.0
|
let z = x -. 1.0
|
||||||
let x =
|
let x =
|
||||||
list.index_fold(lanczos_p, 0.0, fn(acc: Float, v: Float, index: Int) {
|
list.index_fold(lanczos_p, 0.0, fn(acc, v, index) {
|
||||||
case index > 0 {
|
case index > 0 {
|
||||||
True -> acc +. v /. { z +. conversion.int_to_float(index) }
|
True -> acc +. v /. { z +. conversion.int_to_float(index) }
|
||||||
False -> v
|
False -> v
|
||||||
|
|
|
@ -23,11 +23,11 @@ pub fn float_list_all_close_test() {
|
||||||
let rtol = 0.01
|
let rtol = 0.01
|
||||||
let atol = 0.1
|
let atol = 0.1
|
||||||
predicates.all_close(xarr, yarr, rtol, atol)
|
predicates.all_close(xarr, yarr, rtol, atol)
|
||||||
|> fn(zarr: Result(List(Bool), String)) -> Result(Bool, Nil) {
|
|> fn(zarr) {
|
||||||
case zarr {
|
case zarr {
|
||||||
Ok(arr) ->
|
Ok(arr) ->
|
||||||
arr
|
arr
|
||||||
|> list.all(fn(a: Bool) -> Bool { a })
|
|> list.all(fn(a) { a })
|
||||||
|> Ok
|
|> Ok
|
||||||
_ ->
|
_ ->
|
||||||
Nil
|
Nil
|
||||||
|
|
Loading…
Reference in a new issue