-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolynomial.java
49 lines (42 loc) · 1.02 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
48
49
public class Polynomial {
public double[] coeff = new double[100];
public Polynomial() {
coeff[0] = 0.0;
}
public Polynomial(double[] arr) {
coeff = arr;
}
public Polynomial add(Polynomial b) {
// Adds two polynomials
double[] newq = new double[100];
double[] larger = b.coeff.clone();
double smaller[] = coeff;
int size = b.coeff.length;
if (size < coeff.length) {
size = coeff.length;
larger = coeff.clone();
smaller = b.coeff.clone();
}
for (int i = 0; i < size - 1; i++) {
newq[i] = larger[i];
}
for (int i = 0; i < size - 1; i++) {
newq[i] += smaller[i];
}
Polynomial c = new Polynomial(newq);
return c;
}
public double evaluate(double x) {
// Evaluates polynomial
double result = 0;
double temp = 0;
for (int i = 0; i < coeff.length - 1; i++) {
temp = Math.pow(x, i);
result += coeff[i] * temp;
}
return result;
}
public boolean hasRoot(double x) {
return evaluate(x) == 0;
}
}