forked from tamzid-chowdhury/Java-Polynomial-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDensePolynomial.java
More file actions
569 lines (477 loc) · 18.3 KB
/
DensePolynomial.java
File metadata and controls
569 lines (477 loc) · 18.3 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
import java.util.ArrayList;
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeMap;
public class DensePolynomial implements Polynomial {
private int[] coefficients;
private String input;
/**
* Creates an instance of a DensePolynomial object from the canonical string representation
* Precondition: String must not contain any negative numbers; Coefficients and degrees must both be integers
* Postcondition: Returns instance of DensePolynomial with coefficients stored in an array where indices represent degrees
* @param s the canonical string representation of a polynomial
* @throws IllegalArgumentException if a coefficient or degree are not integers or we are given a string that is not canonical
*/
public DensePolynomial(String s) {
this.input = s;
if(wellFormed() == false) {
throw new IllegalArgumentException("The input string given is invalid");
}
input = input.replace(" - ", " + -");
String[] terms = input.split("\\+");
ArrayList<Integer> coefficients = new ArrayList<Integer>();
ArrayList<Integer> degrees = new ArrayList<Integer>();
for(String term: terms) {
term = term.strip();
if(term.contains("x^")) {
if(term.charAt(0) == 'x') {
term = term.replace("x^", "");
int degree = Integer.parseInt(term);
coefficients.add(1);
degrees.add(degree);
}
else {
String[] nums = term.split("x\\^");
int coefficient = Integer.parseInt(nums[0]);
int degree = Integer.parseInt(nums[1]);
coefficients.add(coefficient);
degrees.add(degree);
}
}
else if(term.contains("x")) {
if(term.equals("x")) {
coefficients.add(1);
degrees.add(1);
}
else if(term.equals("-x")){
coefficients.add(-1);
degrees.add(1);
}
else {
String[] nums = term.split("x");
int coefficient = Integer.parseInt(nums[0]);
coefficients.add(coefficient);
degrees.add(1);
}
}
else {
int coefficient = Integer.parseInt(term);
coefficients.add(coefficient);
degrees.add(0);
}
}
this.coefficients = new int[degrees.get(0)+1];
for(int i = 0; i < degrees.size(); i++) {
this.coefficients[degrees.get(i)] = coefficients.get(i);
}
}
/**
* Creates an instance of a DensePolynomial object from a coefficient array and using the toString method for the input
* Precondition: None
* Postcondition: Returns instance of DensePolynomial with coefficients represented in an array with indices representing degrees
* @param coefficients the integer array
*
*/
private DensePolynomial(int[] coefficients) {
this.coefficients = coefficients;
if(this.isZero()){
this.coefficients = new int[]{0};
}
this.input = this.toString();
}
/**
* Returns the degree of the polynomial.
* Precondition: None
* Postcondition: Returns the degree of the polynomial as an integer
* @return the largest exponent with a non-zero coefficient. If all terms have zero exponents, it returns 0.
*/
@Override
public int degree() {
int degree = 0;
for(int i = 0; i < coefficients.length; i++) {
if(coefficients[i] != 0) {
degree = i;
}
}
return degree;
}
/**
* Returns the coefficient corresponding to the given exponent. Returns 0 if there is no term with that exponent
* in the polynomial.
* Precondition: d is not negative
* Postcondition: Returns the coefficient of the corresponding exponent
*
* @param d the exponent whose coefficient is returned.
* @return the coefficient of the term of whose exponent is d.
* @throws IllegalArgumentException if exponent is a negative number
*/
@Override
public int getCoefficient(int d) {
int polDegree = degree();
if(d < 0) {
throw new IllegalArgumentException("We cannot have negative exponents for a dense polynomial");
}
if(d > polDegree) {
return 0;
}
return coefficients[d];
}
/**
* Checks the polynomial to see if it represents the zero constant
* Precondition: None
* Postcondition: returns true if polynomial represents the zero constant, if not then it returns false
* @return true if the polynomial represents the zero constant
*/
@Override
public boolean isZero() {
for(int i = 0; i < coefficients.length; i++) {
if(coefficients[i] != 0) {
return false;
}
}
return true;
}
/**
* Returns a polynomial by adding the parameter to the current instance. Neither the current instance nor the
* parameter are modified.
* Precondition: q is not null && SparsePolynomial cannot have negative exponents
* Postcondition: returns a polynomial representing the sum of this + q
* @param q the non-null polynomial to add to <code>this</code>
* @return <code>this + </code>q
* @throws NullPointerException if q is null
* @throws IllegalArgumentException if DensePolynomial + SparsePolynomial with Negative Exponents
*/
@Override
public Polynomial add(Polynomial q) {
if(q == null) {
throw new NullPointerException("Argument is null");
}
DensePolynomial d;
if(q instanceof SparsePolynomial) {
SparsePolynomial s = (SparsePolynomial) q;
TreeMap<Integer,Integer> map = s.getMap();
NavigableSet<Integer> degrees = map.navigableKeySet();
for(Integer degree: degrees) {
if (degree < 0) {
throw new IllegalArgumentException("DensePolynomial cannot have negative exponents");
}
}
d = convertToDense(s);
}
else {
d = (DensePolynomial) q;
}
int[] p1 = (this.degree() > d.degree()) ? new int[this.degree()+ 1] : new int[d.degree()+1];
for(int i = 0; i < this.coefficients.length; i++) {
p1[i] = this.coefficients[i];
}
int[] p2 = (this.degree() > d.degree()) ? new int[this.degree()+ 1] : new int[d.degree()+1];
for(int i = 0; i < d.coefficients.length; i++) {
p2[i] = d.coefficients[i];
}
int[] sum = (this.degree() > d.degree()) ? new int[this.degree()+ 1] : new int[d.degree()+1];
for(int i = 0; i < sum.length; i++) {
sum[i] = p1[i] + p2[i];
}
return new DensePolynomial(sum);
}
/**
* Returns a polynomial by multiplying the parameter to the current instance. Neither the current instance nor the
* parameter are modified.
*
* Precondition: q is not null && SparsePolynomial cannot have negative exponents
* Postcondition: returns a polynomial representing the product of this * q
*
* @param q the non-null polynomial to multiply to <code>this</code>
* @return <code>this * </code>q
* @throws NullPointerException if q is null
* @throws IllegalArgumentException if DensePolynomial * SparcePolynomial with Negative Exponents
*/
@Override
public Polynomial multiply(Polynomial q) {
if(q == null) {
throw new NullPointerException("Argument is null");
}
DensePolynomial d;
if(q instanceof SparsePolynomial) {
SparsePolynomial s = (SparsePolynomial) q;
TreeMap<Integer,Integer> map = s.getMap();
NavigableSet<Integer> degrees = map.navigableKeySet();
for(Integer degree: degrees) {
if (degree < 0) {
throw new IllegalArgumentException("DensePolynomial cannot have negative exponents");
}
}
d = convertToDense(s);
}
else {
d = (DensePolynomial) q;
}
int m = this.coefficients.length;
int n = d.coefficients.length;
int[] prod = new int[m + n - 1];
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
prod[i+j] += this.coefficients[i] * d.coefficients[j];
}
}
return new DensePolynomial(prod);
}
/**
* Returns a polynomial by subtracting the parameter to the current instance. Neither the current instance nor the
* parameter are modified.
*
* Precondition: q is not null && SparsePolynomial cannot have negative exponents
* Postcondition: returns a polynomial representing the difference of this - q
*
* @param q the non-null polynomial to subtract to <code>this</code>
* @return <code>this - </code>q
* @throws NullPointerException if q is null
* @throws IllegalArgumentException if DensePolynomial - SparcePolynomial with Negative Exponents
*/
@Override
public Polynomial subtract(Polynomial q) {
if(q == null) {
throw new NullPointerException("Argument is null");
}
DensePolynomial d;
if(q instanceof SparsePolynomial) {
SparsePolynomial s = (SparsePolynomial) q;
TreeMap<Integer,Integer> map = s.getMap();
NavigableSet<Integer> degrees = map.navigableKeySet();
for(Integer degree: degrees) {
if (degree < 0) {
throw new IllegalArgumentException("DensePolynomial cannot have negative exponents");
}
}
d = convertToDense(s);
}
else {
d = (DensePolynomial) q;
}
int[] p1 = (this.degree() > d.degree()) ? new int[this.degree()+ 1] : new int[d.degree()+1];
for(int i = 0; i < this.coefficients.length; i++) {
p1[i] = this.coefficients[i];
}
int[] p2 = (this.degree() > d.degree()) ? new int[this.degree()+ 1] : new int[d.degree()+1];
for(int i = 0; i < d.coefficients.length; i++) {
p2[i] = d.coefficients[i];
}
int[] diff = (this.degree() > d.degree()) ? new int[this.degree()+ 1] : new int[d.degree()+1];
for(int i = 0; i < diff.length; i++) {
diff[i] = p1[i] - p2[i];
}
return new DensePolynomial(diff);
}
/**
* Returns a polynomial by negating the current instance. The current instance is not modified.
* Precondition: None
* Postcondition: Returns the negated polynomial of this
* @return -this
*/
@Override
public Polynomial minus() {
int[] negatedCoeff = this.coefficients;
for(int i = 0; i < negatedCoeff.length; i++) {
negatedCoeff[i] = -(negatedCoeff[i]);
}
return new DensePolynomial(negatedCoeff);
}
/**
* Checks the input string to make sure that we are given a string that contains a valid canonical string as well
* as makes sure that invariant holds true that both the coefficients and degrees are all integers
* Precondition: None
* PostCondition: returns true if class invariant holds and canonical string can be formed else returns false
*
* @return {@literal true} if the class invariant holds and we are given a canonical string or {@literal false} if the class invarient is not
* true and we are not given a canonical string
*/
@Override
public boolean wellFormed() {
if(isValid(input) == false)
return false;
if(input.equals("0")) {
return true;
}
input = input.replace(" - ", " + -");
String[] terms = input.split("\\+");
ArrayList<Integer> coefficients = new ArrayList<Integer>();
ArrayList<Integer> degrees = new ArrayList<Integer>();
for(String term: terms) {
term = term.strip();
if(term.contains("x^")) {
if(term.charAt(0) == 'x') {
term = term.replace("x^", "");
int degree = Integer.parseInt(term);
coefficients.add(1);
degrees.add(degree);
}
else {
String[] nums = term.split("x\\^");
int coefficient = Integer.parseInt(nums[0]);
int degree = Integer.parseInt(nums[1]);
coefficients.add(coefficient);
degrees.add(degree);
}
}
else if(term.contains("x")) {
if(term.equals("x")) {
coefficients.add(1);
degrees.add(1);
}
else if(term.equals("-x")){
coefficients.add(-1);
degrees.add(1);
}
else {
String[] nums = term.split("x");
int coefficient = Integer.parseInt(nums[0]);
coefficients.add(coefficient);
degrees.add(1);
}
}
else {
int coefficient = Integer.parseInt(term);
coefficients.add(coefficient);
degrees.add(0);
}
}
//if coefficients contains a 0 then we are not in canonical form
if(coefficients.contains(0)) {
return false;
}
//check if the degrees are in descending order
for(int i = 0; i < degrees.size()-1; i++) {
if(degrees.get(i) < degrees.get(i+1)) {
return false;
}
}
//check if degrees are negative for dense polynomial
for(int i = 0; i < degrees.size(); i++) {
if(degrees.get(i) < 0) {
return false;
}
}
return true;
}
/**
* Checks the input string to make sure that we are not given a string that contains any characters that would not result
* in a valid polynomial expression. Any characters besides digits, plus, minus, the letter x, ^, or a space would result in
* an invalid expression
* Precondition: None
* Postcondition: Returns false if String would not represent a valid polynomial expression else returns true
*
* @param s the canonical string representation of a polynomial
*/
private boolean isValid(String s) {
char[] chSearch = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '^', 'x', ' '};
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
boolean isPresent = false;
for (int j = 0; j < chSearch.length; j++) {
if (chSearch[j] == ch) {
isPresent = true;
}
}
if(isPresent == false) {
return false;
}
}
return true;
}
/**
* Method takes the polynomial object and returns a string representation in canonical form
* Precondition: None
* Postcondition: Returns string representation of polynomial
* @return String representation of the polynomial in canonical form
*/
@Override
public String toString() {
String s = "";
ArrayList<String> terms = new ArrayList<String>();
if(this.isZero()){
return "0";
}
for(int i=0; i < coefficients.length; i++) {
if(coefficients[i] != 0) {
String term = "";
if(i == 0) {
term += coefficients[i];
}
else if(i == 1) {
if(coefficients[i] == 1) {
term += "x";
}
else {
term += coefficients[i] + "x";
}
}
else {
if(coefficients[i] == 1) {
term += "x^" + i;
}
else {
term += coefficients[i] + "x^" + i;
}
}
terms.add(term);
}
}
for(int i=terms.size()-1; i>0; i--) {
s += terms.get(i) + " + ";
}
s += terms.get(0);
return s;
}
/**
* Equals method overrode from the object class checks if the polynomials have the same coefficients and exponents
*
* Precondition: o must be instance of DensePolynomial
* Postcondition: return true if this and o represent same polynomial
*
* @return true if the two polynomial objects represent the same polynomial
* @throws IllegalArgumentException if object is not instance of DensePolynomial
*/
@Override
public boolean equals(Object o) {
if(!(o instanceof DensePolynomial)) {
throw new IllegalArgumentException("Object is not a DensePolynomial");
}
DensePolynomial other = (DensePolynomial) o;
return Arrays.equals(this.coefficients, other.coefficients);
}
/**
* Getter method which returns the coefficients array of the polynomial object
*
* Precondition: None
* Postcondition: returns coefficients array
*
* @return the coefficients array
*
*/
public int[] getCoefficients() {
return coefficients;
}
/**
* Converts an instance of a sparsepolynomial object into a densepolynomial object
*
* Preconditions: None
* Postcondition: returns a DensePolynomial which has the same polynomial representation
* as the original SparsePolynomial
*
* @return SparePolynomial representation of our instance
*
*/
private DensePolynomial convertToDense(SparsePolynomial s) {
TreeMap<Integer,Integer> map = s.getMap();
NavigableSet<Integer> degrees = map.navigableKeySet();
for(Integer degree: degrees) {
assert degree >= 0;
}
int[] coefficients = new int[s.degree()+1];
for(Integer degree: degrees) {
coefficients[degree] = map.get(degree);
}
return new DensePolynomial(coefficients);
}
}