Skip to content

Commit

Permalink
Change toString: x^2/y^2 to (x^2)/(y^2)
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Jul 18, 2023
1 parent 0e4495d commit fecc994
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/fintamath/expressions/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
19 changes: 17 additions & 2 deletions src/fintamath/expressions/binary/PowExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,23 @@ std::string PowExpression::toString() const {
}

std::shared_ptr<IFunction> PowExpression::getOutputFunction() const {
if (*rhsChild == Rational(1, 2)) {
return std::make_shared<Sqrt>();
if (auto rhsChildRat = cast<Rational>(rhsChild)) {
const Integer &numerator = rhsChildRat->numerator();
const Integer &denominator = rhsChildRat->denominator();

if (numerator == 1) {
if (denominator == 2) {
return std::make_shared<Sqrt>();
}

return std::make_shared<Root>();
}
}

if (auto rhsChildExpr = cast<IExpression>(rhsChild); rhsChildExpr && is<Div>(rhsChildExpr->getFunction())) {
if (*rhsChildExpr->getChildren().front() == Integer(1)) {
return std::make_shared<Root>();
}
}

return IBinaryExpression::getFunction();
Expand Down
4 changes: 2 additions & 2 deletions tests/src/expressions/ExpressionFunctionsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
14 changes: 7 additions & 7 deletions tests/src/expressions/ExpressionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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(),
Expand All @@ -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
Expand Down Expand Up @@ -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(), "???");
Expand Down
2 changes: 1 addition & 1 deletion tests/src/functions/calculus/IntegralTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit fecc994

Please sign in to comment.