Skip to main content

solve_poly

Function solve_poly 

Source
pub fn solve_poly(e: &Expr, var: &str) -> Result<Expr, Error>
Expand description

Polynomial root finding: roots(poly, x) → all roots satisfying poly = 0 (column vector).

Only up to degree 2: degree 1 is delegated to solve_linear (the same copy of the math), degree 2 uses the closed-form root formula. The Cardano/Ferrari closed forms for degree 3 and above have terrible conditioning in floating point and nobody could read what is written anyway — rather than giving a “seemingly exact approximation”, reject explicitly and state the degree.

The dimension of the roots automatically equals the dimension of x: the equation must be dimensionally homogeneous (dim_check checks the equation first in the Call branch; Σ aᵢxⁱ homogeneous ⇒ dim(aᵢ) + i·dim(x) constant ⇒ the roots’ dimension is dim(x)), so no extra dimension derivation is needed here.

Multiple roots appear repeated by multiplicity (x² − 2x + 1 gives two 1s), consistent with the fundamental theorem of algebra.

The real emission lane returns NaN for a negative discriminant (not an exception); a constant negative discriminant is rejected before emission. The generic eval_value is the complex value layer and gives complex roots; the complex generation lane requires the formula to contain j explicitly. The two roots are computed with the stable q/a and c/q, and the return order still corresponds to the original (−b−√D), (−b+√D).