Skip to content

Commit

Permalink
Add test & refactor DivExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Jun 25, 2023
1 parent 28aee46 commit bde7468
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/fintamath/expressions/binary/DivExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,12 @@ ArgumentPtr DivExpression::denominatorSumSimplify(const ArgumentPtr &lhs, const
for (const auto &child : rhsChildren) {
ArgumentPtr childForCheck = child;
unwrapNeg(childForCheck);

if (const auto &divChild = cast<DivExpression>(childForCheck)) {
multiplicator.emplace_back(divChild->rhsChild);
continue;
}

if (const auto &rationalChild = cast<Rational>(childForCheck)) {
multiplicator.emplace_back(std::make_shared<Integer>(rationalChild->denominator()));
continue;
Expand Down Expand Up @@ -526,6 +528,7 @@ ArgumentPtr DivExpression::denominatorSumSimplify(const ArgumentPtr &lhs, const

ArgumentPtr DivExpression::denominatorMulSimplify(const ArgumentsPtrVector &rhsChildren) {
ArgumentsPtrVector multiplicator;

for (const auto &child : rhsChildren) {
if (const auto &rationalChild = cast<Rational>(child)) {
multiplicator.emplace_back(std::make_shared<Integer>(rationalChild->denominator()));
Expand Down
25 changes: 22 additions & 3 deletions tests/src/expressions/ExpressionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,13 @@ TEST(ExpressionTests, stringConstructorTest) {
EXPECT_EQ(Expression("(a b)/-2").toString(), "-1/2 a b");
EXPECT_EQ(Expression("(a b)/(-a - b)").toString(), "-b + b^2/(a + b)");
EXPECT_EQ(Expression("(x^5)/(x - y)").toString(), "x^4 + x^3 y + x^2 y^2 + x y^3 + y^4 + y^5/(x - y)");
EXPECT_EQ(Expression("(3 x + 5/9)/(2y - 9/x + 3/2 x + 1/2 + 2 y / x)").toString(), "2 - (16 y - 72)/(6 x^2 + 8 x y + 2 x + 8 y - 36) + (-16 x^2 y)/(6 x^3 + 8 x^2 y + 2 x^2 + 8 x y - 36 x) + (-16 x)/(54 x^2 + 72 x y + 18 x + 72 y - 324)");
EXPECT_EQ(Expression("(a/x + b/(y+3/r)/4)/(3+t/5)").toString(), "(5 a)/(t x + 15 x) + (25 b r)/(20 r t y + 300 r y + 60 t + 900)");
EXPECT_EQ(Expression("(x/a - (b+5)/(y-8/(12 y))/4)/(8-a/5)").toString(), "-(300 b y + 1500 y)/(-240 a y^2 + 160 a + 9600 y^2 - 6400) + (5 x)/(-a^2 + 40 a)");
EXPECT_EQ(Expression("(3 x + 5/9)/(2y - 9/x + 3/2 x + 1/2 + 2 y / x)").toString(),
"2 - (16 y - 72)/(6 x^2 + 8 x y + 2 x + 8 y - 36) + (-16 x^2 y)/(6 x^3 + 8 x^2 y + 2 x^2 + 8 x y - 36 x) + "
"(-16 x)/(54 x^2 + 72 x y + 18 x + 72 y - 324)");
EXPECT_EQ(Expression("(a/x + b/(y+3/r)/4)/(3+t/5)").toString(),
"(5 a)/(t x + 15 x) + (25 b r)/(20 r t y + 300 r y + 60 t + 900)");
EXPECT_EQ(Expression("(x/a - (b+5)/(y-8/(12 y))/4)/(8-a/5)").toString(),
"-(300 b y + 1500 y)/(-240 a y^2 + 160 a + 9600 y^2 - 6400) + (5 x)/(-a^2 + 40 a)");

EXPECT_EQ(Expression("0^a").toString(), "0");
EXPECT_EQ(Expression("(a b)^0").toString(), "1");
Expand Down Expand Up @@ -686,6 +690,21 @@ TEST(ExpressionTests, stringConstructorLargeTest) {
"x^7 y^23 + 593775 x^6 y^24 - 142506 x^5 y^25 + 27405 x^4 y^26 - 4060 x^3 y^27 + 435 x^2 y^28 - 30 x y^29 + "
"y^30");

EXPECT_EQ(
Expression("(a + b + c^2) / ((a + b + c^3) / (5/2 * (a + b) / (3/b + c/2)))").toString(),
"5 c - (2500 a^6 b^2 c^5 + 7500 a^5 b^3 c^5 + 7500 a^4 b^4 c^5 + 2500 a^3 b^5 c^5)/(500 a^7 b^2 c + 3000 a^7 b + "
"2000 a^6 b^3 c + 500 a^6 b^2 c^4 + 12000 a^6 b^2 + 3000 a^6 b c^3 + 3000 a^5 b^4 c + 1500 a^5 b^3 c^4 + 18000 "
"a^5 b^3 + 9000 a^5 b^2 c^3 + 2000 a^4 b^5 c + 1500 a^4 b^4 c^4 + 12000 a^4 b^4 + 9000 a^4 b^3 c^3 + 500 a^3 b^6 "
"c + 500 a^3 b^5 c^4 + 3000 a^3 b^5 + 3000 a^3 b^4 c^3) - (18750 a^4 c + 37500 a^3 b c + 18750 a^2 b^2 c)/(625 "
"a^4 b c + 3750 a^4 + 1250 a^3 b^2 c + 625 a^3 b c^4 + 7500 a^3 b + 3750 a^3 c^3 + 625 a^2 b^3 c + 625 a^2 b^2 "
"c^4 + 3750 a^2 b^2 + 3750 a^2 b c^3) - (-3000 a^3 b c^4 - 6000 a^2 b^2 c^4 - 3000 a b^3 c^4)/(100 a^5 b c + 600 "
"a^5 + 300 a^4 b^2 c + 100 a^4 b c^4 + 1800 a^4 b + 600 a^4 c^3 + 300 a^3 b^3 c + 200 a^3 b^2 c^4 + 1800 a^3 b^2 "
"+ 1200 a^3 b c^3 + 100 a^2 b^4 c + 100 a^2 b^3 c^4 + 600 a^2 b^3 + 600 a^2 b^2 c^3) - (187500 a^3 c^4 + 375000 "
"a^2 b c^4 + 187500 a b^2 c^4)/(6250 a^4 b c + 37500 a^4 + 12500 a^3 b^2 c + 6250 a^3 b c^4 + 75000 a^3 b + "
"37500 a^3 c^3 + 6250 a^2 b^3 c + 6250 a^2 b^2 c^4 + 37500 a^2 b^2 + 37500 a^2 b c^3) + (3125 a^5 b^2 + 9375 a^4 "
"b^3 + 9375 a^3 b^4 + 3125 a^2 b^5)/(625 a^4 b^2 c + 3750 a^4 b + 1250 a^3 b^3 c + 625 a^3 b^2 c^4 + 7500 a^3 "
"b^2 + 3750 a^3 b c^3 + 625 a^2 b^4 c + 625 a^2 b^3 c^4 + 3750 a^2 b^3 + 3750 a^2 b^2 c^3)");

EXPECT_EQ(
Expression("sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin("
"sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin("
Expand Down

0 comments on commit bde7468

Please sign in to comment.