Struct Polynomial

Source
pub struct Polynomial<T> { /* private fields */ }
Expand description

Polynomial representation

Implementations§

Source§

impl<T: MonomialValue> Polynomial<T>

Source

pub fn new(mono_vec: Vec<Monomial<T>>) -> Polynomial<T>

Constructs a new Polynomial<T>

§Examples
let mono_vec: Vec<Monomial<i32>> = vec![
    Monomial::new(2, 2),
    Monomial::new(-1, 1),
    Monomial::new(10, 0),
];

let poly: Polynomial<i32> = Polynomial::new(mono_vec);
Source

pub fn max_exp(&self) -> Monomial<T>

Returns the monomial with the max exponent

§Examples
let poly: Polynomial<i32> = Polynomial::try_from("x^2 - 5x - 100").unwrap();

assert_eq!(poly.max_exp().get_exp(), 2);
Source

pub fn len(&self) -> usize

Returns the number of Monomials in the Polynomial, also referred to as its ‘length’

§Examples
let poly: Polynomial<i32> = Polynomial::default();

assert_eq!(poly.len(), 0);
Source

pub fn equation_type(&self) -> EquationType

Returns the equation type

§Examples
let poly: Polynomial<i32> = Polynomial::try_from("5x - 100").unwrap();

assert_eq!(poly.equation_type(), EquationType::Linear);
Source

pub fn push(&mut self, mono: Monomial<T>)

Add a monomial

§Examples
let mut poly: Polynomial<i32> = Polynomial::try_from("5x - 100").unwrap();
let mono: Monomial<i32> = Monomial::try_from("2x^2").unwrap();

poly.push(mono);

assert_eq!(format!("{poly}"), "2x^2 + 5x - 100");
Source

pub fn find_by_exp(&self, exp: i32) -> Monomial<T>

Find monomial in a polynomial by the exponent if don’t find the monomial returns Monomial::default()

§Examples
let poly: Polynomial<i32> = Polynomial::try_from("2x^2 + 5x - 100").unwrap();

assert_eq!(poly.find_by_exp(1), Monomial::new(5, 1));
assert_eq!(poly.find_by_exp(10), Monomial::default());
Source

pub fn div_mono(self, rhs: Monomial<T>) -> Self

Returns a new polynomial as result of dividing a monomial

§Examples
let poly: Polynomial<i32> = Polynomial::try_from("10x - 10").unwrap();
let mono: Monomial<i32> = Monomial::try_from("2").unwrap();

let result = poly.div_mono(mono);

assert_eq!(format!("{result}"), "5x - 5");
Source

pub fn mul_mono(self, rhs: Monomial<T>) -> Self

Returns a new polynomial as result of multiplying a monomial

§Examples
let poly: Polynomial<i32> = Polynomial::try_from("10x - 10").unwrap();
let mono: Monomial<i32> = Monomial::try_from("2").unwrap();

let result = poly.mul_mono(mono);

assert_eq!(format!("{result}"), "20x - 20");
Source

pub fn roots(&self) -> Option<Vec<T>>

Returns an Option containing the roots of the equation This function uses different strategies based on EquationType

§Examples
let poly: Polynomial<i32> = Polynomial::try_from("x - 9").unwrap();

assert_eq!(poly.roots(), Some(vec![9]));

Trait Implementations§

Source§

impl<T: MonomialValue> Add for Polynomial<T>

Source§

type Output = Polynomial<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: Clone> Clone for Polynomial<T>

Source§

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

Returns a copy 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> Debug for Polynomial<T>

Source§

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

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

impl<T: MonomialValue> Default for Polynomial<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: MonomialValue> Display for Polynomial<T>

Source§

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

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

impl<T: MonomialValue> Div for Polynomial<T>

Source§

type Output = (Polynomial<T>, Polynomial<T>)

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl<T: MonomialValue> From<Vec<Monomial<T>>> for Polynomial<T>

Source§

fn from(value: Vec<Monomial<T>>) -> Self

Converts to this type from the input type.
Source§

impl<T: MonomialValue> Index<usize> for Polynomial<T>

Source§

type Output = Monomial<T>

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, T: MonomialValue> IntoIterator for &'a Polynomial<T>

Source§

type Item = &'a Monomial<T>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, Monomial<T>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: MonomialValue> IntoIterator for Polynomial<T>

Source§

type Item = Monomial<T>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<<Polynomial<T> as IntoIterator>::Item>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: MonomialValue> Mul for Polynomial<T>

Source§

type Output = Polynomial<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: MonomialValue> Neg for Polynomial<T>

Source§

type Output = Polynomial<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<T: PartialEq> PartialEq for Polynomial<T>

Source§

fn eq(&self, other: &Polynomial<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: MonomialValue> TryFrom<&str> for Polynomial<T>

Source§

type Error = &'static str

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

fn try_from(value: &str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T: MonomialValue> TryFrom<Vec<T>> for Polynomial<T>

Source§

type Error = &'static str

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

fn try_from(value: Vec<T>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T> StructuralPartialEq for Polynomial<T>

Auto Trait Implementations§

§

impl<T> Freeze for Polynomial<T>

§

impl<T> RefUnwindSafe for Polynomial<T>
where T: RefUnwindSafe,

§

impl<T> Send for Polynomial<T>
where T: Send,

§

impl<T> Sync for Polynomial<T>
where T: Sync,

§

impl<T> Unpin for Polynomial<T>
where T: Unpin,

§

impl<T> UnwindSafe for Polynomial<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> 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.