Multiplies `node`, a constant or fraction of two constant nodes, by -1 Returns a node
506872
function negativeCoefficient(node) {
  if (NodeType.isConstant(node)) {
    node = NodeCreator.constant(0 - parseFloat(node.value));
  }
  else {
    const numeratorValue = 0 - parseFloat(node.args[0].value);
    node.args[0] = NodeCreator.constant(numeratorValue);
  }
  return node;
}
826704
def split_by_value( total, nodes, headdivisor=2.0 ):
    """"""
    head_sum,tail_sum = 0,0
    divider = 0
    for node in nodes[::-1]:
        if head_sum < total/headdivisor:
            head_sum += node[0]
            divider -= 1
        else:
            break
    return (head_sum,nodes[divider:]),(total-head_sum,nodes[:divider])
1353540
function evaluateConstantSum(node) {
  if (Node.Type.isParenthesis(node)) {
    node = node.content;
  }
  if (!Node.Type.isOperator(node) || node.op !== '+') {
    return Node.Status.noChange(node);
  }
  if (node.args.some(node => !Node.Type.isConstantOrConstantFraction(node))) {
    return Node.Status.noChange(node);
  }

  // functions needed to evaluate the sum
  const summingFunctions = [
    arithmeticSearch,
    addConstantFractions,
    addConstantAndFraction,
  ];
  for (let i = 0; i < summingFunctions.length; i++) {
    const status = summingFunctions[i](node);
    if (status.hasChanged()) {
      if (Node.Type.isConstantOrConstantFraction(status.newNode)) {
        return status;
      }
    }
  }

  let newNode = clone(node);
  const substeps = [];
  let status;

  // STEP 1: group fractions and constants separately
  status = groupConstantsAndFractions(newNode);
  substeps.push(status);
  newNode = Node.Status.resetChangeGroups(status.newNode);

  const constants = newNode.args[0];
  const fractions = newNode.args[1];

  // STEP 2A: evaluate arithmetic IF there's > 1 constant
  // (which is the case if it's a list surrounded by parenthesis)
  if (Node.Type.isParenthesis(constants)) {
    const constantList = constants.content;
    const evaluateStatus = arithmeticSearch(constantList);
    status = Node.Status.childChanged(newNode, evaluateStatus, 0);
    substeps.push(status);
    newNode = Node.Status.resetChangeGroups(status.newNode);
  }

  // STEP 2B: add fractions IF there's > 1 fraction
  // (which is the case if it's a list surrounded by parenthesis)
  if (Node.Type.isParenthesis(fractions)) {
    const fractionList = fractions.content;
    const evaluateStatus = addConstantFractions(fractionList);
    status = Node.Status.childChanged(newNode, evaluateStatus, 1);
    substeps.push(status);
    newNode = Node.Status.resetChangeGroups(status.newNode);
  }

  // STEP 3: combine the evaluated constant and fraction
  // the fraction might have simplified to a constant (e.g. 1/3 + 2/3 -> 2)
  // so we just call evaluateConstantSum again to cycle through
  status = evaluateConstantSum(newNode);
  substeps.push(status);
  newNode = Node.Status.resetChangeGroups(status.newNode);

  return Node.Status.nodeChanged(
    ChangeTypes.SIMPLIFY_ARITHMETIC, node, newNode, true, substeps);
}
988949
function isMultiplicationOfEqualNodes(node) {
  if (!Node.Type.isOperator(node) || node.op !== '*') {
    return false;
  }

  // return if they are all equal nodes
  return node.args.reduce((a, b) => {
    return a.equals(b);
  });

}
1056435
function initializeNode (node) {
  if (!node || node.length === 0) {
    return 0
  }
  const tag = node.get(0).name
  if (nodeTypes.mostPositive.indexOf(tag) >= 0) {
    return 5 + getClassWeight(node)
  }
  if (nodeTypes.positive.indexOf(tag) >= 0) {
    return 3 + getClassWeight(node)
  }
  if (nodeTypes.negative.indexOf(tag) >= 0) {
    return -3 + getClassWeight(node)
  }
  if (nodeTypes.mostNegative.indexOf(tag) >= 0) {
    return -5 + getClassWeight(node)
  }
  return -1
}
785979
function(node, a, b)
{
    var idx = 1/(b.x - a.x);
    var tx1 = (node.bb_l == a.x ? -Infinity : (node.bb_l - a.x)*idx);
    var tx2 = (node.bb_r == a.x ?  Infinity : (node.bb_r - a.x)*idx);
    var txmin = min(tx1, tx2);
    var txmax = max(tx1, tx2);
    
    var idy = 1/(b.y - a.y);
    var ty1 = (node.bb_b == a.y ? -Infinity : (node.bb_b - a.y)*idy);
    var ty2 = (node.bb_t == a.y ?  Infinity : (node.bb_t - a.y)*idy);
    var tymin = min(ty1, ty2);
    var tymax = max(ty1, ty2);
    
    if(tymin <= txmax && txmin <= tymax){
        var min_ = max(txmin, tymin);
        var max_ = min(txmax, tymax);
        
        if(0.0 <= max_ && min_ <= 1.0) return max(min_, 0.0);
    }
    
    return Infinity;
}
364746
function getPercentage() {
  const type = NodeType.PercentageType;
  const token = tokens[pos];
  const line = token.ln;
  const column = token.col;
  const content = getNumberOrInterpolation();
  const end = getLastPosition(content, line, column, 1);

  // Skip `%`.
  pos++;

  return newNode(type, content, line, column, end);
}
653371
def constant_segments(node = self.node)
      segments = []

      if has_child_node?(node)
        segments.concat(constant_segments(node.children[0]))
      end

      segments << [node.type, name_for_node(node)]

      return segments
    end
