symengine / symengine

SymEngine is a fast symbolic manipulation library, written in C++
https://symengine.org
Other
1.17k stars 281 forks source link

Equality checking of division #2044

Open bingao opened 2 months ago

bingao commented 2 months ago

Hi.

I encountered an error when I tried to compare if two symbolic values are equal. They are expressed as $\frac{-x}{1-(y-z)}$ and $\frac{x}{(y-z)-1}$, which should be equal. But in the following code, the Boolean variable is_eq is false.

auto x = SymEngine::symbol("x");
auto y = SymEngine::symbol("y");
auto z = SymEngine::symbol("z");
auto is_eq = SymEngine::eq(
    *SymEngine::div(
        SymEngine::mul(SymEngine::minus_one, x),
        SymEngine::sub(SymEngine::one, SymEngine::sub(y, z))
    ),
    *SymEngine::div(x, SymEngine::sub(SymEngine::sub(y, z), SymEngine::one))
);

Any suggestion? Is it a bug? Thank you.

bjodah commented 2 months ago

I think the missing building block to check for equality of structurally different expressions are functions related to simplification. It is "just" a matter of someone e.g. porting some of the simplification routines present in sympy to SymEngine.

Relates issue:

EDIT: for this expression it might be possible to use cancel to canonicalize the rational function so that it auto-simplifies? https://github.com/symengine/symengine/blob/master/symengine/tests/polynomial/test_cancel.cpp

If not you can see if there's anything missing in the implementation here compared to cancel from sympy's polytools module.

bingao commented 2 months ago

Thank you @bjodah cancel is a solution. But my code indeed compares elements of two 2x2 matrices, I have to use cancel for each element and compare their denominators and numerators. It works but maybe a simplification function as you suggested will be better.