Ejemplo n.º 1
0
  SXElement SXElement::__add__(const SXElement& y) const {
    // NOTE: Only simplifications that do not result in extra nodes area allowed

    if (!CasadiOptions::simplification_on_the_fly) return BinarySX::create(OP_ADD, *this, y);

    if (node->isZero())
      return y;
    else if (y->isZero()) // term2 is zero
      return *this;
    else if (y.hasDep() && y.getOp()==OP_NEG) // x + (-y) -> x - y
      return __sub__(-y);
    else if (hasDep() && getOp()==OP_NEG) // (-x) + y -> y - x
      return y.__sub__(getDep());
    else if (hasDep() && getOp()==OP_MUL &&
            y.hasDep() && y.getOp()==OP_MUL &&
            getDep(0).isConstant() && getDep(0).getValue()==0.5 &&
            y.getDep(0).isConstant() && y.getDep(0).getValue()==0.5 &&
            y.getDep(1).isEqual(getDep(1), SXNode::eq_depth_)) // 0.5x+0.5x = x
      return getDep(1);
    else if (hasDep() && getOp()==OP_DIV &&
            y.hasDep() && y.getOp()==OP_DIV &&
            getDep(1).isConstant() && getDep(1).getValue()==2 &&
            y.getDep(1).isConstant() && y.getDep(1).getValue()==2 &&
            y.getDep(0).isEqual(getDep(0), SXNode::eq_depth_)) // x/2+x/2 = x
      return getDep(0);
    else if (hasDep() && getOp()==OP_SUB && getDep(1).isEqual(y, SXNode::eq_depth_))
      return getDep(0);
    else if (y.hasDep() && y.getOp()==OP_SUB && isEqual(y.getDep(1), SXNode::eq_depth_))
      return y.getDep(0);
    else // create a new branch
      return BinarySX::create(OP_ADD, *this, y);
  }
Ejemplo n.º 2
0
  SXElement SXElement::__div__(const SXElement& y) const {
    // Only simplifications that do not result in extra nodes area allowed

    if (!CasadiOptions::simplification_on_the_fly) return BinarySX::create(OP_DIV, *this, y);

    if (y->isZero()) // term2 is zero
      return casadi_limits<SXElement>::nan;
    else if (node->isZero()) // term1 is zero
      return 0;
    else if (y->isOne()) // term2 is one
      return *this;
    else if (y->isMinusOne())
      return -(*this);
    else if (isEqual(y, SXNode::eq_depth_)) // terms are equal
      return 1;
    else if (isDoubled() && y.isEqual(2))
      return node->dep(0);
    else if (isOp(OP_MUL) && y.isEqual(node->dep(0), SXNode::eq_depth_))
      return node->dep(1);
    else if (isOp(OP_MUL) && y.isEqual(node->dep(1), SXNode::eq_depth_))
      return node->dep(0);
    else if (node->isOne())
      return y.inv();
    else if (y.hasDep() && y.getOp()==OP_INV)
      return (*this)*y.inv();
    else if (isDoubled() && y.isDoubled())
      return node->dep(0) / y->dep(0);
    else if (y.isConstant() && hasDep() && getOp()==OP_DIV && getDep(1).isConstant() &&
            y.getValue()*getDep(1).getValue()==1) // (x/5)/0.2
      return getDep(0);
    else if (y.hasDep() && y.getOp()==OP_MUL &&
            y.getDep(1).isEqual(*this, SXNode::eq_depth_)) // x/(2*x) = 1/2
      return BinarySX::create(OP_DIV, 1, y.getDep(0));
    else if (hasDep() && getOp()==OP_NEG &&
            getDep(0).isEqual(y, SXNode::eq_depth_))      // (-x)/x = -1
      return -1;
    else if (y.hasDep() && y.getOp()==OP_NEG &&
            y.getDep(0).isEqual(*this, SXNode::eq_depth_))      // x/(-x) = 1
      return -1;
    else if (y.hasDep() && y.getOp()==OP_NEG && hasDep() &&
            getOp()==OP_NEG && getDep(0).isEqual(y.getDep(0), SXNode::eq_depth_))  // (-x)/(-x) = 1
      return 1;
    else if (isOp(OP_DIV) && y.isEqual(node->dep(0), SXNode::eq_depth_))
      return node->dep(1).inv();
    else // create a new branch
      return BinarySX::create(OP_DIV, *this, y);
  }
Ejemplo n.º 3
0
  SXElement SXElement::__mul__(const SXElement& y) const {

    if (!CasadiOptions::simplification_on_the_fly) return BinarySX::create(OP_MUL, *this, y);

    // Only simplifications that do not result in extra nodes area allowed
    if (y.isEqual(*this, SXNode::eq_depth_))
      return sq();
    else if (!isConstant() && y.isConstant())
      return y.__mul__(*this);
    else if (node->isZero() || y->isZero()) // one of the terms is zero
      return 0;
    else if (node->isOne()) // term1 is one
      return y;
    else if (y->isOne()) // term2 is one
      return *this;
    else if (y->isMinusOne())
      return -(*this);
    else if (node->isMinusOne())
      return -y;
    else if (y.hasDep() && y.getOp()==OP_INV)
      return (*this)/y.inv();
    else if (hasDep() && getOp()==OP_INV)
      return y/inv();
    else if (isConstant() && y.hasDep() && y.getOp()==OP_MUL && y.getDep(0).isConstant() &&
            getValue()*y.getDep(0).getValue()==1) // 5*(0.2*x) = x
      return y.getDep(1);
    else if (isConstant() && y.hasDep() && y.getOp()==OP_DIV && y.getDep(1).isConstant() &&
            getValue()==y.getDep(1).getValue()) // 5*(x/5) = x
      return y.getDep(0);
    else if (hasDep() && getOp()==OP_DIV && getDep(1).isEqual(y, SXNode::eq_depth_)) // ((2/x)*x)
      return getDep(0);
    else if (y.hasDep() && y.getOp()==OP_DIV &&
            y.getDep(1).isEqual(*this, SXNode::eq_depth_)) // ((2/x)*x)
      return y.getDep(0);
    else     // create a new branch
      return BinarySX::create(OP_MUL, *this, y);
  }
Ejemplo n.º 4
0
  SXElement SXElement::__sub__(const SXElement& y) const {
    // Only simplifications that do not result in extra nodes area allowed

    if (!CasadiOptions::simplification_on_the_fly) return BinarySX::create(OP_SUB, *this, y);

    if (y->isZero()) // term2 is zero
      return *this;
    if (node->isZero()) // term1 is zero
      return -y;
    if (isEqual(y, SXNode::eq_depth_)) // the terms are equal
      return 0;
    else if (y.hasDep() && y.getOp()==OP_NEG) // x - (-y) -> x + y
      return __add__(-y);
    else if (hasDep() && getOp()==OP_ADD && getDep(1).isEqual(y, SXNode::eq_depth_))
      return getDep(0);
    else if (hasDep() && getOp()==OP_ADD && getDep(0).isEqual(y, SXNode::eq_depth_))
      return getDep(1);
    else if (y.hasDep() && y.getOp()==OP_ADD && isEqual(y.getDep(1), SXNode::eq_depth_))
      return -y.getDep(0);
    else if (y.hasDep() && y.getOp()==OP_ADD && isEqual(y.getDep(0), SXNode::eq_depth_))
      return -y.getDep(1);
    else // create a new branch
      return BinarySX::create(OP_SUB, *this, y);
  }