504606
function(_root,_umi,_hanlder){
        var _node = _root,
            _hanlder = _hanlder||_f;
        if (_umi!='/'){
            _u._$forEach(
                _umi.split('/'),function(_name){
                    var _result = _hanlder(_node,_name);
                    if (!_result) 
                        return !0;
                    _node = _result;
                }
            );
        }
        return _node;
    }
747518
function removeMultiplicationByNegativeOne(node) {
  if (node.op !== '*') {
    return Node.Status.noChange(node);
  }
  const minusOneIndex = node.args.findIndex(arg => {
    return Node.Type.isConstant(arg) && arg.value === '-1';
  });
  if (minusOneIndex < 0) {
    return Node.Status.noChange(node);
  }

  // We might merge/combine the negative one into another node. This stores
  // the index of that other node in the arg list.
  let nodeToCombineIndex;
  // If minus one is the last term, maybe combine with the term before
  if (minusOneIndex + 1 === node.args.length) {
    nodeToCombineIndex = minusOneIndex - 1;
  }
  else {
    nodeToCombineIndex = minusOneIndex + 1;
  }

  let nodeToCombine = node.args[nodeToCombineIndex];
  // If it's a constant, the combining of those terms is handled elsewhere.
  if (Node.Type.isConstant(nodeToCombine)) {
    return Node.Status.noChange(node);
  }

  let newNode = clone(node);

  // Get rid of the -1
  nodeToCombine = Negative.negate(clone(nodeToCombine));

  // replace the node next to -1 and remove -1
  newNode.args[nodeToCombineIndex] = nodeToCombine;
  newNode.args.splice(minusOneIndex, 1);

  // if there's only one operand left, move it up the tree
  if (newNode.args.length === 1) {
    newNode = newNode.args[0];
  }
  return Node.Status.nodeChanged(
    ChangeTypes.REMOVE_MULTIPLYING_BY_NEGATIVE_ONE, node, newNode);
}
988519
public void calcCost(Node node)
    {
        node.cost = 0.0;
        if (alphaFloat_ != null)
        {
            float c = 0.0f;
            for (int i = 0; node.fVector.get(i) != -1; i++)
            {
                c += alphaFloat_[node.fVector.get(i) + node.y];
            }
            node.cost = costFactor_ * c;
        }
        else
        {
            double c = 0.0;
            for (int i = 0; node.fVector.get(i) != -1; i++)
            {
                c += alpha_[node.fVector.get(i) + node.y];
            }
            node.cost = costFactor_ * c;
        }
    }
961246
def get_node_sum(self, age=None):
        
        if age is None:
            age = self.age

        return age if self.comp == 1 else int((pow(self.comp, age+1) - 1) / (self.comp - 1))
