Skip to content

Equality of two expressions similar to sympy #496

Open

Description

@dominikgeissler

Hello!
When trying to check the equality for two expressions I want to utilize a method similar to the .equals() method in sympy (see example below)

import sympy as sp
import symengine as se

# In Symengine
expr1: se.Expr = se.S("(y + 1)**10/1024")
expr2: se.Expr = se.S("((1/2 + (1/2)*y)**10)")
print(expr1 == expr2)                       # False
print(expr1.expand() == expr2.expand())     # True

# In Sympy
expr1: sp.Expr = sp.S("(y + 1)**10/1024")
expr2: sp.Expr = sp.S("((1/2 + (1/2)*y)**10)")
print(expr1 == expr2)                       # False
print(expr1.expand() == expr2.expand())     # True
# 👇 behaviour I want to have
print(expr1.equals(expr2))                  # True

Is there a way to do this in symengine without calling additional methods (as .expand()) or using sympy?

Thanks in advance!

Activity

  1. rikardn commented on Oct 9, 2024

    @rikardn
    Contributor

    The closest in symengine is to use the is_zero property for the difference of the expressions. It is currently quite weak and will not be able to handle this expression.

    I think in the end the only feasible way of comparing two polynomials for equality is to first expand them so the underlying function would need to do this. Doing it without expanding is a hard problem in general (see https://en.wikipedia.org/wiki/Polynomial_identity_testing).

    The implementation is here https://github.com/symengine/symengine/blob/master/symengine/test_visitors.cpp if you'd like to take a look. Perhaps adding an is_equal function would be also be of value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions