-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomial.java
47 lines (38 loc) · 1.17 KB
/
Polynomial.java
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
public class Polynomial{
double[] poly;
public Polynomial() {
poly = new double[1];
}
public Polynomial(double[] arr) {
poly = new double[arr.length];
for (int i = 0; i < arr.length; i++) {
poly[i] = arr[i];
}
}
public Polynomial add(Polynomial temp) {
int minlen = Math.min(temp.poly.length, poly.length);
int maxlen = Math.max(temp.poly.length, poly.length);
Polynomial other = new Polynomial();
other.poly = new double[maxlen];
for (int i = 0; i < minlen; i++) {
other.poly[i] = poly[i] + temp.poly[i];
}
for (int i = minlen; i < maxlen; i++) {
if (poly.length > temp.poly.length) {
other.poly[i] = poly[i];
}
else other.poly[i] = temp.poly[i];
}
return other;
}
public double evaluate(double x) {
double ret = 0;
for (int i = 0; i < poly.length; i++) {
ret = ret + (poly[i]*(Math.pow(x, i)));
}
return ret;
}
public boolean hasRoot(int root) {
return (evaluate(root) == 0);
}
}