Upgrade to gleam v0.33

This commit is contained in:
Hayleigh Thompson 2023-12-19 10:10:36 +00:00
parent 20c3d67e6d
commit 5c74fa8b7d
12 changed files with 116 additions and 145 deletions

View file

@ -4,9 +4,10 @@ version = "1.0.1"
licences = ["Apache-2.0"]
description = "A basic maths library"
repository = { type = "github", user = "gleam-community", repo = "maths" }
gleam = ">= 0.33.0"
[dependencies]
gleam_stdlib = "~> 0.33"
gleam_stdlib = "~> 0.34"
[dev-dependencies]
gleeunit = "~> 1.0"

View file

@ -2,10 +2,10 @@
# You typically do not need to edit this file
packages = [
{ name = "gleam_stdlib", version = "0.33.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "3CEAD7B153D896499C78390B22CC968620C27500C922AED3A5DD7B536F922B25" },
{ name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" },
{ name = "gleeunit", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D3682ED8C5F9CAE1C928F2506DE91625588CC752495988CBE0F5653A42A6F334" },
]
[requirements]
gleam_stdlib = { version = "~> 0.33" }
gleam_stdlib = { version = "~> 0.34" }
gleeunit = { version = "~> 1.0" }

View file

@ -176,15 +176,12 @@ fn find_divisors(n: Int) -> List(Int) {
let assert Ok(sqrt_result) = elementary.square_root(nabs)
let max: Int = conversion.float_to_int(sqrt_result) + 1
list.range(2, max)
|> list.fold(
[1, n],
fn(acc: List(Int), i: Int) -> List(Int) {
case n % i == 0 {
True -> [i, n / i, ..acc]
False -> acc
}
},
)
|> list.fold([1, n], fn(acc: List(Int), i: Int) -> List(Int) {
case n % i == 0 {
True -> [i, n / i, ..acc]
False -> acc
}
})
|> list.unique()
|> list.sort(int.compare)
}

View file

@ -103,10 +103,9 @@ pub fn combination(n: Int, k: Int) -> Result(Int, String) {
False -> n - k
}
list.range(1, min)
|> list.fold(
1,
fn(acc: Int, x: Int) -> Int { acc * { n + 1 - x } / x },
)
|> list.fold(1, fn(acc: Int, x: Int) -> Int {
acc * { n + 1 - x } / x
})
|> Ok
}
}
@ -240,7 +239,8 @@ pub fn permutation(n: Int, k: Int) -> Result(Int, String) {
False -> {
let assert Ok(v1) = factorial(n)
let assert Ok(v2) = factorial(n - k)
v1 / v2
v1
/ v2
|> Ok
}
}
@ -416,17 +416,14 @@ pub fn cartesian_product(xarr: List(a), yarr: List(a)) -> List(#(a, a)) {
yarr
|> set.from_list()
xset
|> set.fold(
set.new(),
fn(accumulator0: set.Set(#(a, a)), member0: a) -> set.Set(#(a, a)) {
set.fold(
yset,
accumulator0,
fn(accumulator1: set.Set(#(a, a)), member1: a) -> set.Set(#(a, a)) {
set.insert(accumulator1, #(member0, member1))
},
)
},
)
|> set.fold(set.new(), fn(accumulator0: set.Set(#(a, a)), member0: a) -> set.Set(
#(a, a),
) {
set.fold(yset, accumulator0, fn(accumulator1: set.Set(#(a, a)), member1: a) -> set.Set(
#(a, a),
) {
set.insert(accumulator1, #(member0, member1))
})
})
|> set.to_list()
}

View file

@ -819,7 +819,8 @@ pub fn logarithm(x: Float, base: option.Option(Float)) -> Result(Float, String)
// Apply the "change of base formula"
let assert Ok(numerator) = logarithm_10(x)
let assert Ok(denominator) = logarithm_10(a)
numerator /. denominator
numerator
/. denominator
|> Ok
}
False ->

View file

@ -95,14 +95,11 @@ pub fn norm(arr: List(Float), p: Float) -> Float {
_ -> {
let agg: Float =
arr
|> list.fold(
0.0,
fn(acc: Float, a: Float) -> Float {
let assert Ok(result) =
elementary.power(piecewise.float_absolute_value(a), p)
result +. acc
},
)
|> list.fold(0.0, fn(acc: Float, a: Float) -> Float {
let assert Ok(result) =
elementary.power(piecewise.float_absolute_value(a), p)
result +. acc
})
let assert Ok(result) = elementary.power(agg, 1.0 /. p)
result
}
@ -480,10 +477,10 @@ pub fn variance(arr: List(Float), ddof: Int) -> Result(Float, String) {
})
|> arithmetics.float_sum()
|> fn(a: Float) -> Float {
a /. {
conversion.int_to_float(list.length(arr)) -. conversion.int_to_float(
ddof,
)
a
/. {
conversion.int_to_float(list.length(arr))
-. conversion.int_to_float(ddof)
}
}
|> Ok

View file

@ -445,7 +445,8 @@ fn round_ties_up(p: Float, x: Float) -> Float {
let remainder: Float = xabs -. xabs_truncated
case remainder {
_ if remainder >=. 0.5 && x >=. 0.0 ->
float_sign(x) *. truncate_float(xabs +. 1.0) /. p
float_sign(x) *. truncate_float(xabs +. 1.0)
/. p
_ -> float_sign(x) *. xabs_truncated /. p
}
}
@ -576,7 +577,8 @@ pub fn int_absolute_value(x: Int) -> Int {
/// </div>
///
pub fn float_absolute_difference(a: Float, b: Float) -> Float {
a -. b
a
-. b
|> float_absolute_value()
}
@ -616,7 +618,8 @@ pub fn float_absolute_difference(a: Float, b: Float) -> Float {
/// </div>
///
pub fn int_absolute_difference(a: Int, b: Int) -> Int {
a - b
a
- b
|> int_absolute_value()
}
@ -944,15 +947,12 @@ pub fn list_minimum(
_ -> {
let assert Ok(val0) = list.at(arr, 0)
arr
|> list.fold(
val0,
fn(acc: a, element: a) {
case compare(element, acc) {
order.Lt -> element
_ -> acc
}
},
)
|> list.fold(val0, fn(acc: a, element: a) {
case compare(element, acc) {
order.Lt -> element
_ -> acc
}
})
|> Ok
}
}
@ -1003,15 +1003,12 @@ pub fn list_maximum(
_ -> {
let assert Ok(val0) = list.at(arr, 0)
arr
|> list.fold(
val0,
fn(acc: a, element: a) {
case compare(acc, element) {
order.Lt -> element
_ -> acc
}
},
)
|> list.fold(val0, fn(acc: a, element: a) {
case compare(acc, element) {
order.Lt -> element
_ -> acc
}
})
|> Ok
}
}
@ -1209,19 +1206,16 @@ pub fn extrema(
let assert Ok(val_max) = list.at(arr, 0)
let assert Ok(val_min) = list.at(arr, 0)
arr
|> list.fold(
#(val_min, val_max),
fn(acc: #(a, a), element: a) {
let first: a = pair.first(acc)
let second: a = pair.second(acc)
case compare(element, first), compare(second, element) {
order.Lt, order.Lt -> #(element, element)
order.Lt, _ -> #(element, second)
_, order.Lt -> #(first, element)
_, _ -> #(first, second)
}
},
)
|> list.fold(#(val_min, val_max), fn(acc: #(a, a), element: a) {
let first: a = pair.first(acc)
let second: a = pair.second(acc)
case compare(element, first), compare(second, element) {
order.Lt, order.Lt -> #(element, element)
order.Lt, _ -> #(element, second)
_, order.Lt -> #(first, element)
_, _ -> #(first, second)
}
})
|> Ok
}
}

View file

@ -98,7 +98,8 @@ fn float_absolute_value(x: Float) -> Float {
}
fn float_absolute_difference(a: Float, b: Float) -> Float {
a -. b
a
-. b
|> float_absolute_value()
}

View file

@ -144,9 +144,8 @@ pub fn linear_space(
case endpoint {
True -> {
let increment: Float =
piecewise.float_absolute_value(start -. stop) /. conversion.int_to_float(
num - 1,
)
piecewise.float_absolute_value(start -. stop)
/. conversion.int_to_float(num - 1)
list.range(0, num - 1)
|> list.map(fn(i: Int) -> Float {
start +. conversion.int_to_float(i) *. increment *. direction
@ -155,9 +154,8 @@ pub fn linear_space(
}
False -> {
let increment: Float =
piecewise.float_absolute_value(start -. stop) /. conversion.int_to_float(
num,
)
piecewise.float_absolute_value(start -. stop)
/. conversion.int_to_float(num)
list.range(0, num - 1)
|> list.map(fn(i: Int) -> Float {
start +. conversion.int_to_float(i) *. increment *. direction

View file

@ -87,9 +87,10 @@ pub fn erf(x: Float) -> Float {
// Formula 7.1.26 given in Abramowitz and Stegun.
let t: Float = 1.0 /. { 1.0 +. p *. x }
let y: Float =
1.0 -. { { { { a5 *. t +. a4 } *. t +. a3 } *. t +. a2 } *. t +. a1 } *. t *. elementary.exponential(
-1.0 *. x *. x,
)
1.0
-. { { { { a5 *. t +. a4 } *. t +. a3 } *. t +. a2 } *. t +. a1 }
*. t
*. elementary.exponential(-1.0 *. x *. x)
sign *. y
}
@ -126,22 +127,17 @@ const lanczos_p: List(Float) = [
fn gamma_lanczos(x: Float) -> Float {
case x <. 0.5 {
True ->
elementary.pi() /. {
elementary.sin(elementary.pi() *. x) *. gamma_lanczos(1.0 -. x)
}
elementary.pi()
/. { elementary.sin(elementary.pi() *. x) *. gamma_lanczos(1.0 -. x) }
False -> {
let z = x -. 1.0
let x: Float =
list.index_fold(
lanczos_p,
0.0,
fn(acc: Float, v: Float, index: Int) {
case index > 0 {
True -> acc +. v /. { z +. conversion.int_to_float(index) }
False -> v
}
},
)
list.index_fold(lanczos_p, 0.0, fn(acc: Float, v: Float, index: Int) {
case index > 0 {
True -> acc +. v /. { z +. conversion.int_to_float(index) }
False -> v
}
})
let t: Float = z +. lanczos_g +. 0.5
let assert Ok(v1) = elementary.power(2.0 *. elementary.pi(), 0.5)
let assert Ok(v2) = elementary.power(t, z +. 0.5)
@ -171,13 +167,9 @@ pub fn incomplete_gamma(a: Float, x: Float) -> Result(Float, String) {
case a >. 0.0 && x >=. 0.0 {
True -> {
let assert Ok(v) = elementary.power(x, a)
v *. elementary.exponential(-1.0 *. x) *. incomplete_gamma_sum(
a,
x,
1.0 /. a,
0.0,
1.0,
)
v
*. elementary.exponential(-1.0 *. x)
*. incomplete_gamma_sum(a, x, 1.0 /. a, 0.0, 1.0)
|> Ok
}

View file

@ -80,27 +80,26 @@ pub fn list_cartesian_product_test() {
[1, 2, 3]
|> combinatorics.cartesian_product([1, 2, 3])
|> set.from_list()
|> should.equal(set.from_list([
#(1, 1),
#(1, 2),
#(1, 3),
#(2, 1),
#(2, 2),
#(2, 3),
#(3, 1),
#(3, 2),
#(3, 3),
]))
|> should.equal(
set.from_list([
#(1, 1),
#(1, 2),
#(1, 3),
#(2, 1),
#(2, 2),
#(2, 3),
#(3, 1),
#(3, 2),
#(3, 3),
]),
)
[1.0, 10.0]
|> combinatorics.cartesian_product([1.0, 2.0])
|> set.from_list()
|> should.equal(set.from_list([
#(1.0, 1.0),
#(1.0, 2.0),
#(10.0, 1.0),
#(10.0, 2.0),
]))
|> should.equal(
set.from_list([#(1.0, 1.0), #(1.0, 2.0), #(10.0, 1.0), #(10.0, 2.0)]),
)
}
pub fn list_permutation_test() {
@ -125,14 +124,16 @@ pub fn list_permutation_test() {
[1, 2, 3]
|> combinatorics.list_permutation()
|> set.from_list()
|> should.equal(set.from_list([
[1, 2, 3],
[2, 1, 3],
[3, 1, 2],
[1, 3, 2],
[2, 3, 1],
[3, 2, 1],
]))
|> should.equal(
set.from_list([
[1, 2, 3],
[2, 1, 3],
[3, 1, 2],
[1, 3, 2],
[2, 3, 1],
[3, 2, 1],
]),
)
// Repeated elements are treated as distinct for the
// purpose of permutations, so two identical elements
@ -178,8 +179,9 @@ pub fn list_combination_test() {
let assert Ok(result) = combinatorics.list_combination([1, 2, 3, 4], 2)
result
|> set.from_list()
|> should.equal(set.from_list([[1,
2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]))
|> should.equal(
set.from_list([[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]),
)
// Test with some arbitrary inputs
let assert Ok(result) = combinatorics.list_combination([1, 2, 3, 4], 3)

View file

@ -316,26 +316,17 @@ pub fn math_round_ties_away_test() {
|> should.equal(Ok(12.0))
// Round 1. digit BEFORE decimal point
piecewise.round(
12.0654,
option.Some(-1),
option.Some(piecewise.RoundTiesAway),
piecewise.round(12.0654, option.Some(-1), option.Some(piecewise.RoundTiesAway),
)
|> should.equal(Ok(10.0))
// Round 2. digit BEFORE decimal point
piecewise.round(
12.0654,
option.Some(-2),
option.Some(piecewise.RoundTiesAway),
piecewise.round(12.0654, option.Some(-2), option.Some(piecewise.RoundTiesAway),
)
|> should.equal(Ok(0.0))
// Round 2. digit BEFORE decimal point
piecewise.round(
12.0654,
option.Some(-3),
option.Some(piecewise.RoundTiesAway),
piecewise.round(12.0654, option.Some(-3), option.Some(piecewise.RoundTiesAway),
)
|> should.equal(Ok(0.0))
}