Skip to content

Commit

Permalink
Fix "(1/2 + sqrt(3)) / (x^(1/4) + 6)"
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Oct 16, 2023
1 parent a0c54a2 commit 43f9b94
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/fintamath/expressions/binary/DivExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,14 @@ std::pair<ArgumentPtr, ArgumentPtr> DivExpression::mulSumSimplify(const Argument
ArgumentPtr result = divExpr(lhs, rhsChildren.front());
simplifyChild(result);

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

if (is<Mul>(resultExpr->getFunction()) && is<Rational>(resultExpr->getChildren().front())) {
return {};
}
}

ArgumentPtrVector multiplicators;
Expand Down
5 changes: 3 additions & 2 deletions tests/src/expressions/ExpressionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ TEST(ExpressionTests, stringConstructorTest) {
EXPECT_EQ(Expression("(3x^6 + 5x^5 - 2x^4 + 4x^3 + x^2 + 3x - 5) / (x^4 + 3x^2 - 2)").toString(),
"3 x^2 + 5 x - 11 - (11 x^3 - 40 x^2 - 13 x + 27)/(x^4 + 3 x^2 - 2)");
EXPECT_EQ(Expression("(6x^8 - 7x^6 + 9x^4 - 4x^2 + 8) / (2x^3 - x^2 + 3x - 1)").toString(),
"3 x^5 + (3 x^4)/2 - (29 x^3)/4 - (35 x^2)/8 + (223 x)/16 + 317/32 - (1289 x^2 + 505 x - 573)/(64 x^3 - 32 x^2 + 96 x - 32)");
"3 x^5 - 8 x^3 - 4 + (3 x^7 - 5 x^5 + 33 x^4 - 8 x^2 + 12 x + 4)/(2 x^3 - x^2 + 3 x - 1)");
EXPECT_EQ(Expression("(2 a^3 + 5 a^2 b + 4 a b^2 + b^3)/(25 a^2 + 40 a b + 15 b^2)").toString(),
"(2 a)/25 + (9 b)/125 - (2 a b^2 + 2 b^3)/(625 a^2 + 1000 a b + 375 b^2)");
"(2 a^3 + 5 a^2 b + 4 a b^2 + b^3)/(25 a^2 + 40 a b + 15 b^2)");
EXPECT_EQ(Expression("(25 a^2 + 40 a b + 15 b^2)/(2 a^3 + 5 a^2 b + 4 a b^2 + b^3)").toString(),
"(25 a^2 + 40 a b + 15 b^2)/(2 a^3 + 5 a^2 b + 4 a b^2 + b^3)");
EXPECT_EQ(Expression("(x^2 + 2x + 1)/(x^3 + 3x^2 + 3x + 1)").toString(), "1/(x + 1)");
Expand Down Expand Up @@ -363,6 +363,7 @@ TEST(ExpressionTests, stringConstructorTest) {
EXPECT_EQ(Expression("(a+b)*(a+b)/(a+b)").toString(), "a + b");
EXPECT_EQ(Expression("(a+b)*(a+b)*(1/(a+b))").toString(), "a + b");
EXPECT_EQ(Expression("(x^2+2x+1)/(x+1)").toString(), "x + 1");
EXPECT_EQ(Expression("(1/2 + sqrt(3)) / (x^(1/4) + 6)").toString(), "(2 sqrt(3) + 1)/(2 root(x, 4) + 12)");

EXPECT_EQ(Expression("(x/y)^2").toString(), "(x^2)/(y^2)");
EXPECT_EQ(Expression("(x/y)^(1/2)").toString(), "sqrt(x)/sqrt(y)");
Expand Down

0 comments on commit 43f9b94

Please sign in to comment.