Skip to main content

Module poly_ex

Module poly_ex 

Source
Expand description

Public sparse polynomial view (Poly) over explicit generators. Public polynomial view: Poly — an expression seen as a sparse polynomial in an explicit list of generators, with symbolic or exact rational coefficients.

A Poly is built from an Ex with Poly::new (or Ex::as_poly). The expression is expanded and its terms are collected by monomial in the generators; every coefficient is an Ex that is free of the generators — an exact rational number or a symbolic parameter expression such as a + 1. The view is exact: no numeric approximation happens anywhere, and Poly::to_ex rebuilds an expression equal to the original.

Terms are reported in descending lexicographic order of their exponent vectors (the order of SymPy’s Poly.terms()), so the leading term of Poly(x² y + x y² + y³, x, y) is x² y.

§Representation

A polynomial whose coefficients are all rational literals is stored as an exact MultiPoly in Lex order (the order of Poly::terms), and arithmetic between two such polynomials runs on rationals without touching the expression arena. As soon as a symbolic coefficient appears the polynomial is stored as one Ex per monomial, and mixed operations convert the exact side to that form. Both representations report the same terms in the same order.

§Examples

use symplex::prelude::*;
use symplex::poly_ex::Poly;

let ctx = Context::new();
let (x, y, a) = (ctx.symbol("x"), ctx.symbol("y"), ctx.symbol("a"));
let e = &a * &x.powi(2) + &x * &y * 3 - &y + 1;
let p = Poly::new(&e, &[&x, &y]).unwrap();
assert_eq!(p.num_terms(), 4);
assert_eq!(p.total_degree(), Some(2));
assert_eq!(p.leading_coeff(), a);
assert_eq!(p.coeff_monomial(&[1, 1]).unwrap(), ctx.int(3));
assert_eq!(p.to_ex(), e.expand());

Structs§

Poly
A sparse multivariate polynomial view of an expression over explicit generators, with coefficients that are expressions free of the generators.