-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial.java
More file actions
56 lines (54 loc) · 1.34 KB
/
Copy pathPolynomial.java
File metadata and controls
56 lines (54 loc) · 1.34 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
56
import java.lang.Math;
public class Polynomial {
double[] coefficient;
public Polynomial(){
coefficient= new double[1];
this.coefficient[0] = 0;
}
public Polynomial(double array[]){
coefficient = array;
// coefficient = new double[array.length];
// for(int i=0;i<array.length;i++) {
// this.coefficient[i]= array[i];
// }
}
public Polynomial add(Polynomial p) {
double[] dummy = new double[Math.max(p.coefficient.length, this.coefficient.length)];
// for(int j=0;j<dummy.length;j++) {
// dummy[j]=0;
// }
Polynomial n= new Polynomial(dummy);
for(int i=0; i<Math.max(p.coefficient.length, this.coefficient.length); i++) {
if(i<p.coefficient.length && i<this.coefficient.length)
{
dummy[i] = p.coefficient[i] + this.coefficient[i];
}
else if(i>=p.coefficient.length && i<this.coefficient.length)
{
dummy[i] = this.coefficient[i];
}
else if(i<p.coefficient.length && i>=this.coefficient.length)
{
dummy[i]=p.coefficient[i];
}
}
//Polynomial privat = new Polynomial(dummy);
return n;
}
public double evaluate(double value) {
double v=0;
for(int i=0;i<this.coefficient.length;i++) {
v=v+this.coefficient[i]*Math.pow(value,i);
}
return v;
}
public boolean hasRoot(double value) {
double r=evaluate(value);
if(r==0.0) {
return true;
}
else {
return false;
}
}
}