[][src]Struct libslide::math::poly::Poly

pub struct Poly {
    pub vec: Vec<isize>,
}

A polynomial in integer space Z. TODO: Currently, this container only services polynomials with non-negative degrees.

Fields

vec: Vec<isize>

Underlying polynomial coefficient representation. The degree of each coefficient is its index in the vector.

Implementations

impl Poly[src]

pub fn new(vec: Vec<isize>) -> Self[src]

Creates a new Poly from a vector of coefficients, with the degree of each coefficient being its index in the vector.

pub fn empty() -> Self[src]

Creates an empty Poly.

pub fn is_zero(&self) -> bool[src]

Returns whether the polynomial is equivalent to 0.

pub fn is_one(&self) -> bool[src]

Returns whether the polynomial is equivalent to 1.

pub fn deg(&self) -> usize[src]

Returns the degree of the polynomial.

pub fn primitive(self) -> Self[src]

Returns the primitive polynomial of self over the integers.

Examples:

This example is not tested
// 6x^2 + 4x + 2 -> 3x^2 + 2x + 1
assert_eq!(poly![2, 4, 6], poly![1, 2, 3]);

fn add_term(self, coeff: isize, pow: usize) -> Self[src]

Adds a term of form coeffx^pow to self.

Examples:

This example is not tested
// (x + 2) + 3x^2 -> 3x^2 + x + 2
assert_eq!(poly![2, 1].add_term(3, 2), poly![2, 1, 3]);

fn mul_term(self, coeff: isize, pow: usize) -> Self[src]

Multiplies a term of form coeffx^pow to self.

Examples:

This example is not tested
// (x + 2) * 3x^2 -> 3x^3 + 6x^2
assert_eq!(poly![2, 1].mul_term(3, 2), poly![0, 0, 6, 3]);

pub fn mul_scalar(self, c: isize) -> Self[src]

Multiplies each term in the polynomial by a scalar.

pub fn div_scalar(self, c: isize) -> Result<Self, &'static str>[src]

Divides each term in the polynomial by a scalar. If the scalar divisor is 0, an error is returned.

fn sub(self, other: Self) -> Self[src]

Subtracts other from self, yielding a new polynomial.

Examples:

This example is not tested
// (x + 2) - (3x^2 + 2x) -> -3x^2 - x + 2
assert_eq!(poly![2, 1].sub(poly![0, 2, 3]), poly![2, -1, -3]);

fn truncate_zeros(self) -> Self[src]

Removes leading zero terms in a polynomial.

pub fn div(self, other: Poly) -> Result<(Self, Self), &'static str>[src]

Divides one polynomial by another, returning a tuple of (quotient, remainder) or an error if division failed.

Examples:

This example is not tested
// (x^2 - 4) / (x + 2) -> ((x - 2), 0)
assert_eq!(poly![-4, 0, 1].div(poly![2, 1]), Ok((poly![-2, 1], poly![])));

// (x^2 - 2x) / (x + 1) -> ((x - 3), 3)
assert_eq!(poly![-2, 0, 1].div(poly![1, 1]), Ok((poly![-3, 1], poly![3])));

pub fn max_norm(&self) -> usize[src]

Returns the max norm of a polynomial. This is equivalent to the largest absolute value of each term's coefficient.

pub fn lc(&self) -> isize[src]

Returns the leading coefficient, i.e. the coefficient of the highest-degree term, of the polynomial. If the polynomial is empty, the leading coefficient is 0.

pub fn eval(&self, x: isize) -> isize[src]

Evaluates the polynomial at a value x.

Examples:

This example is not tested
// (x^2 - 4)(1) -> -3
assert_eq!(poly![-4, 0, 1].eval(1), -3);

pub fn from_expr(
    expr: RcExpr,
    relative_to: Option<RcExpr>
) -> Result<(Self, Option<RcExpr>), String>
[src]

Transforms an expression into a polynomial relative to some term. If relative_to is not none, the constructed polynomial will be relative to that term. Otherwise, the polynomial will be relative to the term in the expression sequence.

If transformation is successful, the return value is a tuple of (polynomial, relative term). Transformation may fail for a number of reasons, including the expression containing a non-unique term, consisting of non-integer coefficients, or non-integer exponents.

Examples

This example is not tested
from_expr("x + 2x^2", None) == Some(poly![0, 1, 2], Some(Var("x")))
from_expr("5", None) == Some(poly![5], None)
from_expr("x + 2x^2", Some(Var("x"))) == Some(poly![0, 1, 2], Some(Var("x")))
from_expr("y + 2y^2", Some(Var("x"))) == None
from_expr("2.5x", None) == None
from_expr("x^{2.5}", None) == None
from_expr("x^{-2}", None) == None

pub fn to_expr(&self, relative_to: RcExpr, span: Span) -> RcExpr[src]

Converts a Poly polynomial, relative to some term, into an expression.

A artificial span the elements of the converted expression are derived from must be provided. In general, span should be the span of an expression previously converted the Poly this method is called on.

Examples

This example is not tested
poly![1, 2, 3].to_expr("x") == "3 * (x ^ 2) + 2 * x + 1"

pub fn to_string(&self, var: &str) -> String[src]

Prints the Poly as a polynomial string.

Trait Implementations

impl Clone for Poly[src]

impl Debug for Poly[src]

impl Default for Poly[src]

impl Eq for Poly[src]

impl<'_> From<&'_ Vec<isize>> for Poly[src]

impl From<Vec<isize>> for Poly[src]

impl PartialEq<Poly> for Poly[src]

impl StructuralEq for Poly[src]

impl StructuralPartialEq for Poly[src]

Auto Trait Implementations

impl RefUnwindSafe for Poly

impl Send for Poly

impl Sync for Poly

impl Unpin for Poly

impl UnwindSafe for Poly

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.