```  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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
```
```use crate::Polynomial;

use num::Zero;
use smallvec::smallvec;

where
T: for<'r> AddAssign<&'r T> + Zero,
{
type Output = Polynomial<T>;

fn add(self, rhs: Polynomial<T>) -> Polynomial<T> {
let mut ret = self;
ret += &rhs;
ret
}
}

where
T: for<'r> AddAssign<&'r T> + Zero,
{
type Output = Polynomial<T>;

fn add(self, rhs: &Polynomial<T>) -> Polynomial<T> {
let mut ret = self;
ret += rhs;
ret
}
}

where
T: for<'r> AddAssign<&'r T> + Zero,
{
type Output = Polynomial<T>;

fn add(self, rhs: Polynomial<T>) -> Polynomial<T> {
let mut ret = rhs;
ret += self;
ret
}
}

where
T: for<'r> AddAssign<&'r T> + Zero + Clone,
{
type Output = Polynomial<T>;

fn add(self, rhs: &Polynomial<T>) -> Polynomial<T> {
let mut ret = self.clone();
ret += rhs;
ret
}
}

where
T: for<'r> AddAssign<&'r T> + Zero,
{
fn add_assign(&mut self, rhs: Polynomial<T>) {
}
}

where
T: for<'r> AddAssign<&'r T> + Zero,
{
fn add_assign(&mut self, rhs: &Polynomial<T>) {
if self.rev_coeffs.len() < rhs.rev_coeffs.len() {
// the following line causes clippy to emit a spurious warning (using - in an AddAssign-implementation)
#[allow(clippy::suspicious_op_assign_impl)]
let num_add = rhs.rev_coeffs.len() - self.rev_coeffs.len();
self.rev_coeffs.push(T::zero());
}
//self.rev_coeffs.resize_with(rhs.rev_coeffs.len(), T::zero); // SmallVec doesn't have resize_with :(
}
self.rev_coeffs
.iter_mut()
.zip(rhs.rev_coeffs.iter())
.for_each(|(l, r)| *l += r);
self.fixup_coefficients();
}
}

impl<T> Zero for Polynomial<T>
where
T: for<'r> AddAssign<&'r T> + Zero,
{
fn zero() -> Self {
Self {
rev_coeffs: smallvec![T::zero()],
}
}

fn is_zero(&self) -> bool {
self.order() == 0 && self.rev_coeffs.first().unwrap().is_zero()
}
}

#[cfg(test)]
mod tests {
use crate::*;

#[test]
let a = Polynomial::new(coefficients![1f32, 3.0, 3.0, 0.0]);
let b = Polynomial::new(coefficients![1f32, 0.0, 1.0]);

let expected = coefficients![1f32, 4.0, 3.0, 1.0];

// Polynomial + Polynomial
let c = a.clone() + b.clone();
assert_eq!(c.order(), 3);
assert_eq!(c.coeffs(), expected);

// Polynomial + Polynomial (swapped)
let c = b.clone() + a.clone();
assert_eq!(c.order(), 3);
assert_eq!(c.coeffs(), expected);

// Polynomial + &Polynomial
let c = a.clone() + &b;
assert_eq!(c.order(), 3);
assert_eq!(c.coeffs(), expected);

// &Polynomial + Polynomial
let c = &a + b.clone();
assert_eq!(c.order(), 3);
assert_eq!(c.coeffs(), expected);

// &Polynomial + &Polynomial
let c = &a + &b;
assert_eq!(c.order(), 3);
assert_eq!(c.coeffs(), expected);

let b = Polynomial::new(coefficients![-1f32, -3.0, -3.0, 1.0]);
let c = a + b;
assert_eq!(c.order(), 0);
assert_eq!(c.coeffs(), coefficients![1f32]);
}

#[test]
let a = Polynomial::new(coefficients![1f32, 3.0, 3.0, 0.0]);
let b = Polynomial::new(coefficients![1f32, 0.0, 1.0]);

let expected = coefficients![1f32, 4.0, 3.0, 1.0];

// Polynomial += Polynomial
let mut c = a.clone();
c += b.clone();
assert_eq!(c.order(), 3);
assert_eq!(c.coeffs(), expected);

// Polynomial += &Polynomial
let mut c = a.clone();
c += &b;
assert_eq!(c.order(), 3);
assert_eq!(c.coeffs(), expected);

// Polynomial += Polynomial (swapped)
let mut c = b.clone();
c += a.clone();
assert_eq!(c.order(), 3);
assert_eq!(c.coeffs(), expected);

// Polynomial += &Polynomial (swapped)
let mut c = b.clone();
c += &a;
assert_eq!(c.order(), 3);
assert_eq!(c.coeffs(), expected);