Update examples

This commit is contained in:
NicklasXYZ 2023-09-17 14:06:59 +02:00
parent c7e703a3c4
commit 7ca932dc2f
10 changed files with 466 additions and 372 deletions

View file

@ -60,16 +60,16 @@ import gleam_community/maths/piecewise
/// <summary>Example:</summary>
///
/// 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)
/// }
/// </details>
@ -109,16 +109,16 @@ pub fn do_gcd(x: Int, y: Int) -> Int {
/// <summary>Example:</summary>
///
/// 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)
/// }
/// </details>
@ -147,16 +147,16 @@ pub fn lcm(x: Int, y: Int) -> Int {
/// <summary>Example:</summary>
///
/// 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])
/// }
/// </details>
@ -201,16 +201,16 @@ pub fn find_divisors(n: Int) -> List(Int) {
/// <summary>Example:</summary>
///
/// 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])
/// }
/// </details>
@ -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$$.
///
/// <details>
/// <summary>Example:</summary>
///
/// 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)
/// }
/// </details>
@ -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$$.
///
/// <details>
/// <summary>Example:</summary>
///
/// 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)
/// }
/// </details>
@ -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$$.
///
/// <details>
/// <summary>Example:</summary>
///
/// 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)
/// }
/// </details>
@ -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$$.
///
/// <details>
/// <summary>Example:</summary>
///
/// 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)
/// }
/// </details>
@ -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.
///
/// <details>
/// <summary>Example:</summary>
///
/// 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])
/// }
/// </details>
@ -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.
///
/// <details>
/// <summary>Example:</summary>
///
/// 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])
/// }
/// </details>
@ -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.
///
/// <details>
/// <summary>Example:</summary>
///
/// 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])
/// }
/// </details>
@ -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.
///
/// <details>
/// <summary>Example:</summary>
///
/// 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])
/// }
/// </details>

View file

@ -56,22 +56,22 @@ import gleam/set
/// <summary>Example:</summary>
///
/// 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))
/// }
/// </details>
@ -127,23 +127,27 @@ pub fn combination(n: Int, k: Int) -> Result(Int, String) {
/// <summary>Example:</summary>
///
/// 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))
/// }
/// </details>
@ -192,22 +196,22 @@ pub fn factorial(n) -> Result(Int, String) {
/// <summary>Example:</summary>
///
/// 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))
/// }
/// </details>
@ -256,10 +260,14 @@ pub fn permutation(n: Int, k: Int) -> Result(Int, String) {
/// <summary>Example:</summary>
///
/// 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]]))
/// }
/// </details>
///
@ -309,16 +317,27 @@ fn do_list_combination(arr: List(a), k: Int, prefix: List(a)) -> List(List(a)) {
/// </a>
/// </div>
///
/// Generate all permutations based on a given list.
/// Generate all permutations of a given list.
///
/// <details>
/// <summary>Example:</summary>
///
/// 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],
/// ]))
/// }
/// </details>
///
@ -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)])
/// }
/// </details>

View file

@ -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
/// <summary>Example:</summary>
///
/// 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)
/// }
/// </details>
@ -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))
/// }
/// </details>
@ -123,11 +124,12 @@ fn do_to_int(a: Float) -> Int
/// <summary>Example</summary>
///
/// 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())
/// }
/// </details>
///
@ -154,13 +156,14 @@ pub fn degrees_to_radians(x: Float) -> Float {
/// <summary>Example</summary>
///
/// 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.)
/// }
/// </details>

View file

@ -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()
/// }
/// </details>
@ -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()
/// }
/// </details>
@ -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)
/// }
/// </details>
@ -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)
/// }
/// </details>
@ -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)
/// }
/// </details>
@ -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()
/// }
/// </details>
@ -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)
/// }
/// </details>
@ -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)
/// }
/// </details>
@ -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)
/// }
/// </details>
@ -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)
/// }
/// </details>
@ -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)
/// }
/// </details>
@ -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)
/// }
/// </details>
@ -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)
/// }
/// </details>
@ -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()
/// }
/// </details>
@ -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()
/// }
/// </details>
@ -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()
/// }
/// </details>
@ -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()
/// }
/// </details>
@ -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()
/// }
/// </details>
@ -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()
/// }
/// </details>
@ -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()
/// }
/// </details>
@ -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()
/// }
/// </details>
@ -1235,12 +1235,12 @@ pub fn tau() -> Float {
/// <summary>Example</summary>
///
/// 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()
/// }
/// </details>

View file

@ -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
/// <summary>Example:</summary>
///
/// 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()
/// }
/// </details>
@ -127,23 +127,24 @@ pub fn norm(arr: List(Float), p: Float) -> Float {
/// <summary>Example:</summary>
///
/// 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()
/// }
/// </details>
@ -154,74 +155,13 @@ pub fn norm(arr: List(Float), p: Float) -> Float {
/// </a>
/// </div>
///
pub fn float_manhatten_distance(
pub fn manhatten_distance(
xarr: List(Float),
yarr: List(Float),
) -> Result(Float, String) {
minkowski_distance(xarr, yarr, 1.0)
}
/// <div style="text-align: right;">
/// <a href="https://github.com/gleam-community/maths/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// </div>
///
/// 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$$.
///
/// <details>
/// <summary>Example:</summary>
///
/// 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)
/// }
/// </details>
///
/// <div style="text-align: right;">
/// <a href="#">
/// <small>Back to top </small>
/// </a>
/// </div>
///
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
}
}
/// <div style="text-align: right;">
/// <a href="https://github.com/gleam-community/maths/issues">
/// <small>Spot a typo? Open an issue!</small>
@ -242,27 +182,28 @@ pub fn int_manhatten_distance(
/// <summary>Example:</summary>
///
/// 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()
/// }
/// </details>
@ -318,23 +259,24 @@ pub fn minkowski_distance(
/// <summary>Example:</summary>
///
/// 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()
/// }
/// </details>
@ -371,17 +313,17 @@ pub fn euclidean_distance(
/// <summary>Example:</summary>
///
/// 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.))
/// }
/// </details>
@ -419,21 +361,21 @@ pub fn mean(arr: List(Float)) -> Result(Float, String) {
/// <summary>Example:</summary>
///
/// 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))
/// }
/// </details>
@ -495,7 +437,7 @@ pub fn median(arr: List(Float)) -> Result(Float, String) {
/// <summary>Example:</summary>
///
/// 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.))
/// }
/// </details>
@ -571,7 +513,7 @@ pub fn variance(arr: List(Float), ddof: Int) -> Result(Float, String) {
/// <summary>Example:</summary>
///
/// 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.))
/// }
/// </details>

View file

@ -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))
/// }
/// </details>
@ -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))
/// }
/// </details>
@ -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))
/// }
/// </details>
@ -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))
/// }
/// </details>
@ -482,6 +482,28 @@ fn round_up(p: Float, x: Float) -> Float {
@external(javascript, "../../maths.mjs", "ceiling")
fn do_ceiling(a: Float) -> Float
/// <div style="text-align: right;">
/// <a href="https://github.com/gleam-community/maths/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// </div>
///
/// 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.
///
/// </details>
///
/// <div style="text-align: right;">
/// <a href="#">
/// <small>Back to top </small>
/// </a>
/// </div>
///
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 {
}
}
/// <div style="text-align: right;">
/// <a href="https://github.com/gleam-community/maths/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// </div>
///
/// 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.
///
/// </details>
///
/// <div style="text-align: right;">
/// <a href="#">
/// <small>Back to top </small>
/// </a>
/// </div>
///
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 {
/// <summary>Example</summary>
///
/// 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)
/// }
/// </details>
@ -555,13 +599,13 @@ pub fn float_absolute_difference(a: Float, b: Float) -> Float {
/// <summary>Example:</summary>
///
/// 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)
/// }
/// </details>
@ -583,7 +627,9 @@ pub fn int_absolute_difference(a: Int, b: Int) -> Int {
/// </a>
/// </div>
///
/// 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).
///
/// <div style="text-align: right;">
/// <a href="#">
@ -617,8 +663,9 @@ fn do_float_sign(a: Float) -> Float
/// </a>
/// </div>
///
/// 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).
///
/// <div style="text-align: right;">
/// <a href="#">
@ -738,19 +785,22 @@ pub fn int_flip_sign(x: Int) -> Int {
/// </a>
/// </div>
///
/// 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.
///
/// <details>
/// <summary>Example</summary>
///
/// 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)
/// }
/// </details>
@ -781,19 +831,22 @@ pub fn minimum(x: a, y: a, compare: fn(a, a) -> order.Order) -> a {
/// </a>
/// </div>
///
/// 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.
///
/// <details>
/// <summary>Example</summary>
///
/// 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)
/// }
/// </details>
@ -818,19 +871,22 @@ pub fn maximum(x: a, y: a, compare: fn(a, a) -> order.Order) -> a {
/// </a>
/// </div>
///
/// 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.
///
/// <details>
/// <summary>Example</summary>
///
/// 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))
/// }
/// </details>
@ -851,23 +907,24 @@ pub fn minmax(x: a, y: a, compare: fn(a, a) -> order.Order) -> #(a, a) {
/// </a>
/// </div>
///
/// Returns the minimum value of a list.
/// Returns the minimum value of a given list.
///
/// <details>
/// <summary>Example:</summary>
///
/// 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))
/// }
/// </details>
@ -908,23 +965,24 @@ pub fn list_minimum(
/// </a>
/// </div>
///
/// Returns the maximum value of a list.
/// Returns the maximum value of a given list.
///
/// <details>
/// <summary>Example:</summary>
///
/// 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))
/// }
/// </details>
@ -972,23 +1030,24 @@ pub fn list_maximum(
/// </a>
/// </div>
///
/// Returns the indices of the minimum values in a list.
/// Returns the indices of the minimum values in a given list.
///
/// <details>
/// <summary>Example:</summary>
///
/// 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]))
/// }
/// </details>
@ -1041,23 +1100,24 @@ pub fn arg_minimum(
/// </a>
/// </div>
///
/// Returns the indices of the maximum values in a list.
/// Returns the indices of the maximum values in a given list.
///
/// <details>
/// <summary>Example:</summary>
///
/// 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]))
/// }
/// </details>
@ -1110,23 +1170,24 @@ pub fn arg_maximum(
/// </a>
/// </div>
///
/// 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.
///
/// <details>
/// <summary>Example:</summary>
///
/// 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)))
/// }
/// </details>

View file

@ -49,20 +49,20 @@ import gleam/list
/// <summary>Example:</summary>
///
/// 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])
/// }
/// </details>
@ -104,20 +104,21 @@ pub fn arange(start: Float, stop: Float, step: Float) -> List(Float) {
/// <summary>Example:</summary>
///
/// 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()
/// }
/// </details>
@ -183,20 +184,21 @@ pub fn linear_space(
/// <summary>Example:</summary>
///
/// 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()
/// }
/// </details>
@ -243,27 +245,28 @@ pub fn logarithmic_space(
/// <summary>Example:</summary>
///
/// 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()
/// }
/// </details>

View file

@ -61,7 +61,7 @@ import gleam_community/maths/arithmetics
/// <summary>Example</summary>
///
/// 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(
/// </a>
/// </div>
///
/// 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(
/// <summary>Example</summary>
///
/// 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)
/// }
/// </details>
///
@ -213,15 +218,15 @@ fn do_ceiling(a: Float) -> Float
/// <summary>Example:</summary>
///
/// 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)
/// }
/// </details>
@ -261,13 +266,13 @@ pub fn is_power(x: Int, y: Int) -> Bool {
/// <summary>Example:</summary>
///
/// 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)
/// }
/// </details>
@ -303,13 +308,13 @@ fn do_sum(arr: List(Int)) -> Int {
/// <summary>Example:</summary>
///
/// 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)
/// }
/// </details>
@ -336,13 +341,13 @@ pub fn is_even(x: Int) -> Bool {
/// <summary>Example:</summary>
///
/// 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)
/// }
/// </details>

View file

@ -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)

View file

@ -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()
}