0
$\begingroup$

I need to convert n-dimensional vectors into hyperspherical coordinates, which involves doing calculating an arctan with an argument which is the square root of a sum of squares (i.e., the radius of a truncated vector) once for every non-radial dimension. The squares can all be precomputed and re-used, but the sum to be rooted is different every time.

Is there a faster way to compute the angles than just calculating a new square root followed by an arctan every time? Either a way to incrementally update the square root, or directly compute an arctan of squared inputs?

$\endgroup$
4
  • $\begingroup$ Does this require atan() or atan2()? I suspect it is the latter, but it would be beneficial to clarify this in the question. $\endgroup$
    – njuffa
    Commented Aug 16 at 3:48
  • 2
    $\begingroup$ I am not convinced the arctangent computation has a material impact on overall performance. To get an idea of how much this computation would benefit from use of a fast atan2() implementation, you could try the fast, low-precision, algorithm from a previous answer of mine. $\endgroup$
    – njuffa
    Commented Aug 16 at 4:12
  • $\begingroup$ Indeed the summing of the $n$ components to get $x$ for $\arctan(\sqrt x)$ could be a dominating aspect for large enough $n$. The summation might be susceptible to optimization by "updating". There's not enough detail in the Question's body to discuss that idea. More notation would be welcome (use $\LaTeX$ for typeset mathematics). $\endgroup$
    – hardmath
    Commented Aug 18 at 15:01
  • $\begingroup$ @hardmath The summation is indeed amenable to updating; there's only one square and one addition per coordinate. $\endgroup$ Commented Aug 18 at 16:57

1 Answer 1

2
$\begingroup$

sqrt is fairly fast (about the cost of division), while arctan is more expensive. As such creating a function for x->arctan(sqrt(x)) is likely the way to go. The ideal implementation will depend a lot on the precision you're targeting.

$\endgroup$
1
  • 1
    $\begingroup$ I hope for the OP to supply more details about the precision required, in which case we could flesh out your idea of an ideal implementation of $\arctan(\sqrt x)$. $\endgroup$
    – hardmath
    Commented Aug 18 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.