EDIT: added assert as suggested by Wrzlprmft, which does not raise any exception.
I have to write code with many expressions of type a**b, under the following circumstances
import sympy as sp
a, b = sp.symbols('a,b', real=True, positive=True)
expr1 = a**b
expr2 = pow(a,b)
assert expr1 is expr2
What are the expected/possible differences between a**b and pow(a,b)?
(e.g., when simplifying, collecting, etc., expressions).
Should I expect possible wrong results in any case?
So far, I found none.
I don't think I would use the third argument of pow as my bases will be real (exponents will typically be integers or ratios between one- or two-digit integers), so this or this are not relevant.
This or this are focused on comparisons with math.pow, although some answers also include both ** and pow in comparisons of performance, showing minor differences.
None of the questions / answers deal with sympy.
If math.pow or sympy.Pow provide any advantage in any case, I welcome the clarifications.
I don't foresee I would perform heavy computations, so minor performance differences will likely not impact my use case.
In your context,
a**bandpow(a,b)are as identical as they can possibly be: They refer to the very same object:ischecks for object identity in Python. While such a check is rarely what you need for practical applications, it makes for the easiest answer to your question. (Note that Python objects can be equivalent in every respect, but still be different objects. For example, you shouldn’t rely on object identity being given in future SymPy implementations.)Moreover, the Python documentation specifies:
Thus, the two are the same for any arguments per language definition, i.e.,
pow(a,b)simply callsa.__pow__(b)(which is whata**bdoes under the hood). I don’t want to dig into the respective source code now, but consider this: