Skip to content

Commit

Permalink
Fix simplify with Inf and Undefined - part 4
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Nov 22, 2023
1 parent cc3e25e commit a05d1ec
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/fintamath/expressions/binary/DivExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/binary/LogExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
4 changes: 0 additions & 4 deletions src/fintamath/expressions/binary/PowExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
18 changes: 8 additions & 10 deletions src/fintamath/expressions/polynomial/MulExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComplexInf>(lhs) || is<ComplexInf>(rhs)) {
Expand Down Expand Up @@ -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<Inf>(rhs) ? NegInf().clone() : Inf().clone();
}
Expand All @@ -143,10 +145,6 @@ ArgumentPtr MulExpression::constSimplify(const IFunction & /*func*/, const Argum
}
}

if (*lhs == Integer(0)) {
return lhs;
}

return {};
}

Expand Down
27 changes: 19 additions & 8 deletions tests/src/expressions/ExpressionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit a05d1ec

Please sign in to comment.