gleam_community/maths/sequences


Sequences: A module containing functions for generating various types of sequences, ranges and intervals.

Functions

pub fn arange(
  start: Float,
  stop: Float,
  step: Float,
) -> List(Float)

The function returns a list with evenly spaced values within a given interval based on a start, stop value and a given increment (step-length) between consecutive values. The list returned includes the given start value but excludes the stop value.

Example:
import gleeunit/should
import gleam_community/maths/sequences

pub fn example () {
  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
  sequences.arange(5.0, 1.0, 1.0)
  |> should.equal([])
  
  // Points returned since
  // start smaller than stop but negative step
  sequences.arange(5.0, 1.0, -1.0)
  |> should.equal([5.0, 4.0, 3.0, 2.0])
}
pub fn geometric_space(
  start: Float,
  stop: Float,
  num: Int,
  endpoint: Bool,
) -> Result(List(Float), String)

The function returns a list 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 function, but with endpoints specified directly.

Example:
import gleeunit/should
import gleam_community/maths/elementary
import gleam_community/maths/sequences
import gleam_community/maths/predicates

pub fn example () {
  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) =
    predicates.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)
  sequences.geometric_space(0.0, 1000.0, 3, False)
  |> should.be_error()

  sequences.geometric_space(-1000.0, 0.0, 3, False)
  |> should.be_error()

  // A negative number of points (-3) does not work
  sequences.geometric_space(10.0, 1000.0, -3, False)
  |> should.be_error()
}
pub fn linear_space(
  start: Float,
  stop: Float,
  num: Int,
  endpoint: Bool,
) -> Result(List(Float), String)

Generate a linearly spaced list of points over a specified interval. The endpoint of the interval can optionally be included/excluded.

Example:
import gleeunit/should
import gleam_community/maths/elementary
import gleam_community/maths/sequences
import gleam_community/maths/predicates

pub fn example () {
  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) =
    predicates.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
  sequences.linear_space(10.0, 50.0, -5, True)
  |> should.be_error()
}
pub fn logarithmic_space(
  start: Float,
  stop: Float,
  num: Int,
  endpoint: Bool,
  base: Float,
) -> Result(List(Float), String)

Generate a logarithmically spaced list of points over a specified interval. The endpoint of the interval can optionally be included/excluded.

Example:
import gleeunit/should
import gleam_community/maths/elementary
import gleam_community/maths/sequences
import gleam_community/maths/predicates

pub fn example () {
  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) =
    predicates.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
  sequences.logarithmic_space(1.0, 3.0, -3, False, 10.0)
  |> should.be_error()
}
Search Document