gleastsq

Package Version Hex Docs

A basic least squares fitting library for Gleam. This library uses the Nx library from Elixir to perform matrix operations.

gleam add gleastsq
import gleam/option.{None}
import gleam/list
import gleam/io
import gleastsq

fn parabola(x: Float, params: List(Float)) -> Float {
  let assert [a, b, c] = params
  a *. x *. x +. b *. x +. c
}

pub fn main() {
  let x = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
  let y = list.map(x, fn(x) { x *. x })
  let initial_guess = [1.0, 1.0, 1.0]

  let assert Ok(result) =
    gleastsq.least_squares(
      x,
      y,
      parabola,
      initial_guess,
      max_iterations: None,
      epsilon: None,
      tolerance: None,
      lambda_reg: None,
    )

  io.debug(result) // [1.0, 0.0, 0.0] (within numerical error)
}

Further documentation can be found at https://hexdocs.pm/gleastsq.

Search Document