Derivative of angular function by cartesian coordinates using Legendre polynomials?

211 Views Asked by At

I'm programing some numerical evaluation of force dependent on angle $\phi$ between vector ${\vec a}=(x,y)$ and normalized direction vector ${\hat d}$. To achive maximal performance I wan't to avoid using slow functions like $\sin()$, $\cos()$ $\tan()$ $\arctan()$ $\sqrt()$ etc. as much as possible.

I found that Legendre polynomials are great tool for that. I can very effectively express angular dependence of potential energy in form of

$E(\phi) = \sum_i c_i \cos(i \phi )$

as a polynominal of $\cos( \phi ) = {\vec a} \cdot {\hat d}/r $

where $r = \sqrt{ {\vec a}.{\vec a} }$

Especially, If the angular energy fuction $E(\phi)$ depend only on even frequecies $i$ the polynominal contains only even powers, so I do not have to computed even the slow $\sqrt()$ function and work only with $r^2 = {\vec a}.{\vec a}$.

The problem is, however, in transfromation from polar to cartesian coordinates.

$x = r \cos \phi$

$y = r \sin \phi$

$\partial E/\partial x = (\partial E/\partial \cos \phi)(\partial \cos \phi /\partial x)$

$\partial E/\partial y = (\partial E/\partial \cos \phi)(\partial \cos \phi /\partial y)$

The $\partial E/\partial cos \phi$ is easy since it is just polynominal.

$\partial \cos \phi /\partial x = 1/r$ is also quite cheap ( even though it requires computation of $\sqrt()$ to get $r$, in case of odd-frequnecies will cancel with $r$ in the polynominal so, it is sufficient to compute just $r^2$ ).

I don't know, however, what to do with

$\partial \cos \phi / \partial y = (1/r) (\partial \cos \phi/ \partial sin \phi ) $

because $\partial \cos \phi/ \partial \sin \phi = -\tan \phi $

  1. It has sigularities which makes it numerically unstable
  2. It is slow to compute

Is there any way how to get over this issue and compute $\partial E/\partial x$ and $\partial E/\partial y$ in numberically stable and efficient way ?

I can quite efficiently compute also $\sin \phi = (a_x d_y - a_y d_x)/r $ ... if it does help somehow? Perhabs building the legender polynominals not just from $\cos \phi$ but also from $ \sin \phi $ can solve it somehow? ( but I'm not sure how )