Esempio n. 1
0
  // replace every x in tree by (x - vx)
  // i.e. replace fVar with (fvar - vx)
  private void translateX(ExpressionNode en, double vx, int varNo) {
    ExpressionValue left = en.getLeft();
    ExpressionValue right = en.getRight();

    // left tree
    if (left == fVars[varNo]) {
      try { // is there a constant number to the right?
        MyDouble num = (MyDouble) right;
        double temp;
        switch (en.getOperation()) {
          case ExpressionNode.PLUS:
            temp = num.getDouble() - vx;
            if (Kernel.isZero(temp)) {
              expression = expression.replaceAndWrap(en, fVars[varNo]);
            } else if (temp < 0) {
              en.setOperation(ExpressionNode.MINUS);
              num.set(-temp);
            } else {
              num.set(temp);
            }
            return;

          case ExpressionNode.MINUS:
            temp = num.getDouble() + vx;
            if (Kernel.isZero(temp)) {
              expression = expression.replaceAndWrap(en, fVars[varNo]);
            } else if (temp < 0) {
              en.setOperation(ExpressionNode.PLUS);
              num.set(-temp);
            } else {
              num.set(temp);
            }
            return;

          default:
            en.setLeft(shiftXnode(vx, varNo));
        }
      } catch (Exception e) {
        en.setLeft(shiftXnode(vx, varNo));
      }
    } else if (left instanceof ExpressionNode) {
      translateX((ExpressionNode) left, vx, varNo);
    }

    // right tree
    if (right == fVars[varNo]) {
      en.setRight(shiftXnode(vx, varNo));
    } else if (right instanceof ExpressionNode) {
      translateX((ExpressionNode) right, vx, varNo);
    }
  }
Esempio n. 2
0
 private boolean initIneqs(
     ExpressionNode fe, FunctionalNVar functional, IneqTree tree, boolean negate) {
   int op = fe.getOperation();
   ExpressionNode leftTree = fe.getLeftTree();
   ExpressionNode rightTree = fe.getRightTree();
   if (op == ExpressionNode.GREATER
       || op == ExpressionNode.GREATER_EQUAL
       || op == ExpressionNode.LESS
       || op == ExpressionNode.LESS_EQUAL) {
     Inequality newIneq =
         new Inequality(
             kernel,
             leftTree,
             rightTree,
             adjustOp(op, negate),
             getFunction().getFunctionVariables(),
             functional);
     if (newIneq.getType() != Inequality.INEQUALITY_INVALID) {
       if (newIneq.getType() != Inequality.INEQUALITY_1VAR_X
           && newIneq.getType() != Inequality.INEQUALITY_1VAR_Y)
         newIneq.getBorder().setInverseFill(newIneq.isAboveBorder());
       tree.setIneq(newIneq);
     }
     return newIneq.getType() != Inequality.INEQUALITY_INVALID;
   } else if (op == ExpressionNode.AND
       || op == ExpressionNode.OR
       || op == ExpressionNode.EQUAL_BOOLEAN
       || op == ExpressionNode.NOT_EQUAL) {
     tree.operation = adjustOp(op, negate);
     tree.left = new IneqTree();
     tree.right = new IneqTree();
     return initIneqs(leftTree, functional, tree.left, negate)
         && initIneqs(rightTree, functional, tree.right, negate);
   } else if (op == ExpressionNode.NOT) {
     return initIneqs(leftTree, functional, tree, !negate);
   } else if (op == ExpressionNode.FUNCTION_NVAR) {
     FunctionalNVar nv = (FunctionalNVar) leftTree.getLeft();
     IneqTree otherTree = nv.getIneqs();
     if (otherTree == null || otherTree.getSize() == 0) {
       return false;
     }
     tree.left = otherTree.left;
     tree.right = otherTree.right;
     tree.operation = otherTree.operation;
     tree.ineq = otherTree.ineq;
     return true;
   } else return false;
 }