Polynomial

Struct Polynomial 

Source
pub struct Polynomial<T> {
    pub data: Vec<T>,
}

Fields§

§data: Vec<T>

Implementations§

Source§

impl<T> Polynomial<T>

Source

pub fn from_vec(data: Vec<T>) -> Self

Examples found in repository?
crates/competitive/src/math/polynomial.rs (line 22)
21        fn zero() -> Self {
22            Self::from_vec(Vec::new())
23        }
24    }
25    impl<T: Zero + One> One for Polynomial<T> {
26        fn one() -> Self {
27            Self::from_vec(vec![Zero::zero(), One::one()])
28        }
29    }
30    impl<T: Clone + Zero + Add<Output = T> + Mul<Output = T>> Polynomial<T> {
31        pub fn assign(&self, x: T) -> T {
32            let mut res = Zero::zero();
33            for c in self.data.iter().rev().cloned() {
34                res = res * x.clone() + c;
35            }
36            res
37        }
38    }
39    impl<T> Index<usize> for Polynomial<T> {
40        type Output = T;
41        fn index(&self, index: usize) -> &Self::Output {
42            &self.data[index]
43        }
44    }
45    impl<T> IndexMut<usize> for Polynomial<T> {
46        fn index_mut(&mut self, index: usize) -> &mut Self::Output {
47            &mut self.data[index]
48        }
49    }
50    impl<T: Copy + Add<Output = T>> Add<&Polynomial<T>> for &Polynomial<T> {
51        type Output = Polynomial<T>;
52        fn add(self, rhs: &Polynomial<T>) -> Self::Output {
53            let (x, y) = if self.length() < rhs.length() {
54                (rhs, self)
55            } else {
56                (self, rhs)
57            };
58            let mut x = x.clone();
59            for j in 0..y.length() {
60                x[j] = x[j] + y[j];
61            }
62            x
63        }
64    }
65    impl<T: Copy + Sub<Output = T>> Sub<&Polynomial<T>> for &Polynomial<T> {
66        type Output = Polynomial<T>;
67        fn sub(self, rhs: &Polynomial<T>) -> Self::Output {
68            let (x, y) = if self.length() < rhs.length() {
69                (rhs, self)
70            } else {
71                (self, rhs)
72            };
73            let mut x = x.clone();
74            for j in 0..y.length() {
75                x[j] = x[j] - y[j];
76            }
77            x
78        }
79    }
80    impl<T: Copy + Zero + Add<Output = T> + Mul<Output = T>> Mul<&Polynomial<T>> for &Polynomial<T> {
81        type Output = Polynomial<T>;
82        fn mul(self, rhs: &Polynomial<T>) -> Self::Output {
83            let mut res =
84                Polynomial::from_vec(vec![Zero::zero(); self.length() + rhs.length() - 1]);
85            for i in 0..self.length() {
86                for j in 0..rhs.length() {
87                    res[i + j] = res[i + j] + self[i] * rhs[j];
88                }
89            }
90            res
91        }
92    }
93    impl<T: Copy + Zero + Sub<Output = T> + Mul<Output = T> + Div<Output = T>> Div<&Polynomial<T>>
94        for &Polynomial<T>
95    {
96        type Output = Polynomial<T>;
97        fn div(self, rhs: &Polynomial<T>) -> Self::Output {
98            let mut x = self.clone();
99            let mut res = Polynomial::from_vec(vec![]);
100            for i in (rhs.length() - 1..x.length()).rev() {
101                let t = x[i] / rhs[rhs.length() - 1];
102                res.data.push(t);
103                for j in 0..rhs.length() {
104                    x[i - j] = x[i - j] - t * rhs[rhs.length() - 1 - j];
105                }
106            }
107            res.data.reverse();
108            res
109        }
Source

pub fn length(&self) -> usize

Examples found in repository?
crates/competitive/src/math/polynomial.rs (line 53)
52        fn add(self, rhs: &Polynomial<T>) -> Self::Output {
53            let (x, y) = if self.length() < rhs.length() {
54                (rhs, self)
55            } else {
56                (self, rhs)
57            };
58            let mut x = x.clone();
59            for j in 0..y.length() {
60                x[j] = x[j] + y[j];
61            }
62            x
63        }
64    }
65    impl<T: Copy + Sub<Output = T>> Sub<&Polynomial<T>> for &Polynomial<T> {
66        type Output = Polynomial<T>;
67        fn sub(self, rhs: &Polynomial<T>) -> Self::Output {
68            let (x, y) = if self.length() < rhs.length() {
69                (rhs, self)
70            } else {
71                (self, rhs)
72            };
73            let mut x = x.clone();
74            for j in 0..y.length() {
75                x[j] = x[j] - y[j];
76            }
77            x
78        }
79    }
80    impl<T: Copy + Zero + Add<Output = T> + Mul<Output = T>> Mul<&Polynomial<T>> for &Polynomial<T> {
81        type Output = Polynomial<T>;
82        fn mul(self, rhs: &Polynomial<T>) -> Self::Output {
83            let mut res =
84                Polynomial::from_vec(vec![Zero::zero(); self.length() + rhs.length() - 1]);
85            for i in 0..self.length() {
86                for j in 0..rhs.length() {
87                    res[i + j] = res[i + j] + self[i] * rhs[j];
88                }
89            }
90            res
91        }
92    }
93    impl<T: Copy + Zero + Sub<Output = T> + Mul<Output = T> + Div<Output = T>> Div<&Polynomial<T>>
94        for &Polynomial<T>
95    {
96        type Output = Polynomial<T>;
97        fn div(self, rhs: &Polynomial<T>) -> Self::Output {
98            let mut x = self.clone();
99            let mut res = Polynomial::from_vec(vec![]);
100            for i in (rhs.length() - 1..x.length()).rev() {
101                let t = x[i] / rhs[rhs.length() - 1];
102                res.data.push(t);
103                for j in 0..rhs.length() {
104                    x[i - j] = x[i - j] - t * rhs[rhs.length() - 1 - j];
105                }
106            }
107            res.data.reverse();
108            res
109        }
110    }
111    impl<T: Copy + Zero + Sub<Output = T> + Mul<Output = T> + Div<Output = T>> Rem<&Polynomial<T>>
112        for &Polynomial<T>
113    {
114        type Output = Polynomial<T>;
115        fn rem(self, rhs: &Polynomial<T>) -> Self::Output {
116            let mut x = self.clone();
117            for i in (rhs.length() - 1..x.length()).rev() {
118                let t = x[i] / rhs[rhs.length() - 1];
119                for j in 0..rhs.length() {
120                    x[i - j] = x[i - j] - t * rhs[rhs.length() - 1 - j];
121                }
122            }
123            x.data.truncate(rhs.length() - 1);
124            x
125        }
Source§

impl<T: Clone + Zero + Add<Output = T> + Mul<Output = T>> Polynomial<T>

Source

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

Source§

impl<T: Copy + Zero + One + Add<Output = T> + Mul<Output = T>> Polynomial<T>

Source

pub fn pow(&self, n: usize) -> Self

Trait Implementations§

Source§

impl<T: Copy + Add<Output = T>> Add<&Polynomial<T>> for &Polynomial<T>

Source§

type Output = Polynomial<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Polynomial<T>) -> Self::Output

