1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Issue using Sympy to simplify piecewise functions

Discussão em 'Python' iniciado por Stack, Outubro 3, 2024 às 10:52.

  1. Stack

    Stack Membro Participativo

    I've spent one week doing math research based on a computation that turned out to be incorrect due to an error in Sympy's simplification of piecewise functions. Does anyone know why this mistake occurs? It seems very strange to me that the sympy.simplify() function can transform an expression into something else that yields a different value when evaluated. I'm using : Python version: 3.10.9 Sympy version: 1.11.1.

    Here is a MWE.

    import sympy as sp
    from sympy import Piecewise, Eq, cos, sin, Ne

    j = sp.symbols('j', real=True)

    function = Piecewise((0, Eq(j, 0)), (-Piecewise((0, Ne(j, 0)), (2, True)), True)) - Piecewise((0, Eq(j, 0)), (-Piecewise((1, Ne(j, 0)), (0, True)), True))

    function_simple = sp.simplify(function)

    print(function.subs({j:1})) #prints 1
    print(function_simple.subs({j:1})) #prints -1


    NB: I've just tried this on Google Colab with Sympy 1.13.3 and Python 3.10.12, and the result is correct. I guess it was a bug that has been fixed in the newer versions of Sympy.

    Continue reading...

Compartilhe esta Página