Search code examples
pythonexpression-treessympy

where are expressions getting converted to expression trees and are solved?


I am debugging sympy module on how are expressions getting evaluated and i used a simple code given below:

    from sympy.abc import x,y,z
    ee = x+y+x
    print(ee)

and kept a breakpoint at 2nd line and the next function it goes is to

    def __sympifyit_wrapper(a, b):

inside decorators.py with 'a' as x, 'b' as y and func as Add. Can anyone tell me which file is assigning a and b here and what is running in the background.


Solution

  • x, y, and z are all sympy.core.symbol.Symbol objects. Symbols are derived from sympy.core.expr.AtomicExpr, which in turn derives from sympy.core.expr.Expr.

    Calling x+_ (where _ is anything at all) calls the associated __add__ function, which is decorated with sympy.core.decorators._simpyfyit, which in turn is a simple wrapper around sympy.core.decorators.__simpyfyit, which is the function you see running in your breakpoint (the first bit of executed code)