PolynomialClassification

Trait PolynomialClassification 

Source
pub trait PolynomialClassification {
    // Required methods
    fn is_polynomial(&self) -> bool;
    fn is_polynomial_in(&self, vars: &[Symbol]) -> bool;
    fn polynomial_variables(&self) -> Vec<Symbol>;
    fn classify(&self) -> ExpressionClass;
    fn is_intpoly_compatible(&self) -> bool;
    fn try_as_intpoly(&self) -> Option<(IntPoly, Symbol)>;
}
Expand description

Trait for polynomial classification

Provides automatic detection of polynomial structure. Implemented for Expression to enable classification-based algorithm routing.

§Examples

use mathhook_core::core::polynomial::PolynomialClassification;
use mathhook_core::core::ExpressionClass;
use mathhook_core::{expr, symbol};

let x = symbol!(x);
let poly = expr!(x ^ 2 + x + 1);

assert!(poly.is_polynomial());
assert_eq!(poly.polynomial_variables().len(), 1);

match poly.classify() {
    ExpressionClass::UnivariatePolynomial { degree, .. } => {
        assert_eq!(degree, 2);
    }
    _ => panic!("Expected univariate polynomial"),
}

Required Methods§

Source

fn is_polynomial(&self) -> bool

Check if expression is a valid polynomial

A polynomial is an expression composed only of:

  • Constants (integers, rationals)
  • Symbols (variables)
  • Addition and subtraction
  • Multiplication
  • Non-negative integer powers
§Examples
use mathhook_core::core::polynomial::PolynomialClassification;
use mathhook_core::{expr, symbol};

let x = symbol!(x);

// Polynomials
assert!(expr!(x ^ 2).is_polynomial());
assert!(expr!(x + 1).is_polynomial());
assert!(expr!(3 * x).is_polynomial());

// Not polynomials
assert!(!expr!(sin(x)).is_polynomial());
Source

fn is_polynomial_in(&self, vars: &[Symbol]) -> bool

Check if polynomial in specific variables

Returns true if the expression is a polynomial when treating only the given variables as indeterminates (others are constants).

§Arguments
  • vars - The variables to treat as indeterminates
§Examples
use mathhook_core::core::polynomial::PolynomialClassification;
use mathhook_core::{expr, symbol};

let x = symbol!(x);
let y = symbol!(y);
let poly = expr!(x * y);

assert!(poly.is_polynomial_in(&[x.clone()]));
assert!(poly.is_polynomial_in(&[y.clone()]));
assert!(poly.is_polynomial_in(&[x.clone(), y.clone()]));
Source

fn polynomial_variables(&self) -> Vec<Symbol>

Get polynomial variables (empty if not polynomial)

Returns all symbols that appear in the expression.

§Examples
use mathhook_core::core::polynomial::PolynomialClassification;
use mathhook_core::{expr, symbol};

let x = symbol!(x);
let y = symbol!(y);
let poly = expr!(x + y);

let vars = poly.polynomial_variables();
assert_eq!(vars.len(), 2);
Source

fn classify(&self) -> ExpressionClass

Classify expression type for routing

Returns the classification that determines which algorithm to use. This enables intelligent dispatch to specialized algorithms.

§Examples
use mathhook_core::core::polynomial::PolynomialClassification;
use mathhook_core::core::ExpressionClass;
use mathhook_core::{expr, symbol};

let x = symbol!(x);

// Integer classification
assert_eq!(expr!(5).classify(), ExpressionClass::Integer);

// Univariate polynomial
match expr!(x ^ 2).classify() {
    ExpressionClass::UnivariatePolynomial { degree, .. } => {
        assert_eq!(degree, 2);
    }
    _ => panic!("Expected univariate polynomial"),
}
Source

fn is_intpoly_compatible(&self) -> bool

Check if expression can be represented as IntPoly

Returns true if the expression is a univariate polynomial with integer coefficients only. This is a fast heuristic check.

§Examples
use mathhook_core::core::polynomial::PolynomialClassification;
use mathhook_core::{expr, symbol};

let x = symbol!(x);

assert!(expr!(x ^ 2 + 2 * x + 1).is_intpoly_compatible());
assert!(!expr!(1.5 * x + 2).is_intpoly_compatible());
Source

fn try_as_intpoly(&self) -> Option<(IntPoly, Symbol)>

Try to convert to IntPoly

Returns IntPoly and variable if expression is a univariate integer polynomial, None otherwise.

§Examples
use mathhook_core::core::polynomial::PolynomialClassification;
use mathhook_core::{expr, symbol};

let x = symbol!(x);
let poly_expr = expr!(x ^ 2 + 2 * x + 3);

if let Some((intpoly, var)) = poly_expr.try_as_intpoly() {
    assert_eq!(var, x);
    assert_eq!(intpoly.degree(), Some(2));
}

Implementors§