[Python] Any way to do integer division in sympy?

Discussão em 'Python' iniciado por Stack, Maio 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have a very long expression that I think can be simplified, and I thought sympy would be the perfect way to do it. Unfortunately the formula relies on a couple of integer divides, and I can't find any way to represent those in sympy.

    >>> x=Symbol('x')
    >>> (x+1)/2
    x/2 + 1/2


    Clearly not what I want, 1/2 isn't an integer.

    >>> (x+1)//2
    TypeError: unsupported operand type(s) for //: 'Add' and 'int'


    Obviously sympy doesn't handle //.

    >>> Integer((x+1)/2)
    # A long list of error messages, ending with ...
    TypeError: Integer can only work with integer expressions.


    It seems that Integer is only intended to work on constant numbers, not formulas.

    There's a function trunc but it doesn't seem to do anything similar to what I want.

    Is there any way to represent an integer division in sympy?

    Continue reading...

Compartilhe esta Página