373897
def traverse_const(node):
        
        if node.token == 'NUMBER':
            return node.t

        if node.token == 'UNARY':
            mid = node.operator
            if mid == 'MINUS':
                result = ' -' + Translator.traverse_const(node.operand)
            elif mid == 'ADDRESS':
                if node.operand.scope == SCOPE.global_ or node.operand.token in ('LABEL', 'FUNCTION'):
                    result = Translator.traverse_const(node.operand)
                else:
                    syntax_error_not_constant(node.operand.lineno)
                    return
            else:
                raise InvalidOperatorError(mid)
            return result

        if node.token == 'BINARY':
            mid = node.operator
            if mid == 'PLUS':
                mid = '+'
            elif mid == 'MINUS':
                mid = '-'
            elif mid == 'MUL':
                mid = '*'
            elif mid == 'DIV':
                mid = '/'
            elif mid == 'MOD':
                mid = '%'
            elif mid == 'POW':
                mid = '^'
            elif mid == 'SHL':
                mid = '>>'
            elif mid == 'SHR':
                mid = '<<'
            else:
                raise InvalidOperatorError(mid)

            return '(%s) %s (%s)' % (Translator.traverse_const(node.left), mid, Translator.traverse_const(node.right))

        if node.token == 'TYPECAST':
            if node.type_ in (Type.byte_, Type.ubyte):
                return '(' + Translator.traverse_const(node.operand) + ') & 0xFF'
            if node.type_ in (Type.integer, Type.uinteger):
                return '(' + Translator.traverse_const(node.operand) + ') & 0xFFFF'
            if node.type_ in (Type.long_, Type.ulong):
                return '(' + Translator.traverse_const(node.operand) + ') & 0xFFFFFFFF'
            if node.type_ == Type.fixed:
                return '((' + Translator.traverse_const(node.operand) + ') & 0xFFFF) << 16'
            syntax_error_cant_convert_to_type(node.lineno, str(node.operand), node.type_)
            return

        if node.token in ('VAR', 'VARARRAY', 'LABEL', 'FUNCTION'):
            # TODO: Check what happens with local vars and params
            return node.t

        if node.token == 'CONST':
            return Translator.traverse_const(node.expr)

        if node.token == 'ARRAYACCESS':
            return '({} + {})'.format(node.entry.mangled, node.offset)

        raise InvalidCONSTexpr(node)
19758
function addConstantAndFraction(node) {
  if (!Node.Type.isOperator(node) || node.op !== '+' || node.args.length !== 2) {
    return Node.Status.noChange(node);
  }

  const firstArg = node.args[0];
  const secondArg = node.args[1];
  let constNode, fractionNode;
  if (Node.Type.isConstant(firstArg)) {
    if (Node.Type.isIntegerFraction(secondArg)) {
      constNode = firstArg;
      fractionNode = secondArg;
    }
    else {
      return Node.Status.noChange(node);
    }
  }
  else if (Node.Type.isConstant(secondArg)) {
    if (Node.Type.isIntegerFraction(firstArg)) {
      constNode = secondArg;
      fractionNode = firstArg;
    }
    else {
      return Node.Status.noChange(node);
    }
  }
  else {
    return Node.Status.noChange(node);
  }

  let newNode = clone(node);
  let substeps = [];
  // newConstNode and newFractionNode will end up both constants, or both
  // fractions. I'm naming them based on their original form so we can keep
  // track of which is which.
  let newConstNode, newFractionNode;
  let changeType;
  if (Number.isInteger(parseFloat(constNode.value))) {
    const denominatorNode = fractionNode.args[1];
    const denominatorValue = parseInt(denominatorNode);
    const constNodeValue = parseInt(constNode.value);
    const newNumeratorNode = Node.Creator.constant(
      constNodeValue * denominatorValue);
    newConstNode = Node.Creator.operator(
      '/', [newNumeratorNode, denominatorNode]);
    newFractionNode = fractionNode;
    changeType = ChangeTypes.CONVERT_INTEGER_TO_FRACTION;
  }
  else {
    // round to 4 decimal places
    let dividedValue = fractionNode.eval();
    if (dividedValue < 1) {
      dividedValue = parseFloat(dividedValue.toPrecision(4));
    }
    else {
      dividedValue = parseFloat(dividedValue.toFixed(4));
    }
    newFractionNode = Node.Creator.constant(dividedValue);
    newConstNode = constNode;
    changeType = ChangeTypes.DIVIDE_FRACTION_FOR_ADDITION;
  }

  if (Node.Type.isConstant(firstArg)) {
    newNode.args[0] = newConstNode;
    newNode.args[1] = newFractionNode;
  }
  else {
    newNode.args[0] = newFractionNode;
    newNode.args[1] = newConstNode;
  }

  substeps.push(Node.Status.nodeChanged(changeType, node, newNode));
  newNode = Node.Status.resetChangeGroups(newNode);

  // If we changed an integer to a fraction, we need to add the steps for
  // adding the fractions.
  if (changeType === ChangeTypes.CONVERT_INTEGER_TO_FRACTION) {
    const addFractionStatus = addConstantFractions(newNode);
    substeps = substeps.concat(addFractionStatus.substeps);
  }
  // Otherwise, add the two constants
  else {
    const evalNode = Node.Creator.constant(newNode.eval());
    substeps.push(Node.Status.nodeChanged(
      ChangeTypes.SIMPLIFY_ARITHMETIC, newNode, evalNode));
  }

  const lastStep = substeps[substeps.length - 1];
  newNode = Node.Status.resetChangeGroups(lastStep.newNode);

  return Node.Status.nodeChanged(
    ChangeTypes.SIMPLIFY_ARITHMETIC, node, newNode, true, substeps);
}
1030574
def p_expr_minus(self, tree):
        ''' '''
        tree.value = convertstr(tree[0].value) - convertstr(tree[2].value)
