-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial.java
More file actions
48 lines (39 loc) · 1.41 KB
/
Copy pathPolynomial.java
File metadata and controls
48 lines (39 loc) · 1.41 KB
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
public class Polynomial {
double[] coefficients;
public Polynomial() {
this.coefficients = new double[0];
}
public Polynomial(double[] coefficients) {
this.coefficients = coefficients;
}
public Polynomial add(Polynomial other) {
int length = other.coefficients.length;
if(this.coefficients.length > length) {
length = this.coefficients.length;
}
double[] newPoly = new double[length];
for(int i = 0; i < this.coefficients.length; i++) {
newPoly[i] = this.coefficients[i];
}
for(int i = 0; i < other.coefficients.length; i++) {
newPoly[i] += other.coefficients[i];
}
return new Polynomial(newPoly);
}
public double evaluate(double x) {
double returnVAl = 0;
double scalingX = 1;
for(double item: this.coefficients) {
returnVAl += scalingX * item;
scalingX = scalingX * x;
}
return returnVAl;
}
public boolean hasRoot(double x) {
if(evaluate(x) == 0)
{
return true;
}
return false;
}
}