Skip to content

Commit

Permalink
Fix solve(x = x sqrt(x))
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Jul 18, 2023
1 parent 6ecd8db commit 0e4495d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/fintamath/expressions/functions/ExpressionFunctionSolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ std::shared_ptr<const INumber> getMulElementPower(const std::shared_ptr<const IE

ArgumentPtr getElementRate(const ArgumentPtr &elem, const Variable &var);

ArgumentsPtrVector getVariablePowerRates(const ArgumentPtr &elem, const Variable &var);
ArgumentsPtrVector getVariableIntPowerRates(const ArgumentPtr &elem, const Variable &var);

ArgumentsPtrVector solveCubicEquation(const ArgumentsPtrVector &coeffAtPow);

Expand All @@ -41,7 +41,7 @@ Expression solve(const Expression &rhs) {
// TODO: remove this if when inequalities will be implemented
if (!is<Eqv>(compExpr->getFunction())) {
auto var = cast<Variable>(compExpr->getVariables().front());
ArgumentsPtrVector powerRate = getVariablePowerRates(compExpr->getChildren().front(), var);
ArgumentsPtrVector powerRate = getVariableIntPowerRates(compExpr->getChildren().front(), var);

if (powerRate.size() == 2) {
compExpr->markAsSolution();
Expand All @@ -50,7 +50,7 @@ Expression solve(const Expression &rhs) {
}
if (is<Eqv>(compExpr->getFunction())) {
auto var = cast<Variable>(compExpr->getVariables().front());
ArgumentsPtrVector powerRates = getVariablePowerRates(compExpr->getChildren().front(), var);
ArgumentsPtrVector powerRates = getVariableIntPowerRates(compExpr->getChildren().front(), var);
ArgumentsPtrVector roots;

switch (powerRates.size()) {
Expand Down Expand Up @@ -148,7 +148,7 @@ ArgumentPtr getElementRate(const ArgumentPtr &elem, const Variable &var) {
return elem;
}

ArgumentsPtrVector getVariablePowerRates(const ArgumentPtr &elem, const Variable &var) {
ArgumentsPtrVector getVariableIntPowerRates(const ArgumentPtr &elem, const Variable &var) {
ArgumentsPtrVector powerRates;
ArgumentsPtrVector polynomVect;

Expand All @@ -172,6 +172,9 @@ ArgumentsPtrVector getVariablePowerRates(const ArgumentPtr &elem, const Variable

powerRates[size_t(*intPow)] = rate;
}
else {
return {};
}
}

return powerRates;
Expand Down
2 changes: 1 addition & 1 deletion tests/src/expressions/ExpressionFunctionsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ TEST(ExpressionFunctionsTests, orTest) {
TEST(ExpressionFunctionsTests, solveTest) {
EXPECT_EQ(solve(Expression("x - 10 = 0")).toString(), "x = 10");
EXPECT_EQ(solve(Expression("-10 - x = 0")).toString(), "x = -10");
// EXPECT_EQ(solve(Expression("x = x sqrt(x)")).toString(), "x = 0 | x = 1"); // TODO! fix
EXPECT_EQ(solve(Expression("x = x sqrt(x)")).toString(), "x^(3/2) - x = 0");

EXPECT_EQ(solve(Expression("x^2 - 10 = 39")).toString(), "x = -7 | x = 7");
EXPECT_EQ(solve(Expression("x^2 = 0")).toString(), "x = 0");
Expand Down

0 comments on commit 0e4495d

Please sign in to comment.