@@ -273,7 +273,7 @@ pub fn truncate(x: Float, digits: option.Option(Int)) -> Float {
/// - \\(12.07\\) for 2 digits after the decimal point (`digits = 2`)
/// - \\(12.065\\) for 3 digits after the decimal point (`digits = 3`)
///
-/// It is also possible to specify a negative number of digits. In that case, the negative
+/// It is also possible to specify a negative number of digits. In that case, the negative
/// number refers to the digits before the decimal point.
/// - \\(10.0\\) for 1 digit before the decimal point (`digits = -1`)
/// - \\(0.0\\) for 2 digits before the decimal point (`digits = -2`)
@@ -285,7 +285,7 @@ pub fn truncate(x: Float, digits: option.Option(Int)) -> Float {
/// - \\(12.07\\) for 2 digits after the decimal point (`digits = 2`)
/// - \\(12.065\\) for 3 digits after the decimal point (`digits = 3`)
///
-/// It is also possible to specify a negative number of digits. In that case, the negative
+/// It is also possible to specify a negative number of digits. In that case, the negative
/// number refers to the digits before the decimal point.
/// - \\(10.0\\) for 1 digit before the decimal point (`digits = -1`)
/// - \\(0.0\\) for 2 digits before the decimal point (`digits = -2`)
@@ -309,7 +309,7 @@ pub fn truncate(x: Float, digits: option.Option(Int)) -> Float {
/// - \\(12.06\\) for 2 digits after the decimal point (`digits = 2`)
/// - \\(12.065\\) for 3 digits after the decimal point (`digits = 3`)
///
-/// It is also possible to specify a negative number of digits. In that case, the negative
+/// It is also possible to specify a negative number of digits. In that case, the negative
/// number refers to the digits before the decimal point.
/// - \\(10.0\\) for 1 digit before the decimal point (`digits = -1`)
/// - \\(0.0\\) for 2 digits before the decimal point (`digits = -2`)
@@ -321,7 +321,7 @@ pub fn truncate(x: Float, digits: option.Option(Int)) -> Float {
/// - \\(12.06\\) for 2 digits after the decimal point (`digits = 2`)
/// - \\(12.065\\) for 3 digits after the decimal point (`digits = 3`)
///
-/// It is also possible to specify a negative number of digits. In that case, the negative
+/// It is also possible to specify a negative number of digits. In that case, the negative
/// number refers to the digits before the decimal point.
/// - \\(10.0\\) for 1 digit before the decimal point (`digits = -1`)
/// - \\(0.0\\) for 2 digits before the decimal point (`digits = -2`)
@@ -500,7 +500,7 @@ fn do_ceiling(a: Float) -> Float
/// The absolute value:
///
/// \\[
-/// \forall x \in \mathbb{R}, \\; |x| \in \mathbb{R}_{+}.
+/// \forall x \in \mathbb{R}, \\; |x| \in \mathbb{R}_{+}.
/// \\]
///
/// The function takes an input \\(x\\) and returns a positive float value.
@@ -529,7 +529,7 @@ pub fn float_absolute_value(x: Float) -> Float {
/// The absolute value:
///
/// \\[
-/// \forall x \in \mathbb{Z}, \\; |x| \in \mathbb{Z}_{+}.
+/// \forall x \in \mathbb{Z}, \\; |x| \in \mathbb{Z}_{+}.
/// \\]
///
/// The function takes an input \\(x\\) and returns a positive integer value.
@@ -709,7 +709,7 @@ fn do_int_sign(a: Int) -> Int
///
///
///
-/// The function takes two arguments \\(x, y \in \mathbb{R}\\) and returns \\(x\\)
+/// The function takes two arguments \\(x, y \in \mathbb{R}\\) and returns \\(x\\)
/// such that it has the same sign as \\(y\\).
///
///
@@ -735,7 +735,7 @@ pub fn float_copy_sign(x: Float, y: Float) -> Float {
///
///
///
-/// The function takes two arguments \\(x, y \in \mathbb{Z}\\) and returns \\(x\\)
+/// The function takes two arguments \\(x, y \in \mathbb{Z}\\) and returns \\(x\\)
/// such that it has the same sign as \\(y\\).
///
///
@@ -949,11 +949,9 @@ pub fn minmax(x: a, y: a, compare: fn(a, a) -> order.Order) -> #(a, a) {
pub fn list_minimum(
arr: List(a),
compare: fn(a, a) -> order.Order,
-) -> Result(a, String) {
+) -> Result(a, Nil) {
case arr {
- [] ->
- "Invalid input argument: The list is empty."
- |> Error
+ [] -> Error(Nil)
[x, ..rest] ->
Ok(
list.fold(rest, x, fn(acc: a, element: a) {
@@ -1003,11 +1001,9 @@ pub fn list_minimum(
pub fn list_maximum(
arr: List(a),
compare: fn(a, a) -> order.Order,
-) -> Result(a, String) {
+) -> Result(a, Nil) {
case arr {
- [] ->
- "Invalid input argument: The list is empty."
- |> Error
+ [] -> Error(Nil)
[x, ..rest] ->
Ok(
list.fold(rest, x, fn(acc: a, element: a) {
@@ -1063,11 +1059,9 @@ pub fn list_maximum(
pub fn arg_minimum(
arr: List(a),
compare: fn(a, a) -> order.Order,
-) -> Result(List(Int), String) {
+) -> Result(List(Int), Nil) {
case arr {
- [] ->
- "Invalid input argument: The list is empty."
- |> Error
+ [] -> Error(Nil)
_ -> {
let assert Ok(min) =
arr
@@ -1133,11 +1127,9 @@ pub fn arg_minimum(
pub fn arg_maximum(
arr: List(a),
compare: fn(a, a) -> order.Order,
-) -> Result(List(Int), String) {
+) -> Result(List(Int), Nil) {
case arr {
- [] ->
- "Invalid input argument: The list is empty."
- |> Error
+ [] -> Error(Nil)
_ -> {
let assert Ok(max) =
arr
@@ -1203,11 +1195,9 @@ pub fn arg_maximum(
pub fn extrema(
arr: List(a),
compare: fn(a, a) -> order.Order,
-) -> Result(#(a, a), String) {
+) -> Result(#(a, a), Nil) {
case arr {
- [] ->
- "Invalid input argument: The list is empty."
- |> Error
+ [] -> Error(Nil)
[x, ..rest] ->
Ok(
list.fold(rest, #(x, x), fn(acc: #(a, a), element: a) {
diff --git a/src/gleam_community/maths/predicates.gleam b/src/gleam_community/maths/predicates.gleam
index 9cdd74c..37391e9 100644
--- a/src/gleam_community/maths/predicates.gleam
+++ b/src/gleam_community/maths/predicates.gleam
@@ -20,12 +20,12 @@
////
-////
+////
//// ---
-////
-//// Predicates: A module containing functions for testing various mathematical
+////
+//// Predicates: A module containing functions for testing various mathematical
//// properties of numbers.
-////
+////
//// * **Tests**
//// * [`is_close`](#is_close)
//// * [`list_all_close`](#all_close)
@@ -38,7 +38,7 @@
//// * [`is_divisible`](#is_divisible)
//// * [`is_multiple`](#is_multiple)
//// * [`is_prime`](#is_prime)
-////
+////
import gleam/int
import gleam/list
@@ -54,16 +54,16 @@ import gleam_community/maths/piecewise
///
///
///
-/// Determine if a given value \\(a\\) is close to or equivalent to a reference value
+/// Determine if a given value \\(a\\) is close to or equivalent to a reference value
/// \\(b\\) based on supplied relative \\(r_{tol}\\) and absolute \\(a_{tol}\\) tolerance
-/// values. The equivalance of the two given values are then determined based on
+/// values. The equivalance of the two given values are then determined based on
/// the equation:
///
/// \\[
/// \|a - b\| \leq (a_{tol} + r_{tol} \cdot \|b\|)
/// \\]
///
-/// `True` is returned if statement holds, otherwise `False` is returned.
+/// `True` is returned if statement holds, otherwise `False` is returned.
///
/// Example
///
@@ -135,7 +135,7 @@ fn float_absolute_difference(a: Float, b: Float) -> Float {
/// let rtol: Float = 0.01
/// let atol: Float = 0.10
/// predicates.all_close(xarr, yarr, rtol, atol)
-/// |> fn(zarr: Result(List(Bool), String)) -> Result(Bool, Nil) {
+/// |> fn(zarr: Result(List(Bool), Nil)) -> Result(Bool, Nil) {
/// case zarr {
/// Ok(arr) ->
/// arr
@@ -159,13 +159,11 @@ pub fn all_close(
yarr: List(Float),
rtol: Float,
atol: Float,
-) -> Result(List(Bool), String) {
+) -> Result(List(Bool), Nil) {
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
+ False -> Error(Nil)
True ->
list.zip(xarr, yarr)
|> list.map(fn(z: #(Float, Float)) -> Bool {
@@ -182,10 +180,10 @@ pub fn all_close(
///
///
/// Determine if a given value is fractional.
-///
-/// `True` is returned if the given value is fractional, otherwise `False` is
-/// returned.
-///
+///
+/// `True` is returned if the given value is fractional, otherwise `False` is
+/// returned.
+///
///
/// Example
///
@@ -195,7 +193,7 @@ pub fn all_close(
/// pub fn example () {
/// predicates.is_fractional(0.3333)
/// |> should.equal(True)
-///
+///
/// predicates.is_fractional(1.0)
/// |> should.equal(False)
/// }
@@ -222,7 +220,7 @@ fn do_ceiling(a: Float) -> Float
///
///
/// A function that tests whether a given integer value \\(x \in \mathbb{Z}\\) is a
-/// power of another integer value \\(y \in \mathbb{Z}\\).
+/// power of another integer value \\(y \in \mathbb{Z}\\).
///
///
/// Example:
@@ -262,9 +260,9 @@ pub fn is_power(x: Int, y: Int) -> Bool {
///
///
/// A function that tests whether a given integer value \\(n \in \mathbb{Z}\\) is a
-/// perfect number. A number is perfect if it is equal to the sum of its proper
+/// perfect number. A number is perfect if it is equal to the sum of its proper
/// positive divisors.
-///
+///
///
/// Details
///
@@ -314,7 +312,7 @@ fn do_sum(arr: List(Int)) -> Int {
///
///
///
-/// A function that tests whether a given integer value \\(x \in \mathbb{Z}\\) is even.
+/// A function that tests whether a given integer value \\(x \in \mathbb{Z}\\) is even.
///
///
/// Example:
@@ -325,7 +323,7 @@ fn do_sum(arr: List(Int)) -> Int {
/// pub fn example() {
/// predicates.is_even(-3)
/// |> should.equal(False)
-///
+///
/// predicates.is_even(-4)
/// |> should.equal(True)
/// }
@@ -347,7 +345,7 @@ pub fn is_even(x: Int) -> Bool {
///
///
///
-/// A function that tests whether a given integer value \\(x \in \mathbb{Z}\\) is odd.
+/// A function that tests whether a given integer value \\(x \in \mathbb{Z}\\) is odd.
///
///
/// Example:
@@ -358,7 +356,7 @@ pub fn is_even(x: Int) -> Bool {
/// pub fn example() {
/// predicates.is_odd(-3)
/// |> should.equal(True)
-///
+///
/// predicates.is_odd(-4)
/// |> should.equal(False)
/// }
@@ -380,24 +378,24 @@ pub fn is_odd(x: Int) -> Bool {
///
///
///
-/// A function that tests whether a given integer value \\(x \in \mathbb{Z}\\) is a
-/// prime number. A prime number is a natural number greater than 1 that has no
+/// A function that tests whether a given integer value \\(x \in \mathbb{Z}\\) is a
+/// prime number. A prime number is a natural number greater than 1 that has no
/// positive divisors other than 1 and itself.
-///
-/// The function uses the Miller-Rabin primality test to assess if \\(x\\) is prime.
-/// It is a probabilistic test, so it can mistakenly identify a composite number
+///
+/// The function uses the Miller-Rabin primality test to assess if \\(x\\) is prime.
+/// It is a probabilistic test, so it can mistakenly identify a composite number
/// as prime. However, the probability of such errors decreases with more testing
-/// iterations (the function uses 64 iterations internally, which is typically
+/// iterations (the function uses 64 iterations internally, which is typically
/// more than sufficient). The Miller-Rabin test is particularly useful for large
/// numbers.
-///
+///
///
/// Details
///
/// Examples of prime numbers:
/// - \\(2\\) is a prime number since it has only two divisors: \\(1\\) and \\(2\\).
/// - \\(7\\) is a prime number since it has only two divisors: \\(1\\) and \\(7\\).
-/// - \\(4\\) is not a prime number since it has divisors other than \\(1\\) and itself, such
+/// - \\(4\\) is not a prime number since it has divisors other than \\(1\\) and itself, such
/// as \\(2\\).
///
///
@@ -414,7 +412,7 @@ pub fn is_odd(x: Int) -> Bool {
///
/// predicates.is_prime(4)
/// |> should.equal(False)
-///
+///
/// // Test the 2nd Carmichael number
/// predicates.is_prime(1105)
/// |> should.equal(False)
@@ -512,9 +510,9 @@ pub fn is_between(x: Float, lower: Float, upper: Float) -> Bool {
///
///
///
-/// A function that tests whether a given integer \\(n \in \mathbb{Z}\\) is divisible by another
+/// A function that tests whether a given integer \\(n \in \mathbb{Z}\\) is divisible by another
/// integer \\(d \in \mathbb{Z}\\), such that \\(n \mod d = 0\\).
-///
+///
///
/// Details
///
@@ -555,9 +553,9 @@ pub fn is_divisible(n: Int, d: Int) -> Bool {
///
///
///
-/// A function that tests whether a given integer \\(m \in \mathbb{Z}\\) is a multiple of another
+/// A function that tests whether a given integer \\(m \in \mathbb{Z}\\) is a multiple of another
/// integer \\(k \in \mathbb{Z}\\), such that \\(m = k \times q\\), with \\(q \in \mathbb{Z}\\).
-///
+///
///
/// Details
///
diff --git a/src/gleam_community/maths/sequences.gleam b/src/gleam_community/maths/sequences.gleam
index 9ffe6ec..daff369 100644
--- a/src/gleam_community/maths/sequences.gleam
+++ b/src/gleam_community/maths/sequences.gleam
@@ -20,18 +20,18 @@
////
-////
+////
//// ---
-////
-//// Sequences: A module containing functions for generating various types of
+////
+//// Sequences: A module containing functions for generating various types of
//// sequences, ranges and intervals.
-////
+////
//// * **Ranges and intervals**
//// * [`arange`](#arange)
//// * [`linear_space`](#linear_space)
//// * [`logarithmic_space`](#logarithmic_space)
//// * [`geometric_space`](#geometric_space)
-////
+////
import gleam/iterator
import gleam_community/maths/conversion
@@ -47,7 +47,7 @@ import gleam_community/maths/piecewise
/// The function returns an iterator generating evenly spaced values within a given interval.
/// based on a start value but excludes the stop value. The spacing between values is determined
/// by the step size provided. The function supports both positive and negative step values.
-///
+///
///
/// Example:
///
@@ -59,13 +59,13 @@ import gleam_community/maths/piecewise
/// sequences.arange(1.0, 5.0, 1.0)
/// |> iterator.to_list()
/// |> should.equal([1.0, 2.0, 3.0, 4.0])
-///
+///
/// // No points returned since
/// // start is smaller than stop and the step is positive
/// sequences.arange(5.0, 1.0, 1.0)
/// |> iterator.to_list()
/// |> should.equal([])
-///
+///
/// // Points returned since
/// // start smaller than stop but negative step
/// sequences.arange(5.0, 1.0, -1.0)
@@ -115,10 +115,10 @@ pub fn arange(
///
///
///
-/// The function returns an iterator for generating linearly spaced points over a specified
-/// interval. The endpoint of the interval can optionally be included/excluded. The number of
+/// The function returns an iterator for generating linearly spaced points over a specified
+/// interval. The endpoint of the interval can optionally be included/excluded. The number of
/// points and whether the endpoint is included determine the spacing between values.
-///
+///
///
/// Example:
///
@@ -138,7 +138,7 @@ pub fn arange(
/// 0.0,
/// tol,
/// )
-///
+///
/// result
/// |> list.all(fn(x) { x == True })
/// |> should.be_true()
@@ -160,7 +160,7 @@ pub fn linear_space(
stop: Float,
num: Int,
endpoint: Bool,
-) -> Result(iterator.Iterator(Float), String) {
+) -> Result(iterator.Iterator(Float), Nil) {
let direction: Float = case start <=. stop {
True -> 1.0
False -> -1.0
@@ -184,9 +184,7 @@ pub fn linear_space(
})
|> Ok
}
- False ->
- "Invalid input: num < 1. Valid input is num >= 1."
- |> Error
+ False -> Error(Nil)
}
}
@@ -196,10 +194,10 @@ pub fn linear_space(
///
///
///
-/// The function returns an iterator of logarithmically spaced points over a specified interval.
-/// The endpoint of the interval can optionally be included/excluded. The number of points, base,
+/// The function returns an iterator of logarithmically spaced points over a specified interval.
+/// The endpoint of the interval can optionally be included/excluded. The number of points, base,
/// and whether the endpoint is included determine the spacing between values.
-///
+///
///
/// Example:
///
@@ -241,7 +239,7 @@ pub fn logarithmic_space(
num: Int,
endpoint: Bool,
base: Float,
-) -> Result(iterator.Iterator(Float), String) {
+) -> Result(iterator.Iterator(Float), Nil) {
case num > 0 {
True -> {
let assert Ok(linspace) = linear_space(start, stop, num, endpoint)
@@ -252,9 +250,7 @@ pub fn logarithmic_space(
})
|> Ok
}
- False ->
- "Invalid input: num < 1. Valid input is num >= 1."
- |> Error
+ False -> Error(Nil)
}
}
@@ -264,9 +260,9 @@ pub fn logarithmic_space(
///
///
///
-/// The function returns an iterator of numbers spaced evenly on a log scale (a geometric
-/// progression). Each point in the list is a constant multiple of the previous. The function is
-/// similar to the [`logarithmic_space`](#logarithmic_space) function, but with endpoints
+/// The function returns an iterator of numbers spaced evenly on a log scale (a geometric
+/// progression). Each point in the list is a constant multiple of the previous. The function is
+/// similar to the [`logarithmic_space`](#logarithmic_space) function, but with endpoints
/// specified directly.
///
///
@@ -295,7 +291,7 @@ pub fn logarithmic_space(
/// // Input (start and stop can't be equal to 0.0)
/// sequences.geometric_space(0.0, 1000.0, 3, False)
/// |> should.be_error()
-///
+///
/// sequences.geometric_space(-1000.0, 0.0, 3, False)
/// |> should.be_error()
///
@@ -316,11 +312,9 @@ pub fn geometric_space(
stop: Float,
num: Int,
endpoint: Bool,
-) -> Result(iterator.Iterator(Float), String) {
+) -> Result(iterator.Iterator(Float), Nil) {
case start == 0.0 || stop == 0.0 {
- True ->
- "Invalid input: Neither 'start' nor 'stop' can be zero, as they must be non-zero for logarithmic calculations."
- |> Error
+ True -> Error(Nil)
False ->
case num > 0 {
True -> {
@@ -328,9 +322,7 @@ pub fn geometric_space(
let assert Ok(log_stop) = elementary.logarithm_10(stop)
logarithmic_space(log_start, log_stop, num, endpoint, 10.0)
}
- False ->
- "Invalid input: num < 1. Valid input is num >= 1."
- |> Error
+ False -> Error(Nil)
}
}
}
diff --git a/src/gleam_community/maths/special.gleam b/src/gleam_community/maths/special.gleam
index f6e3438..ae5c8f6 100644
--- a/src/gleam_community/maths/special.gleam
+++ b/src/gleam_community/maths/special.gleam
@@ -20,17 +20,17 @@
////
-////
+////
//// ---
-////
+////
//// Special: A module containing special mathematical functions.
-////
+////
//// * **Special mathematical functions**
//// * [`beta`](#beta)
//// * [`erf`](#erf)
//// * [`gamma`](#gamma)
//// * [`incomplete_gamma`](#incomplete_gamma)
-////
+////
import gleam/list
import gleam_community/maths/conversion
@@ -100,7 +100,7 @@ pub fn erf(x: Float) -> Float {
///
///
///
-/// The gamma function over the real numbers. The function is essentially equal to
+/// The gamma function over the real numbers. The function is essentially equal to
/// the factorial for any positive integer argument: \\(\Gamma(n) = (n - 1)!\\)
///
/// The implemented gamma function is approximated through Lanczos approximation
@@ -163,7 +163,7 @@ fn gamma_lanczos(x: Float) -> Float {
///
///
///
-pub fn incomplete_gamma(a: Float, x: Float) -> Result(Float, String) {
+pub fn incomplete_gamma(a: Float, x: Float) -> Result(Float, Nil) {
case a >. 0.0 && x >=. 0.0 {
True -> {
let assert Ok(v) = elementary.power(x, a)
@@ -173,9 +173,7 @@ pub fn incomplete_gamma(a: Float, x: Float) -> Result(Float, String) {
|> Ok
}
- False ->
- "Invalid input argument: a <= 0 or x < 0. Valid input is a > 0 and x >= 0."
- |> Error
+ False -> Error(Nil)
}
}
diff --git a/test/gleam_community/maths/predicates_test.gleam b/test/gleam_community/maths/predicates_test.gleam
index fcae19c..955e46e 100644
--- a/test/gleam_community/maths/predicates_test.gleam
+++ b/test/gleam_community/maths/predicates_test.gleam
@@ -23,7 +23,7 @@ pub fn float_list_all_close_test() {
let rtol: Float = 0.01
let atol: Float = 0.1
predicates.all_close(xarr, yarr, rtol, atol)
- |> fn(zarr: Result(List(Bool), String)) -> Result(Bool, Nil) {
+ |> fn(zarr: Result(List(Bool), Nil)) -> Result(Bool, Nil) {
case zarr {
Ok(arr) ->
arr