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::solver module provides symbolic and numerical equation solving through the Solver struct. Given any Expr representing a polynomial expression, Solver::solve dispatches to the appropriate exact or numerical algorithm based on the polynomial degree, returning all discovered real and complex roots. A companion factor method attempts to rewrite a polynomial as a product of its linear factors.
pub struct Solver
A zero-sized unit struct that groups all solver operations as associated functions. No instance is needed.
solve
Solves the equation expr = 0 for the given variable, returning a Vec<Expr> containing all discovered roots. Before solving, the expression is normalized to the form expr - 0.
Dispatch is based on the polynomial degree of expr in var:
| Degree | Strategy | Output type |
|---|---|---|
| 0 (constant) | Evaluates the constant; infinite solutions → Err, no solutions → Ok(vec![]) | — |
| 1 (linear) | Exact closed-form: x = -b / a | Expr::Number |
| 2 (quadratic) | Quadratic formula with full discriminant analysis | Expr::Number (real) or Expr::Complex (complex conjugate pair) |
| ≥ 3 | Newton-Raphson starting from multiple seed points across [-100, 100]; duplicates within 1e-6 are deduplicated | Expr::Number |
Calculus::differentiate) is used to compute the derivative analytically. Convergence tolerance is 1e-10 with a maximum of 100 iterations per seed.
The expression to set equal to zero. This may be a raw polynomial expression such as
x^2 - 4, or an explicit subtraction node of the form lhs - rhs representing lhs = rhs. The solver normalises both forms automatically.The name of the variable to solve for. All other
Expr::Symbol nodes in the expression are treated as parameters.Ok(Vec<Expr>) containing the roots (possibly empty if no real roots exist), or Err(MathError::SolverError) in degenerate cases such as infinite solutions for a zero-degree expression.
factor
Attempts to express expr as a product of its linear factors. For quadratic expressions in a single variable, the roots are found via solve and the result is returned as (x - r1) * (x - r2). Expressions that are not quadratics in a single variable, or quadratics whose roots are not purely real Expr::Number values, are returned unchanged.
The polynomial expression to factor. Must be an
Expr::Binary node with Add or Subtract at the top level; other forms are returned as-is without error.Ok(Expr) containing either the factored product (x - r1) * (x - r2) for a factorable quadratic, or the original expression unchanged when factoring is not applicable.
