Permalink
Fetching contributors…
Cannot retrieve contributors at this time
143 lines (131 sloc) 6.16 KB
<!DOCTYPE html>
<html data-require="math math-format polynomials functional">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Adding and subtracting polynomials</title>
<script data-main="../local-only/main.js" src="../local-only/require.js"></script>
</head>
<body>
<div class="exercise">
<div class="vars">
<var id="X">randVar()</var>
<var id="MAX_DEGREE">7</var>
<var id="COLORS">[BLUE, RED, GREEN, PURPLE]</var>
<var id="N">2 + (random() &lt; 0.25 ? 1 : 0)</var>
<var id="SIGNS">[randFromArray(["-", "+"]), randFromArray(["-", "+"])].slice(0, N - 1)</var>
<var id="DISTRIBUTE">_.any(SIGNS, function(s) { return s === "-"; })</var>
<var id="TERMS">_(N).times(function(term) { return randRange(2, 5 - N); })</var>
<var id="NON_ZERO_INDICES">randRangeUnique(1, MAX_DEGREE, 4)</var>
<var id="SORTED_INDICES">sortNumbers(NON_ZERO_INDICES).reverse()</var>
<var id="POLYNOMIALS">
tabulate(function(p) {
var coefs = [];
var terms = shuffle(NON_ZERO_INDICES).slice(0, TERMS[p]);
for (var i = 0; i &lt;= MAX_DEGREE; i++) {
if (terms.indexOf(i) !== -1) {
coefs[i] = randRangeNonZero(-7, 7);
} else {
coefs[i] = 0;
}
}
return new Polynomial(0, MAX_DEGREE, coefs, X);
}, N)
</var>
<var id="SOLUTION">(function() {
var sol = POLYNOMIALS[0];
for (var i = 1; i &lt; N; i++) {
sol = sol.add(POLYNOMIALS[i]);
}
return sol;
})()
</var>
<var id="COEFFICIENTS">(function() {
var coeffs = [[], [], [], []];
var toRemove = [];
for (var j = 0; j &lt; 4; j++) {
for (var i = 0; i &lt; POLYNOMIALS.length; i++) {
var coefficient = POLYNOMIALS[i].coefs[SORTED_INDICES[j]];
if (coefficient !== 0) {
coeffs[j].push(coefficient);
}
}
if (coeffs[j].length === 0) {
toRemove.push(j);
}
}
toRemove = sortNumbers(toRemove);
for (var i = 0; i &lt; toRemove.length; i++) {
coeffs.splice(toRemove[i], 1);
SORTED_INDICES.splice(toRemove[i], 1);
}
return coeffs;
})()
</var>
</div>
<div class="problems">
<div>
<p class="question">Simplify the expression.</p>
<p class="problem"><code>
(<var>POLYNOMIALS[0]</var>) <var>SIGNS[0]</var>
(<span data-if="SIGNS[0] === '+'"><var>POLYNOMIALS[1]</var></span>
<span data-else=""><var>POLYNOMIALS[1].multiply(-1)</var></span>)
<span data-if="N === 3"> <var>SIGNS[1]</var>
(<span data-if="SIGNS[1] === '+'"><var>POLYNOMIALS[2]</var></span>
<span data-else=""><var>POLYNOMIALS[2].multiply(-1)</var></span>)
</span>
</code></p>
<p class="solution" data-type="expression" data-simplify><var>SOLUTION.parsableText()</var></p>
</div>
</div>
<div class="hints">
<div data-if="DISTRIBUTE">
<p>Distribute any negative signs.</p>
<p><code>(<var>POLYNOMIALS[0]</var>) + (<var>POLYNOMIALS[1]</var>)<span data-if="N === 3"> + (<var>POLYNOMIALS[2]</var>)</span></code></p>
</div>
<div>
<p>Since we are adding polynomials, we can simply remove the parentheses.</p>
<p><code><var>POLYNOMIALS[0]</var> + <var>POLYNOMIALS[1]</var><span data-if="N === 3"> + <var>POLYNOMIALS[2]</var></span></code></p>
</div>
<div>
<p>Identify like terms.</p>
<p><code>
<span data-each="POLYNOMIALS as POL">
<span data-each="SORTED_INDICES as n, index" data-unwrap="">
<span data-if="POL.coefs[index] !== 0">
<var>(POL.coefs[index] &lt; 0) ? "-" : (n === 0 &amp;&amp; POL === POLYNOMIALS[0]) ? "" : "+"</var>
\color{<var>COLORS[ n ]</var>}{<var>abs(POL.coefs[index]) === 1 ? "" : abs(POL.coefs[index])</var>
<var>X</var>^<var>index</var>}
</span>
</span>
</span>
</code></p>
</div>
<div>
<p>Combine like terms.</p>
<p><code>
<span data-each="COEFFICIENTS as index1, coeff">
<span data-if="coeff.length &gt; 0">
<span data-if="index1 !== 0"> + </span>\color{<var>COLORS[index1]</var>}{
<span data-if="coeff.length === 1">
<span data-if="coeff[0] !== 1"><var>coeff[0]</var></span>
</span>
<span data-else="">(
<span data-each="coeff as index2, TERM">
<span data-if="index2 !== 0">
<span data-if="TERM > 0">+</span>
</span><var>TERM</var>
</span>)
</span>
<var>X</var>^<var>SORTED_INDICES[index1]</var>}
</span>
</span>
</code></p>
</div>
<div>
<p>Add the coefficients.</p>
<p><code><var>SOLUTION</var></code></p>
</div>
</div>
</div>
</body>
</html>