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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
mod iter;
use crate::number::Number;
use iter::Iter;
use std::cmp;
use std::cmp::Ordering;
use std::ops::{Add, Sub};
use std::iter::{Zip, Take};
#[derive(PartialEq, Debug)]
pub struct P(Vec<Number>);
impl P {
fn degree(&self) -> usize {
let mut i = self.0.len() - 1;
while self.0[i] == 0.0 && i > 0 {
i -= 1;
}
i
}
fn iter(&self) -> Iter {
Iter::new(self.0.clone())
}
fn zip(&self, other: &Self) -> Zip<Take<Iter>, Take<Iter>> {
let deg = cmp::max(self.degree(), other.degree()) + 1;
let a = self.iter().take(deg);
let b = other.iter().take(deg);
a.zip(b)
}
}
impl Add for &P {
type Output = P;
fn add(self, other: Self) -> P {
P(self.zip(other).map(|(x, y)| {x + y}).collect())
}
}
impl Sub for &P {
type Output = P;
fn sub(self, other: Self) -> P {
P(self.zip(other).map(|(x, y)| {x - y}).collect())
}
}
//****************************REFACTORING************************************
pub type Poly = Vec<Number>;
pub fn eval_poly(p: &Poly, t: Number) -> Number {
let mut r = 0.0;
for i in 0..p.len() {
r += p[i] * t.powi(i as i32);
}
r
}
pub fn sub(a: &Poly, b: &Poly) -> Poly {
let mut r = a.clone();
for i in 0..r.len() {
r[i] -= b[i];
}
r
}
pub fn skewed_sum(a: Poly, b: Poly) -> Poly {
let mut r = a.clone();
for i in 0..r.len() - 1 {
r[i + 1] += b[i];
}
r.push(b[b.len() - 1]);
r
}
fn is_zero(p: &Poly) -> bool {
for i in 0..p.len() {
if p[i] != 0.0 {
return false
}
}
true
}
fn degree(p: &Poly) -> usize {
let mut i = p.len() - 1;
while p[i] == 0.0 && i > 0 {
i -= 1;
}
i
}
fn prod(p: &Poly, n: Number) -> Poly {
let mut r = p.clone();
for i in 0..r.len() {
r[i] *= n;
}
r
}
fn shift(p: &Poly, amount: usize) -> Poly {
let mut r = vec![0.0; p.len()];
for i in 0..p.len() {
if i + amount < r.len() {
r[i + amount] = p[i];
}
}
r
}
fn rem(a: &Poly, b: &Poly) -> Poly {
let an = a[degree(a)];
let bn = b[degree(b)];
match degree(a).cmp(°ree(b)) {
Ordering::Equal => sub(a, &prod(b, an / bn)),
Ordering::Greater => sub(a, &prod(&shift(b, (degree(a) - degree(b)) as usize), an / bn)),
Ordering::Less => vec![0.0; b.len()]
}
}
pub fn gcd(a: &Poly, b: &Poly) -> Poly {
let c = rem(a, b);
if is_zero(&c) {
b.clone()
} else {
gcd(b, &c)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn add_test() {
let a = P(vec![1.0]);
let b = P(vec![0.0, 0.0, 0.0, 1.0]);
assert_eq!(&a + &b, P(vec![1.0, 0.0, 0.0, 1.0]));
}
#[test]
fn sub_test() {
let a = P(vec![1.0, 2.0, 3.0]);
let b = P(vec![1.0]);
assert_eq!(&a - &b, P(vec![0.0, 2.0, 3.0]));
}
#[test]
fn poly_is_zero() {
let p = vec![0.0; 5];
assert_eq!(is_zero(&p), true);
}
#[test]
fn poly_is_not_zero() {
let mut p = vec![0.0; 5];
p[2] = 8.0;
assert_eq!(is_zero(&p), false);
}
#[test]
fn degree_is_five() {
let mut p = vec![0.0; 6];
p[5] = 2.0;
assert_eq!(degree(&p), 5);
}
#[test]
fn degree_is_zero() {
let p = vec![0.0; 6];
assert_eq!(degree(&p), 0);
}
#[test]
fn prod_test() {
let p = vec![1.0, 2.0, 3.0, 4.0, 5.0];
assert_eq!(prod(&p, 2.0), vec![2.0, 4.0, 6.0, 8.0, 10.0]);
}
#[test]
fn shift_test() {
let p = vec![1.0, 2.0, 3.0, 0.0, 0.0];
assert_eq!(shift(&p, 2), vec![0.0, 0.0, 1.0, 2.0, 3.0]);
}
#[test]
fn rem_equal() {
let a = vec![6.0, 7.0, 1.0];
let b = vec![-6.0, -5.0, 1.0];
assert_eq!(rem(&a, &b), vec![12.0, 12.0, 0.0]);
}
#[test]
fn rem_greater() {
let a = vec![-6.0, -5.0, 1.0];
let b = vec![12.0, 12.0, 0.0];
assert_eq!(rem(&a, &b), vec![-6.0, -6.0, 0.0]);
}
#[test]
fn gcd_test() {
let a = vec![6.0, 7.0, 1.0];
let b = vec![-6.0, -5.0, 1.0];
assert_eq!(gcd(&a, &b), vec![-6.0, -6.0, 0.0]);
}
}
|