-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomial.java
153 lines (145 loc) · 4.61 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
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) {
if (poly1 == null && poly2 == null) {
return null;
}
if (poly1 == null) {
return new Node(poly2.term.coeff, poly2.term.degree, poly2.next);
}
if (poly2 == null) {
return new Node(poly1.term.coeff, poly1.term.degree, poly1.next);
}
else if (poly1.term.degree == poly2.term.degree) {
if (poly1.term.coeff + poly2.term.coeff == 0) {
return add(poly1.next, poly2.next);
}
else {
Node a = new Node(poly1.term.coeff + poly2.term.coeff, poly1.term.degree, add(poly1.next, poly2.next));
return a;
}
}
else if (poly1.term.degree < poly2.term.degree) {
Node a = new Node(poly1.term.coeff, poly1.term.degree, add(poly1.next, poly2));
return a;
}
else if (poly1.term.degree > poly2.term.degree) {
Node a = new Node(poly2.term.coeff, poly2.term.degree, add(poly1, poly2.next));
return a;
}
else {
System.out.println("ERROR");
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 a = null;
while (poly1 != null) {
a = add(a, multiplyHelper(poly1, poly2));
poly1 = poly1.next;
}
return a;
}
private static Node multiplyHelper(Node poly1, Node poly2) {
Node a = null;
if (poly2 != null) {
a = new Node (poly1.term.coeff * poly2.term.coeff, poly1.term.degree + poly2.term.degree, multiplyHelper(poly1, poly2.next));
}
return a;
}
/**
* 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 a = 0;
if (poly != null) {
a = (float) (poly.term.coeff * Math.pow(x,poly.term.degree) + evaluate(poly.next, x));
}
return a;
}
/**
* 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;
}
}