lib/solveEquation/stepThrough.js
// Simplifies the equation and returns the simplification steps
Simplifies the equation and returns the simplification steps
Simplifies the equation and returns the simplification steps
function addSimplificationSteps(steps, equation, debug=false) {
  let oldEquation = equation.clone();
  const leftSimplifySteps = simplifyExpressionNode(equation.leftNode, false);
  const simplifiedLeftNode = leftSimplifySteps.length !== 0
        ? leftSimplifySteps.slice(-1)[0].newNode
        : equation.leftNode;
  const leftFactorSteps = factor(simplifiedLeftNode, false);
  const leftSubSteps = [];
  for (let i = 0; i < leftSimplifySteps.length; i++) {
    const step = leftSimplifySteps[i];
    leftSubSteps.push(EquationStatus.addLeftStep(equation, step));
  }
  for (let i = 0; i < leftFactorSteps.length; i++) {
    const step = leftFactorSteps[i];
    leftSubSteps.push(EquationStatus.addLeftStep(equation, step));
  }
  if (leftSubSteps.length === 1) {
    const step = leftSubSteps[0];
    if (debug) {
      logSteps(step);
    }
    steps.push(step);
  }
  else if (leftSubSteps.length > 1) {
    const lastStep = leftSubSteps[leftSubSteps.length - 1];
    const finalEquation = EquationStatus.resetChangeGroups(lastStep.newEquation);
    const simplifyStatus = new EquationStatus(
      ChangeTypes.SIMPLIFY_LEFT_SIDE,
      oldEquation, finalEquation, leftSubSteps);
    if (debug) {
      logSteps(simplifyStatus);
    }
    steps.push(simplifyStatus);
  }
  if (steps.length > 0) {
    equation = EquationStatus.resetChangeGroups(
      steps[steps.length - 1].newEquation);
  }
  oldEquation = equation.clone();
  const rightSteps = simplifyExpressionNode(equation.rightNode, false);
  const rightSubSteps = [];
  for (let i = 0; i < rightSteps.length; i++) {
    const step = rightSteps[i];
    rightSubSteps.push(EquationStatus.addRightStep(equation, step));
  }
  if (rightSubSteps.length === 1) {
    const step = rightSubSteps[0];
    if (debug) {
      logSteps(step);
    }
    steps.push(step);
  }
  else if (rightSubSteps.length > 1) {
    const lastStep = rightSubSteps[rightSubSteps.length - 1];
    const finalEquation = EquationStatus.resetChangeGroups(lastStep.newEquation);
    const simplifyStatus = new EquationStatus(
      ChangeTypes.SIMPLIFY_RIGHT_SIDE,
      oldEquation, finalEquation, rightSubSteps);
    if (debug) {
      logSteps(simplifyStatus);
    }
    steps.push(simplifyStatus);
  }
  return steps;
}
function addSimplificationSteps ( steps , equation , debug = false ) { let oldEquation = equation . clone ( ) ; const leftSimplifySteps = simplifyExpressionNode ( equation . leftNode , false ) ; const simplifiedLeftNode = leftSimplifySteps . length !== 0 ? leftSimplifySteps . slice ( - 1 ) [ 0 ] . newNode : equation . leftNode ; const leftFactorSteps = factor ( simplifiedLeftNode , false ) ; const leftSubSteps = [ ] ; for ( let i = 0 ; i < leftSimplifySteps . length ; i ++ ) { const step = leftSimplifySteps [ i ] ; leftSubSteps . push ( EquationStatus . addLeftStep ( equation , step ) ) ; } for ( let i = 0 ; i < leftFactorSteps . length ; i ++ ) { const step = leftFactorSteps [ i ] ; leftSubSteps . push ( EquationStatus . addLeftStep ( equation , step ) ) ; } if ( leftSubSteps . length === 1 ) { const step = leftSubSteps [ 0 ] ; if ( debug ) { logSteps ( step ) ; } steps . push ( step ) ; } else if ( leftSubSteps . length > 1 ) { const lastStep = leftSubSteps [ leftSubSteps . length - 1 ] ; const finalEquation = EquationStatus . resetChangeGroups ( lastStep . newEquation ) ; const simplifyStatus = new EquationStatus ( ChangeTypes . SIMPLIFY_LEFT_SIDE , oldEquation , finalEquation , leftSubSteps ) ; if ( debug ) { logSteps ( simplifyStatus ) ; } steps . push ( simplifyStatus ) ; } if ( steps . length > 0 ) { equation = EquationStatus . resetChangeGroups ( steps [ steps . length - 1 ] . newEquation ) ; } oldEquation = equation . clone ( ) ; const rightSteps = simplifyExpressionNode ( equation . rightNode , false ) ; const rightSubSteps = [ ] ; for ( let i = 0 ; i < rightSteps . length ; i ++ ) { const step = rightSteps [ i ] ; rightSubSteps . push ( EquationStatus . addRightStep ( equation , step ) ) ; } if ( rightSubSteps . length === 1 ) { const step = rightSubSteps [ 0 ] ; if ( debug ) { logSteps ( step ) ; } steps . push ( step ) ; } else if ( rightSubSteps . length > 1 ) { const lastStep = rightSubSteps [ rightSubSteps . length - 1 ] ; const finalEquation = EquationStatus . resetChangeGroups ( lastStep . newEquation ) ; const simplifyStatus = new EquationStatus ( ChangeTypes . SIMPLIFY_RIGHT_SIDE , oldEquation , finalEquation , rightSubSteps ) ; if ( debug ) { logSteps ( simplifyStatus ) ; } steps . push ( simplifyStatus ) ; } return steps ; }
Simplifies the equation and returns the simplification steps
Simplifies the equation and returns the simplification steps
/*
    1. On the left side, we should first simplify,
    and add all those simplify substeps to a list of leftSteps.
    2. Then we should factor the simplified equation, and add all
    those factoring substeps to the leftSteps list.
    3. On the right side, there should be no need to factor,
    because we always move everything to the left side first
    e.g. x^2 + 3x + 2 = 0 <- factor the left side
    e.g. x + 4 + 2 = 0 <- simplify the left side
    e.g. 0 = x^2 + 3x + 2 -> x^2 + 3x + 2 = 0 <- swap to the left side
  */ // no change groups are set here - too much is changing for it to be useful // update `equation` to have the new simplified left node // the updated equation from simplifing the left side is the old equation // (ie the "before" of the before and after) for simplifying the right side. // no change groups are set here - too much is changing for it to be useful