-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial.java
More file actions
55 lines (42 loc) · 1.22 KB
/
Polynomial.java
File metadata and controls
55 lines (42 loc) · 1.22 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
49
50
51
52
53
54
55
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;
}
else{
return false;
}
}
}