Performs the + operation. Read more
Source§

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

Source§

fn clone(&self) -> Polynomial<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> Debug for Polynomial<T>

Source§

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

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

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

Source§

fn default() -> Polynomial<T>

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

impl<T: Copy + Zero + Sub<Output = T> + Mul<Output = T> + Div<Output = T>> Div<&Polynomial<T>> for &Polynomial<T>

Source§

type Output = Polynomial<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &Polynomial<T>) -> Self::Output

Performs the / operation. Read more
Source§

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

Source§

type Output = T

The returned type after indexing.
Source§

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

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

impl<T> IndexMut<usize> for Polynomial<T>

Source§

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

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

impl<T: Copy + Zero + Add<Output = T> + Mul<Output = T>> Mul<&Polynomial<T>> for &Polynomial<T>

Source§

type Output = Polynomial<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Polynomial<T>) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: Zero + One> One for Polynomial<T>

Source§

fn one() -> Self

Source§

fn is_one(&self) -> bool
where Self: PartialEq,

Source§

fn set_one(&mut self)

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: Copy + Zero + Sub<Output = T> + Mul<Output = T> + Div<Output = T>> Rem<&Polynomial<T>> for &Polynomial<T>

Source§

type Output = Polynomial<T>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: &Polynomial<T>) -> Self::Output

Performs the % operation. Read more
Source§

impl<T: Copy + Sub<Output = T>> Sub<&Polynomial<T>> for &Polynomial<T>

Source§

type Output = Polynomial<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Polynomial<T>) -> Self::Output

Performs the - operation. Read more
Source§

impl<T> Zero for Polynomial<T>

Source§

fn zero() -> Self

Source§

fn is_zero(&self) -> bool
where Self: PartialEq,

Source§

fn set_zero(&mut self)

Source§

impl<T: Eq> Eq for Polynomial<T>

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, 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.