gleam_stats/math

A module containing several different kinds of mathematical functions and constants.


Functions

pub fn acos(x: Float) -> Result(Float, String)

The inverse cosine function:

\[ \forall x \in [-1, 1], \; \cos^{-1}{(x)} = y \in [0, \pi ] \]

The function takes a number $$x$$ in its domain $$[-1, 1]$$ as input and returns a numeric value $$y$$ that lies in the range $$[0, \pi ]$$ (an angle in radians). If the input value is outside the domain of the function an error is returned.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.acos(1.0)
   |> should.equal(Ok(0.0))

   math.acos(1.1)
   |> should.be_error()

   math.acos(-1.1)
   |> should.be_error()
 }
pub fn acosh(x: Float) -> Result(Float, String)

The inverse hyperbolic cosine function:

\[ \forall x \in [1, +\infty), \; \cosh^{-1}{(x)} = y \in [0, +\infty) \]

The function takes a number $$x$$ in its domain $$[1, +\infty)$$ as input and returns a numeric value $$y$$ that lies in the range $$[0, +\infty)$$ (an angle in radians). If the input value is outside the domain of the function an error is returned.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.acosh(1.0)
   |> should.equal(Ok(0.0))

   math.acosh(0.0)
   |> should.be_error()
 }
pub fn asin(x: Float) -> Result(Float, String)

The inverse sine function:

\[ \forall x \in [-1, 1], \; \sin^{-1}{(x)} = y \in (-\infty, +\infty) \]

The function takes a number $$x$$ in its domain $$[-1, 1]$$ as input and returns a numeric value $$y$$ that lies in the range $$[-\frac{\pi}{2}, \frac{\pi}{2}]$$ (an angle in radians). If the input value is outside the domain of the function an error is returned.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.asin(0.0)
   |> should.equal(0.0)

   math.asin(1.1)
   |> should.be_error()

   math.asin(-1.1)
   |> should.be_error()
 }
pub fn asinh(x: Float) -> Float

The inverse hyperbolic sine function:

\[ \forall x \in (-\infty, \infty), \; \sinh^{-1}{(x)} = y \in (-\infty, +\infty) \]

The function takes a number $$x$$ in its domain $$(-\infty, +\infty)$$ as input and returns a numeric value $$y$$ that lies in the range $$(-\infty, +\infty)$$ (an angle in radians).

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.asinh(0.0)
   |> should.equal(0.0)
 }
pub fn atan(x: Float) -> Float

The inverse tangent function:

\[ \forall x \in (-\infty, \infty), \; \tan^{-1}{(x)} = y \in [-\frac{\pi}{2}, \frac{\pi}{2}] \]

The function takes a number $$x$$ in its domain $$(-\infty, +\infty)$$ as input and returns a numeric value $$y$$ that lies in the range $$[-\frac{\pi}{2}, \frac{\pi}{2}]$$ (an angle in radians).

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.atan(0.0)
   |> should.equal(0.0)
 }
pub fn atan2(y: Float, x: Float) -> Float

The inverse 2-argument tangent function:

\[ \text{atan2}(y, x) = \begin{cases} \tan^{-1}(\frac y x) &\text{if } x > 0, \\ \tan^{-1}(\frac y x) + \pi &\text{if } x < 0 \text{ and } y \ge 0, \\ \tan^{-1}(\frac y x) - \pi &\text{if } x < 0 \text{ and } y < 0, \\ +\frac{\pi}{2} &\text{if } x = 0 \text{ and } y > 0, \\ -\frac{\pi}{2} &\text{if } x = 0 \text{ and } y < 0, \\ \text{undefined} &\text{if } x = 0 \text{ and } y = 0. \end{cases} \]

The function returns the angle in radians from the x-axis to the line containing the origin $$(0, 0)$$ and a point given as input with coordinates $$(x, y)$$. The numeric value returned by $$\text{atan2}(y, x)$$ is in the range $$[-\pi, \pi]$$.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.atan2(0.0, 0.0)
   |> should.equal(0.0)
 }
pub fn atanh(x: Float) -> Result(Float, String)

The inverse hyperbolic tangent function:

\[ \forall x \in (-1, 1), \; \tanh^{-1}{(x)} = y \in (-\infty, +\infty) \]

