From 30b6a893cbf7411071de50e5789f1f59c83e8e5a Mon Sep 17 00:00:00 2001 From: fintarin Date: Thu, 13 Jul 2023 13:42:26 +0300 Subject: [PATCH] Fix solve(x = x sqrt(x)) --- .../expressions/functions/ExpressionFunctionSolve.cpp | 11 +++++++---- tests/src/expressions/ExpressionFunctionsTests.cpp | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/fintamath/expressions/functions/ExpressionFunctionSolve.cpp b/src/fintamath/expressions/functions/ExpressionFunctionSolve.cpp index ac461c02e..df2d47edd 100644 --- a/src/fintamath/expressions/functions/ExpressionFunctionSolve.cpp +++ b/src/fintamath/expressions/functions/ExpressionFunctionSolve.cpp @@ -24,7 +24,7 @@ std::shared_ptr getMulElementPower(const std::shared_ptr(compExpr->getFunction())) { auto var = cast(compExpr->getVariables().front()); - ArgumentsPtrVector powerRate = getVariablePowerRates(compExpr->getChildren().front(), var); + ArgumentsPtrVector powerRate = getVariableIntPowerRates(compExpr->getChildren().front(), var); if (powerRate.size() == 2) { compExpr->markAsSolution(); @@ -50,7 +50,7 @@ Expression solve(const Expression &rhs) { } if (is(compExpr->getFunction())) { auto var = cast(compExpr->getVariables().front()); - ArgumentsPtrVector powerRates = getVariablePowerRates(compExpr->getChildren().front(), var); + ArgumentsPtrVector powerRates = getVariableIntPowerRates(compExpr->getChildren().front(), var); ArgumentsPtrVector roots; switch (powerRates.size()) { @@ -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; @@ -172,6 +172,9 @@ ArgumentsPtrVector getVariablePowerRates(const ArgumentPtr &elem, const Variable powerRates[size_t(*intPow)] = rate; } + else { + return {}; + } } return powerRates; diff --git a/tests/src/expressions/ExpressionFunctionsTests.cpp b/tests/src/expressions/ExpressionFunctionsTests.cpp index 55ea8c39c..7fc1f5595 100644 --- a/tests/src/expressions/ExpressionFunctionsTests.cpp +++ b/tests/src/expressions/ExpressionFunctionsTests.cpp @@ -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");