This repository has been archived by the owner on Aug 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomial.java
392 lines (362 loc) · 9.34 KB
/
Polynomial.java
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
package poly;
import java.io.IOException;
import java.util.Scanner;
/**
* This class implements evaluate, add and multiply for polynomials.
*
* @author runb-cs112
*
*/
public class Polynomial {
/**
* Reads a polynomial from an input stream (file or keyboard). The storage format
* of the polynomial is:
* <pre>
* <coeff> <degree>
* <coeff> <degree>
* ...
* <coeff> <degree>
* </pre>
* with the guarantee that degrees will be in descending order. For example:
* <pre>
* 4 5
* -2 3
* 2 1
* 3 0
* </pre>
* which represents the polynomial:
* <pre>
* 4*x^5 - 2*x^3 + 2*x + 3
* </pre>
*
* @param sc Scanner from which a polynomial is to be read
* @throws IOException If there is any input error in reading the polynomial
* @return The polynomial linked list (front node) constructed from coefficients and
* degrees read from scanner
*/
public static Node read(Scanner sc)
throws IOException {
Node poly = null;
while (sc.hasNextLine()) {
Scanner scLine = new Scanner(sc.nextLine());
poly = new Node(scLine.nextFloat(), scLine.nextInt(), poly);
scLine.close();
}
return poly;
}
/**
* Returns the sum of two polynomials - DOES NOT change either of the input polynomials.
* The returned polynomial MUST have all new nodes. In other words, none of the nodes
* of the input polynomials can be in the result.
*
* @param poly1 First input polynomial (front of polynomial linked list)
* @param poly2 Second input polynomial (front of polynomial linked list
* @return A new polynomial which is the sum of the input polynomials - the returned node
* is the front of the result polynomial
*/
public static Node add(Node poly1, Node poly2) {
Node ptrn1= poly1;
Node ptrn2 = poly2;
Node first=null;
Node last=null;
while(ptrn1 != null && ptrn2 != null)
{
float sum=0;
if(ptrn1.term.degree > ptrn2.term.degree)
{
Node result= new Node(ptrn2.term.coeff, ptrn2.term.degree, null);
if(last != null)
{
last.next=result;
}
else
{
first=result;
}
last=result;
ptrn2=ptrn2.next;
}
else if(ptrn1.term.degree < ptrn2.term.degree)
{
Node result= new Node(ptrn1.term.coeff, ptrn1.term.degree, null);
if(last != null)
{
last.next=result;
}
else
{
first=result;
}
last=result;
ptrn1=ptrn1.next;
}
else if(ptrn1.term.degree == ptrn2.term.degree)
{
sum= ptrn1.term.coeff + ptrn2.term.coeff;
if (sum == 0) {
ptrn1=ptrn1.next;
ptrn2=ptrn2.next;
continue;
}
Node result= new Node(sum, ptrn1.term.degree, null);
if(last != null)
{
last.next=result;
}
else
{
first=result;
}
last=result;
ptrn1=ptrn1.next;
ptrn2=ptrn2.next;
}
}
while(ptrn1 == null && ptrn2 != null)
{
Node result= new Node(ptrn2.term.coeff, ptrn2.term.degree, null);
if(last != null)
{
last.next=result;
}
else
{
first=result;
}
last=result;
ptrn2=ptrn2.next;
}
while(ptrn1 != null && ptrn2 == null)
{
Node result= new Node(ptrn1.term.coeff, ptrn1.term.degree, null);
if(last != null)
{
last.next=result;
}
else
{
first=result;
}
last=result;
ptrn1=ptrn1.next;
}
return first;
}
/*
Node ptr_final= null;
Node ptr1 = poly1;
Node ptr2 = poly2;
if(ptr1 != null || ptr2 != null) {
if(ptr1 == null && ptr2 != null) {
return ptr2;
}
if(ptr2 == null && ptr1 != null) {
return ptr1;
}
if(ptr1.term.degree > ptr2.term.degree) {
ptr_final = new Node(ptr1.term.coeff,ptr1.term.degree,null);
ptr1=ptr1.next;
}
else if(ptr2.term.degree > ptr1.term.degree) {
ptr_final = new Node(ptr2.term.coeff,ptr2.term.degree,null);
ptr2=ptr2.next;
}
else {
float coeff_sum= ptr1.term.coeff + ptr2.term.coeff;
ptr_final = new Node(coeff_sum,ptr2.term.degree,null);
ptr1 = ptr1.next;
ptr2= ptr2.next;
}
while(ptr1 != null && ptr2 != null) {
if(ptr1.term.degree > ptr2.term.degree) {
Node demo = new Node(ptr1.term.coeff, ptr1.term.degree , ptr_final);
ptr_final = demo;
ptr1 = ptr1.next;
}
else if(ptr2.term.degree > ptr1.term.degree) {
Node demo = new Node(ptr2.term.coeff, ptr2.term.degree , ptr_final);
ptr_final = demo;
ptr2=ptr2.next;
}
else {
float coeff_sum= ptr1.term.coeff + ptr2.term.coeff;
Node n = new Node(coeff_sum,ptr1.term.degree,ptr_final);
ptr_final = n;
ptr1=ptr1.next;
ptr2=ptr2.next;
}
}
while(ptr1 != null) {
Node demo = new Node(ptr1.term.coeff, ptr1.term.degree , ptr_final);
ptr_final = demo;
ptr1 = ptr1.next;
}
while(ptr2 != null) {
Node demo = new Node(ptr2.term.coeff, ptr2.term.degree , ptr_final);
ptr_final = demo;
ptr2 = ptr2.next;
}
return ptr_final;
}
/** COMPLETE THIS METHOD **/
// FOLLOWING LINE IS A PLACEHOLDER TO MAKE THIS METHOD COMPILE
// CHANGE IT AS NEEDED FOR YOUR IMPLEMENTATION
// return null;
//}
/**
* Returns the product of two polynomials - DOES NOT change either of the input polynomials.
* The returned polynomial MUST have all new nodes. In other words, none of the nodes
* of the input polynomials can be in the result.
*
* @param poly1 First input polynomial (front of polynomial linked list)
* @param poly2 Second input polynomial (front of polynomial linked list)
* @return A new polynomial which is the product of the input polynomials - the returned node
* is the front of the result polynomial
*/
public static Node multiply(Node poly1, Node poly2) {
Node first_term=null, last_term= null;
for(Node ptr1= poly1; ptr1 != null; ptr1=ptr1.next)
{
for(Node ptr2= poly2; ptr2 != null; ptr2= ptr2.next)
{
float result= (ptr1.term.coeff)*(ptr2.term.coeff);
int degree= (ptr1.term.degree)+(ptr2.term.degree);
Node resultLL= new Node(result, degree, null);
if(last_term != null)
{
last_term.next=resultLL;
}
else
{
first_term=resultLL;
}
last_term=resultLL;
}
}
Node finalfront=null;
Node finallast = null;
if (first_term == null || last_term == null) {
return null;
}
int highestOrder= last_term.term.degree;
for(int d=0; d <= highestOrder; d++)
{
float sum= 0;
for(Node ptr= first_term; ptr!= null; ptr=ptr.next)
{
if(ptr.term.degree == d)
{
sum+= ptr.term.coeff;
}
}
if (sum == 0) continue;
Node tempNode = new Node(sum, d, null);
if(finallast != null)
{
finallast.next = tempNode;
}
else
{
finalfront = tempNode;
}
finallast = tempNode;
}
return finalfront;
}
/*
Node ptr1= poly1;
Node ptr2 = poly2;
if(ptr1==null || ptr2==null ) {
return null;
}
while(ptr1.next != null) {
ptr1 = ptr1.next;
}
int poly1_highestdegree = ptr1.term.degree;
while(ptr2.next != null) {
ptr2 = ptr2.next;
}
int poly2_highestdegree = ptr2.term.degree;
int sizeOfPoly = poly1_highestdegree + poly2_highestdegree;
Node result = new Node(0,0,null);
Node head = result;
Node tail = result;
for(int i = 0; i< sizeOfPoly; i++) {
Node n = new Node(0,i,head);
head = n;
}
Node n = new Node(0,sizeOfPoly,head);
head = n;
ptr1 = poly1;
ptr2 = poly2;
while(ptr1 != null) {
ptr2 = poly2;
while(ptr2 != null) {
int degree = ptr1.term.degree + ptr2.term.degree;
float coeff = ptr1.term.coeff*ptr2.term.coeff;
Node pointer = head;
for(int j=0; j<sizeOfPoly-degree; j++) {
pointer = pointer.next;
}
pointer.term.coeff = coeff + pointer.term.coeff;
ptr2 = ptr2.next;
}
ptr1 = ptr1.next;
}
Node checker = head;
Node checker2 = tail;
while(checker.term.coeff == 0) {
checker = checker.next;
head = head.next;
}
while(checker2.term.coeff == 0) {
while(checker.next != checker2) {
checker= checker.next;
}
checker.next=null;
checker2 = checker;
checker = head;
}
while(checker != checker2) {
while(checker.next.term.coeff == 0) {
checker.next = checker.next.next;
}
checker = checker.next;
}
return head;
}
/**
* Evaluates a polynomial at a given value.
*
* @param poly Polynomial (front of linked list) to be evaluated
* @param x Value at which evaluation is to be done
* @return Value of polynomial p at x
*/
public static float evaluate(Node poly, float x) {
float result_term = 0;
Node front = poly;
while (front != null) {
double term = Math.pow(x,front.term.degree) * front.term.coeff;
result_term = (float) (result_term + term);
front = front.next;
}
return result_term;
}
/**
* Returns string representation of a polynomial
*
* @param poly Polynomial (front of linked list)
* @return String representation, in descending order of degrees
*/
public static String toString(Node poly) {
if (poly == null) {
return "0";
}
String retval = poly.term.toString();
for (Node current = poly.next ; current != null ;
current = current.next) {
retval = current.term.toString() + " + " + retval;
}
return retval;
}
}