Expression * BinomialCoefficient::shallowReduce(Context& context, AngleUnit angleUnit) { Expression * e = Expression::shallowReduce(context, angleUnit); if (e != this) { return e; } Expression * op0 = editableOperand(0); Expression * op1 = editableOperand(1); #if MATRIX_EXACT_REDUCING if (op0->type() == Type::Matrix || op1->type() == Type::Matrix) { return replaceWith(new Undefined(), true); } #endif if (op0->type() == Type::Rational) { Rational * r0 = static_cast<Rational *>(op0); if (!r0->denominator().isOne() || r0->numerator().isNegative()) { return replaceWith(new Undefined(), true); } } if (op1->type() == Type::Rational) { Rational * r1 = static_cast<Rational *>(op1); if (!r1->denominator().isOne() || r1->numerator().isNegative()) { return replaceWith(new Undefined(), true); } } if (op0->type() != Type::Rational || op1->type() != Type::Rational) { return this; } Rational * r0 = static_cast<Rational *>(op0); Rational * r1 = static_cast<Rational *>(op1); Integer n = r0->numerator(); Integer k = r1->numerator(); if (n.isLowerThan(k)) { return replaceWith(new Undefined(), true); } /* if n is too big, we do not reduce to avoid too long computation. * The binomial coefficient will be evaluate approximatively later */ if (Integer(k_maxNValue).isLowerThan(n)) { return this; } Rational result(1); Integer kBis = Integer::Subtraction(n, k); k = kBis.isLowerThan(k) ? kBis : k; int clippedK = k.extractedInt(); // Authorized because k < n < k_maxNValue for (int i = 0; i < clippedK; i++) { Rational factor = Rational(Integer::Subtraction(n, Integer(i)), Integer::Subtraction(k, Integer(i))); result = Rational::Multiplication(result, factor); } return replaceWith(new Rational(result), true); }
bool DifferentialEvolution::advanceGeneration(StateP state, DemeP deme) { // create donor vectors for each population member for(uint iIter = 0; iIter < deme->size(); iIter++){ createDonorVectors(deme, state); } // perform DE crossover, generate trial vectors (stored in donor_vector) for(uint iIter = 0; iIter < deme->size(); iIter++) { crossover(deme, iIter, state); } // select the better one for each population member and trial vector for(uint iIter = 0; iIter < deme->size(); iIter++) { evaluate(donor_vector[iIter]); if(donor_vector[iIter]->fitness->isBetterThan(deme->at(iIter)->fitness)) replaceWith(deme->at(iIter), donor_vector[iIter]); } //for(uint i = 0; i < deme->size(); i++){ // state->getLogger()->log(5, "deme[" + uint2str(i) + "]: " + dbl2str(deme->at(i)->fitness->getValue()) + "\t" + uint2str(deme->at(i)->index)); //} donor_vector.clear(); return true; }
bool DropCapsid::onContactAstroObj(ContactInfo& cinfo) { if (cinfo.contact.getEventCode() == PhysicsContact::EventCode::POSTSOLVE) { if (cpArbiter* arb = static_cast<cpArbiter*>(cinfo.contact.getContactInfo())) { cpContactPointSet cps = cpArbiterGetContactPointSet(arb); bool moving = false; for (int i = 0; i < cps.count; ++i) { cpVect va = cpBodyGetVelocityAtWorldPoint(arb->body_a, cps.points[i].pointA); cpVect vb = cpBodyGetVelocityAtWorldPoint(arb->body_b, cps.points[i].pointB); cpVect rv = cpvsub(va, vb); cpFloat wa = cpBodyGetAngularVelocity(arb->body_a); cpFloat wb = cpBodyGetAngularVelocity(arb->body_b); cpFloat rw = wa - wb; //if (cpvlengthsq(rv) > 1e-6 || fabs(rw) > 0.5) { if (cpvlengthsq(rv) > 1.0 || fabs(rw) > 5) { moving = true; break; } } if (!moving) { Unit* created = onLandCreate(_game); Player* player = _player; replaceWith(created); // this is destroyed float up = (cinfo.thisBody->getPosition() - cinfo.thatBody->getPosition()).getAngle() / (M_PI / 180.0); created->getNode()->getPhysicsBody()->setRotation(90 - up); created->setPlayer(player); // CCLOG("LANDING up# %f", up); return true; } } } return true; }
bool UndoMulti<T>::deleteID(const QString &id) { for(Undo::List::Iterator it = mUndos.begin(); it != mUndos.end(); ++it) { UndoItem *item = *it; if(item->eventID() == id) { // Found a matching entry - remove it mUndos.remove(it); if(mUndos.count() == 1) { // There is only one entry left after removal. // Replace 'this' multi instance with the remaining single entry. replaceWith(item); return true; } else { delete item; return false; } } } return false; }
Expression * Rational::shallowBeautify(Context & context, AngleUnit angleUnit) { if (m_numerator.isNegative()) { m_numerator.setNegative(false); Opposite * o = new Opposite(this, true); return replaceWith(o, true); } return this; }
void DD::ReplaceString(char *string_, int size_, char *replaceThis_, char *replaceWith_) { std::string s(string_); std::string replaceThis(replaceThis_); std::string replaceWith(replaceWith_); s.replace(s.find(replaceThis), replaceThis.length(), replaceWith); DD_Strcpy(string_, size_, s.c_str()); }
Expression * AbsoluteValue::shallowReduce(Context& context, AngleUnit angleUnit) { Expression * e = Expression::shallowReduce(context, angleUnit); if (e != this) { return e; } Expression * op = editableOperand(0); #if MATRIX_EXACT_REDUCING if (op->type() == Type::Matrix) { return SimplificationEngine::map(this, context, angleUnit); } #endif if (op->sign() == Sign::Positive) { return replaceWith(op, true); } if (op->sign() == Sign::Negative) { Expression * newOp = op->setSign(Sign::Positive, context, angleUnit); return replaceWith(newOp, true); } return this; }
Expression * Division::shallowReduce(Context& context, AngleUnit angleUnit) { Expression * e = Expression::shallowReduce(context, angleUnit); if (e != this) { return e; } Power * p = new Power(operand(1), new Rational(-1), false); Multiplication * m = new Multiplication(operand(0), p, false); detachOperands(); p->shallowReduce(context, angleUnit); replaceWith(m, true); return m->shallowReduce(context, angleUnit); }
Expression * MatrixInverse::shallowReduce(Context& context, AngleUnit angleUnit) { Expression * e = Expression::shallowReduce(context, angleUnit); if (e != this) { return e; } Expression * op = editableOperand(0); #if MATRIX_EXACT_REDUCING if (!op->recursivelyMatches(Expression::IsMatrix)) { detachOperand(op); return replaceWith(new Power(op, new Rational(-1), false), true)->shallowReduce(context, angleUnit); } if (op->type() == Type::Matrix) { Matrix * mat = static_cast<Matrix *>(op); if (mat->numberOfRows() != mat->numberOfColumns()) { return replaceWith(new Undefined(), true); } } return this; #else detachOperand(op); return replaceWith(new Power(op, new Rational(-1), false), true)->shallowReduce(context, angleUnit); #endif }
Expression * NaperianLogarithm::shallowReduce(Context& context, AngleUnit angleUnit) { Expression * e = Expression::shallowReduce(context, angleUnit); if (e != this) { return e; } #if MATRIX_EXACT_REDUCING if (operand(0)->type() == Type::Matrix) { return SimplificationEngine::map(this, context, angleUnit); } #endif const Expression * logOperands[2] = {operand(0)->clone(), new Symbol(Ion::Charset::Exponential)}; Logarithm * l = new Logarithm(logOperands, 2, false); replaceWith(l, true); return l->shallowReduce(context, angleUnit); }
Expression * RealPart::shallowReduce(Context& context, AngleUnit angleUnit) { Expression * e = Expression::shallowReduce(context, angleUnit); if (e != this) { return e; } Expression * op = editableOperand(0); #if MATRIX_EXACT_REDUCING if (op->type() == Type::Matrix) { return SimplificationEngine::map(this, context, angleUnit); } #endif if (op->type() == Type::Rational) { return replaceWith(op, true); } return this; }
bool BSStringT::Replace(const char *_toReplace, const char *_replaceWith) { if (!m_data || !_toReplace) return false; std::string curr(m_data, m_dataLen); std::string toReplace(_toReplace); std::string::iterator currBegin = curr.begin(); std::string::iterator currEnd = curr.end(); std::string::iterator replaceIt = std::search(currBegin, currEnd, toReplace.begin(), toReplace.end(), ci_equal); if (replaceIt != currEnd) { std::string replaceWith(_replaceWith); // we found the substring, now we need to do the modification std::string::size_type replacePos = distance(currBegin, replaceIt); curr.replace(replacePos, toReplace.size(), replaceWith); Set(curr.c_str()); return true; } return false; }
void HorizontalLayout::privateReplaceChild(const ExpressionLayout * oldChild, ExpressionLayout * newChild, bool deleteOldChild, ExpressionLayoutCursor * cursor) { bool oldWasAncestorOfNewLayout = false; if (newChild->hasAncestor(this)) { newChild->editableParent()->detachChild(newChild); oldWasAncestorOfNewLayout = true; } int oldChildIndex = indexOfChild(oldChild); if (newChild->isEmpty()) { if (numberOfChildren() > 1) { /* If the new layout is empty and the horizontal layout has other * children, just delete the old child. */ if (!newChild->hasAncestor(oldChild)) { delete newChild; } removeChildAtIndex(oldChildIndex, deleteOldChild); if (cursor == nullptr) { return; } if (oldChildIndex == 0) { cursor->setPointedExpressionLayout(this); cursor->setPosition(ExpressionLayoutCursor::Position::Left); return; } cursor->setPointedExpressionLayout(editableChild(oldChildIndex -1)); cursor->setPosition(ExpressionLayoutCursor::Position::Right); return; } /* If the new layout is empty and it was the only horizontal layout child, * replace the horizontal layout with this empty layout (only if this is not * the main layout, so only if the layout has a parent). */ if (m_parent) { if (!deleteOldChild) { removeChildAtIndex(indexOfChild(oldChild), false); } if (cursor) { replaceWithAndMoveCursor(newChild, true, cursor); return; } replaceWith(newChild, deleteOldChild); return; } /* If this is the main horizontal layout, the old child its only child and * the new child is Empty, remove the old child and delete the new child. */ assert(m_parent == nullptr); removeChildAtIndex(0, deleteOldChild); delete newChild; if (cursor == nullptr) { return; } cursor->setPointedExpressionLayout(this); cursor->setPosition(ExpressionLayoutCursor::Position::Left); return; } /* If the new child is also an horizontal layout, steal the children of the * new layout then destroy it. */ if (newChild->isHorizontal()) { int indexForInsertion = indexOfChild(oldChild); if (cursor != nullptr) { /* If the old layout is not an ancestor of the new layout, or if the * cursor was on the right of the new layout, place the cursor on the * right of the new layout, which is left of the next sibling or right of * the parent. */ if (!oldWasAncestorOfNewLayout || cursor->position() == ExpressionLayoutCursor::Position::Right) { if (oldChildIndex == numberOfChildren() - 1) { cursor->setPointedExpressionLayout(this); cursor->setPosition(ExpressionLayoutCursor::Position::Right); } else { cursor->setPointedExpressionLayout(editableChild(oldChildIndex + 1)); cursor->setPosition(ExpressionLayoutCursor::Position::Left); } } else { /* Else place the cursor on the left of the new layout, which is right * of the previous sibling or left of the parent. */ if (oldChildIndex == 0) { cursor->setPointedExpressionLayout(this); cursor->setPosition(ExpressionLayoutCursor::Position::Left); } else { cursor->setPointedExpressionLayout(editableChild(oldChildIndex - 1)); cursor->setPosition(ExpressionLayoutCursor::Position::Right); } } } bool oldChildRemovedAtMerge = oldChild->isEmpty(); mergeChildrenAtIndex(static_cast<HorizontalLayout *>(newChild), indexForInsertion + 1, true); if (!oldChildRemovedAtMerge) { removeChildAtIndex(indexForInsertion, deleteOldChild); } return; } // Else, just replace the child. if (cursor != nullptr && !oldWasAncestorOfNewLayout) { cursor->setPosition(ExpressionLayoutCursor::Position::Right); } ExpressionLayout::replaceChild(oldChild, newChild, deleteOldChild); if (cursor == nullptr) { return; } cursor->setPointedExpressionLayout(newChild); }