Skip to content

Commit

Permalink
Refactor setVariables
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Jun 30, 2023
1 parent 6d377e0 commit b495aa0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion include/fintamath/expressions/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Expression : public IExpressionCRTP<Expression> {

void setChildren(const ArgumentsPtrVector &childVect) override;

void setVariables(const std::vector<Variable> &vars, const ArgumentsPtrVector &vals) override;
void setVariables(const std::vector<std::pair<Variable, ArgumentPtr>> &varsToVals) override;

void setVariable(const Variable &var, const Expression &val);

Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/expressions/IExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class IExpression : public IArithmetic {

std::vector<Variable> getVariables() const;

virtual void setVariables(const std::vector<Variable> &vars, const ArgumentsPtrVector &vals);
virtual void setVariables(const std::vector<std::pair<Variable, ArgumentPtr>> &varsToVals);

std::unique_ptr<IMathObject> toMinimalObject() const final;

Expand Down
6 changes: 3 additions & 3 deletions src/fintamath/expressions/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,13 @@ void Expression::setChildren(const ArgumentsPtrVector &childVect) {
child = childVect.front()->toMinimalObject();
}

void Expression::setVariables(const std::vector<Variable> &vars, const ArgumentsPtrVector &vals) {
IExpression::setVariables(vars, vals);
void Expression::setVariables(const std::vector<std::pair<Variable, ArgumentPtr>> &varsToVals) {
IExpression::setVariables(varsToVals);
simplifyChild(child);
}

void Expression::setVariable(const Variable &var, const Expression &val) {
setVariables({var}, ArgumentsPtrVector{val.getChildren().front()});
setVariables({{var, val.child}});
}

Expression operator+(const Variable &lhs, const Variable &rhs) {
Expand Down
16 changes: 9 additions & 7 deletions src/fintamath/expressions/IExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,27 @@ std::vector<Variable> IExpression::getVariables() const {
return vars;
}

void IExpression::setVariables(const std::vector<Variable> &vars, const ArgumentsPtrVector &vals) {
void IExpression::setVariables(const std::vector<std::pair<Variable, ArgumentPtr>> &varsToVals) {
auto children = getChildren();

ArgumentsPtrVector newChildren;

for (auto &child : children) {
if (std::shared_ptr<IExpression> exprChild = cast<IExpression>(child->clone())) {
exprChild->setVariables(vars, vals);
exprChild->setVariables(varsToVals);
newChildren.emplace_back(exprChild);
continue;
}

bool isAdded = false;

for (size_t i = 0; i < vars.size(); i++) {
if (const auto varChild = cast<Variable>(child); varChild && *varChild == vars[i]) {
newChildren.push_back(vals[i]->clone());
isAdded = true;
break;
if (const auto varChild = cast<Variable>(child)) {
for (const auto &varsToVal : varsToVals) {
if (*varChild == varsToVal.first) {
newChildren.push_back(varsToVal.second);
isAdded = true;
break;
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/fintamath/expressions/functions/ExpressionFunctionSolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,22 @@ ArgumentsPtrVector solveQuadraticEquation(const ArgumentsPtrVector &coeffAtPow)
// TODO: remove this try/catch when complex numbers will be implemented
try {
Expression firstRootValue = firstRoot;
firstRootValue.setVariables({c, b, a}, coeffAtPow);
firstRootValue.setVariables({
{c, coeffAtPow[0]}, //
{b, coeffAtPow[1]}, //
{a, coeffAtPow[2]}, //
});

Expression secondRootValue = secondRoot;
secondRootValue.setVariables({c, b, a}, coeffAtPow);
secondRootValue.setVariables({
{c, coeffAtPow[0]}, //
{b, coeffAtPow[1]}, //
{a, coeffAtPow[2]}, //
});

return {firstRootValue.getChildren().front(), secondRootValue.getChildren().front()};
}

catch (const UndefinedException &) {
return {};
}
Expand Down

0 comments on commit b495aa0

Please sign in to comment.