I'm not clear what you are asking, but you can check if an expression has an infinity with expr.has(oo, -oo, zoo, nan)
.
How to make python sympy raise an exception when evaluates or returns Infinity?
题
I'm using sympy python library in order to get differentials, integrals, and functions evaluation. However, when it comes to certain cases like division by 0, naturally some cases will evaluate Infinity or some will just fail.
I've seen Infinity
and NaN
obtained in such error prone situations. I want to make sympy automatically raise an exception when it deals with such.
Basically I evaluate in two different states, one very simple:
fp = fx.subs(x,p)
Where fx
is a symbolic expression (fx=S(fx)
) and p
a float.
Also I evaluate in this syntax like statements:
for x_i in xs:
ys.append(float(fx.subs(x,x_i)))
Where xs
and ys
are lists and so x_i
is a float.
In first statement raising an expception would come after evaluating obtained value, and so in second case. But how to avoid doing this manually? I think is better to set sympy or python environment to treat Infinity
or NaN
especially.
解决方案