Search code examples
pythonpython-3.xsympysymbolic-math

SymPy: Can I safely differentiate atan2()?


I would like to obtain a symbolic expression which is the derivative of atan2(y,x), where y and x are some expressions with a variable z. Can I safely assume that diff(atan2(y,x),z) gives me what I want?

In math.stackexchange.com there is a proof that atan2 is continuously differentialable in (-pi,pi), but is it in SymPy?


Solution

  • The partial derivatives of atan2(y, x) with respect to x and y are computed by SymPy as

    -y/(x**2 + y**2) 
     x/(x**2 + y**2)
    

    and these expressions are continuous as long as x, y do not turn into zero at once. (Assuming real arguments x, y, of course - I don't think anyone puts complex numbers in atan2).

    The above formulas are hardcoded here, so we can be very sure that SymPy will return them.