packages/solver/lib/simplifyExpression/functionsSearch/nthRoot.js
function nthRootConstant(node) {
  let newNode = clone(node);
  const radicandNode = getRadicandNode(node);
  const rootNode = getRootNode(node);

  if (Negative.isNegative(radicandNode)) {
    return Node.Status.noChange(node);
  }
  else if (!Node.Type.isConstant(rootNode) || Negative.isNegative(rootNode)) {
    return Node.Status.noChange(node);
  }

  const radicandValue = parseFloat(radicandNode.value);
  const rootValue = parseFloat(rootNode.value);
  const nthRootValue = math.nthRoot(radicandValue, rootValue);
  // Perfect root e.g. nthRoot(4, 2) = 2
  if (nthRootValue % 1 === 0) {
    newNode = Node.Creator.constant(nthRootValue);
    return Node.Status.nodeChanged(
      ChangeTypes.NTH_ROOT_VALUE, node, newNode);
  }
  // Try to find if we can simplify by finding factors that can be
  // pulled out of the radical
  else {
    // convert the number into the product of its prime factors
    const factors = ConstantFactors.getPrimeFactors(radicandValue);
    if (factors.length > 1) {
      let substeps = [];
      const factorNodes = factors.map(Node.Creator.constant);

      newNode.args[0] = Node.Creator.operator('*', factorNodes);
      substeps.push(Node.Status.nodeChanged(
          ChangeTypes.FACTOR_INTO_PRIMES, node, newNode));

      // run nthRoot on the new node
      const nodeStatus = nthRootMultiplication(newNode);
      if (nodeStatus.hasChanged()) {
        substeps = substeps.concat(nodeStatus.substeps);
        newNode = nodeStatus.newNode;

        return Node.Status.nodeChanged(
          ChangeTypes.NTH_ROOT_VALUE, node, newNode, true, substeps);
      }
    }
  }

  return Node.Status.noChange(node);
}
function nthRootConstant(node) {
  let newNode = clone(node);
  const radicandNode = getRadicandNode(node);
  const rootNode = getRootNode(node);

  if (Negative.isNegative(radicandNode)) {
    return Node.Status.noChange(node);
  }
  else if (!Node.Type.isConstant(rootNode) || Negative.isNegative(rootNode)) {
    return Node.Status.noChange(node);
  }

  const radicandValue = parseFloat(radicandNode.value);
  const rootValue = parseFloat(rootNode.value);
  const nthRootValue = math.nthRoot(radicandValue, rootValue);
  // Perfect root e.g. nthRoot(4, 2) = 2
  if (nthRootValue % 1 === 0) {
    newNode = Node.Creator.constant(nthRootValue);
    return Node.Status.nodeChanged(
      ChangeTypes.NTH_ROOT_VALUE, node, newNode);
  }
  // Try to find if we can simplify by finding factors that can be
  // pulled out of the radical
  else {
    // convert the number into the product of its prime factors
    const factors = ConstantFactors.getPrimeFactors(radicandValue);
    if (factors.length > 1) {
      let substeps = [];
      const factorNodes = factors.map(Node.Creator.constant);

      newNode.args[0] = Node.Creator.operator('*', factorNodes);
      substeps.push(Node.Status.nodeChanged(
          ChangeTypes.FACTOR_INTO_PRIMES, node, newNode));

      // run nthRoot on the new node
      const nodeStatus = nthRootMultiplication(newNode);
      if (nodeStatus.hasChanged()) {
        substeps = substeps.concat(nodeStatus.substeps);
        newNode = nodeStatus.newNode;

        return Node.Status.nodeChanged(
          ChangeTypes.NTH_ROOT_VALUE, node, newNode, true, substeps);
      }
    }
  }

  return Node.Status.noChange(node);
}
function nthRootConstant ( node ) { let newNode = clone ( node ) ; const radicandNode = getRadicandNode ( node ) ; const rootNode = getRootNode ( node ) ; if ( Negative . isNegative ( radicandNode ) ) { return Node . Status . noChange ( node ) ; } else if ( ! Node . Type . isConstant ( rootNode ) || Negative . isNegative ( rootNode ) ) { return Node . Status . noChange ( node ) ; } const radicandValue = parseFloat ( radicandNode . value ) ; const rootValue = parseFloat ( rootNode . value ) ; const nthRootValue = math . nthRoot ( radicandValue , rootValue ) ; // Perfect root e.g. nthRoot(4, 2) = 2 if ( nthRootValue % 1 === 0 ) { newNode = Node . Creator . constant ( nthRootValue ) ; return Node . Status . nodeChanged ( ChangeTypes . NTH_ROOT_VALUE , node , newNode ) ; } // Try to find if we can simplify by finding factors that can be // pulled out of the radical else { // convert the number into the product of its prime factors const factors = ConstantFactors . getPrimeFactors ( radicandValue ) ; if ( factors . length > 1 ) { let substeps = [ ] ; const factorNodes = factors . map ( Node . Creator . constant ) ; newNode . args [ 0 ] = Node . Creator . operator ( '*' , factorNodes ) ; substeps . push ( Node . Status . nodeChanged ( ChangeTypes . FACTOR_INTO_PRIMES , node , newNode ) ) ; // run nthRoot on the new node const nodeStatus = nthRootMultiplication ( newNode ) ; if ( nodeStatus . hasChanged ( ) ) { substeps = substeps . concat ( nodeStatus . substeps ) ; newNode = nodeStatus . newNode ; return Node . Status . nodeChanged ( ChangeTypes . NTH_ROOT_VALUE , node , newNode , true , substeps ) ; } } } return Node . Status . noChange ( node ) ; }
Returns the nthRoot evaluated on a constant node Potentially factors the constant node into primes, and calls nthRootMultiplication on the new nthRoot
Returns the nthRoot evaluated on a constant node Potentially factors the constant node into primes and calls nthRootMultiplication on the new nthRoot