From a05d1ec17134d2e1a9d6c6103d647c941062a175 Mon Sep 17 00:00:00 2001 From: fintarin Date: Wed, 22 Nov 2023 13:52:29 +0300 Subject: [PATCH] Fix simplify with Inf and Undefined - part 4 --- .../expressions/binary/DivExpression.cpp | 2 +- .../expressions/binary/LogExpression.cpp | 2 +- .../expressions/binary/PowExpression.cpp | 4 --- .../expressions/polynomial/MulExpression.cpp | 18 ++++++------- tests/src/expressions/ExpressionTests.cpp | 27 +++++++++++++------ 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/fintamath/expressions/binary/DivExpression.cpp b/src/fintamath/expressions/binary/DivExpression.cpp index 68253bc47..d42ef02d5 100644 --- a/src/fintamath/expressions/binary/DivExpression.cpp +++ b/src/fintamath/expressions/binary/DivExpression.cpp @@ -99,7 +99,7 @@ ArgumentPtr DivExpression::constSimplify(const IFunction & /*func*/, const Argum ArgumentPtr DivExpression::numSimplify(const IFunction & /*func*/, const ArgumentPtr &lhs, const ArgumentPtr &rhs) { static const Integer one = 1; - if (Div().doArgsMatch({one, *rhs})) { + if (*rhs != Integer(0) && Div().doArgsMatch({one, *rhs})) { ArgumentPtr res = mulExpr(lhs, Div()(one, *rhs)); return res; } diff --git a/src/fintamath/expressions/binary/LogExpression.cpp b/src/fintamath/expressions/binary/LogExpression.cpp index 6ed154820..c74f278a2 100644 --- a/src/fintamath/expressions/binary/LogExpression.cpp +++ b/src/fintamath/expressions/binary/LogExpression.cpp @@ -89,7 +89,7 @@ ArgumentPtr LogExpression::constSimplify(const IFunction & /*func*/, const Argum return Undefined().clone(); } - if (*rhs == Integer(1)) { + if (*rhs == Integer(1) && !containsInfinity(lhs)) { return Integer(0).clone(); } diff --git a/src/fintamath/expressions/binary/PowExpression.cpp b/src/fintamath/expressions/binary/PowExpression.cpp index 134a1b1b3..bcfbea3c1 100644 --- a/src/fintamath/expressions/binary/PowExpression.cpp +++ b/src/fintamath/expressions/binary/PowExpression.cpp @@ -289,10 +289,6 @@ ArgumentPtr PowExpression::constSimplify(const IFunction & /*func*/, const Argum return Undefined().clone(); } - if (rhsComplex->real() < Integer(0)) { - return Integer(0).clone(); - } - return ComplexInf().clone(); } diff --git a/src/fintamath/expressions/polynomial/MulExpression.cpp b/src/fintamath/expressions/polynomial/MulExpression.cpp index 862372511..5b04ab0ce 100644 --- a/src/fintamath/expressions/polynomial/MulExpression.cpp +++ b/src/fintamath/expressions/polynomial/MulExpression.cpp @@ -93,8 +93,14 @@ bool MulExpression::isTermsOrderInversed() const { } ArgumentPtr MulExpression::constSimplify(const IFunction & /*func*/, const ArgumentPtr &lhs, const ArgumentPtr &rhs) { - if (*lhs == Integer(0) && isInfinity(rhs)) { - return Undefined().clone(); + if (*lhs == Integer(0)) { + if (isMulInfinity(rhs)) { + return Undefined().clone(); + } + + if (!containsInfinity(rhs)) { + return lhs; + } } if (is(lhs) || is(rhs)) { @@ -127,10 +133,6 @@ ArgumentPtr MulExpression::constSimplify(const IFunction & /*func*/, const Argum } if (rate && inf) { - if (*lhs == Integer(0)) { - return Undefined().clone(); - } - if (*lhs == Integer(-1)) { return is(rhs) ? NegInf().clone() : Inf().clone(); } @@ -143,10 +145,6 @@ ArgumentPtr MulExpression::constSimplify(const IFunction & /*func*/, const Argum } } - if (*lhs == Integer(0)) { - return lhs; - } - return {}; } diff --git a/tests/src/expressions/ExpressionTests.cpp b/tests/src/expressions/ExpressionTests.cpp index af701a521..d84167dd1 100644 --- a/tests/src/expressions/ExpressionTests.cpp +++ b/tests/src/expressions/ExpressionTests.cpp @@ -1360,16 +1360,17 @@ TEST(ExpressionTests, stringConstructorTest) { EXPECT_EQ(Expression("ComplexInf^-2").toString(), "0"); EXPECT_EQ(Expression("ComplexInf^(-2/3)").toString(), "0"); EXPECT_EQ(Expression("Inf^(I+2)").toString(), "ComplexInf"); - EXPECT_EQ(Expression("Inf^(I+2)").toString(), "ComplexInf"); - EXPECT_EQ(Expression("Inf^(I+2)").toString(), "ComplexInf"); - EXPECT_EQ(Expression("Inf^(I+2)").toString(), "ComplexInf"); - EXPECT_EQ(Expression("(-Inf)^(I+2)").toString(), "ComplexInf"); + EXPECT_EQ(Expression("Inf^(I-2)").toString(), "0"); + EXPECT_EQ(Expression("Inf^(-I+2)").toString(), "ComplexInf"); + EXPECT_EQ(Expression("Inf^(-I-2)").toString(), "0"); EXPECT_EQ(Expression("(-Inf)^(I+2)").toString(), "ComplexInf"); - EXPECT_EQ(Expression("(-Inf)^(I+2)").toString(), "ComplexInf"); - EXPECT_EQ(Expression("ComplexInf^(I+2)").toString(), "ComplexInf"); - EXPECT_EQ(Expression("ComplexInf^(I+2)").toString(), "ComplexInf"); - EXPECT_EQ(Expression("ComplexInf^(I+2)").toString(), "ComplexInf"); + EXPECT_EQ(Expression("(-Inf)^(I-2)").toString(), "0"); + EXPECT_EQ(Expression("(-Inf)^(-I+2)").toString(), "ComplexInf"); + EXPECT_EQ(Expression("(-Inf)^(-I-2)").toString(), "0"); EXPECT_EQ(Expression("ComplexInf^(I+2)").toString(), "ComplexInf"); + EXPECT_EQ(Expression("ComplexInf^(I-2)").toString(), "0"); + EXPECT_EQ(Expression("ComplexInf^(-I+2)").toString(), "ComplexInf"); + EXPECT_EQ(Expression("ComplexInf^(-I-2)").toString(), "0"); EXPECT_EQ(Expression("0^Inf").toString(), "0"); EXPECT_EQ(Expression("0^-Inf").toString(), "ComplexInf"); EXPECT_EQ(Expression("0^-1").toString(), "ComplexInf"); @@ -1421,6 +1422,16 @@ TEST(ExpressionTests, stringConstructorTest) { EXPECT_EQ(Expression("sign(Inf)/0").toString(), "ComplexInf"); EXPECT_EQ(Expression("0^sign(Inf)").toString(), "0"); EXPECT_EQ(Expression("sign(Inf)^0").toString(), "1"); + EXPECT_EQ(Expression("0 (Inf!)").toString(), "0 Inf!"); + EXPECT_EQ(Expression("0/(Inf!)").toString(), "0/Inf!"); + EXPECT_EQ(Expression("(Inf!)/0").toString(), "Inf!/0"); + EXPECT_EQ(Expression("0^(Inf!)").toString(), "0^(Inf!)"); + EXPECT_EQ(Expression("(Inf!)^0").toString(), "(Inf!)^0"); + EXPECT_EQ(Expression("log((Inf!), (Inf!))").toString(), "log(Inf!, Inf!)"); + EXPECT_EQ(Expression("log((Inf!), Inf)").toString(), "log(Inf!, Inf)"); + EXPECT_EQ(Expression("log(Inf, (Inf!))").toString(), "log(Inf, Inf!)"); + EXPECT_EQ(Expression("log((Inf!), 1)").toString(), "log(Inf!, 1)"); + EXPECT_EQ(Expression("log(1, (Inf!))").toString(), "log(1, Inf!)"); EXPECT_EQ(Expression("Inf = Inf").toString(), "True"); EXPECT_EQ(Expression("Inf = -Inf").toString(), "False"); EXPECT_EQ(Expression("-Inf = Inf").toString(), "False");