From 8dd35a156422ac66a0f14700642b885d39fb2aeb Mon Sep 17 00:00:00 2001 From: fintarin Date: Sat, 15 Jul 2023 23:39:04 +0300 Subject: [PATCH] Change toString: x^2/y^2 to (x^2)/(y^2) --- src/fintamath/expressions/ExpressionUtils.cpp | 4 ++++ .../expressions/binary/PowExpression.cpp | 19 +++++++++++++++++-- .../expressions/ExpressionFunctionsTests.cpp | 4 ++-- tests/src/expressions/ExpressionTests.cpp | 14 +++++++------- .../src/functions/calculus/IntegralTests.cpp | 2 +- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/fintamath/expressions/ExpressionUtils.cpp b/src/fintamath/expressions/ExpressionUtils.cpp index 63b061e4c..3eaadc095 100644 --- a/src/fintamath/expressions/ExpressionUtils.cpp +++ b/src/fintamath/expressions/ExpressionUtils.cpp @@ -52,6 +52,10 @@ std::string binaryOperatorChildToString(const IOperator &oper, const ArgumentPtr IOperator::Priority operPriority = oper.getOperatorPriority(); IOperator::Priority lhsOperPriority = childOper->getOperatorPriority(); + if (operPriority == IOperator::Priority::Multiplication) { + return putInBrackets(childStr); + } + if (lhsOperPriority > operPriority || (lhsOperPriority == operPriority && !oper.isAssociative())) { return putInBrackets(childStr); } diff --git a/src/fintamath/expressions/binary/PowExpression.cpp b/src/fintamath/expressions/binary/PowExpression.cpp index dd2841001..be6a9ef4d 100644 --- a/src/fintamath/expressions/binary/PowExpression.cpp +++ b/src/fintamath/expressions/binary/PowExpression.cpp @@ -43,8 +43,23 @@ std::string PowExpression::toString() const { } std::shared_ptr PowExpression::getOutputFunction() const { - if (*rhsChild == Rational(1, 2)) { - return std::make_shared(); + if (auto rhsChildRat = cast(rhsChild)) { + const Integer &numerator = rhsChildRat->numerator(); + const Integer &denominator = rhsChildRat->denominator(); + + if (numerator == 1) { + if (denominator == 2) { + return std::make_shared(); + } + + return std::make_shared(); + } + } + + if (auto rhsChildExpr = cast(rhsChild); rhsChildExpr && is
(rhsChildExpr->getFunction())) { + if (*rhsChildExpr->getChildren().front() == Integer(1)) { + return std::make_shared(); + } } return IBinaryExpression::getFunction(); diff --git a/tests/src/expressions/ExpressionFunctionsTests.cpp b/tests/src/expressions/ExpressionFunctionsTests.cpp index 7fc1f5595..58a5a1f4d 100644 --- a/tests/src/expressions/ExpressionFunctionsTests.cpp +++ b/tests/src/expressions/ExpressionFunctionsTests.cpp @@ -36,11 +36,11 @@ TEST(ExpressionFunctionsTests, subTest) { } TEST(ExpressionFunctionsTests, divTest) { - EXPECT_EQ(div(Variable("a"), Expression("b^2")).toString(), "a/b^2"); + EXPECT_EQ(div(Variable("a"), Expression("b^2")).toString(), "a/(b^2)"); EXPECT_EQ(div(10, Expression("a+2")).toString(), "10/(a + 2)"); EXPECT_EQ(div(Variable("a"), Expression("a+2")).toString(), "1 - 2/(a + 2)"); EXPECT_EQ(div(Expression("b+2"), Expression("a+2")).toString(), "(b + 2)/(a + 2)"); - EXPECT_EQ(div(Expression("10+a^3"), Expression("a^2")).toString(), "a + 10/a^2"); + EXPECT_EQ(div(Expression("10+a^3"), Expression("a^2")).toString(), "a + 10/(a^2)"); EXPECT_EQ(div(Expression("a*b"), Expression("b*a")).toString(), "1"); EXPECT_EQ(div(Expression("a+b"), Expression("a+b")).toString(), "1"); } diff --git a/tests/src/expressions/ExpressionTests.cpp b/tests/src/expressions/ExpressionTests.cpp index e0e6492c4..1430f9ba8 100644 --- a/tests/src/expressions/ExpressionTests.cpp +++ b/tests/src/expressions/ExpressionTests.cpp @@ -144,7 +144,7 @@ TEST(ExpressionTests, stringConstructorTest) { EXPECT_EQ(Expression("Pi8").toString(), "8 Pi"); EXPECT_EQ(Expression("exp100").toString(), "E^100"); EXPECT_EQ(Expression("E^101").toString(), "E^101"); - EXPECT_EQ(Expression("E^(-101)").toString(), "1/E^101"); + EXPECT_EQ(Expression("E^(-101)").toString(), "1/(E^101)"); EXPECT_EQ(Expression("log(E,E)").toString(), "1"); EXPECT_EQ(Expression("log(Pi, Pi^10)").toString(), "10"); EXPECT_EQ(Expression("log(E,E^3)").toString(), "3"); @@ -328,8 +328,8 @@ TEST(ExpressionTests, stringConstructorTest) { EXPECT_EQ(Expression("(a b)/1").toString(), "a b"); EXPECT_EQ(Expression("(a b)/-1").toString(), "-a b"); EXPECT_EQ(Expression("(a b)/-2").toString(), "-(a b)/2"); - 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("(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 + (-72 x y - 8 x - 72 y + 324)/(27 x^2 + 36 x y + 9 x + 36 y - 162)"); EXPECT_EQ(Expression("(a/x + b/(y+3/r)/4)/(3+t/5)").toString(), @@ -349,10 +349,10 @@ TEST(ExpressionTests, stringConstructorTest) { 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("(x/y)^2").toString(), "x^2/y^2"); + EXPECT_EQ(Expression("(x/y)^2").toString(), "(x^2)/(y^2)"); EXPECT_EQ(Expression("(x/y)^(1/2)").toString(), "sqrt(x)/sqrt(y)"); EXPECT_EQ(Expression("(x/y)^(1/3)").toString(), "root(x, 3)/root(y, 3)"); - EXPECT_EQ(Expression("(x/y)^x").toString(), "x^x/y^x"); + EXPECT_EQ(Expression("(x/y)^x").toString(), "(x^x)/(y^x)"); EXPECT_EQ(Expression("(x/y)^(1/x)").toString(), "root(x, x)/root(y, x)"); // TODO! implement this @@ -445,9 +445,9 @@ TEST(ExpressionTests, stringConstructorTest) { // EXPECT_EQ(Expression("derivative(5, a^2)").toString(), "0"); // EXPECT_EQ(Expression("derivative(a, a^2)").toString(), "derivative(a, a^2)"); - EXPECT_EQ(Expression("integral(a, a)").toString(), "a^2/2"); + EXPECT_EQ(Expression("integral(a, a)").toString(), "(a^2)/2"); EXPECT_EQ(Expression("integral(a+a, a)").toString(), "integral(2 a, a)"); - EXPECT_EQ(Expression("integral(a, a) + integral(b, b)").toString(), "a^2/2 + b^2/2"); + EXPECT_EQ(Expression("integral(a, a) + integral(b, b)").toString(), "(a^2)/2 + (b^2)/2"); // TODO: integral // EXPECT_EQ(Expression("integral(5, a^2)").toString(), "???"); // EXPECT_EQ(Expression("integral(a, a^2)").toString(), "???"); diff --git a/tests/src/functions/calculus/IntegralTests.cpp b/tests/src/functions/calculus/IntegralTests.cpp index e79b3e586..de36918a0 100644 --- a/tests/src/functions/calculus/IntegralTests.cpp +++ b/tests/src/functions/calculus/IntegralTests.cpp @@ -23,7 +23,7 @@ TEST(IntegralTests, getFunctionTypeTest) { } TEST(IntegralTests, callTest) { - EXPECT_EQ(f(Variable("a"), Variable("a"))->toString(), "a^2/2"); + EXPECT_EQ(f(Variable("a"), Variable("a"))->toString(), "(a^2)/2"); EXPECT_EQ(f(Variable("a"), Variable("b"))->toString(), "integral(a, b)"); EXPECT_EQ(f(Expression("a+a"), Variable("a"))->toString(), "integral(2 a, a)"); // TODO: integral EXPECT_EQ(f(Integer(5), Variable("a"))->toString(), "5 a");