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
use std::ops::{Add, AddAssign};
use crate::Polynomial;
impl Add for Polynomial {
type Output = Self;
fn add(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 add to self---this makes the addition be in-place
self.coeffs[i as usize] += rhs.coeffs[i as usize];
}
}
self.reduce();
self
}
}
}
impl<'a> Add<&'a Polynomial> for &'a Polynomial {
type Output = Polynomial;
fn add(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 Add<isize> for Polynomial {
type Output = Self;
fn add(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> Add<isize> for &'a Polynomial {
type Output = Polynomial;
fn add(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 AddAssign for Polynomial {
fn add_assign(&mut self, rhs: Self) {
if self.is_zero() {
self.coeffs = rhs.coeffs;
} 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 add to self---this makes the addition be in-place
self.coeffs[i as usize] += rhs.coeffs[i as usize];
}
}
self.reduce();
}
}
}
impl<'a> AddAssign<&'a Polynomial> for Polynomial {
fn add_assign(&mut self, rhs: &Self) {
if self.is_zero() {
self.coeffs = rhs.coeffs.clone();
} 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 add to self---this makes the addition be in-place
self.coeffs[i as usize] += rhs.coeffs[i as usize];
}
}
self.reduce();
}
}
}
impl AddAssign<isize> for Polynomial {
fn add_assign(&mut self, rhs: isize) {
if self.is_zero() {
self.coeffs.push(rhs);
} else if rhs != 0 {
self.coeffs[0] += rhs;
self.reduce();
}
}
}