svg_path/root
Bracketed root-finding helpers for scalar functions.
bisect implements standard bracketed bisection. Polynomial root isolation
partitions an interval at derivative roots, then refines each sign-changing
monotone window with bracketed bisection.
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)
Options for isolating and refining real polynomial roots.
pub type PolynomialOptions {
PolynomialOptions(
coefficient_tolerance: Float,
root_tolerance: Float,
value_tolerance: Float,
max_iterations: Int,
)
}
Constructors
-
PolynomialOptions( coefficient_tolerance: Float, root_tolerance: Float, value_tolerance: Float, max_iterations: Int, )
Options for solving a quadratic equation.
pub type QuadraticOptions {
QuadraticOptions(
coefficient_tolerance: Float,
repeated_root_policy: RepeatedRootPolicy,
)
}
Constructors
-
QuadraticOptions( coefficient_tolerance: Float, repeated_root_policy: RepeatedRootPolicy, )
Whether a repeated quadratic root is returned once or with its algebraic multiplicity of two.
pub type RepeatedRootPolicy {
ConsolidateRepeatedRoot
PreserveRepeatedRoot
}
Constructors
-
ConsolidateRepeatedRoot -
PreserveRepeatedRoot
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 consolidate(
roots: List(Float),
tolerance tolerance: Float,
) -> List(Float)
Sort roots and merge neighboring values within tolerance.
pub fn cubic(
a: Float,
b: Float,
c: Float,
d: Float,
) -> Result(List(Float), Error)
Find all distinct real roots of a*x³ + b*x² + c*x + d.
pub fn cubic_with(
a: Float,
b: Float,
c: Float,
d: Float,
options options: PolynomialOptions,
) -> Result(List(Float), Error)
Find all distinct real roots of a cubic using explicit numerical options.
pub fn default_polynomial_options() -> PolynomialOptions
Return the default options for polynomial root isolation.
pub fn inside(
roots: List(Float),
from lower: Float,
to upper: Float,
) -> List(Float)
Keep roots inside a closed interval and return them in ascending order.
pub fn linear(a: Float, b: Float) -> List(Float)
Solve a * x + b = 0 using exact zero classification.
An identically zero or inconsistent constant equation returns no isolated roots.
pub fn polynomial_derivative(
coefficients: List(Float),
) -> List(Float)
Differentiate power-basis coefficients ordered from highest power to the constant term.
pub fn polynomial_roots_with(
coefficients: List(Float),
from lower: Float,
to upper: Float,
options options: PolynomialOptions,
) -> Result(List(Float), Error)
Find all distinct real roots of a power-basis polynomial in a closed interval.
Derivative roots partition the interval into monotone pieces. Sign-changing roots are refined with bracketed bisection, while roots shared with the derivative preserve even-multiplicity roots that do not change sign.
pub fn polynomial_value(
coefficients: List(Float),
at x: Float,
) -> Float
Evaluate a power-basis polynomial whose coefficients are ordered from the highest power to the constant term.
pub fn quadratic(a: Float, b: Float, c: Float) -> List(Float)
Solve a * x² + b * x + c = 0 using exact degree classification.
Real roots are returned in formula order. A repeated root is returned once.
pub fn quadratic_with(
a: Float,
b: Float,
c: Float,
options options: QuadraticOptions,
) -> List(Float)
Solve a * x² + b * x + c = 0 with explicit degree and multiplicity
policy.
Coefficients whose absolute value is less than coefficient_tolerance are
treated as zero. A zero or negative tolerance uses exact zero comparison.
pub fn strictly_inside(
roots: List(Float),
from lower: Float,
to upper: Float,
) -> List(Float)
Keep roots strictly inside an interval and return them in ascending order.