<<

A&A 619, A128 (2018) Astronomy https://doi.org/10.1051/0004-6361/201833162 & c ESO 2018 Astrophysics

CORDIC-like method for solving Kepler’s equation M. Zechmeister

Institut für Astrophysik, Georg-August-Universität, Friedrich-Hund-Platz 1, 37077 Göttingen, Germany e-mail: [email protected]

Received 4 April 2018 / Accepted 14 August 2018

ABSTRACT

Context. Many to solve Kepler’s equations require the evaluation of trigonometric or root functions. Aims. We present an to compute the eccentric anomaly and even its cosine and terms without usage of other transcen- dental functions at run-time. With slight modifications it is also applicable for the hyperbolic case. Methods. Based on the idea of CORDIC, our method requires only and and a short table. The table is inde- pendent of eccentricity and can be hardcoded. Its length depends on the desired precision. Results. The code is short. The convergence is linear for all mean anomalies and eccentricities e (including e = 1). As a stand-alone algorithm, single and double precision is obtained with 29 and 55 iterations, respectively. Half or two-thirds of the iterations can be saved in combination with Newton’s or Halley’s method at the cost of one . Key words. celestial mechanics – methods: numerical

1. Introduction expansion of the sine term and yielded with root finding methods a maximum error of 10−10 after inversion of a fifteen-degree Kepler’s equation relates the mean anomaly M and the eccentric polynomial. Another possibility to reduce the iteration is to use anomaly E in orbits with eccentricity e. For elliptic orbits it is higher order corrections. For instance Padé approximation of given by order [1/1] leads to Halley’s method. E − e sin E = M(E). (1) Pre-computed tables can be an alternative way for fast computation of E. Fukushima(1997) used a table equally The function is illustrated for eccentricities 0, 0.1, 0.5, 0.9, and 1 spaced in E to accelerate a discretised Newton method, while in Fig.1. It is straightforward to compute M(E). But in practice Feinstein & McLaughlin(2006) proposed an equal spacing in M is usually given and the inverse function E(M) must be solved. M, that is, a direct which must be e-dependent and The innumerable publications about the solution of Kepler’s therefore two-dimensional. Both tables can become very large equation (Colwell 1993) highlight its importance in many fields depending on the desired accuracy. of astrophysics (e.g. exoplanet search, planet formation, and So the solution of the transcendental Eq. (1) often comes star cluster evolution), astrodynamics, and trajectory optimisa- back to other transcendental equations which themselves need to tion. N-body hybrid algorithms also make use of this analytic be solved in each iteration. This poses questions as to how those solution of the two-body problem (Wisdom & Holman 1991). often built-in functions are solved and whether there is a way to Nowadays, computers can solve Eq. (1) quickly. But the speed apply a similar, more direct algorithm to Kepler’s equation. increase is counterbalanced by large data sets, simulations, and The implementation details of those built-in functions are extensive data analysis (e.g. with Markov chain Monte Carlo). hardware and software dependent. But sine and cosine are often This explains ongoing efforts to accelerate the computation with computed with Taylor expansion. After range reduction and software and hardware, for example by parallelising and use of folding into an interval around zero, Taylor expansion is here graphic processing units (GPUs; Ford 2009). −16 π quite efficient yielding 10 with 17th degree at 4 . Kepler’s The Newton-Raphson iteration is a common method to solve equation can also be Taylor expanded (Stumpff 1968) but that is Eq. (1) and employs the derivative E0. In each step n the solution less efficient, in particular for e = 1 and around M = 0 where the is refined by derivative becomes infinite. Similarly, root or arcsine functions are other examples where the convergence of the Taylor expan- 0 En − e sin En − M En+1 = En + E (Mn)(M − Mn) = En − · (2) sion is slow. For root-like functions, one again applies Newton- 1 − e cos En Raphson or bisection methods. The arcsine can be computed as arcsin(x) = arctan( √ x ) where Taylor expansion of the arctan- A simple starting estimate might be E0 = M + 0.85e (Danby 1−x2 1987). Each iteration comes at the cost of evaluating one cosine gent function is efficient and one root evaluation is needed. and one sine function. Hence one tries to minimise the number An interesting alternative method to compute trigonomet- of iterations. This can be done with a better starting estimate. For ric functions is the Coordinate Digital Computer example, Markley(1995) provides a starting estimate better than (CORDIC) algorithm which was developed by Volder(1959) for 10−4 by inversion of a cubic polynomial which however requires real-time computation of sine and cosine functions and which also transcendental functions (four roots). Boyd(2007) further for instance found application in pocket . CORDIC polynomialise Kepler’s equation through Chebyshev polynomial can compute those trigonometric and other elementary functions

Article published by EDP Sciences A128, page 1 of9 A&A 619, A128 (2018)

