optimize
stableNumerical optimization routines including golden-section search, bisection root-finding, gradient descent, polynomial fitting, and 2D linear programming.
use plugin optimize::{golden_section, bisection, gradient_descent_1d, …} Functions (14)
- golden_section Find minimum in an interval via golden-section search
- bisection Find a root in an interval via bisection
- gradient_descent_1d Apply 1D gradient descent steps to a scalar
- newton_step Compute one Newton-Raphson step
- linear_interpolate Interpolate a value from x/y sample tables
- polynomial_fit Fit a polynomial to data via least squares
- minimize_quadratic Minimize a quadratic `ax²+bx+c`
- maximize_quadratic Maximize a quadratic `ax²+bx+c`
- minimize Find minimum of a sampled function
- maximize Find maximum of a sampled function
- gradient_descent Multi-dimensional gradient descent
- linear_program_2d Maximize a 2D linear objective with constraints
- check_constraints Check whether a point satisfies constraints
- numerical_derivative Estimate derivative via central finite difference
Overview
optimize is a dependency-free numerical-methods toolkit for finding minima,
maxima, and roots of functions, fitting curves to data, and solving small
optimization problems. There is no plugin state or handle to manage — every
function is a pure call that takes plain numbers and tables and returns a number
or a result table. Functions come in two flavours: closed-form solvers that work
from coefficients (minimize_quadratic, newton_step), and sampled solvers that
work from a table of {x, fx} pairs and interpolate linearly between them
(golden_section, minimize, bisection, numerical_derivative).
Reach for it when you have tabulated data or a known objective and need to optimize, root-find, interpolate, fit a polynomial, run gradient-descent steps, or solve a two-variable linear program with constraints — all without pulling in an external math library.
Common patterns
Sample a function, then find both its minimum and the slope at that point:
use plugin optimize::{minimize, numerical_derivative}
let samples = #{
1: #{x: -2.0, fx: 4.0},
2: #{x: 0.0, fx: 0.5},
3: #{x: 2.0, fx: 4.0}
}
let min = minimize(samples, 1e-6)
let slope = numerical_derivative(samples, min["x"])
print("min at x={min["x"]} (slope ~ {slope})")
Fit a polynomial to noisy data, then interpolate a value between samples:
use plugin optimize::{polynomial_fit, linear_interpolate}
let xs = #{1: 0.0, 2: 1.0, 3: 2.0, 4: 3.0}
let ys = #{1: 1.0, 2: 4.0, 3: 9.0, 4: 16.0}
let coeffs = polynomial_fit(xs, ys, 2)
print("fit: {coeffs[1]} + {coeffs[2]}x + {coeffs[3]}x^2")
let mid = linear_interpolate(xs, ys, 2.5)
print("y at 2.5 = {mid}")
Solve a 2D linear program, then verify the winning point against the constraints:
use plugin optimize::{linear_program_2d, check_constraints}
let constraints = #{
1: #{a: 1.0, b: 1.0, rhs: 4.0},
2: #{a: 2.0, b: 1.0, rhs: 6.0}
}
let best = linear_program_2d(3.0, 2.0, constraints)
let checks = #{
1: #{coeffs: #{1: 1.0, 2: 1.0}, rhs: 4.0, type: "le"}
}
let point = #{1: best["x1"], 2: best["x2"]}
let report = check_constraints(point, checks)
print("value={best["value"]} feasible={report[1]["satisfied"]}")
Find minimum in an interval via golden-section search
Finds the minimum of a function defined by {x, fx} sample pairs within the interval [a, b] using golden-section search. Interpolates between samples. Returns {x, fx} at the minimum. tol is the convergence width.
use plugin optimize::{golden_section}
let samples = #{
1: #{x: 0.0, fx: 4.0},
2: #{x: 1.0, fx: 1.0},
3: #{x: 2.0, fx: 4.0}
}
let result = golden_section(samples, 0.0, 2.0, 1e-6)
print("min at x={result["x"]} f={result["fx"]}")
Search only a sub-interval of the samples to focus on a local basin:
use plugin optimize::{golden_section}
let samples = #{
1: #{x: 0.0, fx: 5.0},
2: #{x: 1.0, fx: 1.0},
3: #{x: 3.0, fx: 0.2},
4: #{x: 5.0, fx: 6.0}
}
let result = golden_section(samples, 2.0, 4.0, 1e-6)
print("local min near x={result["x"]}")
Find a root in an interval via bisection
Finds a root (zero crossing) of a sampled function within [a, b] using the bisection method. The function values at a and b must have opposite signs. Returns the x value of the root.
use plugin optimize::{bisection}
let samples = #{
1: #{x: 0.0, fx: -1.0},
2: #{x: 1.0, fx: 0.0},
3: #{x: 2.0, fx: 3.0}
}
let root = bisection(samples, 0.0, 2.0, 1e-8)
print("root at x={root}")
Apply 1D gradient descent steps to a scalar
Applies a sequence of 1D gradient descent updates starting from x0. Each entry in gradients is a gradient value; the update rule is x -= lr * grad. Returns the final x value.
use plugin optimize::{gradient_descent_1d}
let x = gradient_descent_1d(5.0, 0.1, #{1: 2.0, 2: 1.5, 3: 0.8})
print("converged to x={x}")
Compute one Newton-Raphson step
Computes one Newton-Raphson update: x_new = x - f(x) / f39;(x). Errors if the derivative is zero. Iterate this call to converge to a root.
use plugin optimize::{newton_step}
let x = 2.0
let x1 = newton_step(x, x * x - 2.0, 2.0 * x)
let x2 = newton_step(x1, x1 * x1 - 2.0, 2.0 * x1)
print("sqrt(2) ~ {x2}")
Iterate in a loop to converge a root of f(x) = x^3 - x - 2:
use plugin optimize::{newton_step}
let mut x = 1.5
for _ in 0..6 {
let fx = x * x * x - x - 2.0
let fpx = 3.0 * x * x - 1.0
x = newton_step(x, fx, fpx)
}
print("root ~ {x}")
Interpolate a value from x/y sample tables
Performs piecewise linear interpolation at target_x given matched arrays of x and y values. Values outside the range clamp to the nearest endpoint. Useful for mapping between scales.
use plugin optimize::{linear_interpolate}
let xs = #{1: 0.0, 2: 1.0, 3: 2.0}
let ys = #{1: 0.0, 2: 10.0, 3: 5.0}
let y = linear_interpolate(xs, ys, 1.5)
print("y at 1.5 = {y}")
Fit a polynomial to data via least squares
Fits a polynomial of the given degree to a set of (x, y) data points using least-squares via Gaussian elimination on the normal equations. Returns a table of coefficients [c0, c1, ..., cn] where p(x) = c0 + c1*x + ... + cn*x^n.
use plugin optimize::{polynomial_fit}
let xs = #{1: 0.0, 2: 1.0, 3: 2.0, 4: 3.0}
let ys = #{1: 1.0, 2: 4.0, 3: 9.0, 4: 16.0}
let coeffs = polynomial_fit(xs, ys, 2)
print("c0={coeffs[1]} c1={coeffs[2]} c2={coeffs[3]}")
Minimize a quadratic `ax²+bx+c`
Finds the minimum of the quadratic f(x) = ax² + bx + c analytically. Requires a > 0 (concave up). Returns {x, fx} at the vertex.
use plugin optimize::{minimize_quadratic}
let result = minimize_quadratic(1.0, -4.0, 5.0)
print("min at x={result["x"]} f={result["fx"]}")
Maximize a quadratic `ax²+bx+c`
Finds the maximum of the quadratic f(x) = ax² + bx + c analytically. Requires a < 0 (concave down). Returns {x, fx} at the vertex.
use plugin optimize::{maximize_quadratic}
let result = maximize_quadratic(-1.0, 4.0, 0.0)
print("max at x={result["x"]} f={result["fx"]}")
Find minimum of a sampled function
Finds the minimum of a function defined by {x, fx} sample pairs over the full sample range using golden-section search. Returns {x, fx}. Simpler than golden_section when you don't need to specify a sub-interval.
use plugin optimize::{minimize}
let samples = #{
1: #{x: -2.0, fx: 4.0},
2: #{x: 0.0, fx: 0.5},
3: #{x: 2.0, fx: 4.0}
}
let min = minimize(samples, 1e-6)
print("min: {min["x"]}")
Find maximum of a sampled function
Finds the maximum of a sampled function over the full range by negating values and minimizing. Returns {x, fx} at the maximum.
use plugin optimize::{maximize}
let samples = #{
1: #{x: 0.0, fx: 0.0},
2: #{x: 1.0, fx: 3.0},
3: #{x: 2.0, fx: 0.0}
}
let max = maximize(samples, 1e-6)
print("max: {max["fx"]} at x={max["x"]}")
Multi-dimensional gradient descent
Multi-dimensional gradient descent. x0 is a table of initial coordinates, gradients is a table of gradient steps (each step is a table of the same dimension). Returns the final coordinate table after all steps.
use plugin optimize::{gradient_descent}
let x0 = #{1: 3.0, 2: 3.0}
let steps = #{
1: #{1: 2.0, 2: 2.0},
2: #{1: 1.0, 2: 1.0}
}
let result = gradient_descent(x0, 0.1, steps)
print("x={result[1]} y={result[2]}")
Each step is applied in order, so you can replay a recorded trajectory of gradients for a 3D parameter vector:
use plugin optimize::{gradient_descent}
let x0 = #{1: 1.0, 2: -1.0, 3: 0.5}
let trajectory = #{
1: #{1: 0.4, 2: -0.4, 3: 0.2},
2: #{1: 0.2, 2: -0.2, 3: 0.1}
}
let final = gradient_descent(x0, 0.5, trajectory)
print("settled at {final[1]}, {final[2]}, {final[3]}")
Maximize a 2D linear objective with constraints
Maximizes c1*x1 + c2*x2 subject to a list of linear constraints of the form a*x1 + b*x2 <= rhs with x1, x2 >= 0. Each constraint is {a, b, rhs}. Returns {x1, x2, value} at the optimal vertex.
use plugin optimize::{linear_program_2d}
let constraints = #{
1: #{a: 1.0, b: 1.0, rhs: 4.0},
2: #{a: 2.0, b: 1.0, rhs: 6.0}
}
let result = linear_program_2d(3.0, 2.0, constraints)
print("x1={result["x1"]} x2={result["x2"]} value={result["value"]}")
Check whether a point satisfies constraints
Tests whether a point satisfies a list of linear constraints. Each constraint is {coeffs, rhs, type} where type is "le", "ge", or "eq". Returns a table with one entry per constraint: {satisfied, lhs, rhs, slack}.
use plugin optimize::{check_constraints}
let point = #{1: 1.0, 2: 2.0}
let constraints = #{
1: #{coeffs: #{1: 1.0, 2: 1.0}, rhs: 4.0, type: "le"}
}
let result = check_constraints(point, constraints)
print("satisfied: {result[1]["satisfied"]}")
Estimate derivative via central finite difference
Estimates the derivative of a sampled function at x using the central finite difference formula (f(x+h) - f(x-h)) / (2h). h defaults to 1e-5. Uses linear interpolation between sample points.
use plugin optimize::{numerical_derivative}
let samples = #{
1: #{x: 0.0, fx: 0.0},
2: #{x: 1.0, fx: 1.0},
3: #{x: 2.0, fx: 4.0}
}
let deriv = numerical_derivative(samples, 1.0)
print("f'(1) ~ {deriv}")
Pass an explicit step size h for a wider or tighter finite-difference window:
use plugin optimize::{numerical_derivative}
let samples = #{
1: #{x: 0.0, fx: 0.0},
2: #{x: 1.0, fx: 1.0},
3: #{x: 2.0, fx: 8.0}
}
let deriv = numerical_derivative(samples, 1.0, 0.25)
print("f'(1) with h=0.25 ~ {deriv}")