Menu

#4801 Bogus derivatives when arguments match variable names in DEFGRAD

None
closed
nobody
1
2026-06-28
2026-06-28
No

Look how the partial derivatives of atan2 are defined:

(defgrad %atan2 ($x $y)
  #$$y/(y^2+x^2)$
  #$$-(x/(y^2+x^2))$)

Now when we differentiate an atan2 expression that uses exactly these arguments x and y, but in reverse order, we get bogus answers:

/* Wrong with arguments named y and x: */

(%i1)   diff(atan2(y,x),x);
(%o1)   -(1/(2*x))

(%i2)   diff(atan2(y,x),y);
(%o2)   1/(2*x)

/* Correct when renaming arguments: */

(%i3)   diff(atan2(yy,xx),xx);
(%o3)   -(yy/(yy^2+xx^2))

(%i4)   diff(atan2(yy,xx),yy);
(%o4)   xx/(yy^2+xx^2)

Discussion

  • Raymond Toy

    Raymond Toy - 2026-06-28

    Oops. The derivatives should use $x and $y. defgrad should probably do more error checking. Maybe make sure every variable in the derivatives start with a $?

     
    • David Scherfgen

      David Scherfgen - 2026-06-28

      I think the problem is that variables are substituted sequentially using substitutel instead of in parallel using $psubstitute. I'm on it.

       
  • David Scherfgen

    David Scherfgen - 2026-06-28
    • status: open --> closed
     
  • David Scherfgen

    David Scherfgen - 2026-06-28

    Fixed by commit [7bda3f] by using $psubstitute.

     

    Related

    Commit: [7bda3f]


Log in to post a comment.

Auth0 Logo