π E M E1 E 3 M2 M E5 4 M E4 5 3π M3 4± E2

M1 π 2± E - e sin E = M e = 1 π ± e = 0.9 M E

eccentric anomaly E 0 0 4 e = 0.5 e = 0.1 Fig. 2. Example of five CORDIC rotations given M = 2−sin 2 ≈ 62.49◦ e = 0 and e = 1. Starting with M0 = 0 and E0 = 0, Mn and En approach M 0 and E = 2 ≈ 114.59◦. The arcs indicates the current rotation angle α π π 3π π n 0 4± 2± 4± and direction at each step. mean anomaly M

Fig. 1. Kepler’s equation for five different eccentricities. One major key point of CORDIC is that, besides the angle En, it propagates simultaneously the Cartesian representation, which are cosine and sine terms of En. The next iterations are in a simple and efficient way. In this work we study whether and obtained through trigonometric theorems how the CORDIC algorithm can be applied to Kepler’s equation. cos En+1 = cos(En + σn+1αn+1) (8) = cos E cos α − σ sin E sin α (9) 2. Scale-free CORDIC algorithm for Kepler’s n n+1 n+1 n n+1 sin E = sin(E + σ α ) (10) equation n+1 n n+1 n+1 = σn+1 cos En sin αn+1 + sin En cos αn+1, (11) Analogous to CORDIC, we compose our angle of interest, E, by a number of positive or negative rotations σi = ±1 with angle αi which is a with a ! " # ! cn+1 cos αn+1 −σn+1 sin αn+1 cn Xn = . (12) sn+1 σn+1 sin αn+1 cos αn+1 sn En = σiαi. (3) i=1 Here we introduced the abbreviations cn = cos En and sn = The next iteration n is then sin En. Since the iteration starts with E0 = 0, we simply have c0 = E E σ α . n+1 = n + n+1 n+1 (4) cos E0 = 1 and s0 = sin E0 = 0. The cos αn and sin αn terms can also be pre-computed using αn from Eq. (7) and stored in a table The rotation angle αn is halved in each iteration listing the triples (αn, cos αn, sin αn). Now we have eliminated αn α1 all function calls for sine and cosine of E and α in the iteration. αn+1 = = n (5) 2 2 This is also true and important for the sin En term in Eq. (6) so that we successively approach E. The direction of the next which is simply gathered during the iteration as sn via rotation σ is selected depending on the mean anomaly M cal- Eq. (12). Figures2 and3 illustrate the convergence for input n+1 n M = 2 − sin 2 ≈ 1.0907 and e = 1 towards E = 2. culated from En via Eq. (1). If Mn = En − e sin En overshoots the target value M, then E will be decreased, otherwise increased In AppendixA we provide a short python code which imple- n+1 ments the algorithm described in this section. A slightly more elegant formulation is possible with complex numbers z = ( n −1 En − e sin En > M exp(iEn) = cos En +i sin En and an = exp(iαn) = cos αn +i sin αn. σn+1 = (6) ∗ +1 else. Then Eq. (12) becomes zn+1 = an+1zn or zn+1 = an+1zn depending on σn and in Eq. (6) the substitution sin En = Im(zn) has to be Equations (4)–(6) provide the iteration scheme which so far done. is a binary search or bisection method (with respect to E) to find A more detailed comparison with CORDIC is given the inverse of M(E). Choosing as start value E0 = 0 and start AppendixB. Before analysing the performance in Sect. 5.3, we π rotation α1 = 2 , we can cover the range E ∈ [−π, π] and pre- outline some optimisation possibilities in Sect.3. compute the values of rotation angles with Eq. (5) π α = · (7) 3. Variants and refinements n 2n 3.1. Minimal rotation base The main difference of our algorithm from the CORDIC sine is the modified decision for the rotation direction in Eq. (6) con- The algorithms in Sect.2 and AppendixB use always positive sisting of the additional term e sin E. For the special case e = 0, or negative rotation directions. While after n rotations the max- the eccentric and mean anomalies unify and the iteration con- imum error should be smaller then αn, previous iterations can verges to En → E = M. still be closer to the convergence limit. In Fig.2, for instance,

A128, page 2 of9 M. Zechmeister: CORDIC-like method for solving Kepler’s equation

π M − En + esn αn+1 = En+1 − En = · (14) 5 1 − ecn This means that there is no need to check for a rotation direction π 3± 4 σn. For small angles, there are the approximations sin α ≈ α 4 and cos α ≈ 1. If one works with double precision (2−53), the α2 error (α) = |1 − cos α| < 2 is negligible, meaning smaller than 3 −54 −17 −9 π 2 ≈ 5.5 × 10 , when |α| < 7.5 × 10 (given for a29). Then 2± we can write M(E), E 2