The function takes a number $$x$$ in its domain $$(-1, 1)$$ as input and returns a numeric value $$y$$ that lies in the range $$(-\infty, \infty)$$ (an angle in radians). If the input value is outside the domain of the function an error is returned.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.atanh(0.0)
   |> should.equal(Ok(0.0))

   math.atanh(1.0)
   |> should.be_error()

   math.atanh(-1.0)
   |> should.be_error()
 }
pub fn beta(x: Float, y: Float) -> Float

The beta function over the real numbers:

\[ \text{B}(x, y) = \frac{\Gamma(x) \cdot \Gamma(y)}{\Gamma(x + y)} \]

The beta function is evaluated through the use of the gamma function.

pub fn ceil(x: Float) -> Float

The ceiling function.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.ceil(0.2)
   |> should.equal(1.0)

   math.ceil(0.8)
   |> should.equal(1.0)
 }
pub fn combination(n: Int, k: Int) -> Result(Int, String)

A combinatorial function for computing the number of a $$k$$-combinations of $$n$$ elements:

\[ C(n, k) = \binom{n}{k} = \frac{n!}{k! (n-k)!} \] 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 cos(x: Float) -> Float

The cosine function:

\[ \forall x \in (-\infty, +\infty), \; \cos{(x)} = y \in [-1, 1] \]

The function takes a number $$x$$ in its domain $$(-\infty, \infty)$$ (an angle in radians) as input and returns a numeric value $$y$$ that lies in the range $$[-1, 1]$$.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.cos(0.0)
   |> should.equal(1.0)

   math.cos(math.pi())
   |> should.equal(-1.0)
 }
pub fn cosh(x: Float) -> Float

The hyperbolic cosine function:

\[ \forall x \in (-\infty, \infty), \; \cosh{(x)} = y \in (-\infty, +\infty) \]

The function takes a number $$x$$ in its domain $$(-\infty, \infty)$$ as input (an angle in radians) and returns a numeric value $$y$$ that lies in the range $$(-\infty, \infty)$$. If the input value is too large an overflow error might occur.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.cosh(0.0)
   |> should.equal(0.0)
 }
pub fn erf(x: Float) -> Float
pub fn exp(x: Float) -> Float

The exponential function:

\[ \forall x \in (-\infty, \infty), \; e^{(x)} = y \in (0, +\infty) \]

If the input value is too large an overflow error might occur.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.exp(0.0)
   |> should.equal(1.0)
 }
pub fn factorial(n: Int) -> Result(Int, String)

A combinatorial function for computing the total number of combinations of $$n$$ elements, that is $$n!$$.

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 floor(x: Float) -> Float

The floor function.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.floor(0.2)
   |> should.equal(0.0)

   math.floor(0.8)
   |> should.equal(0.0)
 }
pub fn gamma(x: Float) -> Float

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 using the same coefficients used by the GNU Scientific Library.

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 log(x: Float) -> Result(Float, String)

The natural logarithm function:

\[ \forall x \in (0, \infty), \; \log_{e}{(x)} = y \in (-\infty, +\infty) \]

The function takes a number $$x$$ in its domain $$(0, \infty)$$ as input and returns a numeric value $$y$$ that lies in the range $$(-\infty, \infty)$$. If the input value is outside the domain of the function an error is returned.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example () {
   math.log(1.0)
   |> should.equal(Ok(0.0))

   math.log(math.exp(1.0))
   |> should.equal(1.0)

   math.log(-1.0)
   |> should.be_error()
 }
pub fn log10(x: Float) -> Result(Float, String)

The The base-10 logarithm function:

\[ \forall x \in (0, \infty), \; \log_{10}{(x)} = y \in (-\infty, +\infty) \]

The function takes a number $$x$$ in its domain $$(0, \infty)$$ as input and returns a numeric value $$y$$ that lies in the range $$(-\infty, \infty)$$. If the input value is outside the domain of the function an error is returned.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example () {
   math.log10(1.0)
   |> should.equal(Ok(0.0))

   math.log10(10.0)
   |> should.equal(Ok(1.0))

   math.log10(-1.0)
   |> should.be_error()
 }
pub fn log2(x: Float) -> Result(Float, String)

The The base-2 logarithm function:

\[ \forall x \in (0, \infty), \; \log_{2}{(x)} = y \in (-\infty, +\infty) \]

