예제 #1
0
 /** \brief  Create a binary expression */
 inline static SXElement create(unsigned char op, const SXElement& dep0, const SXElement& dep1) {
   if (dep0.isConstant() && dep1.isConstant()) {
     // Evaluate constant
     double dep0_val = dep0.getValue();
     double dep1_val = dep1.getValue();
     double ret_val;
     casadi_math<double>::fun(op, dep0_val, dep1_val, ret_val);
     return ret_val;
   } else {
     // Expression containing free variables
     return SXElement::create(new BinarySX(op, dep0, dep1));
   }
 }
예제 #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);
  }