Skip to content

roots #

vsl.roots

Root-finding routines in vsl.roots accept callbacks from vsl.func. The currently usable solvers are:

API Method Notes
newton(f, x0, x_eps, fx_eps, n_max) Newton iteration Uses a function and derivative callback (FnFdf).
newton_bisection(f, x_min, x_max, tol, max_iter) Newton with bisection safeguard Requires a bracket and derivative callback.
Bisection.new(f, params).solve() Bisection Requires a sign-changing interval; returns the last iteration or the limit result.

BisectionParams configures bounds, relative and absolute tolerances, and the iteration limit. Bisection.next() exposes individual steps. The Brent type currently has a constructor and an internal algorithm but does not expose a public solve or iteration method yet.

import math
import vsl.func
import vsl.roots

fn objective(x f64, _ []f64) f64 {
    return math.cos(x)
}

f := func.Fn.new(f: objective)
mut solver := roots.Bisection.new(f, roots.BisectionParams{
    xmin: 0.0
    xmax: 3.0
})
result := solver.solve() or { panic('root was not found') }
println(result.x)

See the bisection example, bisection.v, and newton.v.

fn newton #

fn newton(f func.FnFdf, x0 f64, x_eps f64, fx_eps f64, n_max int) !f64

Find the root of a function using Newton's algorithm with the Armijo line search to ensure the absolute value of the function decreases along the iterations.

fn newton_bisection #

fn newton_bisection(f func.FnFdf, x_min f64, x_max f64, tol f64, max_iter int) !f64

Find the root of a function by combining Newton's method with the bisection method

fn Bisection.new #

fn Bisection.new(f func.Fn, params BisectionParams) &Bisection

Bisection.new creates a new Bisection object with the given parameters

fn Brent.new #

fn Brent.new(f func.Fn) &Brent

Brent.new creates a new Brent object

struct Bisection #

struct Bisection {
	f func.Fn @[required]
mut:
	last_iter ?&BisectionIteration // last iteration
pub mut:
	xmin      f64 // lower bound
	xmax      f64 // upper bound
	epsrel    f64 // relative error tolerance
	epsabs    f64 // absolute error tolerance
	n_max     int // maximum number of iterations
	n_f_calls int // number of function calls
	n_iter    int // number of iterations
}

Bisection implements a bisection method for finding the root of a function

fn (Bisection) next #

fn (mut solver Bisection) next() ?&BisectionIteration

next returns the next iteration of the bisection method.

fn (Bisection) solve #

fn (mut solver Bisection) solve() ?&BisectionIteration

solve solves for the root of the function using the bisection method.

struct BisectionIteration #

struct BisectionIteration {
pub mut:
	x         f64
	fx        f64
	n_f_calls int
	n_iter    int
}

BisectionIteration defines a public data structure for this module.

struct BisectionParams #

@[params]
struct BisectionParams {
pub:
	xmin   f64
	xmax   f64
	epsrel f64 = 1e-6
	epsabs f64 = 1e-6
	n_max  int = 100
}

BisectionParams defines a public data structure for this module.

struct Brent #

struct Brent {
pub:
	f func.Fn @[required]
pub mut:
	x1      f64
	x2      f64
	tol     f64
	n_max   int
	n_calls int
	n_iter  int
}

Brent implements Brent's method for finding roots of a function in a given interval. The function must be continuous and the interval must contain a root.

struct BrentIteration #

struct BrentIteration {
	x       f64
	fx      f64
	n_calls int
	n_iter  int
}

BrentIteration is a single iteration of Brent's method