-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolynomial.js
More file actions
339 lines (293 loc) · 8.86 KB
/
polynomial.js
File metadata and controls
339 lines (293 loc) · 8.86 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
const ComplexNumber = require('./complexNumber');
class Polynomial {
constructor(list) {
this.coefficients = list.map((elem) => {
if (typeof(elem) === 'number') {
return new ComplexNumber(elem, 0);
}
if (Array.isArray(elem)){
return new ComplexNumber(elem[0], elem[1]);
}
if (elem instanceof ComplexNumber) {
return elem;
}
throw new Error('Polynomial terms must be a Number, an Array, or a ComplexNumber');
});
this.degree = this.coefficients
.reduce((acc, coefficient, index) => {
return coefficient.textVersion() === new ComplexNumber(0, 0).textVersion() ?
acc :
index;
}, -1);
this.coefficients = this.coefficients.slice(0, this.degree + 1);
}
textVersion() {
return this.coefficients.reduce((prev, current, index) => {
let res;
let coef;
if (current.textVersion() === '1' && index !== 0) {
coef = '';
} else if (current.imaginary === 0 || current.real === 0) {
coef = current.textVersion();
} else {
coef = '(' + current.textVersion() + ')';
}
if (current.textVersion() === '0') {
res = prev;
} else {
if (index === 0) {
res = prev + coef;
} else if (index === 1) {
res = prev + coef + 'x';
} else {
res = prev + coef + 'x^' + index;
}
if (index < this.degree) {
res += ' + ';
}
}
return res;
}, '');
}
push(coefficient) {
this.coefficients.push(coefficient);
this.degree = this.coefficients.length - 1;
}
display() {
console.log(this.textVersion());
return this;
}
plus(arr) {
let summand = arr;
if (!(arr instanceof Polynomial)) {
summand = new Polynomial(summand);
}
const result = [];
const add = (short, long) => {
long.forEach((elem, index) => {
if (short[index]) {
result[index] = elem.plus(short[index]);
} else {
result[index] = elem;
}
});
}
if (this.degree >= summand.degree) {
add (summand.coefficients, this.coefficients);
} else {
add (this.coefficients, summand.coefficients);
}
return new Polynomial(result);
}
times(array) {
let arr;
if (array instanceof Polynomial) {
arr = array;
} else {
arr = new Polynomial(array);
}
const result = [];
this.coefficients.forEach((elem, ind) => {
arr.coefficients.forEach((element, index) => {
if (result[ind + index]) {
result[ind + index] = result[ind + index].plus(elem.times(element));
} else {
result[ind + index] = elem.times(element);
}
});
});
return new Polynomial(result);
};
division(divisorArg) {
let divisor = divisorArg;
if (!(divisor instanceof Polynomial)) {
// convert array to a polynomial
divisor = new Polynomial(divisorArg);
}
// quotient is an empty polynomial to begin
let quotient = new Polynomial([]);
// the remainder is the numberator to begin
let remainder = this;
let quotDeg;
let coef;
let n;
if (this.degree >= divisor.degree) {
// quotDeg is the smallest nonzero term
quotDeg = this.degree - divisor.degree;
for (n = 0; n < quotDeg + 1; n++) {
// place 0 in every place up to degree
quotient.push(new ComplexNumber(0, 0));
}
}
// repeat the process until the remainder is smaller than the divisor
n = 0;
while (remainder.degree >= divisor.degree) {
// quotDeg is the smallest nonzero term
quotDeg = remainder.degree - divisor.degree;
// coef is the leading coefficient of the remainder divided by the leading coefficient of the denominator
coef = remainder.coefficients[remainder.degree].times(divisor.coefficients[divisor.degree].pow(-1));
// place the new term and make quotient into a polynomial
quotient.coefficients[quotDeg] = coef;
quotient = new Polynomial(quotient.coefficients);
// remainder = numerator - quotient * divisor
remainder = this.plus(quotient.times(divisor).times([[-1, 0]]));
// clean up the remainder if its leading term wasn't removed
remainder = new Polynomial(remainder.coefficients.slice(0,this.coefficients.length - n + 1));
n++;
}
return {quotient, remainder};
};
divide(arr) {
return this.division(arr).quotient;
}
remainder(arr) {
return this.division(arr).remainder;
}
evaluate(number) {
let numb;
if (typeof(number) === 'number') {
numb = new ComplexNumber(number, 0);
} else {
numb = number;
}
return this.coefficients.reduce(function (prev, current, ind) {
return prev.plus(current.times(numb.pow(ind)));
}, new ComplexNumber(0, 0));
}
factor() {
const factors = [];
let newFactor;
let remainder;
// The first value of factors is the coefficient of the whole
if (this.degree !== -1) {
factors.push(this.coefficients[this.degree]);
}
// Polynomials of degree 0 have no factors
if (this.degree <= 0) {
console.log('there are no factors');
return this;
}
if (this.degree === 1) {
// A polynomial of the form a + bx has a factor of the form -a / b
newFactor = this.coefficients[0].times(this.coefficients[1].pow(-1)).times([-1, 0]);
factors.push(newFactor);
return new Factored(factors);
}
if (this.degree === 2) {
newFactor = quadraticEquation(this.coefficients[2], this.coefficients[1], this.coefficients[0]);
// remainder is (c + bx + ax^2)/(x - z)
remainder = this.divide([newFactor.times([-1, 0]), 1]);
// thing is an array containing the factors of the remainder
const thing = remainder.factor();
thing.factors.push(newFactor);
return thing;
}
if (this.degree === 3) {
newFactor = cubicEquation(this);
// remainder is (ax^3 + bx^2 + cx + d)/(x - newFactor)
remainder = this.divide([newFactor.times([-1, 0]), 1]);
// thing is an array containing the factors of the remainder
const thing = remainder.factor();
thing.factors.push(newFactor);
return thing;
}
return this;
};
expand() {
return this;
};
}
function quadraticEquation (a, b, c) {
// A quadratic c + bx + ax^2 will have two roots
// The value A is -b / (2a)
const A = b.times(a.times([-2,0]).pow(-1));
// The value C is -c / a
const C = c.times(a.pow(-1)).times([-1, 0]);
// The new factor is z = A + sqrt(A^2 + C)
return A.plus(A.pow(2).plus(C).pow(0.5));
}
function cubicEquation (poly) {
// Divide everything by the leading coefficient x^3+Ax^2+Bx+C=(ax^3+bx^2+cx+d)/a
const reducedPoly = poly.divide([poly.coefficients[3]]);
const A = reducedPoly.coefficients[2];
const B = reducedPoly.coefficients[1];
const C = reducedPoly.coefficients[0];
// Set thing = 1 / -3. This value comes up repeatedly.
const thing = new ComplexNumber(-3, 0).pow(-1);
// set x = t - A/3
const x = new Polynomial([A.times(thing),1]);
// write a new polynomial by replacing x with t - A/3. This new polynomial has no t^2 term.
// T=t^3+Mx+N
const T = x.times(x.times(x)).plus(x.times(x).times([A])).plus(x.times([B])).plus([C]);
const one = T.coefficients[3];
const M = T.coefficients[1];
const N = T.coefficients[0];
// Suppose that there are complex numbers u and v.
// Then (u + v)^3 = u^3 + 3u^2v + 3uv^2 + v^3
// (u + v)^3 = (3uv)(u + v) + u^3 + v^3
// (u + v)^3 + (-3uv)(u + v) + (-u^3 + -v^3) = 0
// If there exist numbers u and v such that M = -3uv and N = -u^3 + -v^3, then t = u + v will satisfy
// t^3 + Mt + N = 0.
// I will solve for u. First note that v = -M / (3u) and therefore N = -u^3 + M^3 / (3u)^3.
// This leads to (u^3)^2 + Nu^3 - (M / 3)^3 = 0. Therefore, u^3 is a solution to x^2 + Nx - (M / 3)^3 = 0.
const uCubed = quadraticEquation(one, N, M.times(thing).pow(3));
// Since M = -3uv, we know that v = M / (-3u)
function getV (number) {
return M.times(number.times([-3, 0]).pow(-1));
}
// Since t = u + v, this means that x = u + v - A / 3
// There are three values of u for a given uCubed.
let solution = new ComplexNumber(0, 0);
let n = -1;
let u;
let v;
while (poly.evaluate(solution).textVersion() !== '0' && n < 2) {
n++;
u = uCubed.pow(1/3).times(new ComplexNumber(-1, 0).pow(n * 2 / 3));
v = getV(u);
solution = u.plus(v).plus(A.times(thing));
}
return solution;
}
class Factored {
constructor(list) {
this.factors = list;
}
textVersion() {
let str = '';
this.factors.forEach((elem, index) => {
if (index === 0) {
if (elem.textVersion() !== '1') {
if(elem.real === 0 || elem.imaginary === 0) {
str += elem.textVersion();
} else {
str += '(' + elem.textVersion() + ')';
}
}
} else {
if (elem.textVersion() === '0') {
str += 'x';
} else if (elem.real > 0 && elem.imaginary === 0) {
str += '(x - ' + (elem.textVersion()) + ')';
} else {
str += '(x + ' + (elem.times([-1, 0]).textVersion()) + ')';
}
}
});
return str;
};
display() {
console.log(this.textVersion());
return this;
};
expand() {
return this.factors.reduce( function (prev, current, index) {
if (index === 0) {
return new Polynomial([current]);
} else {
return prev.times([current.times([-1, 0]), [1, 0]]);
}
}, new Polynomial([]));
};
}
module.exports = Polynomial;