The function takes a number $$x$$ in its domain $$(0, \infty)$$ as input and returns a numeric value $$y$$ that lies in the range $$(-\infty, \infty)$$. If the input value is outside the domain of the function an error is returned.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example () {
   math.log2(1.0)
   |> should.equal(Ok(0.0))

   math.log2(2.0)
   |> should.equal(Ok(1.0))

   math.log2(-1.0)
   |> should.be_error()
 }
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:

\[ P(n, k) = \frac{n!}{(n - k)!} \]

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 pi() -> Float

The mathematical constant pi: $$\pi \approx 3.1415\dots$$

pub fn pow(x: Float, y: Float) -> Result(Float, String)

The exponentiation function: $$y = x^{a}$$.

Note that the function is not defined if:

  1. The base is negative ($$x < 0$$) and the exponent is fractional ($$a = \frac{n}{m}$$ is an irrreducible fraction). An error will be returned as an imaginary number will otherwise have to be returned.
  2. The base is zero ($$x = 0$$) and the exponent is negative ($$a < 0$$) then the expression is equivalent to the exponent $$y$$ divided by $$0$$ and an error will have to be returned as the expression is otherwise undefined.
Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.pow(2., -1.)
   |> should.equal(0.5)

   math.pow(2., 2.)
   |> should.equal(4.0)

   math.pow(-1., 0.5)
   |> should.be_error()
 }
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)
 }
pub fn sign(x: Float) -> Float

The sign function which returns the sign of the input, indicating whether it is positive, negative, or zero.

pub fn sin(x: Float) -> Float

The sine function:

\[ \forall x \in (-\infty, +\infty), \; \sin{(x)} = y \in [-1, 1] \]

The function takes a number $$x$$ in its domain $$(-\infty, \infty)$$ (an angle in radians) as input and returns a numeric value $$y$$ that lies in the range $$[-1, 1]$$.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.sin(0.0)
   |> should.equal(0.0)

   math.sin(0.5 *. math.pi())
   |> should.equal(1.0)
 }
pub fn sinh(x: Float) -> Float

The hyperbolic sine function:

\[ \forall x \in (-\infty, +\infty), \; \sinh{(x)} = y \in (-\infty, +\infty) \]

The function takes a number $$x$$ in its domain $$(-\infty, +\infty)$$ as input (an angle in radians) and returns a numeric value $$y$$ that lies in the range $$(-\infty, +\infty)$$. If the input value is too large an overflow error might occur.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.sinh(0.0)
   |> should.equal(0.0)
 }
pub fn tan(x: Float) -> Float

The tangent function:

\[ \forall x \in (-\infty, +\infty), \; \tan{(x)} = y \in (-\infty, +\infty) \]

The function takes a number $$x$$ in its domain $$(-\infty, +\infty)$$ as input (an angle in radians) and returns a numeric value $$y$$ that lies in the range $$(-\infty, +\infty)$$.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.tan(0.0)
   |> should.equal(0.0)
 }
pub fn tanh(x: Float) -> Float

The hyperbolic tangent function:

\[ \forall x \in (-\infty, \infty), \; \tanh{(x)} = y \in [-1, 1] \]

The function takes a number $$x$$ in its domain $$(-\infty, \infty)$$ as input (an angle in radians) and returns a numeric value $$y$$ that lies in the range $$[-1, 1]$$.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example () {
   math.tanh(0.0)
   |> should.equal(0.0)

   math.tanh(25.0)
   |> should.equal(1.0)

   math.tanh(-25.0)
   |> should.equal(-1.0)
 }
pub fn tau() -> Float

The mathematical constant tau: $$\tau = 2 \cdot \pi \approx 6.283\dots$$

pub fn to_degrees(x: Float) -> Float

Convert a value in degrees to a value measured in radians. That is, $$1 \text{ radians } = \frac{180}{\pi} \text{ degrees }$$.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.to_degrees(0.0)
   |> should.equal(0.0)

   math.to_degrees(2 *. pi())
   |> should.equal(360.)
 }
pub fn to_radians(x: Float) -> Float

Convert a value in degrees to a value measured in radians. That is, $$1 \text{ degrees } = \frac{\pi}{180} \text{ radians }$$.

Example:
 import gleeunit/should
 import gleam_stats/math

 pub fn example() {
   math.sin(0.0)
   |> should.equal(0.0)
 }