MathCore’sDocumentation 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.
Solver module gives you a unified interface for finding the roots of polynomial equations — from trivial linear cases all the way to higher-degree polynomials that require numerical approximation. Equations are expressed as strings, treated as equal to zero, and dispatched to the most appropriate solving strategy based on the polynomial degree detected at runtime. The result is always a Vec<Expr>, letting you handle real and complex roots uniformly under the same return type.
MathCore::solve
solve parses the input string into an Expr, determines the degree of the polynomial in var, and delegates to the correct solver. All equations are interpreted as being set equal to zero — pass the left-hand side only.
| Parameter | Type | Description |
|---|---|---|
equation | &str | Expression string representing the left-hand side (= 0) |
var | &str | The variable to solve for |
Ok(Vec<Expr>) containing the roots as Expr::Number or Expr::Complex values, or a MathError if solving fails.
Passing an equation like
"x^2 - 4" is equivalent to solving x² - 4 = 0. MathCore does not accept equality syntax such as "x^2 = 4" — always rearrange to bring everything to one side.Solving strategies by degree
MathCore inspects the degree of the expression in the target variable and selects a strategy accordingly:Degree 0 — Constant equations
If the expression contains no occurrences of the target variable, MathCore evaluates the constant. A result of zero means every value satisfies the equation (infinite solutions); any other constant means no solutions exist.Degree 1 — Linear equations
Linear equations are solved exactly by extracting the coefficientsa and b from a*x + b = 0, then computing x = -b / a.
Degree 2 — Quadratic equations
Quadratic equations are solved using the quadratic formula:x = (-b ± √(b²-4ac)) / 2a.
The discriminant Δ = b² - 4ac determines the nature of the roots:
- Δ > 0 — two distinct real roots (
Expr::Number) - Δ = 0 — one repeated real root (
Expr::Number) - Δ < 0 — two complex conjugate roots (
Expr::Complex)
Higher degree — Newton-Raphson numerical solver
For polynomials of degree 3 and above, MathCore falls back to Newton-Raphson numerical root-finding. The solver searches across multiple initial points spanning[-100, 100] and automatically deduplicates roots that converge within 1e-6 of each other.
Factoring polynomials with MathCore::factor
factor attempts to express a polynomial as a product of its linear factors. It currently supports degree-2 polynomials with two distinct real roots. Internally it calls solve to find the roots, then constructs the factored form (x - r₁)(x - r₂).
factor returns the original expression unchanged if it cannot factor it — for example, when the polynomial has complex roots, degree ≠ 2, or contains more than one variable. No error is raised in those cases.Using Solver directly
The underlying solver::Solver struct is also part of the public API if you are working with already-parsed Expr trees rather than raw strings:
Newton-Raphson internals
The numerical solver uses the derivative of the equation (computed symbolically viaCalculus::differentiate) to drive each Newton-Raphson iteration:
| Setting | Value |
|---|---|
| Max iterations | 100 |
| Convergence tolerance | 1e-10 |
| Initial search points | -100, -10, -1, 0, 1, 10, 100 |
| Duplicate threshold | 1e-6 |
[-100, 100].
