diff --git a/src/gleam_community/maths/arithmetics.gleam b/src/gleam_community/maths/arithmetics.gleam index aee0d34..1b60d19 100644 --- a/src/gleam_community/maths/arithmetics.gleam +++ b/src/gleam_community/maths/arithmetics.gleam @@ -60,16 +60,16 @@ import gleam_community/maths/piecewise /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int as intx +/// import gleam_community/maths/arithmetics /// /// pub fn example() { -/// intx.lcm(1, 1) +/// arithmetics.lcm(1, 1) /// |> should.equal(1) /// -/// intx.lcm(100, 10) +/// arithmetics.lcm(100, 10) /// |> should.equal(10) /// -/// intx.gcd(-36, -17) +/// arithmetics.gcd(-36, -17) /// |> should.equal(1) /// } /// @@ -109,16 +109,16 @@ pub fn do_gcd(x: Int, y: Int) -> Int { /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int as intx +/// import gleam_community/maths/arithmetics /// /// pub fn example() { -/// intx.lcm(1, 1) +/// arithmetics.lcm(1, 1) /// |> should.equal(1) /// -/// intx.lcm(100, 10) +/// arithmetics.lcm(100, 10) /// |> should.equal(100) /// -/// intx.lcm(-36, -17) +/// arithmetics.lcm(-36, -17) /// |> should.equal(612) /// } /// @@ -147,16 +147,16 @@ pub fn lcm(x: Int, y: Int) -> Int { /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int as intx +/// import gleam_community/maths/arithmetics /// /// pub fn example() { -/// intx.divisors(4) +/// arithmetics.divisors(4) /// |> should.equal([1, 2, 4]) /// -/// intx.divisors(6) +/// arithmetics.divisors(6) /// |> should.equal([1, 2, 3, 6]) /// -/// intx.proper_divisors(13) +/// arithmetics.proper_divisors(13) /// |> should.equal([1, 13]) /// } /// @@ -201,16 +201,16 @@ pub fn find_divisors(n: Int) -> List(Int) { /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int as intx +/// import gleam_community/maths/arithmetics /// /// pub fn example() { -/// intx.proper_divisors(4) +/// arithmetics.proper_divisors(4) /// |> should.equal([1, 2]) /// -/// intx.proper_divisors(6) +/// arithmetics.proper_divisors(6) /// |> should.equal([1, 2, 3]) /// -/// intx.proper_divisors(13) +/// arithmetics.proper_divisors(13) /// |> should.equal([1]) /// } /// @@ -239,23 +239,23 @@ pub fn proper_divisors(n: Int) -> List(Int) { /// \sum_{i=1}^n x_i /// \\] /// -/// In the formula, $$n$$ is the length of the list and $$x_i$$ is the value in the input list indexed by $$i$$. +/// In the formula, $$n$$ is the length of the list and $$x_i \in \mathbb{R}$$ is the value in the input list indexed by $$i$$. /// ///
/// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float_list +/// import gleam_community/maths/arithmetics /// /// pub fn example () { /// // An empty list returns an error /// [] -/// |> float_list.sum() +/// |> arithmetics.float_sum() /// |> should.equal(0.0) /// /// // Valid input returns a result /// [1.0, 2.0, 3.0] -/// |> float_list.sum() +/// |> arithmetics.float_sum() /// |> should.equal(6.0) /// } ///
@@ -287,23 +287,23 @@ pub fn float_sum(arr: List(Float)) -> Float { /// \sum_{i=1}^n x_i /// \\] /// -/// In the formula, $$n$$ is the length of the list and $$x_i$$ is the value in the input list indexed by $$i$$. +/// In the formula, $$n$$ is the length of the list and $$x_i \in \mathbb{Z}$$ is the value in the input list indexed by $$i$$. /// ///
/// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int_list +/// import gleam_community/maths/arithmetics /// /// pub fn example () { /// // An empty list returns 0 /// [] -/// |> int_list.sum() +/// |> arithmetics.int_sum() /// |> should.equal(0) /// /// // Valid input returns a result /// [1, 2, 3] -/// |> int_list.sum() +/// |> arithmetics.int_sum() /// |> should.equal(6) /// } ///
@@ -335,23 +335,23 @@ pub fn int_sum(arr: List(Int)) -> Int { /// \prod_{i=1}^n x_i /// \\] /// -/// In the formula, $$n$$ is the length of the list and $$x_i$$ is the value in the input list indexed by $$i$$. +/// In the formula, $$n$$ is the length of the list and $$x_i \in \mathbb{R}$$ is the value in the input list indexed by $$i$$. /// ///
/// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float_list +/// import gleam_community/maths/arithmetics /// /// pub fn example () { /// // An empty list returns 0.0 /// [] -/// |> float_list.sum() +/// |> arithmetics.float_product() /// |> should.equal(0.0) /// /// // Valid input returns a result /// [1.0, 2.0, 3.0] -/// |> float_list.product() +/// |> arithmetics.float_product() /// |> should.equal(6.0) /// } ///
@@ -383,23 +383,23 @@ pub fn float_product(arr: List(Float)) -> Float { /// \prod_{i=1}^n x_i /// \\] /// -/// In the formula, $$n$$ is the length of the list and $$x_i$$ is the value in the input list indexed by $$i$$. +/// In the formula, $$n$$ is the length of the list and $$x_i \in \mathbb{Z}$$ is the value in the input list indexed by $$i$$. /// ///
/// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int_list +/// import gleam_community/maths/arithmetics /// /// pub fn example () { /// // An empty list returns 0 /// [] -/// |> int_list.product() +/// |> arithmetics.int_product() /// |> should.equal(0) /// /// // Valid input returns a result /// [1, 2, 3] -/// |> int_list.product() +/// |> arithmetics.int_product() /// |> should.equal(6) /// } ///
@@ -432,23 +432,23 @@ pub fn int_product(arr: List(Int)) -> Int { /// \\] /// /// In the formula, $$v_j$$ is the $$j$$'th element in the cumulative sum of $$n$$ elements. -/// That is, $$n$$ is the length of the list and $$x_i$$ is the value in the input list indexed by $$i$$. +/// That is, $$n$$ is the length of the list and $$x_i \in \mathbb{R}$$ is the value in the input list indexed by $$i$$. /// The value $$v_j$$ is thus the sum of the $$1$$ to $$j$$ first elements in the given list. /// ///
/// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float_list +/// import gleam_community/maths/arithmetics /// /// pub fn example () { /// [] -/// |> float_list.cumulative_sum() +/// |> arithmetics.float_cumulative_sum() /// |> should.equal([]) /// /// // Valid input returns a result /// [1.0, 2.0, 3.0] -/// |> float_list.cumulative_sum() +/// |> arithmetics.float_cumulative_sum() /// |> should.equal([1.0, 3.0, 6.0]) /// } ///
@@ -481,23 +481,23 @@ pub fn float_cumulative_sum(arr: List(Float)) -> List(Float) { /// \\] /// /// In the formula, $$v_j$$ is the $$j$$'th element in the cumulative sum of $$n$$ elements. -/// That is, $$n$$ is the length of the list and $$x_i$$ is the value in the input list indexed by $$i$$. +/// That is, $$n$$ is the length of the list and $$x_i \in \mathbb{Z}$$ is the value in the input list indexed by $$i$$. /// The value $$v_j$$ is thus the sum of the $$1$$ to $$j$$ first elements in the given list. /// ///
/// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int_list +/// import gleam_community/maths/arithmetics /// /// pub fn example () { /// [] -/// |> int_list.cumulative_sum() +/// |> arithmetics.int_cumulative_sum() /// |> should.equal([]) /// /// // Valid input returns a result /// [1, 2, 3] -/// |> int_list.cumulative_sum() +/// |> arithmetics.int_cumulative_sum() /// |> should.equal([1, 3, 6]) /// } ///
@@ -530,24 +530,24 @@ pub fn int_cumulative_sum(arr: List(Int)) -> List(Int) { /// \\] /// /// In the formula, $$v_j$$ is the $$j$$'th element in the cumulative product of $$n$$ elements. -/// That is, $$n$$ is the length of the list and $$x_i$$ is the value in the input list indexed by $$i$$. +/// That is, $$n$$ is the length of the list and $$x_i \in \mathbb{R}$$ is the value in the input list indexed by $$i$$. /// The value $$v_j$$ is thus the sum of the $$1$$ to $$j$$ first elements in the given list. /// ///
/// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float_list +/// import gleam_community/maths/arithmetics /// /// pub fn example () { /// // An empty list returns an error /// [] -/// |> float_list.cumulative_product() +/// |> arithmetics.float_cumulative_product() /// |> should.equal([]) /// /// // Valid input returns a result /// [1.0, 2.0, 3.0] -/// |> float_list.cumulative_product() +/// |> arithmetics.float_cumulative_product() /// |> should.equal([1.0, 2.0, 6.0]) /// } ///
@@ -580,24 +580,24 @@ pub fn float_cumumlative_product(arr: List(Float)) -> List(Float) { /// \\] /// /// In the formula, $$v_j$$ is the $$j$$'th element in the cumulative product of $$n$$ elements. -/// That is, $$n$$ is the length of the list and $$x_i$$ is the value in the input list indexed by $$i$$. +/// That is, $$n$$ is the length of the list and $$x_i \in \mathbb{Z}$$ is the value in the input list indexed by $$i$$. /// The value $$v_j$$ is thus the product of the $$1$$ to $$j$$ first elements in the given list. /// ///
/// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int_list +/// import gleam_community/maths/arithmetics /// /// pub fn example () { /// // An empty list returns an error /// [] -/// |> int_list.cumulative_product() +/// |> arithmetics.int_cumulative_product() /// |> should.equal([]) /// /// // Valid input returns a result /// [1, 2, 3] -/// |> int_list.cumulative_product() +/// |> arithmetics.int_cumulative_product() /// |> should.equal([1, 2, 6]) /// } ///
diff --git a/src/gleam_community/maths/combinatorics.gleam b/src/gleam_community/maths/combinatorics.gleam index b657c7b..9963a25 100644 --- a/src/gleam_community/maths/combinatorics.gleam +++ b/src/gleam_community/maths/combinatorics.gleam @@ -56,22 +56,22 @@ import gleam/set /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int as intx +/// import gleam_community/maths/combinatorics /// /// pub fn example() { /// // Invalid input gives an error /// // Error on: n = -1 < 0 -/// intx.combination(-1, 1) +/// combinatorics.combination(-1, 1) /// |> should.be_error() /// /// // Valid input returns a result -/// intx.combination(4, 0) +/// combinatorics.combination(4, 0) /// |> should.equal(Ok(1)) /// -/// intx.combination(4, 4) +/// combinatorics.combination(4, 4) /// |> should.equal(Ok(1)) /// -/// intx.combination(4, 2) +/// combinatorics.combination(4, 2) /// |> should.equal(Ok(6)) /// } /// @@ -127,23 +127,27 @@ pub fn combination(n: Int, k: Int) -> Result(Int, String) { /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int as intx +/// import gleam_community/maths/combinatorics /// /// pub fn example() { /// // Invalid input gives an error -/// intx.factorial(-1) +/// combinatorics.factorial(-1) /// |> should.be_error() /// /// // Valid input returns a result -/// intx.factorial(0) +/// combinatorics.factorial(0) /// |> should.equal(Ok(1)) -/// intx.factorial(1) +/// +/// combinatorics.factorial(1) /// |> should.equal(Ok(1)) -/// intx.factorial(2) +/// +/// combinatorics.factorial(2) /// |> should.equal(Ok(2)) -/// intx.factorial(3) +/// +/// combinatorics.factorial(3) /// |> should.equal(Ok(6)) -/// intx.factorial(4) +/// +/// combinatorics.factorial(4) /// |> should.equal(Ok(24)) /// } /// @@ -192,22 +196,22 @@ pub fn factorial(n) -> Result(Int, String) { /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int as intx +/// import gleam_community/maths/combinatorics /// /// pub fn example() { /// // Invalid input gives an error /// // Error on: n = -1 < 0 -/// intx.permutation(-1, 1) +/// combinatorics.permutation(-1, 1) /// |> should.be_error() /// /// // Valid input returns a result -/// intx.permutation(4, 0) +/// combinatorics.permutation(4, 0) /// |> should.equal(Ok(1)) /// -/// intx.permutation(4, 4) +/// combinatorics.permutation(4, 4) /// |> should.equal(Ok(1)) /// -/// intx.permutation(4, 2) +/// combinatorics.permutation(4, 2) /// |> should.equal(Ok(12)) /// } /// @@ -256,10 +260,14 @@ pub fn permutation(n: Int, k: Int) -> Result(Int, String) { /// Example: /// /// import gleeunit/should -/// import gleam/list -/// import gleam_community/maths/list as listx +/// import gleam/set +/// import gleam_community/maths/combinatorics /// /// pub fn example () { +/// let assert Ok(result) = combinatorics.list_combination([1, 2, 3, 4], 3) +/// result +/// |> set.from_list() +/// |> should.equal(set.from_list([[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]])) /// } /// /// @@ -309,16 +317,27 @@ fn do_list_combination(arr: List(a), k: Int, prefix: List(a)) -> List(List(a)) { /// /// /// -/// Generate all permutations based on a given list. +/// Generate all permutations of a given list. /// ///
/// Example: /// /// import gleeunit/should -/// import gleam/list -/// import gleam_community/maths/list as listx +/// import gleam/set +/// import gleam_community/maths/combinatorics /// /// pub fn example () { +/// [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], +/// ])) /// } ///
/// @@ -368,15 +387,15 @@ fn concat(lists: List(List(a))) -> List(a) { /// /// import gleeunit/should /// import gleam/list -/// import gleam_community/maths/list as listx +/// import gleam_community/maths/combinatorics /// /// pub fn example () { /// [] -/// |> listx.cartesian_product([]) +/// |> combinatorics.cartesian_product([]) /// |> should.equal([]) /// /// [1.0, 10.0] -/// |> listx.cartesian_product([1.0, 2.0]) +/// |> combinatorics.cartesian_product([1.0, 2.0]) /// |> should.equal([#(1.0, 1.0), #(1.0, 2.0), #(10.0, 1.0), #(10.0, 2.0)]) /// } /// diff --git a/src/gleam_community/maths/conversion.gleam b/src/gleam_community/maths/conversion.gleam index d45a5f2..f5cdfa8 100644 --- a/src/gleam_community/maths/conversion.gleam +++ b/src/gleam_community/maths/conversion.gleam @@ -23,7 +23,7 @@ //// //// --- //// -//// Conversion: A module containing various functions for converting between types and different quantities. +//// Conversion: A module containing various functions for converting between types and quantities. //// //// * **Misc. functions** //// * [`float_to_int`](#float_to_int) @@ -47,13 +47,13 @@ import gleam/int /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int as intx +/// import gleam_community/maths/conversion /// /// pub fn example() { -/// intx.to_float(-1) +/// conversion.int_to_float(-1) /// |> should.equal(-1.0) /// -/// intx.to_float(1) +/// conversion.int_to_float(1) /// |> should.equal(1.0) /// } /// @@ -82,16 +82,17 @@ pub fn int_to_float(x: Int) -> Float { /// /// import gleeunit/should /// import gleam/option -/// import gleam_community/maths/float as floatx +/// import gleam_community/maths/conversion +/// import gleam_community/maths/piecewise /// /// pub fn example() { -/// floatx.to_int(12.0654) +/// conversion.float_to_int(12.0654) /// |> should.equal(12) /// /// // Note: Making the following function call is equivalent /// // but instead of returning a value of type 'Int' a value /// // of type 'Float' is returned. -/// floatx.round(12.0654, option.Some(0), option.Some(floatx.RoundToZero)) +/// piecewise.round(12.0654, option.Some(0), option.Some(piecewise.RoundToZero)) /// |> should.equal(Ok(12.0)) /// } /// @@ -123,11 +124,12 @@ fn do_to_int(a: Float) -> Int /// Example /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx +/// import gleam_community/maths/conversion +/// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.to_radian(360.) -/// |> should.equal(2. *. floatx.pi()) +/// conversion.degrees_to_radians(360.) +/// |> should.equal(2. *. elementary.pi()) /// } /// /// @@ -154,13 +156,14 @@ pub fn degrees_to_radians(x: Float) -> Float { /// Example /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx +/// import gleam_community/maths/conversion +/// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.to_degree(0.0) +/// conversion.radians_to_degrees(0.0) /// |> should.equal(0.0) /// -/// floatx.to_degree(2. *. floatx.pi()) +/// conversion.radians_to_degrees(2. *. elementary.pi()) /// |> should.equal(360.) /// } /// diff --git a/src/gleam_community/maths/elementary.gleam b/src/gleam_community/maths/elementary.gleam index 88ffb92..1455731 100644 --- a/src/gleam_community/maths/elementary.gleam +++ b/src/gleam_community/maths/elementary.gleam @@ -136,10 +136,10 @@ fn do_acos(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.acosh(1.0) +/// elementary.acosh(1.0) /// |> should.equal(Ok(0.0)) /// -/// floatx.acosh(0.0) +/// elementary.acosh(0.0) /// |> should.be_error() /// } /// @@ -188,13 +188,13 @@ fn do_acosh(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.asin(0.0) +/// elementary.asin(0.0) /// |> should.equal(Ok(0.0)) /// -/// floatx.asin(1.1) +/// elementary.asin(1.1) /// |> should.be_error() /// -/// floatx.asin(-1.1) +/// elementary.asin(-1.1) /// |> should.be_error() /// } /// @@ -242,7 +242,7 @@ fn do_asin(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.asinh(0.0) +/// elementary.asinh(0.0) /// |> should.equal(0.0) /// } /// @@ -283,7 +283,7 @@ fn do_asinh(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.atan(0.0) +/// elementary.atan(0.0) /// |> should.equal(0.0) /// } /// @@ -333,7 +333,7 @@ fn do_atan(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.atan2(0.0, 0.0) +/// elementary.atan2(0.0, 0.0) /// |> should.equal(0.0) /// } /// @@ -375,13 +375,13 @@ fn do_atan2(a: Float, b: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.atanh(0.0) +/// elementary.atanh(0.0) /// |> should.equal(Ok(0.0)) /// -/// floatx.atanh(1.0) +/// elementary.atanh(1.0) /// |> should.be_error() /// -/// floatx.atanh(-1.0) +/// elementary.atanh(-1.0) /// |> should.be_error() /// } /// @@ -429,10 +429,10 @@ fn do_atanh(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.cos(0.0) +/// elementary.cos(0.0) /// |> should.equal(1.0) /// -/// floatx.cos(floatx.pi()) +/// elementary.cos(elementary.pi()) /// |> should.equal(-1.0) /// } /// @@ -474,7 +474,7 @@ fn do_cos(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.cosh(0.0) +/// elementary.cosh(0.0) /// |> should.equal(0.0) /// } /// @@ -515,10 +515,10 @@ fn do_cosh(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.sin(0.0) +/// elementary.sin(0.0) /// |> should.equal(0.0) /// -/// floatx.sin(0.5 *. floatx.pi()) +/// elementary.sin(0.5 *. elementary.pi()) /// |> should.equal(1.0) /// } /// @@ -561,7 +561,7 @@ fn do_sin(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.sinh(0.0) +/// elementary.sinh(0.0) /// |> should.equal(0.0) /// } /// @@ -603,7 +603,7 @@ fn do_sinh(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.tan(0.0) +/// elementary.tan(0.0) /// |> should.equal(0.0) /// } /// @@ -644,13 +644,13 @@ fn do_tan(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example () { -/// floatx.tanh(0.0) +/// elementary.tanh(0.0) /// |> should.equal(0.0) /// -/// floatx.tanh(25.0) +/// elementary.tanh(25.0) /// |> should.equal(1.0) /// -/// floatx.tanh(-25.0) +/// elementary.tanh(-25.0) /// |> should.equal(-1.0) /// } /// @@ -692,7 +692,7 @@ fn do_tanh(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.exponential(0.0) +/// elementary.exponential(0.0) /// |> should.equal(1.0) /// } /// @@ -734,13 +734,13 @@ fn do_exponential(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example () { -/// floatx.natural_logarithm(1.0) +/// elementary.natural_logarithm(1.0) /// |> should.equal(Ok(0.0)) /// -/// floatx.natural_logarithm(floatx.e()) +/// elementary.natural_logarithm(elementary.e()) /// |> should.equal(Ok(1.0)) /// -/// floatx.natural_logarithm(-1.0) +/// elementary.natural_logarithm(-1.0) /// |> should.be_error() /// } /// @@ -791,13 +791,13 @@ fn do_natural_logarithm(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example () { -/// floatx.logarithm(1.0, option.Some(10.0)) +/// elementary.logarithm(1.0, option.Some(10.0)) /// |> should.equal(Ok(0.0)) /// -/// floatx.logarithm(floatx.e(), option.Some(floatx.e())) +/// elementary.logarithm(elementary.e(), option.Some(elementary.e())) /// |> should.equal(Ok(1.0)) /// -/// floatx.logarithm(-1.0, option.Some(2.0)) +/// elementary.logarithm(-1.0, option.Some(2.0)) /// |> should.be_error() /// } /// @@ -859,13 +859,13 @@ pub fn logarithm(x: Float, base: option.Option(Float)) -> Result(Float, String) /// import gleam_community/maths/elementary /// /// pub fn example () { -/// floatx.logarithm_2(1.0) +/// elementary.logarithm_2(1.0) /// |> should.equal(Ok(0.0)) /// -/// floatx.logarithm_2(2.0) +/// elementary.logarithm_2(2.0) /// |> should.equal(Ok(1.0)) /// -/// floatx.logarithm_2(-1.0) +/// elementary.logarithm_2(-1.0) /// |> should.be_error() /// } /// @@ -914,13 +914,13 @@ fn do_logarithm_2(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example () { -/// floatx.logarithm_10(1.0) +/// elementary.logarithm_10(1.0) /// |> should.equal(Ok(0.0)) /// -/// floatx.logarithm_10(10.0) +/// elementary.logarithm_10(10.0) /// |> should.equal(Ok(1.0)) /// -/// floatx.logarithm_10(-1.0) +/// elementary.logarithm_10(-1.0) /// |> should.be_error() /// } /// @@ -975,13 +975,13 @@ fn do_logarithm_10(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.power(2., -1.) +/// elementary.power(2., -1.) /// |> should.equal(Ok(0.5)) /// -/// floatx.power(2., 2.) +/// elementary.power(2., 2.) /// |> should.equal(Ok(4.0)) /// -/// floatx.power(-1., 0.5) +/// elementary.power(-1., 0.5) /// |> should.be_error() /// } /// @@ -1037,13 +1037,13 @@ fn do_ceiling(a: Float) -> Float /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.square_root(1.0) +/// elementary.square_root(1.0) /// |> should.equal(Ok(1.0)) /// -/// floatx.square_root(4.0) +/// elementary.square_root(4.0) /// |> should.equal(Ok(2.0)) /// -/// floatx.square_root(-1.0) +/// elementary.square_root(-1.0) /// |> should.be_error() /// } /// @@ -1089,13 +1089,13 @@ pub fn square_root(x: Float) -> Result(Float, String) { /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.cube_root(1.0) +/// elementary.cube_root(1.0) /// |> should.equal(Ok(1.0)) /// -/// floatx.cube_root(27.0) +/// elementary.cube_root(27.0) /// |> should.equal(Ok(3.0)) /// -/// floatx.cube_root(-1.0) +/// elementary.cube_root(-1.0) /// |> should.be_error() /// } /// @@ -1141,16 +1141,16 @@ pub fn cube_root(x: Float) -> Result(Float, String) { /// import gleam_community/maths/elementary /// /// pub fn example() { -/// floatx.nth_root(1.0, 2) +/// elementary.nth_root(1.0, 2) /// |> should.equal(Ok(1.0)) /// -/// floatx.nth_root(27.0, 3) +/// elementary.nth_root(27.0, 3) /// |> should.equal(Ok(3.0)) /// -/// floatx.nth_root(256.0, 4) +/// elementary.nth_root(256.0, 4) /// |> should.equal(Ok(4.0)) /// -/// floatx.nth_root(-1.0, 2) +/// elementary.nth_root(-1.0, 2) /// |> should.be_error() /// } /// @@ -1235,12 +1235,12 @@ pub fn tau() -> Float { /// Example /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx +/// import gleam_community/maths/elementary /// /// pub fn example() { /// // Test that the constant is approximately equal to 2.7128... -/// floatx.e() -/// |> floatx.is_close(2.7128, 0.0, 0.000001) +/// elementary.e() +/// |> elementary.is_close(2.7128, 0.0, 0.000001) /// |> should.be_true() /// } /// diff --git a/src/gleam_community/maths/metrics.gleam b/src/gleam_community/maths/metrics.gleam index 8d07c8f..5b2c4b0 100644 --- a/src/gleam_community/maths/metrics.gleam +++ b/src/gleam_community/maths/metrics.gleam @@ -23,12 +23,11 @@ //// //// --- //// -//// Metrics: A module offering functions for calculating distances and types of metrics. +//// Metrics: A module offering functions for calculating distances and other types of metrics. //// //// * **Distances** //// * [`norm`](#norm) -//// * [`float_manhatten_distance`](#float_manhatten_distance) -//// * [`int_manhatten_distance`](#int_manhatten_distance) +//// * [`manhatten_distance`](#float_manhatten_distance) //// * [`minkowski_distance`](#minkowski_distance) //// * [`euclidean_distance`](#euclidean_distance) //// * **Basic statistical measures** @@ -65,20 +64,21 @@ import gleam/float /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx -/// import gleam_community/maths/float_list +/// import gleam_community/maths/elementary +/// import gleam_community/maths/metrics +/// import gleam_community/maths/tests /// /// pub fn example () { -/// let assert Ok(tol) = floatx.power(-10.0, -6.0) +/// let assert Ok(tol) = elementary.power(-10.0, -6.0) /// /// [1.0, 1.0, 1.0] -/// |> float_list.norm(1.0) -/// |> floatx.is_close(3.0, 0.0, tol) +/// |> metrics.norm(1.0) +/// |> tests.is_close(3.0, 0.0, tol) /// |> should.be_true() /// /// [1.0, 1.0, 1.0] -/// |> float_list.norm(-1.0) -/// |> floatx.is_close(0.3333333333333333, 0.0, tol) +/// |> metrics.norm(-1.0) +/// |> tests.is_close(0.3333333333333333, 0.0, tol) /// |> should.be_true() /// } /// @@ -127,23 +127,24 @@ pub fn norm(arr: List(Float), p: Float) -> Float { /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx -/// import gleam_community/maths/float_list +/// import gleam_community/maths/elementary +/// import gleam_community/maths/metrics +/// import gleam_community/maths/tests /// /// pub fn example () { -/// let assert Ok(tol) = floatx.power(-10.0, -6.0) +/// let assert Ok(tol) = elementary.power(-10.0, -6.0) /// /// // Empty lists returns 0.0 -/// float_list.manhatten_distance([], []) +/// metrics.float_manhatten_distance([], []) /// |> should.equal(Ok(0.0)) /// /// // Differing lengths returns error -/// float_list.manhatten_distance([], [1.0]) +/// metrics.manhatten_distance([], [1.0]) /// |> should.be_error() /// -/// let assert Ok(result) = float_list.manhatten_distance([0.0, 0.0], [1.0, 2.0]) +/// let assert Ok(result) = metrics.manhatten_distance([0.0, 0.0], [1.0, 2.0]) /// result -/// |> floatx.is_close(3.0, 0.0, tol) +/// |> tests.is_close(3.0, 0.0, tol) /// |> should.be_true() /// } /// @@ -154,74 +155,13 @@ pub fn norm(arr: List(Float), p: Float) -> Float { /// /// /// -pub fn float_manhatten_distance( +pub fn manhatten_distance( xarr: List(Float), yarr: List(Float), ) -> Result(Float, String) { minkowski_distance(xarr, yarr, 1.0) } -///
-/// -/// Spot a typo? Open an issue! -/// -///
-/// -/// Calculcate the Manhatten distance between two lists (representing vectors): -/// -/// \\[ -/// \sum_{i=1}^n \left|x_i - y_i \right| -/// \\] -/// -/// In the formula, $$n$$ is the length of the two lists and $$x_i, y_i$$ are the values in the respective input lists indexed by $$i, j$$. -/// -///
-/// Example: -/// -/// import gleeunit/should -/// import gleam_community/maths/int_list -/// -/// pub fn example () { -/// // Empty lists returns 0 -/// int_list.manhatten_distance([], []) -/// |> should.equal(Ok(0)) -/// -/// // Differing lengths returns error -/// int_list.manhatten_distance([], [1]) -/// |> should.be_error() -/// -/// let assert Ok(result) = int_list.manhatten_distance([0, 0], [1, 2]) -/// result -/// |> should.equal(3) -/// } -///
-/// -///
-/// -/// Back to top ↑ -/// -///
-/// -pub fn int_manhatten_distance( - xarr: List(Int), - yarr: List(Int), -) -> Result(Int, String) { - let xlen: Int = list.length(xarr) - let ylen: Int = list.length(yarr) - case xlen == ylen { - False -> - "Invalid input argument: length(xarr) != length(yarr). Valid input is when length(xarr) == length(yarr)." - |> Error - True -> - list.zip(xarr, yarr) - |> list.map(fn(tuple: #(Int, Int)) -> Int { - piecewise.int_absolute_value(pair.first(tuple) - pair.second(tuple)) - }) - |> arithmetics.int_sum() - |> Ok - } -} - ///
/// /// Spot a typo? Open an issue! @@ -242,27 +182,28 @@ pub fn int_manhatten_distance( /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx -/// import gleam_community/maths/float_list +/// import gleam_community/maths/elementary +/// import gleam_community/maths/metrics +/// import gleam_community/maths/tests /// /// pub fn example () { -/// let assert Ok(tol) = floatx.power(-10.0, -6.0) +/// let assert Ok(tol) = elementary.power(-10.0, -6.0) /// /// // Empty lists returns 0.0 -/// float_list.minkowski_distance([], [], 1.0) +/// metrics.minkowski_distance([], [], 1.0) /// |> should.equal(Ok(0.0)) /// /// // Differing lengths returns error -/// float_list.minkowski_distance([], [1.0], 1.0) +/// metrics.minkowski_distance([], [1.0], 1.0) /// |> should.be_error() /// /// // Test order < 1 -/// float_list.minkowski_distance([0.0, 0.0], [0.0, 0.0], -1.0) +/// metrics.minkowski_distance([0.0, 0.0], [0.0, 0.0], -1.0) /// |> should.be_error() /// -/// let assert Ok(result) = float_list.minkowski_distance([0.0, 0.0], [1.0, 2.0], 1.0) +/// let assert Ok(result) = metrics.minkowski_distance([0.0, 0.0], [1.0, 2.0], 1.0) /// result -/// |> floatx.is_close(3.0, 0.0, tol) +/// |> tests.is_close(3.0, 0.0, tol) /// |> should.be_true() /// } /// @@ -318,23 +259,24 @@ pub fn minkowski_distance( /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx -/// import gleam_community/maths/float_list +/// import gleam_community/maths/elementary +/// import gleam_community/maths/metrics +/// import gleam_community/maths/tests /// /// pub fn example () { -/// let assert Ok(tol) = floatx.power(-10.0, -6.0) +/// let assert Ok(tol) = elementary.power(-10.0, -6.0) /// /// // Empty lists returns 0.0 -/// float_list.euclidean_distance([], []) +/// metrics.euclidean_distance([], []) /// |> should.equal(Ok(0.0)) /// /// // Differing lengths returns error -/// float_list.euclidean_distance([], [1.0]) +/// metrics.euclidean_distance([], [1.0]) /// |> should.be_error() /// -/// let assert Ok(result) = float_list.euclidean_distance([0.0, 0.0], [1.0, 2.0]) +/// let assert Ok(result) = metrics.euclidean_distance([0.0, 0.0], [1.0, 2.0]) /// result -/// |> floatx.is_close(2.23606797749979, 0.0, tol) +/// |> tests.is_close(2.23606797749979, 0.0, tol) /// |> should.be_true() /// } /// @@ -371,17 +313,17 @@ pub fn euclidean_distance( /// Example: /// /// import gleeunit/should -/// import gleam_stats/stats +/// import gleam_community/maths/metrics /// /// pub fn example () { /// // An empty list returns an error /// [] -/// |> stats.mean() +/// |> metrics.mean() /// |> should.be_error() /// /// // Valid input returns a result /// [1., 2., 3.] -/// |> stats.mean() +/// |> metrics.mean() /// |> should.equal(Ok(2.)) /// } /// @@ -419,21 +361,21 @@ pub fn mean(arr: List(Float)) -> Result(Float, String) { /// Example: /// /// import gleeunit/should -/// import gleam_stats/stats +/// import gleam_community/maths/metrics /// /// pub fn example () { /// // An empty list returns an error /// [] -/// |> stats.median() +/// |> metrics.median() /// |> should.be_error() /// /// // Valid input returns a result /// [1., 2., 3.] -/// |> stats.median() +/// |> metrics.median() /// |> should.equal(Ok(2.)) /// /// [1., 2., 3., 4.] -/// |> stats.median() +/// |> metrics.median() /// |> should.equal(Ok(2.5)) /// } /// @@ -495,7 +437,7 @@ pub fn median(arr: List(Float)) -> Result(Float, String) { /// Example: /// /// import gleeunit/should -/// import gleam_stats/stats +/// import gleam_community/maths/metrics /// /// pub fn example () { /// // Degrees of freedom @@ -503,12 +445,12 @@ pub fn median(arr: List(Float)) -> Result(Float, String) { /// /// // An empty list returns an error /// [] -/// |> stats.var(ddof) +/// |> metrics.variance(ddof) /// |> should.be_error() /// /// // Valid input returns a result /// [1., 2., 3.] -/// |> stats.var(ddof) +/// |> metrics.variance(ddof) /// |> should.equal(Ok(1.)) /// } /// @@ -571,7 +513,7 @@ pub fn variance(arr: List(Float), ddof: Int) -> Result(Float, String) { /// Example: /// /// import gleeunit/should -/// import gleam_stats/stats +/// import gleam_community/maths/metrics /// /// pub fn example () { /// // Degrees of freedom @@ -579,12 +521,12 @@ pub fn variance(arr: List(Float), ddof: Int) -> Result(Float, String) { /// /// // An empty list returns an error /// [] -/// |> stats.std(ddof) +/// |> metrics.standard_deviationddof) /// |> should.be_error() /// /// // Valid input returns a result /// [1., 2., 3.] -/// |> stats.std(ddof) +/// |> metrics.standard_deviation(ddof) /// |> should.equal(Ok(1.)) /// } /// diff --git a/src/gleam_community/maths/piecewise.gleam b/src/gleam_community/maths/piecewise.gleam index 3345f20..f20500b 100644 --- a/src/gleam_community/maths/piecewise.gleam +++ b/src/gleam_community/maths/piecewise.gleam @@ -93,16 +93,16 @@ import gleam_community/maths/elementary /// /// import gleeunit/should /// import gleam/option -/// import gleam_community/maths/float as floatx +/// import gleam_community/maths/piecewise /// /// pub fn example() { -/// floatx.ceiling(12.0654, option.Some(1)) +/// piecewise.ceiling(12.0654, option.Some(1)) /// |> should.equal(Ok(12.1)) /// -/// floatx.ceiling(12.0654, option.Some(2)) +/// piecewise.ceiling(12.0654, option.Some(2)) /// |> should.equal(Ok(12.07)) /// -/// floatx.ceiling(12.0654, option.Some(3)) +/// piecewise.ceiling(12.0654, option.Some(3)) /// |> should.equal(Ok(12.066)) /// } /// @@ -148,16 +148,16 @@ pub fn ceiling(x: Float, digits: option.Option(Int)) -> Result(Float, String) { /// /// import gleeunit/should /// import gleam/option -/// import gleam_community/maths/float as floatx +/// import gleam_community/maths/piecewise /// /// pub fn example() { -/// floatx.floor(12.0654, option.Some(1)) +/// piecewise.floor(12.0654, option.Some(1)) /// |> should.equal(Ok(12.0)) /// -/// floatx.floor(12.0654, option.Some(2)) +/// piecewise.floor(12.0654, option.Some(2)) /// |> should.equal(Ok(12.06)) /// -/// floatx.floor(12.0654, option.Some(3)) +/// piecewise.floor(12.0654, option.Some(3)) /// |> should.equal(Ok(12.065)) /// } /// @@ -203,16 +203,16 @@ pub fn floor(x: Float, digits: option.Option(Int)) -> Result(Float, String) { /// /// import gleeunit/should /// import gleam/option -/// import gleam_community/maths/float as floatx +/// import gleam_community/maths/piecewise /// /// pub fn example() { -/// floatx.truncate(12.0654, option.Some(1)) +/// piecewise.truncate(12.0654, option.Some(1)) /// |> should.equal(Ok(12.0)) /// -/// floatx.truncate(12.0654, option.Some(2)) +/// piecewise.truncate(12.0654, option.Some(2)) /// |> should.equal(Ok(12.0)) /// -/// floatx.truncate(12.0654, option.Some(3)) +/// piecewise.truncate(12.0654, option.Some(3)) /// |> should.equal(Ok(12.0)) /// } /// @@ -319,34 +319,34 @@ pub fn truncate(x: Float, digits: option.Option(Int)) -> Result(Float, String) { /// /// import gleeunit/should /// import gleam/option -/// import gleam_community/maths/float as floatx +/// import gleam_community/maths/piecewise /// /// pub fn example() { /// // The default number of digits is 0 if None is provided -/// floatx.round(12.0654, option.None, option.Some(floatx.RoundNearest)) +/// piecewise.round(12.0654, option.None, option.Some(piecewise.RoundNearest)) /// |> should.equal(Ok(12.0)) /// /// // The default rounding mode is "RoundNearest" if None is provided -/// floatx.round(12.0654, option.None, option.None) +/// piecewise.round(12.0654, option.None, option.None) /// |> should.equal(Ok(12.0)) /// /// // Try different rounding modes -/// floatx.round(12.0654, option.Some(2), option.Some(floatx.RoundNearest)) +/// piecewise.round(12.0654, option.Some(2), option.Some(piecewise.RoundNearest)) /// |> should.equal(Ok(12.07)) /// -/// floatx.round(12.0654, option.Some(2), option.Some(floatx.RoundTiesAway)) +/// piecewise.round(12.0654, option.Some(2), option.Some(piecewise.RoundTiesAway)) /// |> should.equal(Ok(12.07)) /// -/// floatx.round(12.0654, option.Some(2), option.Some(floatx.RoundTiesUp)) +/// piecewise.round(12.0654, option.Some(2), option.Some(piecewise.RoundTiesUp)) /// |> should.equal(Ok(12.07)) /// -/// floatx.round(12.0654, option.Some(2), option.Some(floatx.RoundToZero)) +/// piecewise.round(12.0654, option.Some(2), option.Some(piecewise.RoundToZero)) /// |> should.equal(Ok(12.06)) /// -/// floatx.round(12.0654, option.Some(2), option.Some(floatx.RoundDown)) +/// piecewise.round(12.0654, option.Some(2), option.Some(piecewise.RoundDown)) /// |> should.equal(Ok(12.06)) /// -/// floatx.round(12.0654, option.Some(2), option.Some(floatx.RoundUp)) +/// piecewise.round(12.0654, option.Some(2), option.Some(piecewise.RoundUp)) /// |> should.equal(Ok(12.07)) /// } /// @@ -482,6 +482,28 @@ fn round_up(p: Float, x: Float) -> Float { @external(javascript, "../../maths.mjs", "ceiling") fn do_ceiling(a: Float) -> Float +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +/// The absolute value: +/// +/// \\[ +/// \forall x, y \in \mathbb{R}, \\; |x| \in \mathbb{R}_{+}. +/// \\] +/// +/// The function takes an input $$x$$ and returns a positive float value. +/// +/// +/// +///
+/// +/// Back to top ↑ +/// +///
+/// pub fn float_absolute_value(x: Float) -> Float { case x >. 0.0 { True -> x @@ -489,6 +511,28 @@ pub fn float_absolute_value(x: Float) -> Float { } } +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +/// The absolute value: +/// +/// \\[ +/// \forall x, y \in \mathbb{Z}, \\; |x| \in \mathbb{Z}_{+}. +/// \\] +/// +/// The function takes an input $$x$$ and returns a positive integer value. +/// +/// +/// +///
+/// +/// Back to top ↑ +/// +///
+/// pub fn int_absolute_value(x: Int) -> Int { case x > 0 { True -> x @@ -515,13 +559,13 @@ pub fn int_absolute_value(x: Int) -> Int { /// Example /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx +/// import gleam_community/maths/piecewise /// /// pub fn example() { -/// floatx.absolute_difference(-10.0, 10.0) +/// piecewise.float_absolute_difference(-10.0, 10.0) /// |> should.equal(20.0) /// -/// floatx.absolute_difference(0.0, -2.0) +/// piecewise.float_absolute_difference(0.0, -2.0) /// |> should.equal(2.0) /// } /// @@ -555,13 +599,13 @@ pub fn float_absolute_difference(a: Float, b: Float) -> Float { /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int as intx +/// import gleam_community/maths/piecewise /// /// pub fn example() { -/// intx.absolute_difference(-10, 10) +/// piecewise.absolute_difference(-10, 10) /// |> should.equal(20) /// -/// intx.absolute_difference(0, -2) +/// piecewise.absolute_difference(0, -2) /// |> should.equal(2) /// } /// @@ -583,7 +627,9 @@ pub fn int_absolute_difference(a: Int, b: Int) -> Int { /// ///
/// -/// The sign function that returns the sign of the input, indicating whether it is positive (+1), negative (-1), or zero (0). +/// The function takes an input $$x \in \mathbb{R}$$ and returns the sign of +/// the input, indicating whether it is positive (+1.0), negative (-1.0), or +/// zero (0.0). /// ///
/// @@ -617,8 +663,9 @@ fn do_float_sign(a: Float) -> Float /// ///
/// -/// The sign function which returns the sign of the input, indicating -/// whether it is positive (+1), negative (-1), or zero (0). +/// The function takes an input $$x \in \mathbb{Z}$$ and returns the sign of +/// the input, indicating whether it is positive (+1), negative (-1), or zero +/// (0). /// ///
/// @@ -738,19 +785,22 @@ pub fn int_flip_sign(x: Int) -> Int { /// ///
/// -/// The minimum function that takes two arguments $$x, y \in \mathbb{R}$$ and returns the smallest of the two. +/// The minimum function takes two arguments $$x, y$$ along with a function +/// for comparing $$x, y$$. The function returns the smallest of the two given +/// values. /// ///
/// Example /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx +/// import gleam/float +/// import gleam_community/maths/piecewise /// /// pub fn example() { -/// floatx.minimum(2.0, 1.5) +/// piecewise.minimum(2.0, 1.5, float.compare) /// |> should.equal(1.5) /// -/// floatx.minimum(1.5, 2.0) +/// piecewise.minimum(1.5, 2.0, float.compare) /// |> should.equal(1.5) /// } ///
@@ -781,19 +831,22 @@ pub fn minimum(x: a, y: a, compare: fn(a, a) -> order.Order) -> a { /// /// /// -/// The maximum function that takes two arguments $$x, y \in \mathbb{R}$$ and returns the largest of the two. +/// The maximum function takes two arguments $$x, y$$ along with a function +/// for comparing $$x, y$$. The function returns the largest of the two given +/// values. /// ///
/// Example /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx +/// import gleam/float +/// import gleam_community/maths/piecewise /// /// pub fn example() { -/// floatx.maximum(2.0, 1.5) +/// piecewise.maximum(2.0, 1.5, float.compare) /// |> should.equal(1.5) /// -/// floatx.maximum(1.5, 2.0) +/// piecewise.maximum(1.5, 2.0, float.compare) /// |> should.equal(1.5) /// } ///
@@ -818,19 +871,22 @@ pub fn maximum(x: a, y: a, compare: fn(a, a) -> order.Order) -> a { /// /// /// -/// The minmax function that takes two arguments $$x, y \in \mathbb{R}$$ and returns a tuple with the smallest value first and largest second. +/// The minmax function takes two arguments $$x, y$$ along with a function +/// for comparing $$x, y$$. The function returns a tuple with the smallest +/// value first and largest second. /// ///
/// Example /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx +/// import gleam/float +/// import gleam_community/maths/piecewise /// /// pub fn example() { -/// floatx.minmax(2.0, 1.5) +/// piecewise.minmax(2.0, 1.5, float.compare) /// |> should.equal(#(1.5, 2.0)) /// -/// floatx.minmax(1.5, 2.0) +/// piecewise.minmax(1.5, 2.0, float.compare) /// |> should.equal(#(1.5, 2.0)) /// } ///
@@ -851,23 +907,24 @@ pub fn minmax(x: a, y: a, compare: fn(a, a) -> order.Order) -> #(a, a) { /// /// /// -/// Returns the minimum value of a list. +/// Returns the minimum value of a given list. /// ///
/// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int_list +/// import gleam/int +/// import gleam_community/maths/piecewise /// /// pub fn example () { /// // An empty lists returns an error /// [] -/// |> int_list.minimum() +/// |> piecewise.list_minimum(int.compare) /// |> should.be_error() /// /// // Valid input returns a result /// [4, 4, 3, 2, 1] -/// |> int_list.minimum() +/// |> piecewise.list_minimum(int.compare) /// |> should.equal(Ok(1)) /// } ///
@@ -908,23 +965,24 @@ pub fn list_minimum( /// /// /// -/// Returns the maximum value of a list. +/// Returns the maximum value of a given list. /// ///
/// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float_list +/// import gleam/float +/// import gleam_community/maths/piecewise /// /// pub fn example () { /// // An empty lists returns an error /// [] -/// |> float_list.maximum() +/// |> piecewise.list_maximum(float.compare) /// |> should.be_error() /// /// // Valid input returns a result /// [4.0, 4.0, 3.0, 2.0, 1.0] -/// |> float_list.maximum() +/// |> piecewise.list_maximum(float.compare) /// |> should.equal(Ok(4.0)) /// } ///
@@ -972,23 +1030,24 @@ pub fn list_maximum( /// /// /// -/// Returns the indices of the minimum values in a list. +/// Returns the indices of the minimum values in a given list. /// ///
/// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float_list +/// import gleam/float +/// import gleam_community/maths/piecewise /// /// pub fn example () { /// // An empty lists returns an error /// [] -/// |> float_list.arg_minimum() +/// |> piecewise.arg_minimum(float.compare) /// |> should.be_error() /// /// // Valid input returns a result /// [4.0, 4.0, 3.0, 2.0, 1.0] -/// |> float_list.arg_minimum() +/// |> piecewise.arg_minimum(float.compare) /// |> should.equal(Ok([4])) /// } ///
@@ -1041,23 +1100,24 @@ pub fn arg_minimum( /// /// /// -/// Returns the indices of the maximum values in a list. +/// Returns the indices of the maximum values in a given list. /// ///
/// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float_list +/// import gleam/float +/// import gleam_community/maths/piecewise /// /// pub fn example () { /// // An empty lists returns an error /// [] -/// |> float_list.arg_maximum() +/// |> piecewise.arg_maximum(float.compare) /// |> should.be_error() /// /// // Valid input returns a result /// [4.0, 4.0, 3.0, 2.0, 1.0] -/// |> float_list.arg_maximum() +/// |> piecewise.arg_maximum(float.compare) /// |> should.equal(Ok([0, 1])) /// } ///
@@ -1110,23 +1170,24 @@ pub fn arg_maximum( /// /// /// -/// Returns a tuple consisting of the minimum and maximum value of a list. +/// Returns a tuple consisting of the minimum and maximum values of a given list. /// ///
/// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float_list +/// import gleam/float +/// import gleam_community/maths/piecewise /// /// pub fn example () { /// // An empty lists returns an error /// [] -/// |> float_list.extrema() +/// |> piecewise.extrema(float.compare) /// |> should.be_error() /// /// // Valid input returns a result /// [4.0, 4.0, 3.0, 2.0, 1.0] -/// |> float_list.extrema() +/// |> piecewise.extrema(float.compare) /// |> should.equal(Ok(#(1.0, 4.0))) /// } ///
diff --git a/src/gleam_community/maths/sequences.gleam b/src/gleam_community/maths/sequences.gleam index 4ed0812..0ebb705 100644 --- a/src/gleam_community/maths/sequences.gleam +++ b/src/gleam_community/maths/sequences.gleam @@ -49,20 +49,20 @@ import gleam/list /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float_list +/// import gleam_community/maths/sequences /// /// pub fn example () { -/// float_list.arange(1.0, 5.0, 1.0) +/// sequences.arange(1.0, 5.0, 1.0) /// |> should.equal([1.0, 2.0, 3.0, 4.0]) /// /// // No points returned since /// // start smaller than stop and positive step -/// float_list.arange(5.0, 1.0, 1.0) +/// sequences.arange(5.0, 1.0, 1.0) /// |> should.equal([]) /// /// // Points returned since /// // start smaller than stop but negative step -/// float_list.arange(5.0, 1.0, -1.0) +/// sequences.arange(5.0, 1.0, -1.0) /// |> should.equal([5.0, 4.0, 3.0, 2.0]) /// } /// @@ -104,20 +104,21 @@ pub fn arange(start: Float, stop: Float, step: Float) -> List(Float) { /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx -/// import gleam_community/maths/float_list +/// import gleam_community/maths/elementary +/// import gleam_community/maths/sequences +/// import gleam_community/maths/tests /// /// pub fn example () { -/// let assert Ok(tol) = floatx.power(-10.0, -6.0) -/// let assert Ok(linspace) = float_list.linear_space(10.0, 50.0, 5, True) +/// let assert Ok(tol) = elementary.power(-10.0, -6.0) +/// let assert Ok(linspace) = sequences.linear_space(10.0, 50.0, 5, True) /// let assert Ok(result) = -/// float_list.all_close(linspace, [10.0, 20.0, 30.0, 40.0, 50.0], 0.0, tol) +/// tests.all_close(linspace, [10.0, 20.0, 30.0, 40.0, 50.0], 0.0, tol) /// result /// |> list.all(fn(x) { x == True }) /// |> should.be_true() /// /// // A negative number of points (-5) does not work -/// float_list.linear_space(10.0, 50.0, -5, True) +/// sequences.linear_space(10.0, 50.0, -5, True) /// |> should.be_error() /// } /// @@ -183,20 +184,21 @@ pub fn linear_space( /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx -/// import gleam_community/maths/float_list +/// import gleam_community/maths/elementary +/// import gleam_community/maths/sequences +/// import gleam_community/maths/tests /// /// pub fn example () { -/// let assert Ok(tol) = floatx.power(-10.0, -6.0) -/// let assert Ok(logspace) = float_list.logarithmic_space(1.0, 3.0, 3, True, 10.0) +/// let assert Ok(tol) = elementary.power(-10.0, -6.0) +/// let assert Ok(logspace) = sequences.logarithmic_space(1.0, 3.0, 3, True, 10.0) /// let assert Ok(result) = -/// float_list.all_close(logspace, [10.0, 100.0, 1000.0], 0.0, tol) +/// tests.all_close(logspace, [10.0, 100.0, 1000.0], 0.0, tol) /// result /// |> list.all(fn(x) { x == True }) /// |> should.be_true() /// /// // A negative number of points (-3) does not work -/// float_list.logarithmic_space(1.0, 3.0, -3, False, 10.0) +/// sequences.logarithmic_space(1.0, 3.0, -3, False, 10.0) /// |> should.be_error() /// } /// @@ -243,27 +245,28 @@ pub fn logarithmic_space( /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx -/// import gleam_community/maths/float_list +/// import gleam_community/maths/elementary +/// import gleam_community/maths/sequences +/// import gleam_community/maths/tests /// /// pub fn example () { -/// let assert Ok(tol) = floatx.power(-10.0, -6.0) -/// let assert Ok(logspace) = float_list.geometric_space(10.0, 1000.0, 3, True) +/// let assert Ok(tol) = elementary.power(-10.0, -6.0) +/// let assert Ok(logspace) = sequences.geometric_space(10.0, 1000.0, 3, True) /// let assert Ok(result) = -/// float_list.all_close(logspace, [10.0, 100.0, 1000.0], 0.0, tol) +/// tests.all_close(logspace, [10.0, 100.0, 1000.0], 0.0, tol) /// result /// |> list.all(fn(x) { x == True }) /// |> should.be_true() /// /// // Input (start and stop can't be equal to 0.0) -/// float_list.geometric_space(0.0, 1000.0, 3, False) +/// sequences.geometric_space(0.0, 1000.0, 3, False) /// |> should.be_error() /// -/// float_list.geometric_space(-1000.0, 0.0, 3, False) +/// sequences.geometric_space(-1000.0, 0.0, 3, False) /// |> should.be_error() /// /// // A negative number of points (-3) does not work -/// float_list.geometric_space(10.0, 1000.0, -3, False) +/// sequences.geometric_space(10.0, 1000.0, -3, False) /// |> should.be_error() /// } /// diff --git a/src/gleam_community/maths/tests.gleam b/src/gleam_community/maths/tests.gleam index 8ff3107..4ae75c4 100644 --- a/src/gleam_community/maths/tests.gleam +++ b/src/gleam_community/maths/tests.gleam @@ -61,7 +61,7 @@ import gleam_community/maths/arithmetics /// Example /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx +/// import gleam_community/maths/tests /// /// pub fn example () { /// let val: Float = 99. @@ -115,7 +115,7 @@ fn float_absolute_difference(a: Float, b: Float) -> Float { /// /// import gleeunit/should /// import gleam/list -/// import gleam_community/maths/float_list +/// import gleam_community/maths/tests /// /// pub fn example () { /// let val: Float = 99. @@ -126,7 +126,7 @@ fn float_absolute_difference(a: Float, b: Float) -> Float { /// // if 'val' is within 1 percent of 'ref_val' +/- 0.1 /// let rtol: Float = 0.01 /// let atol: Float = 0.10 -/// float_list.all_close(xarr, yarr, rtol, atol) +/// tests.all_close(xarr, yarr, rtol, atol) /// |> fn(zarr: Result(List(Bool), String)) -> Result(Bool, Nil) { /// case zarr { /// Ok(arr) -> @@ -173,7 +173,7 @@ pub fn all_close( /// /// /// -/// Determine if a given value $$a$$ is fractional. +/// Determine if a given value is fractional. /// /// `True` is returned if the given value is fractional, otherwise `False` is returned. /// @@ -181,9 +181,14 @@ pub fn all_close( /// Example /// /// import gleeunit/should -/// import gleam_community/maths/float as floatx +/// import gleam_community/maths/tests /// /// pub fn example () { +/// tests.is_fractional(0.3333) +/// |> should.equal(True) +/// +/// tests.is_fractional(1.0) +/// |> should.equal(False) /// } /// /// @@ -213,15 +218,15 @@ fn do_ceiling(a: Float) -> Float /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int as intx +/// import gleam_community/maths/tests /// /// pub fn example() { /// // Check if 4 is a power of 2 (it is) -/// intx.is_power(4, 2) +/// tests.is_power(4, 2) /// |> should.equal(True) /// /// // Check if 5 is a power of 2 (it is not) -/// intx.is_power(5, 2) +/// tests.is_power(5, 2) /// |> should.equal(False) /// } /// @@ -261,13 +266,13 @@ pub fn is_power(x: Int, y: Int) -> Bool { /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int as intx +/// import gleam_community/maths/tests /// /// pub fn example() { -/// intx.is_perfect(6) +/// tests.is_perfect(6) /// |> should.equal(True) /// -/// intx.is_perfect(28) +/// tests.is_perfect(28) /// |> should.equal(True) /// } /// @@ -303,13 +308,13 @@ fn do_sum(arr: List(Int)) -> Int { /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int as intx +/// import gleam_community/maths/tests /// /// pub fn example() { -/// intx.is_even(-3) +/// tests.is_even(-3) /// |> should.equal(False) /// -/// intx.is_even(-4) +/// tests.is_even(-4) /// |> should.equal(True) /// } /// @@ -336,13 +341,13 @@ pub fn is_even(x: Int) -> Bool { /// Example: /// /// import gleeunit/should -/// import gleam_community/maths/int as intx +/// import gleam_community/maths/tests /// /// pub fn example() { -/// intx.is_odd(-3) +/// tests.is_odd(-3) /// |> should.equal(True) /// -/// intx.is_odd(-4) +/// tests.is_odd(-4) /// |> should.equal(False) /// } /// diff --git a/test/gleam/gleam_community_maths_metrics.gleam b/test/gleam/gleam_community_maths_metrics.gleam index 0c91b6d..0872c4e 100644 --- a/test/gleam/gleam_community_maths_metrics.gleam +++ b/test/gleam/gleam_community_maths_metrics.gleam @@ -58,34 +58,33 @@ pub fn float_list_manhatten_test() { let assert Ok(tol) = elementary.power(-10.0, -6.0) // Empty lists returns 0.0 - metrics.float_manhatten_distance([], []) + metrics.manhatten_distance([], []) |> should.equal(Ok(0.0)) // Differing lengths returns error - metrics.float_manhatten_distance([], [1.0]) + metrics.manhatten_distance([], [1.0]) |> should.be_error() // Manhatten distance (p = 1) - let assert Ok(result) = - metrics.float_manhatten_distance([0.0, 0.0], [1.0, 2.0]) + let assert Ok(result) = metrics.manhatten_distance([0.0, 0.0], [1.0, 2.0]) result |> tests.is_close(3.0, 0.0, tol) |> should.be_true() } -pub fn int_list_manhatten_test() { - // Empty lists returns 0 - metrics.int_manhatten_distance([], []) - |> should.equal(Ok(0)) +// pub fn int_list_manhatten_test() { +// // Empty lists returns 0 +// metrics.int_manhatten_distance([], []) +// |> should.equal(Ok(0)) - // Differing lengths returns error - metrics.int_manhatten_distance([], [1]) - |> should.be_error() +// // Differing lengths returns error +// metrics.int_manhatten_distance([], [1]) +// |> should.be_error() - let assert Ok(result) = metrics.int_manhatten_distance([0, 0], [1, 2]) - result - |> should.equal(3) -} +// let assert Ok(result) = metrics.int_manhatten_distance([0, 0], [1, 2]) +// result +// |> should.equal(3) +// } pub fn float_list_minkowski_test() { let assert Ok(tol) = elementary.power(-10.0, -6.0) diff --git a/test/gleam/gleam_community_maths_special.gleam b/test/gleam/gleam_community_maths_special.gleam index 6aa3587..78d84ee 100644 --- a/test/gleam/gleam_community_maths_special.gleam +++ b/test/gleam/gleam_community_maths_special.gleam @@ -1,30 +1,92 @@ +import gleam_community/maths/elementary import gleam_community/maths/special import gleam_community/maths/tests import gleeunit import gleeunit/should import gleam/result -import gleam/io pub fn main() { gleeunit.main() } pub fn float_beta_function_test() { - io.debug("") - io.debug("TODO: Implement tests for 'float.beta'.") + let assert Ok(tol) = elementary.power(-10.0, -6.0) + + // Valid input returns a result + special.beta(-0.5, 0.5) + |> tests.is_close(0.0, 0.0, tol) + |> should.be_true() + + special.beta(0.5, 0.5) + |> tests.is_close(3.1415926535897927, 0.0, tol) + |> should.be_true() + + special.beta(0.5, -0.5) + |> tests.is_close(0.0, 0.0, tol) + |> should.be_true() + + special.beta(5.0, 5.0) + |> tests.is_close(0.0015873015873015873, 0.0, tol) + |> should.be_true() } pub fn float_error_function_test() { - io.debug("") - io.debug("TODO: Implement tests for 'float.erf'.") + let assert Ok(tol) = elementary.power(-10.0, -6.0) + + // Valid input returns a result + special.erf(-0.5) + |> tests.is_close(-0.5204998778130465, 0.0, tol) + |> should.be_true() + + special.erf(0.5) + |> tests.is_close(0.5204998778130465, 0.0, tol) + |> should.be_true() + + special.erf(1.0) + |> tests.is_close(0.8427007929497148, 0.0, tol) + |> should.be_true() + + special.erf(2.0) + |> tests.is_close(0.9953222650189527, 0.0, tol) + |> should.be_true() + + special.erf(10.0) + |> tests.is_close(1.0, 0.0, tol) + |> should.be_true() } pub fn float_gamma_function_test() { - io.debug("") - io.debug("TODO: Implement tests for 'float.gamma'.") + let assert Ok(tol) = elementary.power(-10.0, -6.0) + + // Valid input returns a result + special.gamma(-0.5) + |> tests.is_close(-3.5449077018110318, 0.0, tol) + |> should.be_true() + + special.gamma(0.5) + |> tests.is_close(1.7724538509055159, 0.0, tol) + |> should.be_true() + + special.gamma(1.0) + |> tests.is_close(1.0, 0.0, tol) + |> should.be_true() + + special.gamma(2.0) + |> tests.is_close(1.0, 0.0, tol) + |> should.be_true() + + special.gamma(3.0) + |> tests.is_close(2.0, 0.0, tol) + |> should.be_true() + + special.gamma(10.0) + |> tests.is_close(362_880.0, 0.0, tol) + |> should.be_true() } pub fn float_incomplete_gamma_function_test() { + let assert Ok(tol) = elementary.power(-10.0, -6.0) + // Invalid input gives an error // 1st arg is invalid special.incomplete_gamma(-1.0, 1.0) @@ -37,21 +99,21 @@ pub fn float_incomplete_gamma_function_test() { // Valid input returns a result special.incomplete_gamma(1.0, 0.0) |> result.unwrap(-999.0) - |> tests.is_close(0.0, 0.0, 0.01) + |> tests.is_close(0.0, 0.0, tol) |> should.be_true() special.incomplete_gamma(1.0, 2.0) |> result.unwrap(-999.0) - |> tests.is_close(0.864664716763387308106, 0.0, 0.01) + |> tests.is_close(0.864664716763387308106, 0.0, tol) |> should.be_true() special.incomplete_gamma(2.0, 3.0) |> result.unwrap(-999.0) - |> tests.is_close(0.8008517265285442280826, 0.0, 0.01) + |> tests.is_close(0.8008517265285442280826, 0.0, tol) |> should.be_true() special.incomplete_gamma(3.0, 4.0) |> result.unwrap(-999.0) - |> tests.is_close(1.523793388892911312363, 0.0, 0.01) + |> tests.is_close(1.523793388892911312363, 0.0, tol) |> should.be_true() }