How to make sympy acknowledge that 0/0 is a division by zero
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities
--
Chapters
00:00 Question
01:04 Accepted answer (Score 2)
01:46 Thank you
--
Full question
https://stackoverflow.com/questions/5007...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #sympy
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities
--
Chapters
00:00 Question
01:04 Accepted answer (Score 2)
01:46 Thank you
--
Full question
https://stackoverflow.com/questions/5007...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #sympy
#avk47
ACCEPTED ANSWER
Score 2
As you noticed, the issue is that x/x evaluates to 1 immediately, so a subsequent substitution of x by 0 is not really happening.
A way to avoid evaluation is to pass evaluate=False flag to the product of x with 1/x:
>>> expr = Mul(x, 1/x, evaluate=False)
>>> expr
x/x
>>> expr.subs(x, 0)
nan
The result is "Not A Number", meaning this does not evaluate to a number.
(In SymPy, all division is multiplication by reciprocal quantity: there are Add, Mul, Pow but no Div or Sub).