Q: atoms(S(1))

25 views
Skip to first unread message

Audrius-St

unread,
Apr 29, 2022, 2:15:18 PM4/29/22
to sympy
Hello,

import sympy as sp

In the following example code, it is not clear to me why the first expression returns
expr.atoms(sp.S(1)) {1}

whereas the second expression returns
expr.atoms(sp.S(1)) set()

def main():

    x = sp.symbols('x')

    expr = x + 1
    print(f'expr.atoms(sp.S(1)) {expr.atoms(sp.S(1))}')

    expr = x - 1
    print(f'expr.atoms(sp.S(1)) {expr.atoms(sp.S(1))}')


if __name__ == "__main__":
    main()


Aaron Meurer

unread,
Apr 29, 2022, 3:50:43 PM4/29/22
to sy...@googlegroups.com
atoms() works at the expression tree level, meaning it will only
return something if it exactly appears in the expression. In the
example x - 1, the expression is Add(x, -1), which doesn't contain 1.
It instead contains -1. Note that SymPy doesn't have a subtraction
class. Subtraction is represented as addition of a negative, where the
negative is represented as multiplication by -1 (or in the case of
number, just the negative number).

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/bcd40bf3-44aa-4297-a218-2c71aadd3682n%40googlegroups.com.

Audrius-St

unread,
Apr 29, 2022, 8:29:30 PM4/29/22
to sympy
Understood. Thank you for your explanation.
Reply all
Reply to author
Forward
0 new messages