Skip to content

ITE evaluates all args at the same time #23295

@nkanazawa1989

Description

@nkanazawa1989

In the API documentation it says

ITE(A, B, C) evaluates and returns the result of B if A is true else it returns the result of C.

however it likely evaluates B even if A is false.

import sympy as sym
beta = sym.Symbol("beta")
expr = sym.ITE(sym.Abs(beta) > 0, 1/beta > 1, False)
expr.subs(beta, 0)

This yields TypeError: Invalid comparison of non-real zoo. We can manually evaluate each args but it means we cannot safely lambdify this expression. Seems like these lines are causing the problem.

sympy/sympy/core/basic.py

Lines 1076 to 1082 in 66d7aef

for i, arg in enumerate(args):
if not hasattr(arg, '_eval_subs'):
continue
arg = arg._subs(old, new, **hints)
if not _aresame(arg, args[i]):
hit = True
args[i] = arg

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions