-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial.java
More file actions
40 lines (40 loc) · 756 Bytes
/
Copy pathPolynomial.java
File metadata and controls
40 lines (40 loc) · 756 Bytes
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
public class Polynomial {
double[] a;
public Polynomial() {
a=new double[1];
}
public Polynomial(double[] b) {
int b_le=b.length;
a=new double[b_le];
for(int i=0;i<b_le;i++){
a[i]=b[i];
}
}
public Polynomial add(Polynomial b){
int b_le=b.a.length,a_le=a.length;
Polynomial mx,mn;
if(a_le>=b_le){
mx=new Polynomial(a);
mn=new Polynomial(b.a);
}
else{
mx=new Polynomial(b.a);
mn=new Polynomial(a);
}
for(int i=0;i<mn.a.length;i++)
mx.a[i]+=mn.a[i];
return mx;
}
public double evaluate(double x){
int a_le=a.length;
double res=0;
for(int i=0;i<a_le;++i){
//System.out.println(a[i]);
res+=a[i]*Math.pow(x,i);
}
return res;
}
public boolean hasRoot(double x){
return evaluate(x)==0.0;
}
}