Skip to content

Commit

Permalink
Simplify x/y = 0
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Jul 11, 2023
1 parent 0ec2c47 commit 0234b87
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/fintamath/expressions/binary/CompExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ ArgumentPtr CompExpression::preSimplify() const {

CompExpression::SimplifyFunctionsVector CompExpression::getFunctionsForPostSimplify() const {
static const CompExpression::SimplifyFunctionsVector simplifyFunctions = {
&CompExpression::divSimplify, //
&CompExpression::coeffSimplify, //
};
return simplifyFunctions;
Expand All @@ -80,6 +81,14 @@ std::shared_ptr<IFunction> CompExpression::getOppositeFunction(const IFunction &
return oppositeFunctions.at(function.toString());
}

ArgumentPtr CompExpression::divSimplify(const IFunction &func, const ArgumentPtr &lhs, const ArgumentPtr &rhs) {
if (const auto lhsExpr = cast<IExpression>(lhs); lhsExpr && is<Div>(lhsExpr->getFunction())) {
return makeExpr(func, lhsExpr->getChildren().front(), rhs);
}

return {};
}

ArgumentPtr CompExpression::coeffSimplify(const IFunction &func, const ArgumentPtr &lhs, const ArgumentPtr &rhs) {
if (auto lhsExpr = cast<IExpression>(lhs)) {
if (is<Neg>(lhsExpr->getFunction())) {
Expand Down
2 changes: 2 additions & 0 deletions src/fintamath/expressions/binary/CompExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class CompExpression : public IBinaryExpressionCRTP<CompExpression, true> {
static std::shared_ptr<IFunction> getLogicOppositeFunction(const IFunction &function);

private:
static ArgumentPtr divSimplify(const IFunction &func, const ArgumentPtr &lhs, const ArgumentPtr &rhs);

static ArgumentPtr coeffSimplify(const IFunction &func, const ArgumentPtr &lhs, const ArgumentPtr &rhs);

bool isSolution = false;
Expand Down
2 changes: 1 addition & 1 deletion tests/src/FintamathTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ TEST(FintamathTests, fintamathTests) {
EXPECT_EQ(expr.toString(), "x^2 - x y + y^2 = 0");

expr = eqv(x / x - y / y, x / y);
EXPECT_EQ(expr.toString(), "x/y = 0");
EXPECT_EQ(expr.toString(), "x = 0");

expr = eqv(pow(x, 2) - 10, 39);
EXPECT_EQ(solve(expr).toString(), "x = -7 | x = 7");
Expand Down
2 changes: 1 addition & 1 deletion tests/src/expressions/ExpressionFunctionsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ TEST(ExpressionFunctionsTests, solveTest) {
EXPECT_EQ(solve(Expression("x y = 0")).toString(), "x y = 0");
EXPECT_EQ(solve(Expression("2 x^a = 0")).toString(), "x^a = 0");
EXPECT_EQ(solve(Expression("x^b a = 0")).toString(), "a x^b = 0");
EXPECT_EQ(solve(Expression("x/y = 0")).toString(), "x/y = 0");
EXPECT_EQ(solve(Expression("x/y = 0")).toString(), "x = 0");
EXPECT_EQ(solve(Expression("x^2 - 2*sin(2) = 0")).toString(), "x = -sqrt(2) sqrt(sin(2)) | x = sqrt(2) sqrt(sin(2))");

EXPECT_EQ(solve(Expression("E >= Ey")).toString(), "E y - E <= 0");
Expand Down

0 comments on commit 0234b87

Please sign in to comment.