iteration n c = c + α s (15) E (M) n+1 n n+1 n π 3 s = s + α c . (16) ± E (M) 1 n+1 n n+1 n eccentric anomaly E 4 5 E (M ) n n 3.3. Halley’s method M=2-sin 2, E=2 0 Halley’s method has a cubic convergence. Ignoring third order 0 π π π 0 ± ± 3± π terms, one can apply the approximations sin α ≈ α and cos α ≈ 4 2 4 2 3 1 − α . The error (α) = |α − sin α| < | α | is negligible in double mean anomaly M 2 √ 6 3 −54 −6 precision, when α < 6 × 2 = 6.93 × 10 given for α19 ≈ Fig. 3. CORDIC-like approximation of Kepler’s equation (e = 1). The 5.99 × 10−6. Similar to Sect. 3.2, the iteration scheme with co- curves E3 and E5 illustrate the convergence towards Kepler’s equation rotation for Halley’s method is after three and five iterations. The coloured points Mn, En are passed during the iterations to find E(M = 2 − sin 2) (same colour-coding as in Fig.2). (1 − ecn)(M − Mn) αn+1 = (17) 2 1 (1 − ecn) + 2 esn(M − Mn)  α2  E3 is closer to E than E4 and E5. Consecutive rotations will  n+1  sn+1 = 1 −  sn + αn+1cn (18) compensate this. A very extreme case is M = 0, where E0 = 0 is  2  already the exact value, but the first rotation brings it to E = 90◦ 1  α2  followed by only negative rotation towards zero.  n+1  cn+1 = 1 −  cn + αn+1 sn. (19) The CORDIC algorithm requires positive and negative rota-  2  tions to have always the same scale Kn after n iterations (AppendixB). But since we could not pull out Kn from the iteration, we can depart from this concept and allow for only 4. Hyperbolic mode zero and positive rotations which could be called unidirectional For eccentricities e ≥ 1, Kepler’s equation is (Jain et al. 2013) or one-sided (Maharatna et al. 2004). The deci- sion to rotate or not to rotate becomes M = e sinh H − H. (20) ( 1 (E + α ) − e sin(E + α ) < M This case can be treated as being similar to the elliptic case, when σ = n n+1 n n+1 (13) n+1 0 else. the trigonometric terms are replaced by hyperbolic analogues. Equations (6), (7), and (12) become ( Thereby we compose En in Eq. (3) by minimal rotations and can −1 e sinh H − H > M expect also a better propagation of precision. Moreover, while σ = n n (21) n+1 +1 else the term sin(En + αn+1) = snca + cn sa still needs to be computed in each iteration to derive for σn+1, the term cn+1 is updated only 4 ln 2 αn = (22) when σn+1 = 1 which will be on average in about 50% of the 2n ! " # ! cases. We note also that with the minimal rotation base the curve c cosh α σ sinh α c in Eq. (1) is approached from below. n+1 = n+1 n+1 n+1 n . (23) sn+1 σn+1 sinh αn+1 cosh αn+1 sn

3.2. CORDIC-like Newton method where a sign change in Eq. (23) leads to hyperbolic rotations. This equation set covers a range of H ∈ [−4 ln 2, 4 ln 2]. How- The convergence of the CORDIC-like algorithms in Sects.2 and ever, H extends to infinity. In the elliptic case, this is handled by 3.1 is linear, while Newton’s method is quadratic. Of course, an argument reduction which makes use of the periodicity. In the our algorithm can serve start values (and their cosine and sine hyperbolic case, the corresponding nice base points H0 are terms) for other root finders at any stage. The quadratic conver- − gence should bring the accuracy from, for example, 10 8 down H0 = m ln 2 (24) to 10−16 at the cost of mainly one division. We think this should 1 c = cosh H = [exp H + exp(−H )] = 2m−1 + 2−m−1 (25) be preferred over the other possible optimisations mentioned in 0 0 2 0 0 AppendixB. 1 m−1 −m−1 Still, we do not want to lose the ability to provide the cosine s0 = sinh H0 = [exp H0 − exp(−H0)] = 2 − 2 . (26) 2 and sine terms after one (or more) Newton iterations. We can propagate them simultaneously in a similar manner as in Eqs. (4) For m = 0, this becomes H0 = 0, c0 = 1, and s0 = 0, in other and (12) but now using small angle approximations. Newton’s words similar to the elliptic case. For m , 0, the start triple is method Eq. (2) directly proposes a rotation angle still simple to compute using only additions and bitshifts.

A128, page 3 of9 A&A 619, A128 (2018)

4 10-12 e = 1.0 3 e = 0.9 10-13 e = 0.5 - E| -8 2 55 3´10 10-16

= |E -14 -16 1 δ 10 10 E/(1-e) 2´10-16/E 10-16E 0 ± -15 H0 4ln2 10 H -1 0 sinh(H)-H, H hyperbolic anomaly H m = 0, ±1, ±2, ... 10-16 -2 ln(2M/e) absolute deviation ln(2M/e+1) -3 10-17 -5 0 5 10 15 20 0 0.5 1 1.5 2 2.5 3 mean anomaly M mean anomaly M Fig. 4. Range extension for hyperbolic Kepler’s equation (black curve) Fig. 5. Accuracy. For visibility, zero deviations (δ = 0) within machine − in case e = 1. The start value H0 (blue thick line) and the convergence precision were lifted to δ = 2 × 10 17. domain of ±4 ln 2 (shaded area) covers Kepler’s equation. Nice base points (black dots) and two approximations (dotted and dashed curves) are also indicated. The deviations δ55 become large in this corner, because the inversion of Eq. (1) is ill-posed. For e = 1, third order expansion 1 3 1 3 The main challenge is to obtain the integer m from mean of Eq. (1) is M = E − (E − 6 E ) = 6 E . If the quadratic term in anomaly M. In elliptic case, this needs a division with 2π; in 1 2 1 − 6 E is below machine precision , it cannot be propagated hyperbolic case, it becomes a logarithmic operation. Figure4 numerically√ in the and leads to a maximum deviation and AppendixD show that of E < 6 ≈ 10−8 at E = 10−8 (M = 10−24).   M  Figure7 illustrates the ill-posed problem. For e = 1 and very m = sign M · max 0, floor 1 + log (27) small M, the cubic approximation provides a very accurate ref- 2 e erence. For comparison, we overplot our E55 solution as well as provides start values with the required coarse accuracy of bet- computer generated pairs E-sin(E),E. Both exhibit a discreti- − ter than 4 ln 2. In floating point representation (IEEE 754), the sation of 10 8 in E. We conclude that Newton’s, Halley’s, and second argument in the maximum function of Eq. (27) extracts other methods which directly try to solve Eq. (1) are as well − simply the exponent of M/e. In fixed point representation, it is only accurate to 10 8 in this corner when operating with double the most significant non-zero bit (Walther 1971). This means a precision! Therefore, the M, E pairs mentioned above were not call is not necessary (cf. Appendix A.2). generated with built-in sin()-function but using a Taylor expan- The hyperbolic iterations do not return cos H and sin H, but sion of Kepler’s equations cosh H and sinh H which are the quantities needed to compute ! distance, velocity, and Cartesian position. E3 E5 M = E(1 − e) − e − + − · · · · (28) 3! 5! 5. Accuracy and performance study We may call the term in brackets rest function of sine. It dif- 5.1. Accuracy of the minimal rotation base algorithm fers by a factor of −E−3 compared to its first naming in Stumpff The CORDIC-like algorithms have a linear convergence rate. (1959); see also Eq. (32). We find that the deviations approximately follow the form The maximum error for En in iteration n is given by αn. For −6 −8 instance, we expect from Eq. (7) α22 < 10 , α29 < 10 (sin- −12 −16 E gle precision), α42 < 10 and α55 < 10 (double precision). δ (E, e) = |E − E| ≈ 10−16 + 10−16 + 10−16E, (29) To verify if double precision can be achieved in practice, we 55 55 1 − e cos E forward-calculated with Eq. (1) 1000 (M(E), E) pairs uniformly sampled in E over [0, π]. Here E might be seen as the true value. as indicated by the curves in Figs.5 and6. For very small Then we injected M into our algorithms to solve the inverse M and E, the constant term 10−16 dominates. Then the mid- problem E(M). dle term, which is related to the derivative of Eq. (1), sets in. The differences between re-computed and true eccentric Finally, the linear term 10−16E takes over which describes the anomaly, δ55 = |E55 − E|, are shown in Fig.5 for the cases accuracy to which E can be stored. The middle term leads to an e = 0.5, 0.9, and 1. For M & 0.25 and e ≤ 1, the devia- ascending and a descending branch which can be approximated −15 −16 E −16 2 tions are smaller than 10 and close to machine precision  by 10 1−e and 10 E and forms a local maximum. The two as indicated by the grey lines. Towards M = 0 and e > 0.9, √ q lines intersect at E ≈ 2(1 − e) and δ (e) ≈ 10−16 2 . For the scatter increases. Figure6 greatly magnifies this corner by 55 1−e plotting the deviations against the logarithm of E for e = 0.5, highest resolvable√ eccentricity below one, this provides δ(e = 0.999 999 999 9, and 1. 1 − 10−16) ≈ 2 × 10−8.

A128, page 4 of9 M. Zechmeister: CORDIC-like method for solving Kepler’s equation

expensive than additions. Then there is the question about the 10-8 e = 1.0 level of optimisation done in the code, by the compiler, or in e = 0.9999999999 -9 built-in routines. 10 e = 0.5 It can be also instructive to count the number of floating point - E| -10 -8 55 10 3´10 operations. In AppendixA we see five multiplications, four addi- 10-16 tions, and one comparison in each iteration. For single precision -11 = |E 10 -16 this must be executed 29 times. On the other hand, a Newton δ -14 10 E/(1-e) -16 -12 2´10 /E iteration in Eq. (2) has one division, two multiplications, four 10 -12 10-16E additions, and one sine and cosine term. In case the latter two -13 10 -10 terms are computed via Taylor expansion to 17th order and with a Horner scheme, each will contribute additional nine multipli- -14 1 - 10 e = 1 - 10 10 1 - 10 cations and nine additions. Furthermore, the cosine and sine rou- -15 tines have to check the need for range reduction each time. Actu- 10 0.99999999 0.999999

absolute deviation ally, also an additional final sine and cosine operation should be 0.9999 10-16 0.99 loaded to Newton’s methods, since sin E and cos E are usually needed in subsequent calculations. 10-17 -25 -20 -15 -10 -5 Given same weight to all listed operations, we count approx- 10 10 10 10 10 0.01 1 imately ten operations per CORDIC iteration vs. about per 43 mean anomaly M operation per Newton iteration. Then we estimate that four Fig. 6. Similar to Fig.4, but with logarithmic scaling. CORDIC iteration are as expensive as one Newton iteration. We have implemented both CORDIC-like and Newton’s method in a C program. The Newton’s method used the start E M . e -8 guess 0 = + 0 85 and called cos and sin function from the 7´10 standard math.h library. The run time was measured with python timeit feature for α (10−8) and for 1000 mean anomalies uni- -8 29 6´10 formly sampled from 0 to π. The run-time of the CORDIC- like algorithm is independent of e, while Newton’s method 5´10-8 is fast for small eccentricities and slower for high eccentrici- ties. We measured that our CORDIC-like method has double, 4´10-8 same, and half speed for e = 1, 0.01, and 0, respectively, or that 14 CORDIC-like iteration are as expensive as one Newton 3´10-8 iteration.

eccentric anomaly E -8 2´10 E-sin(E), E 5.4. Comparison with Fukushima(1997) E (M) 1´10-8 55 There are some similarities between this and the work of (6M)1/3 Fukushima(1997), and a comparison can be enlightening and 0 useful to put our method in context. Fukushima(1997) uses a 0 1´10-23 2´10-23 3´10-23 4´10-23 5´10-23 128 long table sampled uniformly in E which provides an accu- mean anomaly M racy of 1/128. In contrast, we need a table of length log2 128 = 7 to obtain this accuracy and higher accuracy is not limited by a Fig. 7. The ill-posed Kepler equation in the corner M = 0 and e = 1. huge table. While Fukushima(1997) likely needs fewer itera- Comparison of the computer approximations M(E) = E − e sin E (red) tions due to the use of discretised Newton method, we have no and our algorithm E55(M) (blue) with a cubic root (black; from well- divisions. posed inversion of a third-order Taylor expansion). Fukushima(1997) developed an iterative scheme with trun- cated Taylor series of Newton’s method to avoid recomputa- tion of sine and cosine angles. Similar, our methods outlined 5.2. Accuracy of algorithm variants in Sects. 3.2 and 3.3 avoids such recomputations by using trun- We find that the algorithm with positive and negative rotations cated Taylor series for sine and cosine for small angles αn. Our +− (En , Sect.2) provides slightly worse accuracy for all eccentric- method appears less complex since it needs only three short ities (Fig. C.2). For instance for e = 0.9, it is limited to 10−15 and equations. it ranges up to 10−5 for e = 1 at M = 10−16 (E = 10−5). The combination of one final Newton iteration with E as 29 5.5. Universal Kepler’s equation suggested in Sect. 3.2 has a similar accuracy as E55 (in par- ticular at e = 1) and provides therefore an alternative short- Our algorithms can solve eccentric and hyperbolic Kepler’s cut. The combination of one Halley iteration with E19 has equations. The two are very similar and could be unified, sim- generally similar performance, but for high eccentricities it ilar to the method in Walther(1971). Both algorithm includes approaches 10−6. also the eccentricity e = 1 (which are the limits for rectilinear ellipse and hyperbola, i.e. radial cases). The question is, there- fore, whether our algorithm can also be applied to universal 5.3. Performance Kepler’s equation. Before we discuss some performance results, we should mention Following Fukushima(1999) and using the Stump ff func- that a comparison of the algorithm speed is not simple. Depend- tion of third degree, c3 (Stumpff 1959), the universal Kepler’s ing on the hardware, multiplications might be little or much more equation can be written as

A128, page 5 of9 A&A 619, A128 (2018)

3 2 L = G + κG c3(λG ) (30) is good competition for Newton’s method. However, CORDIC √ √ algorithms utilise their full potential when implemented in hard- λG − sin λG = G + κ √ (31) ware, that is, directly as a digital circuit. So-called field pro- λ3 grammable gate arrays (FGPA) might be a possibility to install " # G3 λG5 λ2G7 our algorithm closer to machine layout. Indeed, hardware ori- = G + κ − + − · · · (32) 3! 5! 7! ented approaches can be very successful. This was shown by the GRAvity PipelinE (GRAPE) project (Hut & Makino 1999), with universal mean anomaly L, universal eccentric anomaly G, which tackled N-body problems. By implementing Newtonian 1−e e pair-wise forces efficiently in hardware, it demonstrated a huge Brunnow’s parameter λ = 1+e , and κ = 1+e . In parabolic case performance boost and solved new astrodynamical problems (e = 1, λ = 0), it becomes a cubic√ equation. Using Eq. (31) and the relations L = 1√+λ M and E = λG, one can derive Kepler’s (Sugimoto et al. 2003). 2 λ3 Though we could not completely transfer the original equation, Eq. (1) (see Appendix A in Fukushima 1999). CORDIC algorithm to Kepler’s equation, it might benefit from Equation (31) is expressed with a sine term. However, the ongoing developments and improvement of CORDIC algorithms application of a CORDIC-like algorithm to solve for G seems √ which is still an active field. In the light of CORDIC, solving not possible due to the scaling term λ inside the sine function. Kepler’s equations appears almost as simple as computing a sine function. 6. Summary Acknowledgements. The author thanks the referee Kleomenis Tsiganis for use- We show that the eccentric anomaly E from Kepler’s equation ful comments and M. Kürster and S. Reffert for manuscript reading. This work is can be computed by a CORDIC-like approach and is an interest- supported by the Deutsche Forschungsgemeinschaft under DFG RE 1664/12-1 ◦ ing alternative to other existing tools. The start vector E0 = 0 and Research Unit FOR2544 “Blue Planets around Red Stars”, project no. RE 1664/14-1. and its Cartesian representation (cos E0 = 1 and sin E0 = 0) are rotated using a set of basis angles. The basis angles αn and its trigonometric function values can be pre-computed and stored References in an auxiliary table. Since the table is short and independent of e, it can be also hard-coded. Boyd, J. P. 2007, Appl. Numer. Math., 57, 12 Our method provides E, sin E, and cos E without calling any Burkardt, T. M., & Danby, J. M. A. 1983, Celest. Mech., 31, 317 Colwell, P. 1993, Solving Kepler’s Equation over three Centuries (Richmond, at run-time. The precision is adjustable VA: Willmann-Bell) via the number of iterations. For instance, single precision is Danby, J. M. A. 1987, Celest. Mech., 40, 303 obtained with n = 29 iterations. Using double precision arith- Feinstein, S. A., & McLaughlin, C. A. 2006, Celest. Mech. Dyn. Astron., 96, q 49 −15 2 metic, we found the accuracy is limited to ∼10 1−e in the Ford, E. B. 2009, New Ast., 14, 406 extreme corner (M = 0, e = 1). For accuracy and speed we re- Fukushima, T. 1997, Celest. Mech. Dyn. Astron., 66, 309 Fukushima, T. 1999, Celest. Mech. Dyn. Astron., 75, 201 commend the one-sided algorithm described in Sect. 3.1. Hachaïchi, Y., & Lahbib, Y. 2016, ArXiv e-prints [arXiv: 1606.02468] Our method is very flexible. As a stand-alone method it can Hut, P., & Makino, J. 1999, Science, 283, 501 provide high accuracy, but it can be also serve start value for Jain, A., Khare, K., & Aggarwal, S. 2013, Int. J. Comput. Appl., 63, 1 other refinement routines and coupled with Newton’s and Hal- Maharatna, K., Troya, A., Banerjee, S., & Grass, E. 2004, IEE Proc. Comput. Digital Tech., 151, 448 ley’s method. In this context we proposed in Sects. 3.2 and 3.3 Markley, F. L. 1995, Celest. Mech. Dyn. Astron., 63, 101 to propagate cosine and sine terms simultaneously using small Stumpff, K. 1959, Himmelsmechanik (Berlin: VEB ) angle approximations in the trigonometric addition theorems and Stumpff, K. 1968, Rep. NASA Tech. Note, 29, 4460 derived the limits when they can be applied without accuracy Sugimoto, D. 2003, in Astrophysical Supercomputing using Particle loss. Simulations, eds. J. Makino, & P. Hut, IAU Symp., 208, 1 Volder, J. E. 1959, IRE Trans. Electron. Comput., 330 Though the number of iterations appears relatively large, Walther, J. S. 1971, in Proceedings of the May 18–20, 1971, Spring Joint the computational load per iteration is small. Indeed, a with Computer Conference (New York: ACM), AFIPS 71, 379 simple software implementation we find a performance that Wisdom, J., & Holman, M. 1991, AJ, 102, 1528

A128, page 6 of9 M. Zechmeister: CORDIC-like method for solving Kepler’s equation

Appendix A: Illustrative python code The function frexp returns the mantissa and the exponent of a floating point number. Vice versa, the function ldexp needs a The following codes implement the two-sided algorithms mantissa and an exponent to create a floating point number. described in Sects.2 and4.

Appendix B: Comparison with original CORDIC A.1. Elliptic case algorithm for the sine The CORDIC algorithm for the sine function is in practice fur- 1 # initialise cos-sin look-up table ther optimised to work solely with additive operation and bitshift 2 from math import pi, cos, sin, floor operation and without multiplication or even division. The rota- 3 divtwopi =1/(2 pi) * tion angles α are slightly modified for this purpose such that 4 acs = [(a, cos(a), sin(a)) for a in \ n now for n > 1 5 [pi/2**i for i in range(1,60)]] 6 1 tan α = · (B.1) 7 def Ecs(M, e, n=29): n 2n−2 8 E =2 pi floor(M divtwopi+0.5) * * * ◦ 9 cosE, sinE = 1., 0. As in Sect.2, the first iteration starts with α1 = 90 (tan α1 = ◦ 10 for a,cosa,sina in acs[:n]: ∞). The second angle is also the same α2 = 45 (tan α2 = 1), 11 if E-e sinE > M: while all other angles are somewhat larger than in Eq. (7) and * P∞ ◦ 12 a, sina = -a, -sina the convergence range is n = 1 αn = 1.0549π = 189.9 . Using 13 E += a Eq. (B.1) we can write Eq. (12) as 14 cosE, sinE = cosE*cosa - sinE*sina,\ ! " 1 # ! c 1 −σ − c 15 cosE*sina + sinE*cosa n+1 = cos α n+1 2n 1 n . (B.2) s n+1 1 s 16 return E, cosE, sinE n+1 σn+1 2n−1 1 n

As an example, calling the function with Ecs(2-sin(2), 1) The next move in CORDIC is to take out the factor cos αn should return the triple (1.99999999538762, from this equation and to accumulate it separately. This saves -0.4161468323531165, 0.9092974287451092). two multiplications, but changes the magnitude of the vector by We note that this pure python code illustrates the function- cos αn in each iteration and after n iteration by ality and low complexity (not performance). An implementation n n n−2 in C (including a wrapper to python) and gnuplot with algorithm Y Y 1 Y 1 Kn = cos αi = = √ · (B.3) variants (e.g. one-sided for improved accuracy and speed) are p 2 −i i i 1 + tan αi i 1 + 4 available online1. = 2 = 2 = 0 This scale correction factor Kn can be applied once after the last iteration. The iteration scheme for the scale dependent vector A.2. Hyperbolic case Xn, Yn (where cn = KnXn and sn = KnYn) becomes σ X = X − Y n+1 (B.4) 1 # initialise cosh-sinh look-up table n+1 n n 2n−1 2 from math import log, cosh, sinh, frexp, ldexp σ Y = Y + X n+1 · (B.5) 3 ln2 = log(2) n+1 n n 2n−1 4 acsh = [(a, cosh(a), sinh(a)) for a in \ 5 [4*ln2/2**i for i in range(1,60)]] This could be applied for the solution of Eq. (1), too. However, 6 the check for rotation direction in Eq. (6) requires then a scale 7 def Hcs(M, e, n=29): correction (En − eKnXn > M). So one multiplication remains, 8 m = max(0, frexp(M/e)[1]) while one multiplication can still be saved. The additional mul- 9 if M <0: m = -m tiplication with e could be saved by including it into the start 10 H = m * ln2 vector (i.e. z0 = e exp(iE0) = e = X0 for E0 = 0). 11 coshH = ldexp(1, m-1) + ldexp(1, -m-1) The final move in CORDIC is to map float values to inte- 12 sinhH = ldexp(1, m-1) - ldexp(1, -m-1) ger values which has the great advantage that the division by 13 for a,cosha,sinha in acsh[:n]: a power of two can replaced by bit shifts. But since we could 14 if e*sinhH-H > M: not eliminate all multiplications, the full concept of CORDIC is 15 a, sinha = -a, -sinha unfortunately not directly applicable to solve Kepler’s equation. 16 H += a Therefore, we call our method CORDIC-like. 17 coshH, sinhH = coshH*cosha + sinhH*sinha,\ However, it should be noted that also scale-free CORDIC 18 coshH*sinha + sinhH*cosha algorithms have been developed (Hachaïchi & Lahbib 2016) at 19 return H, coshH, sinhH the expense of some additional bitshift operations. Furthermore, the scale correction Kn, which converges to K∞ ≈ 0.607253 ≈ Calling Hcs(sinh(2)-2, 1) returns the triple 1/1.64676, does not change in double precision for n = 26. This (1.9999999991222275, 3.7621956879000753, means from iteration n = 26 onwards, the concept of CORDIC 3.626860404544669). is applicable, when setting the magnitude of the start vector to K26e. Similar, for n = 29, the cosine term becomes one in dou- ble precision (cos αn = 1), which also indicates a quasi-constant scale factor and means that we can save this multiplication. In 1 https://github.com/mzechmeister/ke Sects. 3.2 and 3.3 we exploit this fact in another way.

A128, page 7 of9 A&A 619, A128 (2018)

Appendix C: Accuracy plots

10-12 e = 1.0 10-8 e = 1.0 e = 0.9999999999 e = 0.9 -9 10-13 e = 0.5 10 e = 0.5 - E| - E| -8 -10 -8 55 3´10 55 10 3´10 10-16 10-16 -11 = |E -14 -16 = |E 10 -16 δ 10 10 E/(1-e) δ -14 10 E/(1-e) -16 -16 -16 2´10 /E -12 2´10 /E 10 -12 10-16E 10-16E -13 1 - 10 10-15 10 -10

-14 1 - 10 e = 1 - 10 10 1 - 10 -16 -15 10 0.99999999 10 0.999999 absolute deviation absolute deviation 0.9999 10-16 0.99 10-17 10-17 0 0.5 1 1.5 2 2.5 3 10-16 10-14 10-12 10-10 10-8 10-6 10-4 0.01 1 eccentric anomaly E eccentric anomaly E

Fig. C.1. Similar to Figs.5 and6, but as function of E.

10-4 10-4 e = 0.9 e = 1 E+- E+- 10-6 55 10-6 55 E55 E55 - E| E29 + 1xNewton - E| E29 + 1xNewton 55 -8 55 -8 10 E19 + 1xHalley 10 E19 + 1xHalley 10-16/(1-e cos E)+10-16E 10-16/(1-e cos E)+10-16E = |E = |E δ 10-16E δ 10-16E 10-10 10-10

10-12 10-12

10-14 10-14 absolute deviation absolute deviation 10-16 10-16

10-16 10-14 10-12 10-10 10-8 10-6 10-4 0.01 1 10-16 10-14 10-12 10-10 10-8 10-6 10-4 0.01 1 eccentric anomaly E eccentric anomaly E Fig. C.2. Accuracy comparison of algorithm variants.

A128, page 8 of9 M. Zechmeister: CORDIC-like method for solving Kepler’s equation

Appendix D: Starting estimate for the hyperbolic The right side of Eq. (D.1) is plotted in Fig.4 as a red case dotted line. It belongs to the family of start guesses given in 2M M 1 Burkardt & Danby(1983)( H0 = ln( e + k)). For e = 2 , e = 1, H H − H ≈ . . It can be shown that Eq. (27) always provides a lower bound for and 0 = 0, the deviation is 0 1 396 = 2 014 ln 2. Since, H in case of M ≥ 0. Starting with Kepler’s equation Eq. (20) and unfortunately, the pre-factor is slightly larger than two, the con- 1 vergence range is expanded to 4 ln 2 by setting α1 = 2 ln 2 in using the identity sinh x = 2 [exp x−exp(−x)] and the inequation exp(−x) ≥ 1 − 2x (for x ≥ 0), we obtain Eq. (22). For this large convergence range, we can further simply the e  H  start guess by omitting the additive one in Eq. (D.1) M = e sinh H − H = exp H − exp(−H) − 2 2 e   " !# 2M 2M M e 1 H > ln = ln 2 · log2 = ln 2 · 1 + log2 (D.2) ≤ exp H − 1 + 2H 1 − e e e 2 e  M  e > ln 2 · floor 1 + log2 (D.3) ≤ [exp H − 1]. e 2 which finally leads to the start value for m in Eq. (27). The right For large values H, the approximation becomes more accu- function in Eq. (D.2) is plotted is in Fig.4 as a blue dashed line. rate. Further reforming and introduction of the binary logarithm M At e = 1 and e = 1, the hyperbolic anomaly is H ≈ 1.729 = yields 2.495 ln 2 and thus inside our convergence domain. ! ! The simple start guess developed here is valid for all e ≥ 1 2M 2M and a lower bound for M ≥ 0, meaning that it is suitable for H ≥ ln + 1 = ln 2 · log2 + 1 . (D.1) e e one-sided algorithms.

A128, page 9 of9