1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::ops::{Neg, Sub, SubAssign};

use crate::Polynomial;

impl Sub for Polynomial {
    type Output = Self;
    fn sub(mut self, rhs: Self) -> Self::Output {
        if self.is_zero() {
            rhs
        } else if rhs.is_zero() {
            self
        } else {
            for i in 0..=(std::cmp::max(self.degree(), rhs.degree())) {
                if i > self.degree() {
                    // if i > deg self, then by the loop condition, i <= deg rhs,
                    // so the indexing is valid
                    self.coeffs.push(-rhs.coeffs[i as usize]);
                } else if i > rhs.degree() {
                    // if i > deg rhs, then whatever higher coefficients remain in self
                    // are already correct
                    break;
                } else {
                    // otherwise just sub from self---this makes the subtraction be in-place
                    self.coeffs[i as usize] -= rhs.coeffs[i as usize];
                }
            }
            self.reduce();
            self
        }
    }
}

impl<'a> Sub<&'a Polynomial> for &'a Polynomial {
    type Output = Polynomial;
    fn sub(self, rhs: Self) -> Self::Output {
        let mut coeffs = Vec::new();
        for i in 0..=(std::cmp::max(self.degree(), rhs.degree())) {
            if i > self.degree() {
                coeffs.push(-rhs.coeffs[i as usize]);
            } else if i > rhs.degree() {
                coeffs.push(self.coeffs[i as usize]);
            } else {
                coeffs.push(self.coeffs[i as usize] - rhs.coeffs[i as usize]);
            }
        }
        let mut output = Polynomial::new(coeffs);
        output.reduce();
        output
    }
}

impl Sub<isize> for Polynomial {
    type Output = Self;
    fn sub(mut self, rhs: isize) -> Self::Output {
        if self.is_zero() {
            Polynomial::constant(-rhs)
        } else if rhs == 0 {
            self
        } else {
            self.coeffs[0] -= rhs;
            self.reduce();
            self
        }
    }
}

impl<'a> Sub<isize> for &'a Polynomial {
    type Output = Polynomial;
    fn sub(self, rhs: isize) -> Self::Output {
        if self.is_zero() {
            Polynomial::constant(-rhs)
        } else if rhs == 0 {
            self.clone()
        } else {
            let mut coeffs = self.coeffs.clone();
            coeffs[0] -= rhs;
            let mut output = Polynomial { coeffs };
            output.reduce();
            output
        }
    }
}

impl Neg for Polynomial {
    type Output = Self;

    fn neg(mut self) -> Self::Output {
        for i in self.coeffs.iter_mut() {
            *i = -*i;
        }
        self
    }
}

impl Neg for &Polynomial {
    type Output = Polynomial;

    fn neg(self) -> Self::Output {
        let mut coeffs = self.coeffs.clone();
        for i in coeffs.iter_mut() {
            *i = -*i;
        }
        let mut output = Polynomial { coeffs };
        output.reduce();
        output
    }
}

impl SubAssign for Polynomial {
    fn sub_assign(&mut self, rhs: Self) {
        if self.is_zero() {
            *self = -rhs;
        } else if !rhs.is_zero() {
            for i in 0..=(std::cmp::max(self.degree(), rhs.degree())) {
                if i > self.degree() {
                    // if i > deg self, then by the loop condition, i <= deg rhs,
                    // so the indexing is valid
                    self.coeffs.push(-rhs.coeffs[i as usize]);
                } else if i > rhs.degree() {
                    // if i > deg rhs, then whatever higher coefficients remain in self
                    // are already correct
                    break;
                } else {
                    // otherwise just sub from self---this makes the subtraction be in-place
                    self.coeffs[i as usize] -= rhs.coeffs[i as usize];
                }
            }
            self.reduce();
        }
    }
}

impl<'a> SubAssign<&'a Polynomial> for Polynomial {
    fn sub_assign(&mut self, rhs: &Self) {
        if self.is_zero() {
            *self = -rhs;
        } else if !rhs.is_zero() {
            for i in 0..=(std::cmp::max(self.degree(), rhs.degree())) {
                if i > self.degree() {
                    // if i > deg self, then by the loop condition, i <= deg rhs,
                    // so the indexing is valid
                    self.coeffs.push(-rhs.coeffs[i as usize]);
                } else if i > rhs.degree() {
                    // if i > deg rhs, then whatever higher coefficients remain in self
                    // are already correct
                    break;
                } else {
                    // otherwise just sub from self---this makes the subtraction be in-place
                    self.coeffs[i as usize] -= rhs.coeffs[i as usize];
                }
            }
            self.reduce();
        }
    }
}

impl SubAssign<isize> for Polynomial {
    fn sub_assign(&mut self, rhs: isize) {
        if self.is_zero() {
            self.coeffs.push(-rhs);
        } else if rhs != 0 {
            self.coeffs[0] -= rhs;
            self.reduce();
        }
    }
}