Skip to content

Commit

Permalink
Creates three substeps for factorSumProductRule
Browse files Browse the repository at this point in the history
  • Loading branch information
alainakafkes committed Jul 19, 2017
1 parent e03f763 commit 2496462
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions lib/factor/factorQuadratic.js
Expand Up @@ -209,6 +209,8 @@ function factorPerfectSquare(node, symbol, aValue, bValue, cValue, negate) {
// e.g. x^2 + 3x + 2 -> (x + 1)(x + 2) or
// or 2x^2 + 5x + 3 -> (2x - 1)(x + 3)
function factorSumProductRule(node, symbol, aValue, bValue, cValue, negate) {
let newNode;

if (bValue && cValue) {
// we factor out the gcd first, providing us with a modified expression to
// factor with new a, b and c values
Expand All @@ -225,19 +227,44 @@ function factorSumProductRule(node, symbol, aValue, bValue, cValue, negate) {
for (const pair of factorPairs) {
if (pair[0] + pair[1] === bValue) {
// To factor, we go through some transformations
// TODO: these should be actual substeps
// 1. Break apart the middle term into two terms using our factor pair (p and q):
// e.g. ax^2 + bx + c -> ax^2 + px + qx + c
// TODO: these should be actual substeps *alaina*
// 1. Break apart the middle term into two terms using our factor pair
// (p and q): e.g. ax^2 + bx + c -> ax^2 + px + qx + c
// 2. Consider the first two terms together and the second two terms
// together (this doesn't require any actual change to the expression)
// e.g. first group: [ax^2 + px] and second group: [qx + c]
// 3. Factor both groups separately
// e.g first group: [ux(rx + s)] and second group [v(rx + s)]
// 4. Finish factoring by combining the factored terms through grouping:
// e.g. (ux + v)(rx + s)
const substeps = [];
let status;

// 1. Break apart the middle term into two terms using our factor pair
// (p and q)
const pValue = pair[0];
const qValue = pair[1];

// SUBSTEP: ax^2 + bx + c -> ax^2 + px + qx + c
const sq = Node.Creator.constant(2);
const a = Node.Creator.constant(aValue);
const b = Node.Creator.constant(bValue);
const c = Node.Creator.constant(cValue);
const p = Node.Creator.constant(pValue);
const q = Node.Creator.constant(qValue);
const ax2 = Node.Creator.polynomialTerm(symbol, sq, a);
const px = Node.Creator.polynomialTerm(symbol, null, p);
const qx = Node.Creator.polynomialTerm(symbol, null, q);
newNode = Node.Creator.operator('+', [ax2, px, qx, c], true);
status = newNode.status.nodeChanged(
ChangeTypes.SIMPLIFY_ARITHMETIC, node, newNode);
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);

// 2. Consider the first two terms together and the second two terms
// together - does not require substep

// 3A. Factor the first group
// factor the first group to get u, r and s:
// u = gcd(a, p), r = a/u and s = p/u
const u = Node.Creator.constant(math.gcd(aValue, pValue));
Expand All @@ -249,6 +276,7 @@ function factorSumProductRule(node, symbol, aValue, bValue, cValue, negate) {
const firstParen = Node.Creator.parenthesis(
Node.Creator.operator('+', [rx, s]));

// 3B. Factor the second group
// factor the second group to get v, we don't need to find r and s again
// v = gcd(c, q)
let vValue = math.gcd(cValue, qValue);
Expand All @@ -257,13 +285,21 @@ function factorSumProductRule(node, symbol, aValue, bValue, cValue, negate) {
}
const v = Node.Creator.constant(vValue);

// SUBSTEP: ux(rx + s) + v(rx + s)
const firstGroup = Node.Creator.operator('*', [ux, firstParen], true);
const secondGroup = Node.Creator.operator('*', [v, firstParen], true);
newNode = Node.Creator.operator('+', [firstGroup, secondGroup]);
status = newNode.status.nodeChanged(
ChangeTypes.FACTOR_SYMBOL, node, newNode);
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);

// create the second parenthesis
const ux = Node.Creator.polynomialTerm(symbol, null, u);
const secondParen = Node.Creator.parenthesis(
Node.Creator.operator('+', [ux, v]));

// create a node in the general factored form for expression
let newNode;
if (gcd === 1) {
newNode = Node.Creator.operator(
'*', [firstParen, secondParen], true);
Expand All @@ -277,6 +313,12 @@ function factorSumProductRule(node, symbol, aValue, bValue, cValue, negate) {
newNode = Negative.negate(newNode);
}

// SUBSTEP: (ux + v)(rx + s)
status = newNode.status.nodeChanged(
ChangeTypes.FACTOR_SUM_PRODUCT_RULE, node, newNode);
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);

return Node.Status.nodeChanged(
ChangeTypes.FACTOR_SUM_PRODUCT_RULE, node, newNode);
}
Expand Down

0 comments on commit 2496462

Please sign in to comment.