TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Nonanti/mathcore/llms.txt
Use this file to discover all available pages before exploring further.
mathcore::ml module provides symbolic and numerical optimization utilities through the Optimization struct. It builds on mathcore::calculus::Calculus for symbolic differentiation, enabling exact gradient and Hessian computation, gradient-descent parameter optimization, forward-mode automatic differentiation, Taylor series expansion, and Jacobian matrix evaluation at a point. All methods are free associated functions on the unit struct Optimization.
pub struct Optimization
gradient
Computes the gradient of a scalar-valued expression with respect to each variable in vars, returning a vector of partial-derivative expressions in the same order as the input variable slice.
Each partial derivative is obtained by calling Calculus::differentiate(expr, var).
The scalar expression to differentiate. Typically a loss function or potential energy that depends on one or more of the listed variables.
Ordered list of variable names. The output gradient vector has the same length and ordering:
result[i] is ∂expr/∂vars[i].Ok(Vec<Expr>) of length vars.len(), or Err(MathError) if any partial differentiation fails.
hessian
Computes the Hessian matrix of second-order partial derivatives for expr. The result is an n × n matrix (as a Vec<Vec<Expr>>) where entry [i][j] equals ∂²expr / (∂vars[i] ∂vars[j]).
The scalar-valued expression to differentiate twice.
Variable names. The Hessian is
len(vars) × len(vars).Ok(Vec<Vec<Expr>>) where the outer vector indexes rows and the inner vector indexes columns.
gradient_descent
Minimises loss_fn using vanilla (full-batch) gradient descent. Gradients are computed once symbolically before the loop, then numerically evaluated at the current parameter values on each iteration.
The scalar loss expression to minimise. Must evaluate to a numeric value at every parameter point visited during descent.
Starting values for every variable that appears in
loss_fn. The keys of this map also define which variables are treated as learnable parameters.Step size multiplier applied to the gradient at each iteration. Typical values are in the range
0.001 – 0.1.Number of gradient-descent steps to perform. There is no early-stopping criterion; the method always runs for the full iteration count.
Ok(HashMap<String, f64>) with the parameter values after all iterations, or Err(MathError) if any gradient evaluation produces a non-numeric result.
automatic_differentiation
Evaluates both the function value and its derivative at a specific numeric point using symbolic differentiation followed by numeric evaluation. This is equivalent to forward-mode automatic differentiation for single-variable expressions.
The expression to evaluate and differentiate.
The variable with respect to which the derivative is computed.
The numeric value at which both
expr and its derivative are evaluated.Ok((f, f')) where f is the function value and f' is the derivative value at value, or Err(MathError) if either evaluation produces a non-numeric result.
taylor_series
Computes the Taylor series expansion of expr about center up to and including terms of degree order. Coefficients are obtained by repeated symbolic differentiation evaluated numerically at the center point:
0.0 * (x − center)^n) but do not affect evaluation.
The function to expand.
The expansion variable.
The point about which the Taylor expansion is computed. Use
0.0 for a Maclaurin series.The highest power to include. An
order of 5 yields terms up to (x − center)^5.Ok(Expr) with the Taylor polynomial as a symbolic expression, or Err(MathError) if differentiation or evaluation fails.
jacobian
Computes the Jacobian matrix of a vector-valued function at a specific numeric point. Entry J[i][j] equals ∂functions[i] / ∂vars[j] evaluated at point.
The component functions
f₁, f₂, ..., fₘ. The Jacobian has m rows, one per function.The variables
x₁, x₂, ..., xₙ with respect to which each partial derivative is taken. The Jacobian has n columns.The numeric point at which all partial derivatives are evaluated. Must contain an entry for every variable in
vars.Ok(DMatrix<f64>) of shape m × n, or Err(MathError) if any partial derivative evaluates to a non-numeric result.
optimize_newton
Finds a local minimum (or maximum) of func with respect to var using Newton’s method for optimisation. At each iteration the current point is updated by x ← x - f'(x) / f''(x), converging to a critical point where f'(x) = 0. Both the first and second derivatives are computed symbolically.
The scalar function to optimise. Must be differentiable at least twice with respect to
var.The optimisation variable.
The starting point for Newton iteration.
Convergence threshold on
|f'(x)|. Iteration stops when the first derivative falls below this value.Maximum number of Newton steps before returning the current estimate.
Ok(f64) with the critical point found, or Err(MathError) if the second derivative is too small to divide by or if evaluation produces a non-numeric result.
lagrange_multipliers
Constructs the Lagrangian for constrained optimisation and returns its gradient with respect to all original variables plus one Lagrange multiplier per constraint. Given an objective f and constraints g₁, g₂, ..., gₘ, the Lagrangian is:
∂L/∂xᵢ and ∂L/∂λⱼ form the KKT stationarity conditions. Each λⱼ variable is named λ0, λ1, etc.
The objective function
f(x₁, ..., xₙ) to optimise.Equality constraints written in the form
g(x) = 0. Each constraint contributes one Lagrange multiplier to the Lagrangian.The original decision variable names. The returned gradient has
vars.len() + constraints.len() components.Ok(Vec<Expr>) containing the partial derivatives of the Lagrangian with respect to each variable and each multiplier, or Err(MathError) if differentiation fails.
