svg_path/root

Bracketed root-finding helpers for scalar functions.

This module intentionally keeps the numerical method small and explicit. bisect implements the standard bisection method: for a continuous function f and a bracket [a, b] where f(a) and f(b) have opposite signs, repeatedly halve the interval until the root estimate is within the requested tolerance.

Types

Errors returned by root-finding helpers.

pub type Error {
  InvalidTolerance(tolerance: Float)
  InvalidMaxIterations(max_iterations: Int)
  NotBracketed(
    left: Float,
    right: Float,
    left_value: Float,
    right_value: Float,
  )
  MaxIterationsReached(estimate: Float, value: Float)
}

Constructors

  • InvalidTolerance(tolerance: Float)

    The tolerance must be greater than zero.

  • InvalidMaxIterations(max_iterations: Int)

    The maximum iteration count must be greater than zero.

  • NotBracketed(
      left: Float,
      right: Float,
      left_value: Float,
      right_value: Float,
    )

    The function values at the bracket endpoints do not have opposite signs.

  • MaxIterationsReached(estimate: Float, value: Float)

    The solver did not converge within the configured iteration count.

Options for bracketed bisection.

pub type Options {
  Options(tolerance: Float, max_iterations: Int)
}

Constructors

  • Options(tolerance: Float, max_iterations: Int)

Values

pub fn bisect(
  f: fn(Float) -> Float,
  from left: Float,
  to right: Float,
) -> Result(Float, Error)

Find a root of f in a bracket using default options.

f(from) and f(to) must have opposite signs, unless either endpoint is already within tolerance of zero. from may be greater than to; the bracket is normalized before solving.

pub fn bisect_with(
  f: fn(Float) -> Float,
  from left: Float,
  to right: Float,
  options options: Options,
) -> Result(Float, Error)

Find a root of f in a bracket using explicit options.

The returned value is an approximation. Convergence succeeds when either abs(f(estimate)) <= tolerance or the current bracket width is no larger than tolerance.

pub fn default_options() -> Options

Return the default bisection options.

Search Document