Poly

Struct Poly 

Source
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>

Source

pub fn add(&self, other: &Self) -> Self

Polynomial addition

Source

pub fn sub(&self, other: &Self) -> Self

Polynomial subtraction

Source

pub fn mul(&self, other: &Self) -> Self

Polynomial multiplication (schoolbook algorithm)

O(n*m) where n, m are degrees

Source

pub fn scale(&self, c: &T) -> Self

Scalar multiplication

Source

pub fn negate(&self) -> Self

Negation

Source

pub fn derivative(&self) -> Self

Derivative

Source

pub fn evaluate(&self, x: &T) -> T

Evaluate polynomial at a point using Horner’s method

Uses Horner’s method: O(n) multiplications instead of O(n²)

Source§

impl Poly<i64>

Source

pub fn evaluate_i64(&self, x: i64) -> i64

Evaluate at integer point (IntPoly-specific optimized version)

Source

pub fn scale_i64(&self, c: i64) -> Self

Scalar multiplication for i64 (IntPoly-specific optimized version)

Source

pub fn derivative_i64(&self) -> Self

Derivative for IntPoly (optimized version)

Source§

impl Poly<i64>

Source

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();
}
Source

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);
Source

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>

Source

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

Source

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

Source

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

Source

pub fn content(&self) -> T

Content: GCD of all coefficients

Source

pub fn primitive_part(&self) -> Result<Self, MathError>

Primitive part: polynomial / content

§Errors

Returns MathError::DivisionByZero if content is zero

Source§

impl Poly<i64>

Source

pub fn is_monic(&self) -> bool

Check if polynomial is monic (leading coefficient = 1)

Source

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

Source

pub fn normalize(&self) -> Self

Normalize IntPoly (make leading coefficient positive)

Source

pub fn content_i64(&self) -> i64

Content for IntPoly (optimized)

Source

pub fn primitive_part_i64(&self) -> Result<Self, MathError>

Primitive part for IntPoly (optimized)

§Errors

Returns MathError::DivisionByZero if content is zero

Source

pub fn gcd_i64(&self, other: &Self) -> Result<Self, MathError>

GCD for IntPoly (optimized)

§Errors

Returns MathError::DivisionByZero if division by zero occurs during computation

Source§

impl<T: Ring> Poly<T>

Source

pub fn from_coeffs(coeffs: Vec<T>) -> Self

Create polynomial from coefficients (ascending order)

§Arguments
  • coeffs - Coefficients where coeffs[i] is coefficient of x^i
§Example
use mathhook_core::core::polynomial::poly::IntPoly;

let p = IntPoly::from_coeffs(vec![1, 2, 3]);
assert_eq!(p.degree(), Some(2));
Source

pub fn zero() -> Self

Create zero polynomial

Source

pub fn constant(c: T) -> Self

Create constant polynomial

Source

pub fn is_zero(&self) -> bool

Check if polynomial is zero

Source

pub fn is_constant(&self) -> bool

Check if polynomial is constant

Source

pub fn degree(&self) -> Option<usize>

Get polynomial degree (None for zero polynomial)

Source

pub fn leading_coeff(&self) -> T

Get leading coefficient (zero for zero polynomial)

Source

pub fn coeff(&self, i: usize) -> T

Get coefficient of x^i

Source

pub fn coefficients(&self) -> &[T]

Get all coefficients

Source§

impl Poly<i64>

Source

pub fn monomial(n: usize) -> Self

Create monomial x^n (IntPoly-specific)

Source

pub fn term(coeff: i64, power: usize) -> Self

Create monomial c * x^n (IntPoly-specific)

Trait Implementations§

Source§

impl<T: Ring> Add for &Poly<T>

Source§

type Output = Poly<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Poly<T>

Performs the + operation. Read more
Source§

impl<T: Ring> Add for Poly<T>

Source§

type Output = Poly<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl<T: Clone + Ring> Clone for Poly<T>

Source§

fn clone(&self) -> Poly<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Ring> Debug for Poly<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Ring> Mul for &Poly<T>

Source§

type Output = Poly<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Poly<T>

Performs the * operation. Read more
Source§

impl<T: Ring> Mul for Poly<T>

Source§

type Output = Poly<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Self

Performs the * operation. Read more
Source§

impl<T: Ring> Neg for &Poly<T>

Source§

type Output = Poly<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Poly<T>

Performs the unary - operation. Read more
Source§

impl<T: Ring> Neg for Poly<T>

Source§

type Output = Poly<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl<T: PartialEq + Ring> PartialEq for Poly<T>

Source§

fn eq(&self, other: &Poly<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Ring> Sub for &Poly<T>

Source§

type Output = Poly<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Poly<T>

Performs the - operation. Read more
Source§

impl<T: Ring> Sub for Poly<T>

Source§

type Output = Poly<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more
Source§

impl<T: Eq + Ring> Eq for Poly<T>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.