Permalink
Fetching contributors…
Cannot retrieve contributors at this time
126 lines (119 sloc) 5.87 KB
<!DOCTYPE html>
<html data-require="math math-format polynomials functional">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Multiplying 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="COLORS">[BLUE, RED, GREEN]</var>
<var id="MAX_DEGREE">4</var>
<var id="NON_ZERO_INDICES" data-ensure="(NON_ZERO_INDICES[0].length !== 3 || NON_ZERO_INDICES[1].length !== 3) &amp;&amp;
(NON_ZERO_INDICES[0].length !== 1 || NON_ZERO_INDICES[1].length !== 1)">
tabulate(function() {
return randRangeUniqueNonZero(0, MAX_DEGREE, randRange(1, 3)).sort().reverse();
}, 2)
</var>
<var id="POL_1, POL_2">tabulate( function(n) {
var coefs = [];
for ( var i = 0; i &lt;= MAX_DEGREE; i++ ) {
var value = 0;
for ( var j = 0; j &lt; NON_ZERO_INDICES[ n ].length; j++ ) {
if ( i === NON_ZERO_INDICES[ n ][ j ] ) {
value = randRangeNonZero( -7, 7 );
break;
}
}
coefs[ i ] = value;
}
return new Polynomial( 0, MAX_DEGREE, coefs, X );
}, 2 )</var>
<var id="LIKE_TERMS">function() {
var likeTerms = [];
var counter = 0;
var areLikeTerms = false;
for ( var i = POL_1.minDegree; i &lt;= POL_1.maxDegree; i++ ) {
if ( POL_1.coefs[ i ] === 0 ) {
continue;
}
for ( var j = POL_2.minDegree; j &lt;= POL_2.maxDegree; j++ ) {
if ( POL_2.coefs[ j ] === 0 ) {
continue;
}
if ( likeTerms[ i + j ] === undefined ) {
likeTerms[ i + j ] = "";
} else if ( likeTerms[ i + j ] === "" ) {
likeTerms[ i + j ] = COLORS[ counter++ ];
areLikeTerms = true;
}
}
}
return areLikeTerms ? likeTerms : false;
}()</var>
<var id="SOLUTION">POL_1.multiply(POL_2)</var>
</div>
<div class="problems">
<div id="multiply">
<p class="question">Expand the expression.</p>
<p class="problem"><code>\large{<span data-each="[ POL_1, POL_2 ] as POL" data-unwrap="">(<var>POL</var>)</span>}</code></p>
<p class="solution" data-type="expression" data-simplify><var>SOLUTION.parsableText()</var></p>
</div>
</div>
<div class="hints">
<div>
<p>First use the distributive property.</p>
<p><code>
<span data-each="NON_ZERO_INDICES[0] as n1, index1">
<span data-each="NON_ZERO_INDICES[1] as n2, index2">
<var>(POL_1.coefs[index1] &lt; 0) ? "-" : (n1 === 0 &amp;&amp; n2 === 0) ? "" : "+"</var>
<var>abs(POL_1.coefs[index1]) === 1 ? "" : abs(POL_1.coefs[index1])</var>
<var>X</var>^<var>index1</var>
(<var>(POL_2.coefs[index2] === 1) ? "" : (POL_2.coefs[index2] === -1) ? "-" : POL_2.coefs[index2]</var>
<var>X</var>^<var>index2</var>)
</span>
</span>
</code></p>
</div>
<div>
<p>Simplify.</p>
<p data-if="LIKE_TERMS"><code>
<span data-each="NON_ZERO_INDICES[0] as n1, index1">
<span data-each="NON_ZERO_INDICES[1] as n2, index2">
<var>(POL_1.coefs[index1] * POL_2.coefs[index2] &lt; 0) ? "-" : (n1 === 0 &amp;&amp; n2 === 0) ? "" : "+"</var>
<var>coefficient(abs(POL_1.coefs[index1] * POL_2.coefs[index2]))</var><var>X</var>^{<var>index1 + index2</var>}
</span>
</span>
</code></p>
<p data-else=""><code><var>SOLUTION</var></code></p>
</div>
<div data-if="LIKE_TERMS" data-unwrap="">
<div>
<p>Identify like terms.</p>
<p><code>
<span data-each="NON_ZERO_INDICES[0] as n1, index1">
<span data-each="NON_ZERO_INDICES[1] as n2, index2">
\color{<var>LIKE_TERMS[index1 + index2 ]</var>}
{<var>(POL_1.coefs[index1] * POL_2.coefs[index2] &lt; 0) ? "-" : (n1 === 0 &amp;&amp; n2 === 0) ? "" : "+"</var>
<var>coefficient(abs(POL_1.coefs[index1] * POL_2.coefs[index2]))</var><var>X</var>^{<var>index1 + index2</var>}}
</span>
</span>
</code></p>
</div>
<div>
<p>Add the coefficients.</p>
<p><code>
<span data-each="new Array(SOLUTION.getNumberOfTerms()) as n, index">
\color{<var>LIKE_TERMS[SOLUTION.getCoefAndDegreeForTerm(n).degree]</var>}
{<var>(SOLUTION.getCoefAndDegreeForTerm(n).coef &lt; 0 || n === 0) ? "" : "+"</var>
<var>expr(SOLUTION.expr()[n + 1])</var>}
</span>
</code></p>
</div>
</div>
</div>
</div>
</body>
</html>