-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynom.java
More file actions
203 lines (175 loc) · 5.92 KB
/
Polynom.java
File metadata and controls
203 lines (175 loc) · 5.92 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import java.util.Scanner;
public class Polynom{
private int COEF_NUMBER = 20;
private final double precision = 0.00001;
private double[] coef;
private int size;
public Polynom(){
coef = new double[COEF_NUMBER];
size = 0;
}
public Polynom(Polynom p){
if(p == null) throw new IllegalArgumentException("The given polynom is null");
coef = new double[COEF_NUMBER];
for(int i=0; i < COEF_NUMBER; i++)
coef[i] = p.coef(i);
updateSize();
}
private boolean equals(double x, float y){
return (x < y + precision) && (x > y - precision);
}
public void readPolynom(){
Scanner sc = new Scanner(System.in);
int i = 0;
while(sc.hasNext()){
coef[i] = sc.nextDouble();
i++;
}
sc.close();
}
public int degree(){
updateSize();
return size -1;
}
public void addMonom(double a, int deg){
if(a!=0&° < 0 && deg >= COEF_NUMBER)
throw new IllegalArgumentException("degree of a monom must be between 0 and " + COEF_NUMBER);
if(coef[deg] == 0)
size++;
coef[deg] = coef[deg] + a;
}
public void reset(){
for(int i=0; i<COEF_NUMBER; i++) coef[i] = 0.0;
}
public double coef(int deg){
if(deg < 0 && deg > COEF_NUMBER)
throw new IllegalArgumentException("degree of the polynom are between 0 and " + COEF_NUMBER);
return coef[deg];
}
@Override
public String toString(){
if(size == 0)
return "0";
String res ="";
for(int i=0; i< COEF_NUMBER; i++){
if(coef[i] != 0){
if(coef[i] > 0){
if(i == 0)
res += " + " + coef[i];
else if (i == 1)
res += " + " + coef[i] + "x";
else
res += " + " + coef[i] + "x^"+i;
}else{//coef[i] < 0
if(i == 0)
res += " - " + (-coef[i]);
else if (i == 1)
res += " - " + (-coef[i]) + "x";
else
res += " - " + (-coef[i]) + "x^"+i;
}
}
}
return res;
}
public String toTab(){
String res = "[" + coef[0];
for(int i = 1; i < COEF_NUMBER-1 ; i++)
res += ", " + coef[i];
res += ", " + coef[COEF_NUMBER-1] + "]";
return res;
}
private void updateSize(){
size = 0;
for(int i = 0; i < COEF_NUMBER ; i++)
if(coef[i] != 0)
size++;
}
public Polynom plus(Polynom other){
Polynom res = new Polynom();
for(int i = 0 ; i < COEF_NUMBER ; i++)
res.addMonom(this.coef(i) + other.coef(i), i);
res.updateSize();
return res;
}
public Polynom minus(Polynom other){
Polynom res = new Polynom();
for(int i = 0 ; i < COEF_NUMBER ; i++)
res.addMonom(this.coef(i) - other.coef(i), i);
res.updateSize();
return res;
}
public boolean equals(Polynom other){
for(int i = 0; i < COEF_NUMBER ; i++)
if(coef[i] != other.coef(i))
return false;
return true;
}
public Polynom mult(Polynom other){
Polynom res = new Polynom();
if(size == 0 || other.equals(res))//0 exception
return res;
for(int i = 0; i < COEF_NUMBER ; i++)
for(int j = 0; j < COEF_NUMBER ; j++)
if(i+j < COEF_NUMBER)
res.addMonom(coef[i]*other.coef(j), j+i);
other.updateSize();
return res;
}
public Polynom derivate(){
Polynom res = new Polynom();
for(int i = 1; i < COEF_NUMBER ; i++)
res.addMonom(i*coef[i], i-1);
return res;
}
public Polynom divisionRest(Polynom p, Polynom q){
if(q.degree() == 0 && q.coef(0) == 0)
throw new IllegalArgumentException("the division cannot be operated with a null polynom");
else if(p.degree() == 0 && p.coef(0) == 0)
return new Polynom(); // the 0 polynom
Polynom quotient = euclidian_division(p, q);
Polynom res = new Polynom();
res = p.minus(q.mult(quotient));
return res;
}
public double getGreaterCoef(){
for(int i=COEF_NUMBER-1;i>=0;i--)
if(coef[i] != 0)
return coef[i];
return 0;
}
public static Polynom pgcd(Polynom p, Polynom q){
if(p.degree() == 0 && q.degree() == 0)
return new Polynom(); // the 0 polynom
if(p.degree() == 0)
return new Polynom(q);
if(q.degree() == 0)
return new Polynom(p);
if(p.degree() >= q.degree())
return pgcd(Polynom.euclidian_rest(p, q), q);
else// (q.degree() < p.degree())
return pgcd(Polynom.euclidian_rest(q, p), p);
}
public static Polynom euclidian_division(Polynom p, Polynom q) {
if(q.degree() == 0 && q.coef(0) == 0)
throw new IllegalArgumentException("the divisor cannot be null !");
Polynom _p = new Polynom(p);
Polynom _q = new Polynom(q);
Polynom res = new Polynom();
Polynom tmp_res = new Polynom();
while(_p.degree() >= _q.degree()){
//find the coefficient of the biggest degree in p and q
double p_d = _p.getGreaterCoef(), q_d = _q.getGreaterCoef();
tmp_res.addMonom(p_d / (double) q_d, _p.degree() - _q.degree());
// substract
_p = _p.minus(tmp_res.mult(_q));
// save the founded value
res = res.plus(tmp_res);
tmp_res.reset();
}
return res;
}
public static Polynom euclidian_rest(Polynom p, Polynom q){
return p.minus(q.mult(Polynom.euclidian_division(p, q)));
}
}