Skip to content

Commit

Permalink
Refactoring of DivExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Jul 6, 2023
1 parent 7403fac commit e557f1b
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/fintamath/expressions/binary/DivExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,32 +288,32 @@ ArgumentPtr DivExpression::sumMulSimplify(const ArgumentPtr &lhs, const Argument
return {};
}

ArgumentsPtrVector divSuccess;
ArgumentsPtrVector divFailure;
ArgumentsPtrVector result;
ArgumentsPtrVector remainder;

for (const auto &child : lhsChildren) {
ArgumentPtr divResult = makeExpr(Div(), child, rhs);
simplifyChild(divResult);

if (const auto divResultExpr = cast<IExpression>(divResult);
divResultExpr && is<Div>(divResultExpr->getFunction()) && *divResultExpr->getChildren().back() == *rhs) {
divFailure.emplace_back(child);
remainder.emplace_back(child);
}
else {
divSuccess.emplace_back(divResult);
result.emplace_back(divResult);
}
}

if (divFailure.size() == lhsChildren.size()) {
if (remainder.size() == lhsChildren.size()) {
return {};
}

if (!divFailure.empty()) {
ArgumentPtr divExpr = makeExpr(Div(), makeExpr(Add(), divFailure), rhs);
divSuccess.emplace_back(divExpr);
if (!remainder.empty()) {
ArgumentPtr divExpr = makeExpr(Div(), makeExpr(Add(), remainder), rhs);
result.emplace_back(divExpr);
}

return makeExpr(Add(), divSuccess);
return makeExpr(Add(), result);
}

std::pair<ArgumentPtr, ArgumentPtr> DivExpression::mulSumSimplify(const ArgumentPtr &lhs, const ArgumentPtr &rhs) {
Expand All @@ -328,22 +328,22 @@ std::pair<ArgumentPtr, ArgumentPtr> DivExpression::mulSumSimplify(const Argument
return {};
}

ArgumentPtr divResult = makeExpr(Div(), lhs, rhsChildren.front());
simplifyChild(divResult);
ArgumentPtr result = makeExpr(Div(), lhs, rhsChildren.front());
simplifyChild(result);

if (const auto divExpr = cast<IExpression>(divResult); divExpr && is<Div>(divExpr->getFunction())) {
if (const auto divExpr = cast<IExpression>(result); divExpr && is<Div>(divExpr->getFunction())) {
return {};
}

ArgumentsPtrVector multiplicator;

for (size_t i = 1; i < rhsChildren.size(); i++) {
multiplicator.emplace_back(makeExpr(Mul(), rhsChildren[i], divResult));
multiplicator.emplace_back(makeExpr(Mul(), rhsChildren[i], result));
}

ArgumentPtr negSum = makeExpr(Neg(), makeExpr(Add(), multiplicator));
ArgumentPtr div = makeExpr(Div(), negSum, rhs);
return {divResult, div};
ArgumentPtr remainder = makeExpr(Div(), negSum, rhs);
return {result, remainder};
}

ArgumentPtr DivExpression::divPowSimplify(const ArgumentPtr &lhs, const ArgumentPtr &rhs) {
Expand Down

0 comments on commit e557f1b

Please sign in to comment.