-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial.java
More file actions
90 lines (74 loc) · 2.29 KB
/
Copy pathPolynomial.java
File metadata and controls
90 lines (74 loc) · 2.29 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public class Polynomial {
double[] coefficients;
int[] expo;
public Polynomial() {
coefficients = new double[1];
expo = new int[1];
coefficients[0] = 0;
expo[0] = 0;
}
public Polynomial(double[] nums, int[] expos) {
coefficients = new double[nums.length];
expo = new int[expos.length];
for (int i=0; i<nums.length; i++)
{
coefficients[i] = nums[i];
}
for (int i=0; i<expos.length; i++)
{
expo[i] = expos[i];
}
}
public Polynomial add(Polynomial poly) {
int maxlen_co = Math.max(this.coefficients.length, poly.coefficients.length);
double[] res_co = new double[maxlen_co];
for (int i=0; i<this.coefficients.length; i++) {
res_co[i] += this.coefficients[i];
}
for (int i=0; i<poly.coefficients.length; i++) {
res_co[i] += poly.coefficients[i];
}
int maxlen_ex = Math.max(this.expo.length, poly.expo.length);
int[] res_ex = new int[maxlen_ex];
for (int i=0; i<this.expo.length; i++) {
res_ex[i] += this.expo[i];
}
for (int i=0; i<poly.expo.length; i++) {
res_ex[i] += poly.expo[i];
}
return new Polynomial(res_co, res_ex);
}
public double evaluate(double x) {
double res = 0;
for (int i=0; i<this.coefficients.length; i++ )
{
res += this.coefficients[i] * Math.pow(x, this.expo[i]);
}
return res;
}
public boolean hasRoot(double x) {
return evaluate(x) == 0;
}
public Polynomial multiply(Polynomial poly) {
double[] new_co;
int[] new_ex;
for(int i=0; i<this.coefficients.length; i++) {
for(int j=0; j<poly.coefficients.length; j++) {
}
}
return new Polynomial(new_co, new_ex);
}
public Polynomial(File file) {
String regex = "[+\\-]";
String[] item = file.split(regex);
int l = item.length;
coefficients = new double[l];
expo = new int[l];
for(int i=0; i<l; i++) {
coefficients[i] = parseDouble(item[i], 10);
expo[i] = parseInt(item[i], 10);
}
}
public saveToFile(String s) {
}
}