gleam_stats/math
A module containing several different kinds of mathematical functions and constants.
- Standard Mathematical functions
- Special Mathematical functions
- Combinatorial functions
- Mathematical constants
Functions
pub fn beta(x: Float, y: Float) -> Float
The beta function over the real numbers.
The implemented beta function is evaluated through the use of the gamma function.
pub fn combination(n: Int, k: Int) -> Result(Int, String)
A combinatorial function for computing the number of a k-combinations of n elements. Also known as “n choose k” or the binomial coefficient.
The implementation uses the effecient iterative multiplicative formula for the computation.
Example:
import gleeunit/should
import gleam_stats/math
pub fn example() {
// Invalid input gives an error
// Error on: n = -1 < 0
math.combination(-1, 1)
|> should.be_error()
// Valid input returns a result
math.combination(4, 0)
|> should.equal(Ok(1))
math.combination(4, 4)
|> should.equal(Ok(1))
math.combination(4, 2)
|> should.equal(Ok(6))
}
pub fn factorial(n: Int) -> Result(Int, String)
A combinatorial function for computing the total number of combinations of n elements.
Example:
import gleeunit/should
import gleam_stats/math
pub fn example() {
// Invalid input gives an error
math.factorial(-1)
|> should.be_error()
// Valid input returns a result
math.factorial(0)
|> should.equal(Ok(1))
math.factorial(1)
|> should.equal(Ok(1))
math.factorial(2)
|> should.equal(Ok(2))
math.factorial(3)
|> should.equal(Ok(6))
math.factorial(4)
|> should.equal(Ok(24))
}
pub fn gammainc(a: Float, x: Float) -> Result(Float, String)
The lower incomplete gamma function over the real numbers.
The implemented incomplete gamma function is evaluated through a power series expansion.
pub fn permutation(n: Int, k: Int) -> Result(Int, String)
A combinatorial function for computing the number of k-permuations (without repetitions) of n elements.
Example:
import gleeunit/should
import gleam_stats/math
pub fn example() {
// Invalid input gives an error
// Error on: n = -1 < 0
math.permutation(-1, 1)
|> should.be_error()
// Valid input returns a result
math.permutation(4, 0)
|> should.equal(Ok(1))
math.permutation(4, 4)
|> should.equal(Ok(1))
math.permutation(4, 2)
|> should.equal(Ok(12))
}
pub fn round(x: Float, precision: Int) -> Float
The function rounds a floating point number to a specific decimal precision.
Example:
import gleeunit/should
import gleam_stats/math
pub fn example() {
math.round(0.4444, 2) |> should.equal(0.44)
math.round(0.4445, 2) |> should.equal(0.44)
math.round(0.4455, 2) |> should.equal(0.45)
math.round(0.4555, 2) |> should.equal(0.46)
}