This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 863
/
polynomials.js
314 lines (242 loc) · 9.71 KB
/
polynomials.js
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
$.extend(KhanUtil, {
Polynomial: function(minDegree, maxDegree, coefs, variable, name) {
var term = function(coef, vari, degree) {
// sort of a weird error behavior
if (typeof coef === "undefined" || coef === 0) {
return null;
}
if (degree === 0) {
return coef;
} else if (degree === 1) {
return ["*", coef, vari];
} else {
return ["*", coef, ["^", vari, degree]];
}
};
// inverse of term. Given an expression it returns the coef and degree.
// calculus needs this for hints
var extractFromExpr = function(expr) {
var coef, degree;
if (typeof expr === "number") {
coef = expr;
degree = 0;
} else if ($.isArray(expr) && !$.isArray(expr[2])) {
coef = expr[1];
degree = 1;
} else if ($.isArray(expr) && $.isArray(expr[2])) {
coef = expr[1];
degree = expr[2][2];
}
return {
coef: coef,
degree: degree
};
};
// These seem royally useless to me
if (maxDegree >= minDegree) {
this.minDegree = minDegree;
this.maxDegree = maxDegree;
} else {
this.minDegree = maxDegree;
this.maxDegree = minDegree;
}
this.coefs = coefs || KhanUtil.randCoefs(this.minDegree, this.maxDegree);
this.variable = (typeof variable !== "undefined") ? variable : "x";
this.name = name || "f";
this.findMaxDegree = function(coefs) {
if (!coefs) {
for (var i = this.maxDegree; i >= this.minDegree; i--) {
if (this.coefs[i] !== 0) {
return i;
}
}
} else {
for (var i = coefs.length - 1; i >= 0; i--) {
if (coefs[i] !== 0) {
return i;
}
}
return -1;
}
};
this.findMinDegree = function(coefs) {
if (!coefs) {
for (var i = this.minDegree; i <= this.maxDegree; i++) {
if (this.coefs[i] !== 0) {
return i;
}
}
} else {
for (var i = 0; i < coefs.length; i++) {
if (coefs[i] !== 0) {
return i;
}
}
return -1;
}
};
this.expr = function(vari) {
if (typeof vari === "undefined") {
vari = this.variable;
}
var expr = ["+"];
for (var i = this.maxDegree; i >= this.minDegree; i--) {
var theTerm = term(this.coefs[i], vari, i);
if (theTerm != null) {
expr.push(theTerm);
}
}
return expr;
};
this.getNumberOfTerms = function() {
// -1 as the first term in the expression for a polynomial is always a "+"
return this.expr().length - 1;
};
this.getCoefAndDegreeForTerm = function(termIndex) {
//returns the coef and degree for a particular term
var numberOfTerms = this.getNumberOfTerms();
//mod twice to always get positive
termIndex = ((termIndex % numberOfTerms) + numberOfTerms) % numberOfTerms;
//upshift by one due to "+" sign at the front of the expression
return extractFromExpr(this.expr()[termIndex + 1]);
};
this.text = function() {
return KhanUtil.expr(this.expr(this.variable));
};
this.toString = this.text;
this.hintEvalOf = function(val) {
return KhanUtil.expr(this.expr(val));
};
this.evalOf = function(val) {
return KhanUtil.expr(this.expr(val), true);
};
this.hint = function(val) {
var hints = [];
hints.push("<p><code>" + this.name + "(" + val + ") = " +
this.hintEvalOf(val) + "</code></p>");
hints.push("<p><code>" + this.name + "(" + val + ") = " +
this.evalOf(val) + "</code></p>");
return hints;
};
// Adds two polynomials
// It assumes the second polynomial's variable is the same as the first polynomial's
// Does not change the polynomials, returns the result
this.add = function(polynomial) {
var coefs = [];
var minDegree = Math.min(this.minDegree, polynomial.minDegree);
var maxDegree = Math.max(this.maxDegree, polynomial.maxDegree);
for (var i = minDegree; i <= maxDegree; i++) {
var value = 0;
value += i <= this.maxDegree ? this.coefs[i] : 0;
value += i <= polynomial.maxDegree ? polynomial.coefs[i] : 0;
coefs[i] = value;
}
return new KhanUtil.Polynomial(minDegree, maxDegree, coefs, this.variable);
};
// Subtracts polynomial from this
// It assumes the second polynomial's variable is the same as the first polynomial's
// Does not change the polynomials, returns the result
this.subtract = function(polynomial) {
return this.add(polynomial.multiply(-1));
}
// Multiply a polynomial by a number or other polynomial
this.multiply = function(value) {
var coefs = [];
if (typeof value === "number") {
for (var i = 0; i < this.coefs.length; i++) {
coefs[i] = this.coefs[i] * value;
}
return new KhanUtil.Polynomial(this.minDegree, this.maxDegree, coefs, this.variable);
// Assume if it's not a number it's a polynomial
} else {
for (var i = this.minDegree; i <= this.maxDegree; i++) {
if (this.coefs[i] === 0) {
continue;
}
for (var j = value.minDegree; j <= value.maxDegree; j++) {
if (value.coefs[j] === 0) {
continue;
}
var coef = this.coefs[i] * value.coefs[j];
if (coefs[i + j] === undefined) {
coefs[i + j] = coef;
} else {
coefs[i + j] += coef;
}
}
}
// Fill in any missing values of coefs with 0s
for (var i = 0; i < coefs.length; i++) {
if (coefs[i] === undefined) {
coefs[i] = 0;
}
}
return new KhanUtil.Polynomial(Math.min(this.minDegree, value.minDegree), coefs.length, coefs, this.variable);
}
}
return this;
},
CompositePolynomial: function(minDegree, maxDegree, coefs, variable, name,
composed, composedCoef) {
var base = new KhanUtil.Polynomial(
minDegree, maxDegree, coefs, variable, name);
$.extend(this, base);
if (!composedCoef) {
composedCoef = KhanUtil.randRangeNonZero(-5, 5);
}
var composedFunc = composed.name + "(" + this.variable + ")";
var tackOn = function(expr, tack) {
expr = $.merge([], expr);
if (expr[0] === "+") {
expr.push(tack);
} else {
expr = ["+", expr, tack];
}
return expr;
};
this.expr = function(vari) {
return tackOn(base.expr(vari), ["*", composedCoef, composedFunc]);
};
this.text = function() {
return KhanUtil.expr(this.expr(this.variable));
};
this.toString = this.text;
this.hintEvalOf = function(val, evalInner) {
if (evalInner) {
return KhanUtil.expr(tackOn(base.expr(val),
["*", composedCoef, composed.evalOf(val)]));
} else {
return KhanUtil.expr(tackOn(base.expr(val),
["*", composedCoef, composed.name + "(" + val + ")"]));
}
};
this.evalOf = function(val) {
return base.evalOf(val) + composedCoef * composed.evalOf(val);
};
this.hint = function(val) {
var hints = [];
hints.push("<p><code>" + this.name + "(" + val + ") = " +
this.hintEvalOf(val) + "</code></p>");
var composedFuncWithVal = composed.name + "(" + val + ")";
hints.push("<p>To solve for the value of <code>" + this.name + "</code>,"
+ "we need to solve for the value of <code>"
+ composedFuncWithVal + "</code>.</p>");
hints = hints.concat(composed.hint(val));
hints.push("<p>That means <code>" + this.name + "(" + val + ") = " +
this.hintEvalOf(val, true) + "</code></p>");
hints.push("<p><code>" + this.name + "(" + val + ") = " +
this.evalOf(val) + "</code></p>");
return hints;
};
return this;
},
randCoefs: function randCoefs(minDegree, maxDegree) {
var coefs = [];
var allZero = true;
for (var i = maxDegree; i >= minDegree; i--) {
coefs[i] = KhanUtil.randRange(-7, 7);
allZero = allZero && coefs[i] === 0;
}
return allZero ? randCoefs(minDegree, maxDegree) : coefs;
}
});