pub struct Poly<T: Ring> { /* private fields */ }Expand description
Generic univariate polynomial with coefficients of type T
Coefficients are stored in ascending order: coeffs[i] is the coefficient of x^i.
The zero polynomial has an empty coefficient vector.
§Examples
use mathhook_core::core::polynomial::poly::IntPoly;
let p = IntPoly::from_coeffs(vec![1, 2, 3]); // 1 + 2x + 3x²
assert_eq!(p.degree(), Some(2));
assert_eq!(p.leading_coeff(), 3);Implementations§
Source§impl<T: Ring> Poly<T>
impl<T: Ring> Poly<T>
Sourcepub fn mul(&self, other: &Self) -> Self
pub fn mul(&self, other: &Self) -> Self
Polynomial multiplication (schoolbook algorithm)
O(n*m) where n, m are degrees
Sourcepub fn derivative(&self) -> Self
pub fn derivative(&self) -> Self
Derivative
Source§impl Poly<i64>
impl Poly<i64>
Sourcepub fn evaluate_i64(&self, x: i64) -> i64
pub fn evaluate_i64(&self, x: i64) -> i64
Evaluate at integer point (IntPoly-specific optimized version)
Sourcepub fn scale_i64(&self, c: i64) -> Self
pub fn scale_i64(&self, c: i64) -> Self
Scalar multiplication for i64 (IntPoly-specific optimized version)
Sourcepub fn derivative_i64(&self) -> Self
pub fn derivative_i64(&self) -> Self
Derivative for IntPoly (optimized version)
Source§impl Poly<i64>
impl Poly<i64>
Sourcepub fn try_from_expression(expr: &Expression, var: &Symbol) -> Option<Self>
pub fn try_from_expression(expr: &Expression, var: &Symbol) -> Option<Self>
Try to convert an Expression to IntPoly
Returns Some(IntPoly) if the expression is a univariate polynomial with integer coefficients. Returns None otherwise.
§Example
use mathhook_core::{symbol, expr};
use mathhook_core::core::polynomial::poly::IntPoly;
let x = symbol!(x);
let poly = expr!((x^2) + (2*x) + 1);
if let Some(int_poly) = IntPoly::try_from_expression(&poly, &x) {
let deriv = int_poly.derivative();
}Sourcepub fn to_expression(&self, var: &Symbol) -> Expression
pub fn to_expression(&self, var: &Symbol) -> Expression
Convert IntPoly back to Expression
§Example
use mathhook_core::symbol;
use mathhook_core::core::polynomial::poly::IntPoly;
let x = symbol!(x);
let p = IntPoly::from_coeffs(vec![1, 2, 3]);
let expr = p.to_expression(&x);Sourcepub fn can_convert(expr: &Expression, var: &Symbol) -> bool
pub fn can_convert(expr: &Expression, var: &Symbol) -> bool
Check if an Expression can be converted to IntPoly
This is a fast check that doesn’t allocate.
Source§impl<T: EuclideanDomain> Poly<T>
impl<T: EuclideanDomain> Poly<T>
Sourcepub fn div_rem(&self, divisor: &Self) -> Result<(Self, Self), MathError>
pub fn div_rem(&self, divisor: &Self) -> Result<(Self, Self), MathError>
Polynomial division with remainder
Returns (quotient, remainder) such that self = quotient * divisor + remainder and degree(remainder) < degree(divisor)
§Errors
Returns MathError::DivisionByZero if divisor is zero
Sourcepub fn pseudo_div_rem(
&self,
divisor: &Self,
) -> Result<(Self, Self, T), MathError>
pub fn pseudo_div_rem( &self, divisor: &Self, ) -> Result<(Self, Self, T), MathError>
Polynomial pseudo-division
Returns (quotient, remainder, multiplier) such that: multiplier * self = quotient * divisor + remainder
This avoids fractions by multiplying by powers of the leading coefficient.
§Errors
Returns MathError::DivisionByZero if divisor is zero
Sourcepub fn gcd(&self, other: &Self) -> Result<Self, MathError>
pub fn gcd(&self, other: &Self) -> Result<Self, MathError>
GCD using Euclidean algorithm
Returns the greatest common divisor of two polynomials. The result is made primitive (content = 1).
§Errors
Returns MathError::DivisionByZero if division by zero occurs during computation
Sourcepub fn primitive_part(&self) -> Result<Self, MathError>
pub fn primitive_part(&self) -> Result<Self, MathError>
Source§impl Poly<i64>
impl Poly<i64>
Sourcepub fn try_make_monic(&self) -> Option<Self>
pub fn try_make_monic(&self) -> Option<Self>
Make polynomial monic by dividing by leading coefficient Returns None if leading coefficient doesn’t divide all coefficients
Sourcepub fn content_i64(&self) -> i64
pub fn content_i64(&self) -> i64
Content for IntPoly (optimized)
Sourcepub fn primitive_part_i64(&self) -> Result<Self, MathError>
pub fn primitive_part_i64(&self) -> Result<Self, MathError>
Source§impl<T: Ring> Poly<T>
impl<T: Ring> Poly<T>
Sourcepub fn from_coeffs(coeffs: Vec<T>) -> Self
pub fn from_coeffs(coeffs: Vec<T>) -> Self
Sourcepub fn is_constant(&self) -> bool
pub fn is_constant(&self) -> bool
Check if polynomial is constant
Sourcepub fn leading_coeff(&self) -> T
pub fn leading_coeff(&self) -> T
Get leading coefficient (zero for zero polynomial)
Sourcepub fn coefficients(&self) -> &[T]
pub fn coefficients(&self) -> &[T]
Get all coefficients
Trait Implementations§
impl<T: Eq + Ring> Eq for Poly<T>
impl<T: Ring> StructuralPartialEq for Poly<T>
Auto Trait Implementations§
impl<T> Freeze for Poly<T>
impl<T> RefUnwindSafe for Poly<T>where
T: RefUnwindSafe,
impl<T> Send for Poly<T>where
T: Send,
impl<T> Sync for Poly<T>where
T: Sync,
impl<T> Unpin for Poly<T>where
T: Unpin,
impl<T> UnwindSafe for Poly<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more