Search code examples
pythonexpressionsympy

is it possible to define an unevaluated symbol in sympy?


I have a list of symbols in sympy:

x,y,z = symbols(["x","y","z"])
_symbols = [x,y,z]

I need to use these symbols in some algorithmically generated expressions, and these expressions must not be evaluated because I'm using them as excercises. Is there a method to prevent evaluation by defining these symbols as UnevaluatedExpr? I was thinking as something as

map(UnevaluatedExpr, _symbols)

but this does not affect the output, as x+x gives anyway 2x. Also something along the lines of

for symbol in _symbols:
   symbol = UnevaluatedExpr(symbol)

does not work.

EDIT: I noticed that there is a somewhat weird behavior of UnevaluatedExpr (at least for me), namely

UnevaluatedExpr(x)**0

gives 1. I would have expected simply x**0 as output, since x could be 0 and in that case it's not true that the result is 1.


Solution

  • Creating your expressions in an evaluate(False) context might be what you want:

    >>> with evaluate(False):
    ...  x + x
    ... 
    x + x