mirror of
https://github.com/sigmasternchen/gleam-community-maths
synced 2025-03-15 07:59:01 +00:00
Update ToC of modules
This commit is contained in:
parent
086cf17088
commit
2a365e6259
4 changed files with 472 additions and 165 deletions
|
@ -27,32 +27,25 @@
|
|||
//// ---
|
||||
////
|
||||
//// * **Rounding functions**
|
||||
//// * [`ceiling`](#ceiling)
|
||||
//// * [`ceil`](#ceil)
|
||||
//// * [`floor`](#floor)
|
||||
//// * [`truncate`](#truncate)
|
||||
//// * [`trunc`](#trunc)
|
||||
//// * [`round`](#round)
|
||||
//// * **Division functions**
|
||||
//// * [`gcd`](#gcd)
|
||||
//// * [`lcm`](#lcm)
|
||||
//// * **Sign and absolute value functions**
|
||||
//// * [`abs2`](#abs2)
|
||||
//// * [`absdiff`](#abs_diff)
|
||||
//// * [`absdiff`](#absdiff)
|
||||
//// * [`sign`](#sign)
|
||||
//// * [`flipsign`](#flipsign)
|
||||
//// * [`copysign`](#copysign)
|
||||
//// * [`flipsign`](#flipsign)
|
||||
//// * **Powers, logs and roots**
|
||||
//// * [`exp`](#exp)
|
||||
//// * [`ln`](#ln)
|
||||
//// * [`log`](#log)
|
||||
//// * [`log10`](#log10)
|
||||
//// * [`log2`](#log2)
|
||||
//// * [`pow`](#pow)
|
||||
////
|
||||
//// * [`isqrt`](#isqrt)
|
||||
//// * [`icbrt`](#icbrt)
|
||||
////
|
||||
//// * [`sqrt`](#sqrt)
|
||||
//// * [`cbrt`](#cbrt)
|
||||
//// * [`nthrt`](#nthrt)
|
||||
//// * [`hypot`](#hypot)
|
||||
//// * **Trigonometric and hyperbolic functions**
|
||||
//// * [`acos`](#acos)
|
||||
|
@ -70,7 +63,7 @@
|
|||
//// * [`tanh`](#tanh)
|
||||
//// * [`deg2rad`](#deg2rad)
|
||||
//// * [`rad2deg`](#rad2deg)
|
||||
//// * **Mathematical functions**
|
||||
//// * **Misc. mathematical functions**
|
||||
//// * [`min`](#min)
|
||||
//// * [`max`](#max)
|
||||
//// * [`minmax`](#minmax)
|
||||
|
@ -83,10 +76,10 @@
|
|||
//// * [`pi`](#pi)
|
||||
//// * [`tau`](#tau)
|
||||
//// * **Tests**
|
||||
//// * [`ispow2`](#ispow2)
|
||||
//// * [`isclose`](#isclose)
|
||||
//// * [`iseven`](#iseven)
|
||||
//// * [`isodd`](#isodd)
|
||||
//// * **Misc. functions**
|
||||
//// * [`to_int`](#to_int)
|
||||
|
||||
|
||||
import gleam/list
|
||||
import gleam/int
|
||||
|
|
|
@ -26,13 +26,25 @@
|
|||
////
|
||||
//// ---
|
||||
////
|
||||
//// * **Miscellaneous functions**
|
||||
//// * [`allclose`](#allclose)
|
||||
//// * **Distances, sums and products**
|
||||
//// * [`sum`](#sum)
|
||||
//// * [`prod`](#prod)
|
||||
//// * [`norm`](#norm)
|
||||
//// * [`cumsum`](#cumsum)
|
||||
//// * [`cumprod`](#cumprod)
|
||||
//// * **Ranges and intervals**
|
||||
//// * [`linspace`](#linspace)
|
||||
//// * [`logspace`](#linspace)
|
||||
//// * [`geomspace`](#linspace)
|
||||
//// * **Misc. mathematical functions**
|
||||
//// * [`amax`](#amax)
|
||||
//// * [`amin`](#amin)
|
||||
//// * [`argmax`](#argmax)
|
||||
//// * [`argmin`](#argmin)
|
||||
//// * [`extrema`](#extrema)
|
||||
//// * **Tests**
|
||||
//// * [`allclose`](#allclose)
|
||||
//// * **Misc. functions**
|
||||
//// * [`trim`](#trim)
|
||||
|
||||
import gleam/list
|
||||
|
@ -47,8 +59,9 @@ import gleam_community/maths/float as floatx
|
|||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
/// Determine if a list of values are close to or equivalent to a
|
||||
/// another list of reference values.
|
||||
/// Return evenly spaced numbers over a specified interval.
|
||||
/// Returns num evenly spaced samples, calculated over the interval [start, stop].
|
||||
/// The endpoint of the interval can optionally be excluded.
|
||||
///
|
||||
/// <details>
|
||||
/// <summary>Example:</summary>
|
||||
|
@ -57,25 +70,7 @@ import gleam_community/maths/float as floatx
|
|||
/// import gleam_stats/stats
|
||||
///
|
||||
/// pub fn example () {
|
||||
/// let val: Float = 99.
|
||||
/// let ref_val: Float = 100.
|
||||
/// let xarr: List(Float) = list.repeat(val, 42)
|
||||
/// let yarr: List(Float) = list.repeat(ref_val, 42)
|
||||
/// // We set 'atol' and 'rtol' such that the values are equivalent
|
||||
/// // if 'val' is within 1 percent of 'ref_val' +/- 0.1
|
||||
/// let rtol: Float = 0.01
|
||||
/// let atol: Float = 0.10
|
||||
/// stats.allclose(xarr, yarr, rtol, atol)
|
||||
/// |> fn(zarr: Result(List(Bool), String)) -> Result(Bool, Nil) {
|
||||
/// case zarr {
|
||||
/// Ok(arr) ->
|
||||
/// arr
|
||||
/// |> list.all(fn(a: Bool) -> Bool { a })
|
||||
/// |> Ok
|
||||
/// _ -> Nil |> Error
|
||||
/// }
|
||||
/// }
|
||||
/// |> should.equal(Ok(True))
|
||||
///
|
||||
/// }
|
||||
/// </details>
|
||||
///
|
||||
|
@ -85,24 +80,135 @@ import gleam_community/maths/float as floatx
|
|||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn allclose(
|
||||
xarr: List(Float),
|
||||
yarr: List(Float),
|
||||
rtol: Float,
|
||||
atol: Float,
|
||||
) -> Result(List(Bool), 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(z: #(Float, Float)) -> Bool {
|
||||
floatx.isclose(pair.first(z), pair.second(z), rtol, atol)
|
||||
})
|
||||
|> Ok
|
||||
pub fn linspace(
|
||||
start: Float,
|
||||
stop: Float,
|
||||
num: Int,
|
||||
endpoint: option.Option,
|
||||
retstep: option.Option,
|
||||
) -> List(Float) {
|
||||
todo
|
||||
}
|
||||
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="https://github.com/nicklasxyz/gleam_stats/issues">
|
||||
/// <small>Spot a typo? Open an issue!</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
/// Return numbers spaced evenly on a log scale.
|
||||
/// In linear space, the sequence starts at base ** start (base to the power of start) and ends with base ** stop.
|
||||
///
|
||||
/// <details>
|
||||
/// <summary>Example:</summary>
|
||||
///
|
||||
/// import gleeunit/should
|
||||
/// import gleam_stats/stats
|
||||
///
|
||||
/// pub fn example () {
|
||||
///
|
||||
/// }
|
||||
/// </details>
|
||||
///
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="#">
|
||||
/// <small>Back to top ↑</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn logspace(
|
||||
start: Float,
|
||||
stop: Float,
|
||||
num: Int,
|
||||
endpoint: option.Option,
|
||||
retstep: option.Option,
|
||||
base: Int,
|
||||
) -> List(Float) {
|
||||
todo
|
||||
}
|
||||
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="https://github.com/nicklasxyz/gleam_stats/issues">
|
||||
/// <small>Spot a typo? Open an issue!</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
/// Return numbers spaced evenly on a log scale (a geometric progression).
|
||||
/// This is similar to logspace, but with endpoints specified directly. Each output sample is a constant multiple of the previous.
|
||||
///
|
||||
/// <details>
|
||||
/// <summary>Example:</summary>
|
||||
///
|
||||
/// import gleeunit/should
|
||||
/// import gleam_stats/stats
|
||||
///
|
||||
/// pub fn example () {
|
||||
///
|
||||
/// }
|
||||
/// </details>
|
||||
///
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="#">
|
||||
/// <small>Back to top ↑</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn geomspace(
|
||||
start: Float,
|
||||
stop: Float,
|
||||
num: Int,
|
||||
endpoint: option.Option,
|
||||
retstep: option.Option,
|
||||
) -> List(Float) {
|
||||
todo
|
||||
}
|
||||
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="https://github.com/nicklasxyz/gleam_stats/issues">
|
||||
/// <small>Spot a typo? Open an issue!</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
/// Calculcate the sum of the elements in a list:
|
||||
///
|
||||
/// \\[
|
||||
/// \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$$.
|
||||
///
|
||||
/// <details>
|
||||
/// <summary>Example:</summary>
|
||||
///
|
||||
/// import gleeunit/should
|
||||
/// import gleam_stats/stats
|
||||
///
|
||||
/// pub fn example () {
|
||||
/// // An empty list returns an error
|
||||
/// []
|
||||
/// |> stats.sum()
|
||||
/// |> should.equal(0.)
|
||||
///
|
||||
/// // Valid input returns a result
|
||||
/// [1., 2., 3.]
|
||||
/// |> stats.sum()
|
||||
/// |> should.equal(6.)
|
||||
/// }
|
||||
/// </details>
|
||||
///
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="#">
|
||||
/// <small>Back to top ↑</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn sum(arr: List(Float)) -> Float {
|
||||
case arr {
|
||||
[] -> 0.0
|
||||
_ ->
|
||||
arr
|
||||
|> list.fold(0.0, fn(acc: Float, a: Float) -> Float { a +. acc })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -112,7 +218,14 @@ pub fn allclose(
|
|||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
/// Returns the indices of the minimum values in a list.
|
||||
/// Calculcate the product of the elements in a list:
|
||||
///
|
||||
/// \\[
|
||||
/// \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$$.
|
||||
///
|
||||
/// <details>
|
||||
/// <summary>Example:</summary>
|
||||
|
@ -121,15 +234,15 @@ pub fn allclose(
|
|||
/// import gleam_stats/stats
|
||||
///
|
||||
/// pub fn example () {
|
||||
/// // An empty lists returns an error
|
||||
/// // An empty list returns an error
|
||||
/// []
|
||||
/// |> stats.argmin()
|
||||
/// |> should.be_error()
|
||||
///
|
||||
/// |> stats.sum()
|
||||
/// |> should.equal(0.)
|
||||
///
|
||||
/// // Valid input returns a result
|
||||
/// [4., 4., 3., 2., 1.]
|
||||
/// |> stats.argmin()
|
||||
/// |> should.equal(Ok([4]))
|
||||
/// [1., 2., 3.]
|
||||
/// |> stats.prod()
|
||||
/// |> should.equal(6.)
|
||||
/// }
|
||||
/// </details>
|
||||
///
|
||||
|
@ -139,30 +252,12 @@ pub fn allclose(
|
|||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn argmin(arr: List(Float)) -> Result(List(Int), String) {
|
||||
pub fn prod(arr: List(Float)) -> Float {
|
||||
case arr {
|
||||
[] ->
|
||||
"Invalid input argument: The list is empty."
|
||||
|> Error
|
||||
_ -> {
|
||||
assert Ok(min) =
|
||||
arr
|
||||
|> amin()
|
||||
[] -> 0.0
|
||||
_ ->
|
||||
arr
|
||||
|> list.index_map(fn(index: Int, a: Float) -> Int {
|
||||
case a -. min {
|
||||
0. -> index
|
||||
_ -> -1
|
||||
}
|
||||
})
|
||||
|> list.filter(fn(index: Int) -> Bool {
|
||||
case index {
|
||||
-1 -> False
|
||||
_ -> True
|
||||
}
|
||||
})
|
||||
|> Ok
|
||||
}
|
||||
|> list.fold(0.0, fn(acc: Float, a: Float) -> Float { a *. acc })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -172,7 +267,14 @@ pub fn argmin(arr: List(Float)) -> Result(List(Int), String) {
|
|||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
/// Returns the indices of the maximum values in a list.
|
||||
/// Calculcate the cumulative sum of the elements in a list:
|
||||
///
|
||||
/// \\[
|
||||
/// \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$$.
|
||||
///
|
||||
/// <details>
|
||||
/// <summary>Example:</summary>
|
||||
|
@ -181,15 +283,15 @@ pub fn argmin(arr: List(Float)) -> Result(List(Int), String) {
|
|||
/// import gleam_stats/stats
|
||||
///
|
||||
/// pub fn example () {
|
||||
/// // An empty lists returns an error
|
||||
/// // An empty list returns an error
|
||||
/// []
|
||||
/// |> stats.argmax()
|
||||
/// |> should.be_error()
|
||||
///
|
||||
/// |> stats.sum()
|
||||
/// |> should.equal(0.)
|
||||
///
|
||||
/// // Valid input returns a result
|
||||
/// [4., 4., 3., 2., 1.]
|
||||
/// |> stats.argmax()
|
||||
/// |> should.equal(Ok([0, 1]))
|
||||
/// [1., 2., 3.]
|
||||
/// |> stats.sum()
|
||||
/// |> should.equal(6.)
|
||||
/// }
|
||||
/// </details>
|
||||
///
|
||||
|
@ -199,31 +301,80 @@ pub fn argmin(arr: List(Float)) -> Result(List(Int), String) {
|
|||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn argmax(arr: List(Float)) -> Result(List(Int), String) {
|
||||
case arr {
|
||||
[] ->
|
||||
"Invalid input argument: The list is empty."
|
||||
|> Error
|
||||
_ -> {
|
||||
assert Ok(max) =
|
||||
arr
|
||||
|> amax()
|
||||
arr
|
||||
|> list.index_map(fn(index: Int, a: Float) -> Int {
|
||||
case a -. max {
|
||||
0. -> index
|
||||
_ -> -1
|
||||
}
|
||||
})
|
||||
|> list.filter(fn(index: Int) -> Bool {
|
||||
case index {
|
||||
-1 -> False
|
||||
_ -> True
|
||||
}
|
||||
})
|
||||
|> Ok
|
||||
}
|
||||
}
|
||||
pub fn cumsum(arr: List(Float)) -> List(Float) {
|
||||
todo
|
||||
}
|
||||
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="https://github.com/nicklasxyz/gleam_stats/issues">
|
||||
/// <small>Spot a typo? Open an issue!</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
/// Calculcate the cumulative product of the elements in a list:
|
||||
///
|
||||
/// \\[
|
||||
/// \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$$.
|
||||
///
|
||||
/// <details>
|
||||
/// <summary>Example:</summary>
|
||||
///
|
||||
/// import gleeunit/should
|
||||
/// import gleam_stats/stats
|
||||
///
|
||||
/// pub fn example () {
|
||||
/// // An empty list returns an error
|
||||
/// []
|
||||
/// |> stats.sum()
|
||||
/// |> should.equal(0.)
|
||||
///
|
||||
/// // Valid input returns a result
|
||||
/// [1., 2., 3.]
|
||||
/// |> stats.prod()
|
||||
/// |> should.equal(6.)
|
||||
/// }
|
||||
/// </details>
|
||||
///
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="#">
|
||||
/// <small>Back to top ↑</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn cumprod(arr: List(Float)) -> List(Float) {
|
||||
todo
|
||||
}
|
||||
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="https://github.com/nicklasxyz/gleam_stats/issues">
|
||||
/// <small>Spot a typo? Open an issue!</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
///
|
||||
/// <details>
|
||||
/// <summary>Example:</summary>
|
||||
///
|
||||
/// import gleeunit/should
|
||||
/// import gleam_stats/stats
|
||||
///
|
||||
/// pub fn example () {
|
||||
///
|
||||
/// }
|
||||
/// </details>
|
||||
///
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="#">
|
||||
/// <small>Back to top ↑</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn norm(xarr: List(Float), yarr: List(Float), p: Int) -> List(Float) {
|
||||
todo
|
||||
}
|
||||
|
||||
/// <div style="text-align: right;">
|
||||
|
@ -336,6 +487,126 @@ pub fn amin(arr: List(Float)) -> Result(Float, String) {
|
|||
}
|
||||
}
|
||||
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="https://github.com/nicklasxyz/gleam_stats/issues">
|
||||
/// <small>Spot a typo? Open an issue!</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
/// Returns the indices of the maximum values in a list.
|
||||
///
|
||||
/// <details>
|
||||
/// <summary>Example:</summary>
|
||||
///
|
||||
/// import gleeunit/should
|
||||
/// import gleam_stats/stats
|
||||
///
|
||||
/// pub fn example () {
|
||||
/// // An empty lists returns an error
|
||||
/// []
|
||||
/// |> stats.argmax()
|
||||
/// |> should.be_error()
|
||||
///
|
||||
/// // Valid input returns a result
|
||||
/// [4., 4., 3., 2., 1.]
|
||||
/// |> stats.argmax()
|
||||
/// |> should.equal(Ok([0, 1]))
|
||||
/// }
|
||||
/// </details>
|
||||
///
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="#">
|
||||
/// <small>Back to top ↑</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn argmax(arr: List(Float)) -> Result(List(Int), String) {
|
||||
case arr {
|
||||
[] ->
|
||||
"Invalid input argument: The list is empty."
|
||||
|> Error
|
||||
_ -> {
|
||||
assert Ok(max) =
|
||||
arr
|
||||
|> amax()
|
||||
arr
|
||||
|> list.index_map(fn(index: Int, a: Float) -> Int {
|
||||
case a -. max {
|
||||
0. -> index
|
||||
_ -> -1
|
||||
}
|
||||
})
|
||||
|> list.filter(fn(index: Int) -> Bool {
|
||||
case index {
|
||||
-1 -> False
|
||||
_ -> True
|
||||
}
|
||||
})
|
||||
|> Ok
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="https://github.com/nicklasxyz/gleam_stats/issues">
|
||||
/// <small>Spot a typo? Open an issue!</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
/// Returns the indices of the minimum values in a list.
|
||||
///
|
||||
/// <details>
|
||||
/// <summary>Example:</summary>
|
||||
///
|
||||
/// import gleeunit/should
|
||||
/// import gleam_stats/stats
|
||||
///
|
||||
/// pub fn example () {
|
||||
/// // An empty lists returns an error
|
||||
/// []
|
||||
/// |> stats.argmin()
|
||||
/// |> should.be_error()
|
||||
///
|
||||
/// // Valid input returns a result
|
||||
/// [4., 4., 3., 2., 1.]
|
||||
/// |> stats.argmin()
|
||||
/// |> should.equal(Ok([4]))
|
||||
/// }
|
||||
/// </details>
|
||||
///
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="#">
|
||||
/// <small>Back to top ↑</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn argmin(arr: List(Float)) -> Result(List(Int), String) {
|
||||
case arr {
|
||||
[] ->
|
||||
"Invalid input argument: The list is empty."
|
||||
|> Error
|
||||
_ -> {
|
||||
assert Ok(min) =
|
||||
arr
|
||||
|> amin()
|
||||
arr
|
||||
|> list.index_map(fn(index: Int, a: Float) -> Int {
|
||||
case a -. min {
|
||||
0. -> index
|
||||
_ -> -1
|
||||
}
|
||||
})
|
||||
|> list.filter(fn(index: Int) -> Bool {
|
||||
case index {
|
||||
-1 -> False
|
||||
_ -> True
|
||||
}
|
||||
})
|
||||
|> Ok
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="https://github.com/nicklasxyz/gleam_stats/issues">
|
||||
/// <small>Spot a typo? Open an issue!</small>
|
||||
|
@ -371,23 +642,69 @@ pub fn amin(arr: List(Float)) -> Result(Float, String) {
|
|||
///
|
||||
pub fn extrema(arr: List(Float)) -> Result(#(Float, Float), String) {
|
||||
todo
|
||||
// case arr {
|
||||
// [] ->
|
||||
// "Invalid input argument: The list is empty."
|
||||
// |> Error
|
||||
// _ -> {
|
||||
// assert Ok(val0) = list.at(arr, 0)
|
||||
// arr
|
||||
// |> list.fold(
|
||||
// val0,
|
||||
// fn(acc: Float, a: Float) {
|
||||
// case a <. acc {
|
||||
// True -> a
|
||||
// False -> acc
|
||||
// }
|
||||
// },
|
||||
// )
|
||||
// |> Ok
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="https://github.com/nicklasxyz/gleam_stats/issues">
|
||||
/// <small>Spot a typo? Open an issue!</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
/// Determine if a list of values are close to or equivalent to a
|
||||
/// another list of reference values.
|
||||
///
|
||||
/// <details>
|
||||
/// <summary>Example:</summary>
|
||||
///
|
||||
/// import gleeunit/should
|
||||
/// import gleam_stats/stats
|
||||
///
|
||||
/// pub fn example () {
|
||||
/// let val: Float = 99.
|
||||
/// let ref_val: Float = 100.
|
||||
/// let xarr: List(Float) = list.repeat(val, 42)
|
||||
/// let yarr: List(Float) = list.repeat(ref_val, 42)
|
||||
/// // We set 'atol' and 'rtol' such that the values are equivalent
|
||||
/// // if 'val' is within 1 percent of 'ref_val' +/- 0.1
|
||||
/// let rtol: Float = 0.01
|
||||
/// let atol: Float = 0.10
|
||||
/// stats.allclose(xarr, yarr, rtol, atol)
|
||||
/// |> fn(zarr: Result(List(Bool), String)) -> Result(Bool, Nil) {
|
||||
/// case zarr {
|
||||
/// Ok(arr) ->
|
||||
/// arr
|
||||
/// |> list.all(fn(a: Bool) -> Bool { a })
|
||||
/// |> Ok
|
||||
/// _ -> Nil |> Error
|
||||
/// }
|
||||
/// }
|
||||
/// |> should.equal(Ok(True))
|
||||
/// }
|
||||
/// </details>
|
||||
///
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="#">
|
||||
/// <small>Back to top ↑</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn allclose(
|
||||
xarr: List(Float),
|
||||
yarr: List(Float),
|
||||
rtol: Float,
|
||||
atol: Float,
|
||||
) -> Result(List(Bool), 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(z: #(Float, Float)) -> Bool {
|
||||
floatx.isclose(pair.first(z), pair.second(z), rtol, atol)
|
||||
})
|
||||
|> Ok
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,14 +26,29 @@
|
|||
////
|
||||
//// ---
|
||||
////
|
||||
//// * **Mathematical functions**
|
||||
//// * **Sign and absolute value functions**
|
||||
//// * [`abs2`](#abs2)
|
||||
//// * [`absdiff`](#abs_diff)
|
||||
//// * [`flipsign`](#flipsign)
|
||||
//// * [`max`](#max)
|
||||
//// * [`min`](#min)
|
||||
//// * [`minmax`](#minmax)
|
||||
//// * [`round`](#round)
|
||||
//// * [`sign`](#sign)
|
||||
//// * [`copysign`](#copysign)
|
||||
//// * [`flipsign`](#flipsign)
|
||||
//// * **Powers, logs and roots**
|
||||
//// * [`exp`](#exp)
|
||||
//// * [`ln`](#ln)
|
||||
//// * [`log`](#log)
|
||||
//// * [`log10`](#log10)
|
||||
//// * [`log2`](#log2)
|
||||
//// * [`pow`](#pow)
|
||||
//// * [`sqrt`](#sqrt)
|
||||
//// * [`cbrt`](#cbrt)
|
||||
//// * [`hypot`](#hypot)
|
||||
//// * **Misc. mathematical functions**
|
||||
//// * [`min`](#min)
|
||||
//// * [`max`](#max)
|
||||
//// * [`minmax`](#minmax)
|
||||
//// * **Division functions**
|
||||
//// * [`gcd`](#gcd)
|
||||
//// * [`lcm`](#lcm)
|
||||
//// * **Combinatorial functions**
|
||||
//// * [`combination`](#combination)
|
||||
//// * [`factorial`](#factorial)
|
||||
|
@ -42,6 +57,8 @@
|
|||
//// * [`ispow2`](#ispow2)
|
||||
//// * [`iseven`](#iseven)
|
||||
//// * [`isodd`](#isodd)
|
||||
//// * **Misc. functions**
|
||||
//// * [`to_float`](#to_float)
|
||||
|
||||
import gleam/list
|
||||
import gleam/int
|
||||
|
|
|
@ -277,7 +277,6 @@ pub fn amin(arr: List(Float)) -> Result(Float, String) {
|
|||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
/// Returns the minimum value of a list.
|
||||
///
|
||||
/// <details>
|
||||
/// <summary>Example:</summary>
|
||||
|
@ -306,23 +305,4 @@ pub fn amin(arr: List(Float)) -> Result(Float, String) {
|
|||
///
|
||||
pub fn extrema(arr: List(Float)) -> Result(#(Float, Float), String) {
|
||||
todo
|
||||
// case arr {
|
||||
// [] ->
|
||||
// "Invalid input argument: The list is empty."
|
||||
// |> Error
|
||||
// _ -> {
|
||||
// assert Ok(val0) = list.at(arr, 0)
|
||||
// arr
|
||||
// |> list.fold(
|
||||
// val0,
|
||||
// fn(acc: Float, a: Float) {
|
||||
// case a <. acc {
|
||||
// True -> a
|
||||
// False -> acc
|
||||
// }
|
||||
// },
|
||||
// )
|
||||
// |> Ok
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue