/** \brief Create a binary expression */ inline static SX create(unsigned char op, const SX& dep0, const SX& 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 SX::create(new BinarySX(op,dep0,dep1)); } }
bool SX::isEquivalent(const SX& y, int depth) const{ if (isEqual(y)) return true; if (isConstant() && y.isConstant()) return y.getValue()==getValue(); if (depth==0) return false; if (hasDep() && y.hasDep() && getOp()==y.getOp()) { if (getDep(0).isEquivalent(y.getDep(0),depth-1) && getDep(1).isEquivalent(y.getDep(1),depth-1)) return true; return (operation_checker<CommChecker>(getOp()) && getDep(0).isEquivalent(y.getDep(1),depth-1) && getDep(1).isEquivalent(y.getDep(0),depth-1)); } return false; }
SX SX::__div__(const SX& y) const{ // Only simplifications that do not result in extra nodes area allowed if(y->isZero()) // term2 is zero return casadi_limits<SX>::nan; else if(node->isZero()) // term1 is zero return 0; else if(y->isOne()) // term2 is one return *this; else if(isEquivalent(y)) // terms are equal return 1; else if(isDoubled() && y.isEqual(2)) return node->dep(0); else if(isOp(OP_MUL) && y.isEquivalent(node->dep(0))) return node->dep(1); else if(isOp(OP_MUL) && y.isEquivalent(node->dep(1))) 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).isEquivalent(*this)) // x/(2*x) = 1/2 return BinarySX::create(OP_DIV,1,y.getDep(0)); else if(hasDep() && getOp()==OP_NEG && getDep(0).isEquivalent(y)) // (-x)/x = -1 return -1; else if(y.hasDep() && y.getOp()==OP_NEG && y.getDep(0).isEquivalent(*this)) // x/(-x) = 1 return -1; else if(y.hasDep() && y.getOp()==OP_NEG && hasDep() && getOp()==OP_NEG && getDep(0).isEquivalent(y.getDep(0))) // (-x)/(-x) = 1 return 1; else if(isOp(OP_DIV) && y.isEquivalent(node->dep(0))) return node->dep(1).inv(); else // create a new branch return BinarySX::create(OP_DIV,*this,y); }