Repository navigation
Expand file tree
/
Copy pathPolynomial.java
More file actions
218 lines (189 loc) · 5.84 KB
/
Copy pathPolynomial.java
File metadata and controls
218 lines (189 loc) · 5.84 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
package Clm;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
/**
*
* @author mike
* holds data and data structure for polynomial
*/
public class Polynomial {
private LinkedList<Double> coeffs;
//creates polynomial
public Polynomial() {
Double[] c = {0.0, 0.0, 0.0};
this.coeffs = new LinkedList(Arrays.asList(c));
}
//creates polynomial from file name of type string
public Polynomial(String s) {
this.coeffs = parseString(s);
}
//gets coeffs from file and adds to coeffs linked list
private LinkedList parseString(String s) {
LinkedList polys = new LinkedList();
int index = 0;
if (!s.isEmpty()) {
try {
for (String co : s.split("\\s*,\\s*")) {
polys.add((Double.parseDouble(co)));
}
} catch (NumberFormatException e) {
System.out.println("Number Formating Error");
}
}
return polys;
}
//creates polynomial from array of coeffs
public Polynomial(Double[] coeffs) {
this.coeffs = new LinkedList(Arrays.asList(coeffs));
}
//creates new polynomial with Linked List of Coeffs
public Polynomial(LinkedList<Double> coeffs) {
this.coeffs = new LinkedList<>();
boolean leadingZero = false;
for (double ci : coeffs) {
if (ci == 0.0 && leadingZero) {
continue;
}
if (ci != 0.0) {
leadingZero = false;
}
this.coeffs.add(ci);
}
if (this.coeffs.isEmpty()) {
this.coeffs.add(0.0);
}
}
//adds Double[] of coeffs to linkedList
public void addArrayToPoly(Double[] d) {
this.coeffs = new LinkedList(Arrays.asList(d));
}
//returns string
@Override
public String toString() {
return this.coeffs.toString() + " ";
}
//prints poly in increasing order
public String printInc() {
String result = "";
int i = 0;
double d;
if ((coeffs.size() - 1) == 0) {
return coeffs.get(0).toString();
}
Iterator<Double> iterator = coeffs.descendingIterator();
while (iterator.hasNext()) {
d = iterator.next();
if (d == 0.0) {
i++;
continue;
} else if (d < 0) {
result += " - " + -d;
} else {
result += " + " + d;
}
if (i != 0) {
result += "x^" + i;
}
i++;
}
return result;
}
//prints polynomial in descending order
public String printDescending() {
String result = "";
for (int i = 0; i < coeffs.size(); i++) {
double d = coeffs.get(i);
if (d == 0) {
continue;
}
if (getPower(i) != 0) {
result += d + "x^" + getPower(i) + " + ";
} else {
result += d;
}
}
return result;
}
//multiplies this polynomial with @param polynomial
public Polynomial multiply(Polynomial x) {
int thisSize = this.coeffs.size();
int xSize = x.coeffs.size();
Double[] result = new Double[((xSize + thisSize) - 1)];
for (int i = 0; i < (thisSize + xSize) - 1; i++) {
result[i] = 0.0;
}
if (thisSize >= xSize) {
for (int i = 0; i < thisSize; i++) {
for (int j = 0; j < xSize; j++) {
result[i + j] += (this.coeffs.get(i) * x.coeffs.get(j));
}
}
} else if (xSize > thisSize) {
for (int i = 0; i < xSize; i++) {
for (int j = 0; j < thisSize; j++) {
result[i + j] += (this.coeffs.get(j) * x.coeffs.get(i));
}
}
}
Polynomial multPoly = new Polynomial(result);
return multPoly;
}
//gets power of polynomial based on linkedList index
public int getPower(int i) {
return coeffs.size() - i - 1;
}
//multiplies this polynomial by a double for subtracting
public Polynomial multiply(double d) {
LinkedList<Double> newCo = new LinkedList<>();
for (double c : coeffs) {
newCo.add(d * c);
}
return new Polynomial(newCo);
}
//adds this polynomial to @param polynomial
public Polynomial add(Polynomial p) {
LinkedList<Double> newCo = new LinkedList<>();
Iterator<Double> pIterator = p.coeffs.descendingIterator();
Iterator<Double> thisIterator = this.coeffs.descendingIterator();
double ci, di;
while (pIterator.hasNext() && thisIterator.hasNext()) {
newCo.addFirst(pIterator.next() + thisIterator.next());
}
while (pIterator.hasNext()) {
newCo.addFirst(pIterator.next());
}
while (thisIterator.hasNext()) {
newCo.addFirst(thisIterator.next());
}
return new Polynomial(newCo);
}
//subtracts
public Polynomial subtract(Polynomial p) {
return add(p.multiply(-1.0));
}
public String printAll() {
String polyStr = "";
int i = coeffs.size()-1;
for(double ci : coeffs) {
polyStr += ci;
if (i != 0 )
polyStr += "x^" + i + " + ";
i--;
}
return polyStr;
}
//prints coeeffs in linkedList
public void printCoeffs() {
System.out.println(coeffs.toString());
}
public double evaluate(double a) {
int i = coeffs.size() - 1;
double result = 0.0;
for (double ci : coeffs) {
result += ci * Math.pow(a, (double) i);
i--;
}
return result;
}
}