261986
function convertMultiplicationToExponent(node) {
  const newNode = clone(node);

  const radicandNode = getRadicandNode(node);

  if (Node.Type.isParenthesis(radicandNode)) {
    const child = radicandNode.content;
    if (isMultiplicationOfEqualNodes(child)) {
      const baseNode = child.args[0];
      const exponentNode = Node.Creator.constant(child.args.length);
      newNode.args[0] = Node.Creator.operator('^', [baseNode, exponentNode]);
      return Node.Status.nodeChanged(
        ChangeTypes.CONVERT_MULTIPLICATION_TO_EXPONENT, node, newNode);
    }
  }
  else if (Node.Type.isOperator(radicandNode, '*')) {
    const children = [];
    radicandNode.args.forEach(child => {
      if (Node.Type.isParenthesis(child)) {
        const grandChild = child.content;
        if (isMultiplicationOfEqualNodes(grandChild)) {
          const baseNode = grandChild.args[0];
          const exponentNode = Node.Creator.constant(grandChild.args.length);
          children.push(Node.Creator.operator('^', [baseNode, exponentNode]));
          return;
        }
      }
      children.push(child);
    });

    newNode.args[0] = Node.Creator.operator('*', children);
    return Node.Status.nodeChanged(
      ChangeTypes.CONVERT_MULTIPLICATION_TO_EXPONENT, node, newNode);
  }

  return Node.Status.noChange(node);
}
1275017
function cancelLikeTerms(node) {
  if (!Node.Type.isOperator(node) || node.op !== '/') {
    return Node.Status.noChange(node);
  }
  let newNode = clone(node);
  const numerator = newNode.args[0];
  const denominator = newNode.args[1];

  // case 1: neither the numerator or denominator is a multiplication of terms
  if (!isMultiplicationOfTerms(numerator) &&
      !isMultiplicationOfTerms(denominator)) {
    const cancelStatus = cancelTerms(numerator, denominator);
    if (cancelStatus.hasChanged) {
      newNode.args[0] = cancelStatus.numerator || Node.Creator.constant(1);
      if (cancelStatus.denominator) {
        newNode.args[1] = cancelStatus.denominator;
      }
      else {
        // If we cancelled out the denominator, the node is now its numerator
        // e.g. (2x*y) / 2x => y (note y isn't a fraction)
        newNode = newNode.args[0];
      }
      return Node.Status.nodeChanged(
        ChangeTypes.CANCEL_TERMS, node, newNode);
    }
    else {
      return Node.Status.noChange(node);
    }
  }

  // case 2: numerator is a multiplication of terms and denominator is not
  // e.g. (2x^2 * 5) / 2x^2 => 5 / 1
  // e.g. (x^2*y) / x  => x^(2 - 1) * y (<-- note that the denominator goes
  // away because we always adjust the exponent in the numerator)
  else if (isMultiplicationOfTerms(numerator) &&
      !isMultiplicationOfTerms(denominator)) {
    const numeratorArgs = Node.Type.isParenthesis(numerator) ?
            numerator.content.args : numerator.args;
    for (let i = 0; i < numeratorArgs.length; i++) {
      const cancelStatus = cancelTerms(numeratorArgs[i], denominator);
      if (cancelStatus.hasChanged) {
        if (cancelStatus.numerator) {
          numeratorArgs[i] = cancelStatus.numerator;
        }
        // if the cancelling out got rid of the numerator node, we remove it from
        // the list
        else {
          numeratorArgs.splice(i, 1);
          // if the numerator is now a "multiplication" of only one term,
          // change it to just that term
          if (numeratorArgs.length === 1) {
            newNode.args[0] = numeratorArgs[0];
          }
        }
        if (cancelStatus.denominator) {
          newNode.args[1] = cancelStatus.denominator;
        }
        else {
          // If we cancelled out the denominator, the node is now its numerator
          // e.g. (2x*y) / 2x => y (note y isn't a fraction)
          newNode = newNode.args[0];
        }
        return Node.Status.nodeChanged(
          ChangeTypes.CANCEL_TERMS, node, newNode);
      }
    }
    return Node.Status.noChange(node);
  }

  // case 3: denominator is a multiplication of terms and numerator is not
  // e.g. 2x^2 / (2x^2 * 5) => 1 / 5
  // e.g. x / (x^2*y) => x^(1-2) / y
  else if (isMultiplicationOfTerms(denominator) &&
      !isMultiplicationOfTerms(numerator)) {
    const denominatorArgs = Node.Type.isParenthesis(denominator) ?
            denominator.content.args : denominator.args;
    for (let i = 0; i < denominatorArgs.length; i++) {
      const cancelStatus = cancelTerms(numerator, denominatorArgs[i]);
      if (cancelStatus.hasChanged) {
        newNode.args[0] = cancelStatus.numerator || Node.Creator.constant(1);
        if (cancelStatus.denominator) {
          denominatorArgs[i] = cancelStatus.denominator;
        }
        // if the cancelling out got rid of the denominator node, we remove it
        // from the list
        else {
          denominatorArgs.splice(i, 1);
          // if the denominator is now a "multiplication" of only one term,
          // change it to just that term
          if (denominatorArgs.length === 1) {
            newNode.args[1] = denominatorArgs[0];
          }
        }
        return Node.Status.nodeChanged(
          ChangeTypes.CANCEL_TERMS, node, newNode);
      }
    }
    return Node.Status.noChange(node);
  }

  // case 4: the numerator and denominator are both multiplications of terms
  else {
    const numeratorArgs = Node.Type.isParenthesis(numerator) ?
      numerator.content.args : numerator.args;
    const denominatorArgs = Node.Type.isParenthesis(denominator) ?
      denominator.content.args : denominator.args;
    for (let i = 0; i < numeratorArgs.length; i++) {
      for (let j = 0; j < denominatorArgs.length; j++) {
        const cancelStatus = cancelTerms(numeratorArgs[i], denominatorArgs[j]);
        if (cancelStatus.hasChanged) {
          if (cancelStatus.numerator) {
            numeratorArgs[i] = cancelStatus.numerator;
          }
          // if the cancelling out got rid of the numerator node, we remove it
          // from the list
          else {
            numeratorArgs.splice(i, 1);
            // if the numerator is now a "multiplication" of only one term,
            // change it to just that term
            if (numeratorArgs.length === 1) {
              newNode.args[0] = numeratorArgs[0];
            }
          }
          if (cancelStatus.denominator) {
            denominatorArgs[j] = cancelStatus.denominator;
          }
          // if the cancelling out got rid of the denominator node, we remove it
          // from the list
          else {
            denominatorArgs.splice(j, 1);
            // if the denominator is now a "multiplication" of only one term,
            // change it to just that term
            if (denominatorArgs.length === 1) {
              newNode.args[1] = denominatorArgs[0];
            }
          }
          return Node.Status.nodeChanged(
            ChangeTypes.CANCEL_TERMS, node, newNode);
        }
      }
    }
    return Node.Status.noChange(node);
  }
}