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

CORDIC-like method for solving Kepler’s equation · 3.2. CORDIC-like Newton method The convergenceof the CORDIC-like algorithms in Sect. 2 and 3.1 is linear, while Newton’s method

  • Upload
    others

  • View
    16

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CORDIC-like method for solving Kepler’s equation · 3.2. CORDIC-like Newton method The convergenceof the CORDIC-like algorithms in Sect. 2 and 3.1 is linear, while Newton’s method

arX

iv:1

808.

0706

2v2

[as

tro-

ph.I

M]

19

Nov

201

8Astronomy & Astrophysics manuscript no. ke_cordic c©ESO 2018November 20, 2018

CORDIC-like method for solving Kepler’s equation

M. Zechmeister1

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

Received 4 April 2018 / Accepted 14 August 2018

ABSTRACT

Context. Many algorithms to solve Kepler’s equations require the evaluation of trigonometric or root functions.Aims. We present an algorithm to compute the eccentric anomaly and even its cosine and sine 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 additions and multiplications and a short table. The table isindependent 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-alonealgorithm, single and double precision is obtained with 29 and 55 iterations, respectively. Half or two-thirds of the iterations can besaved in combination with Newton’s or Halley’s method at the cost of one division.

Key words. celestial mechanics – methods: numerical

1. Introduction

Kepler’s equation relates the mean anomaly M and the eccentricanomaly E in orbits with eccentricity e. For elliptic orbits it isgiven by

E − e sin E = M(E). (1)

The function is illustrated for eccentricities 0, 0.1, 0.5, 0.9, and 1in Fig. 1. It is straightforward to compute M(E). But in practiceM is usually given and the inverse function E(M) must be solved.

The innumerable publications about the solution of Kepler’sequation (Colwell 1993) highlight its importance in many fieldsof astrophysics (e.g. exoplanet search, planet formation, andstar cluster evolution), astrodynamics, and trajectory optimisa-tion. N-body hybrid algorithms also make use of this analyticsolution of the two-body problem (Wisdom & Holman 1991).Nowadays, computers can solve Eq. (1) quickly. But the speedincrease is counterbalanced by large data sets, simulations, andextensive data analysis (e.g. with Markov chain Monte Carlo).This explains ongoing efforts to accelerate the computation withsoftware and hardware, for example by parallelising and use ofgraphic processing units (GPUs; Ford 2009).

The Newton-Raphson iteration is a common method to solveEq. (1) and employs the derivative E′. In each step n the solutionis refined by

En+1 = En + E′(Mn)(M − Mn) = En −En − e sin En − M

1 − e cos En

. (2)

A simple starting estimate might be E0 = M + 0.85e (Danby1987). Each iteration comes at the cost of evaluating one cosineand one sine function. Hence one tries to minimise the numberof iterations. This can be done with a better starting estimate. Forexample, Markley (1995) provides a starting estimate better than10−4 by inversion of a cubic polynomial which however requiresalso transcendental functions (four roots). Boyd (2007) furtherpolynomialise Kepler’s equation through Chebyshev polynomial

expansion of the sine term and yielded with root finding meth-ods a maximum error of 10−10 after inversion of a fifteen-degreepolynomial. Another possibility to reduce the iteration is to usehigher order corrections. For instance Padé approximation of or-der [1/1] leads to Halley’s method.

Pre-computed tables can be an alternative way for fast com-putation of E. Fukushima (1997) used a table equally spaced inE to accelerate a discretised Newton method, while Feinstein& McLaughlin (2006) proposed an equal spacing in M, that is,a direct lookup table which must be e-dependent and thereforetwo-dimensional. Both tables can become very large dependingon the desired accuracy.

So the solution of the transcendental Eq. (1) often comesback to other transcendental equations which themselves need tobe solved in each iteration. This poses questions as to how thoseoften built-in functions are solved and whether there is a way toapply a similar, more direct algorithm to Kepler’s equation.

The implementation details of those built-in functions arehardware and software dependent. But sine and cosine are of-ten computed with Taylor expansion. After range reduction andfolding into an interval around zero, Taylor expansion is herequite efficient yielding 10−16 with 17th degree at π

4. Kepler’s

equation can also be Taylor expanded (Stumpff 1968) but that isless efficient, in particular for e = 1 and around M = 0 where thederivative becomes infinite. Similarly, root or arcsine functionsare other examples where the convergence of the Taylor expan-sion is slow. For root-like functions, one again applies Newton-Raphson or bisection methods. The arcsine can be computed asarcsin(x) = arctan( x√

1−x2) where Taylor expansion of the arctan-

gent function is efficient and one root evaluation is needed.

An interesting alternative method to compute trigonomet-ric functions is the Coordinate Rotation Digital Computer(CORDIC) algorithm which was developed by Volder (1959) forreal-time computation of sine and cosine functions and whichfor instance found application in pocket calculators. CORDICcan compute those trigonometric and other elementary functions

Article number, page 1 of 8

Page 2: CORDIC-like method for solving Kepler’s equation · 3.2. CORDIC-like Newton method The convergenceof the CORDIC-like algorithms in Sect. 2 and 3.1 is linear, while Newton’s method

A&A proofs: manuscript no. ke_cordic

0

π–4

π–2

3π–4

π

0 π–4π–2

3π–4 π

ecce

ntric

ano

mal

y E

mean anomaly M

E - e sin E = M

e = 0e = 0.1e = 0.5e = 0.9e = 1

Fig. 1. Kepler’s equation for five different eccentricities.

in a simple and efficient way. In this work we study whether andhow the CORDIC algorithm can be applied to Kepler’s equation.

2. Scale-free CORDIC algorithm for Kepler’s

equation

Analogous to CORDIC, we compose our angle of interest, E, bya number of positive or negative rotations σi = ±1 with angle αi

En =

n∑

i=1

σiαi. (3)

The next iteration n is then

En+1 = En + σn+1αn+1. (4)

The rotation angle αn is halved in each iteration

αn+1 =αn

2=α1

2n(5)

so that we successively approach E. The direction of the nextrotation σn+1 is selected depending on the mean anomaly Mn

calculated from En via Eq. (1). If Mn = En − e sin En overshootsthe target value M, then En+1 will be decreased, otherwise in-creased

σn+1 =

{

−1 En − e sin En > M

+1 else.(6)

Equations (4)–(6) provide the iteration scheme which so faris a binary search or bisection method (with respect to E) to findthe inverse of M(E). Choosing as start value E0 = 0 and startrotation α1 =

π

2, we can cover the range E ∈ [−π, π] and pre-

compute the values of rotation angles with Eq. (5)

αn =π

2n. (7)

The main difference of our algorithm from the CORDIC sineis the modified decision for the rotation direction in Eq. (6) con-sisting of the additional term e sin E. For the special case e = 0,the eccentric and mean anomalies unify and the iteration con-verges to En → E = M.

E0

E1

E2

E3E4

E5

M0

M1

M2

M3

M4 M5

ME

Fig. 2. Example of five CORDIC rotations given M = 2−sin 2 ≈ 62.49◦

and e = 1. Starting with M0 = 0 and E0 = 0, Mn and En approach Mand E = 2 ≈ 114.59◦ . The arcs indicates the current rotation angle αn

and direction at each step.

One major key point of CORDIC is that, besides the angleEn, it propagates simultaneously the Cartesian representation,which are cosine and sine terms of En. The next iterations areobtained through trigonometric addition theorems

cos En+1 = cos(En + σn+1αn+1) (8)

= cos En cosαn+1 − σn+1 sin En sinαn+1 (9)

sin En+1 = sin(En + σn+1αn+1) (10)

= σn+1 cos En sinαn+1 + sin En cosαn+1, (11)

which is a multiplication with a rotation matrix

(

cn+1

sn+1

)

=

[

cosαn+1 −σn+1 sinαn+1

σn+1 sinαn+1 cosαn+1

] (

cn

sn

)

. (12)

Here we introduced the abbreviations cn = cos En and sn =

sin En.Since the iteration starts with E0 = 0, we simply have c0 =

cos E0 = 1 and s0 = sin E0 = 0. The cosαn and sinαn terms canalso be pre-computed using αn from Eq. (7) and stored in a tablelisting the triples (αn, cosαn, sinαn). Now we have eliminatedall function calls for sine and cosine of E and α in the iteration.This is also true and important for the sin En term in Eq. (6)which is simply gathered during the iteration process as sn viaEq. (12). Figures 2 and 3 illustrate the convergence for inputM = 2 − sin 2 ≈ 1.0907 and e = 1 towards E = 2.

In Appendix A we provide a short python code which imple-ments the algorithm described in this section. A slightly moreelegant formulation is possible with complex numbers zn =

exp(iEn) = cos En+i sin En and an = exp(iαn) = cosαn+i sinαn.Then Eq. (12) becomes zn+1 = an+1zn or zn+1 = a∗

n+1zn depending

on σn and in Eq. (6) the substitution sin En = Im(zn) has to bedone.

A more detailed comparison with CORDIC is given Ap-pendix B. Before analysing the performance in Sect. 5.3, weoutline some optimisation possibilities in Sect. 3.

3. Variants and refinements

3.1. Minimal rotation base

The algorithms in Sect. 2 and Appendix B use always positiveor negative rotation directions. While after n rotations the max-imum error should be smaller then αn, previous iterations canstill be closer to the convergence limit. In Fig. 2, for instance,

Article number, page 2 of 8

Page 3: CORDIC-like method for solving Kepler’s equation · 3.2. CORDIC-like Newton method The convergenceof the CORDIC-like algorithms in Sect. 2 and 3.1 is linear, while Newton’s method

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

0

π–4

π–2

3π–4

π

0 π–4π–2

3π–4 π

ecce

ntric

ano

mal

y E

mean anomaly M

M(E), EE3(M)E5(M)

En(Mn)M=2-sin 2, E=2 0

1

2

3

4

5

itera

tion

nFig. 3. CORDIC-like approximation of Kepler’s equation (e = 1). Thecurves E3 and E5 illustrate the convergence towards Kepler’s equationafter three and five iterations. The coloured points Mn, En are passedduring the iterations to find E(M = 2− sin 2) (same colour-coding as inFig. 2).

E3 is closer to E than E4 and E5. Consecutive rotations willcompensate this. A very extreme case is M = 0, where E0 = 0 isalready the exact value, but the first rotation brings it to E1 = 90◦

followed by only negative rotation towards zero.

The CORDIC algorithm requires positive and negative rota-tions to have always the same scale Kn after n iterations (Ap-pendix B). But since we could not pull out Kn from the itera-tion, we can depart from this concept and allow for only zeroand positive rotations which could be called unidirectional (Jainet al. 2013) or one-sided (Maharatna et al. 2004). The decisionto rotate or not to rotate becomes

σn+1 =

{

1 (En + αn+1) − e sin(En + αn+1) < M

0 else.(13)

Thereby we compose En in Eq. (3) by minimal rotations and canexpect also a better propagation of precision. Moreover, whilethe term sin(En + αn+1) = snca + cnsa still needs to be computedin each iteration to derive for σn+1, the term cn+1 is updated onlywhen σn+1 = 1 which will be on average in about 50% of thecases. We note also that with the minimal rotation base the curvein Eq. (1) is approached from below.

3.2. CORDIC-like Newton method

The convergence of the CORDIC-like algorithms in Sect. 2 and3.1 is linear, while Newton’s method is quadratic. Of course,our algorithm can serve start values (and their cosine and sineterms) for other root finders at any stage. The quadratic conver-gence should bring the accuracy from, for example, 10−8 downto 10−16 at the cost of mainly one division. We think this shouldbe preferred over the other possible optimisations mentioned inAppendix B.

Still, we do not want to lose the ability to provide the cosineand sine terms after one (or more) Newton iterations. We canpropagate them simultaneously in a similar manner as in Eqs. (4)and (12) but now using small angle approximations. Newton’s

method (Eq. (2)) directly proposes a rotation angle

αn+1 = En+1 − En =M − En + esn

1 − ecn

. (14)

This means that there is no need to check for a rotation directionσn. For small angles, there are the approximations sinα ≈ αand cosα ≈ 1. If one works with double precision (2−53), the

error ǫ(α) = |1 − cosα| < α2

2is negligible, meaning smaller than

2−54 ≈ 5.5 × 10−17, when |α| < 7.5 × 10−9 (given for a29). Thenwe can write

cn+1 = cn + αn+1 sn (15)

sn+1 = sn + αn+1cn. (16)

3.3. Halley’s method

Halley’s method has a cubic convergence. Ignoring third orderterms, one can apply the approximations sinα ≈ α and cosα ≈1 − α2

2. The error ǫ(α) = |α − sinα| < | α3

6| is negligible in double

precision, when α <3√

6 × 2−54 = 6.93 × 10−6 given for α19 ≈5.99 × 10−6. Similar to Sect. 3.2, the iteration scheme with co-rotation for Halley’s method is

αn+1 =(1 − ecn)(M − Mn)

(1 − ecn)2 + 12esn(M − Mn)

(17)

sn+1 =

1 −α2

n+1

2

sn + αn+1cn (18)

cn+1 =

1 −α2

n+1

2

cn + αn+1sn. (19)

4. Hyperbolic mode

For eccentricities e ≥ 1, Kepler’s equation is

M = e sinh H − H. (20)

This case can be treated as being similar to the elliptic case, whenthe trigonometric terms are replaced by hyperbolic analogues.Equations (6), (7), and (12) become

σn+1 =

{

−1 e sinh Hn − Hn > M

+1 else(21)

αn =4 ln 2

2n(22)

(

cn+1

sn+1

)

=

[

coshαn+1 σn+1 sinhαn+1

σn+1 sinhαn+1 coshαn+1

] (

cn

sn

)

. (23)

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-ever, H extends to infinity. In the elliptic case, this is handled byan argument reduction which makes use of the periodicity. In thehyperbolic case, the corresponding nice base points H0 are

H0 = m ln 2 (24)

c0 = cosh H0 =1

2[exp H0 + exp(−H0)] = 2m−1 + 2−m−1 (25)

s0 = sinh H0 =1

2[exp H0 − exp(−H0)] = 2m−1 − 2−m−1. (26)

Article number, page 3 of 8

Page 4: CORDIC-like method for solving Kepler’s equation · 3.2. CORDIC-like Newton method The convergenceof the CORDIC-like algorithms in Sect. 2 and 3.1 is linear, while Newton’s method

A&A proofs: manuscript no. ke_cordichy

perb

olic

ano

mal

y H

mean anomaly M

H0 ± 4ln2H0

sinh(H)-H, Hm = 0, ±1, ±2, ...

ln(2M/e)ln(2M/e+1)

-3

-2

-1

0

1

2

3

4

-5 0 5 10 15 20

Fig. 4. Range extension for hyperbolic Kepler’s equation (black curve)in case e = 1. The start value H0 (blue thick line) and the convergencedomain of ±4 ln 2 (shaded area) covers Kepler’s equation. Nice basepoints (black dots) and two approximations (dotted and dashed curves)are also indicated.

For m = 0, this becomes H0 = 0, c0 = 1, and s0 = 0, in otherwords similar to the elliptic case. For m , 0, the start triple isstill simple to compute using only additions and bitshifts.

The main challenge is to obtain the integer m from meananomaly M. In elliptic case, this needs a division with 2π; inhyperbolic case, it becomes a logarithmic operation. Figure 4and Appendix D show that

m = sign M ·max

[

0, floor

(

1 + log2

M

e

)]

(27)

provides start values with the required coarse accuracy of bet-ter than 4 ln 2. In floating point representation (IEEE 754), thesecond argument in the maximum function of Eq. (27) extractssimply the exponent of M/e. In fixed point representation, it isthe most significant non-zero bit (Walther 1971). This means alogarithm call is not necessary (cf. Appendix A.2).

The hyperbolic iterations do not return cos H and sin H, butcosh H and sinh H which are the quantities needed to computedistance, velocity, and Cartesian position.

5. Accuracy and performance study

5.1. Accuracy of the minimal rotation base algorithm

The CORDIC-like algorithms have a linear convergence rate.The maximum error for En in iteration n is given by αn. Forinstance, we expect from Eq. (7) α22 < 10−6, α29 < 10−8 (singleprecision), α42 < 10−12 and α55 < 10−16 (double precision).To verify if double precision can be achieved in practice, weforward-calculated with Eq. (1) 1 000 (M(E), E) pairs uniformlysampled in E over [0, π]. Here E might be seen as the true value.Then we injected M into our algorithms to solve the inverseproblem E(M).

The differences between re-computed and true eccentricanomaly, δ55 = |E55 − E|, are shown in Fig. 5 for the casese = 0.5, 0.9, and 1. For M & 0.25 and e ≤ 1, the devia-tions are smaller than 10−15 and close to machine precision ǫas indicated by the grey lines. Towards M = 0 and e > 0.9,the scatter increases. Figure 6 greatly magnifies this corner by

10-17

10-16

10-15

10-14

10-13

10-12

0 0.5 1 1.5 2 2.5 3

abso

lute

dev

iatio

n δ

= |E

55 -

E|

mean anomaly M

e = 1.0e = 0.9e = 0.53·10-8

10-16

10-16E/(1-e)2·10-16/E

10-16E

Fig. 5. Accuracy. For visibility, zero deviations (δ = 0) within machineprecision were lifted to δ = 2 × 10−17.

plotting the deviations against the logarithm of E for e = 0.5,0.999 999 999 9, and 1.

The deviations δ55 become large in this corner, because theinversion of Eq. (1) is ill-posed. For e = 1, third order expansionof Eq. (1) is M = E − (E − 1

6E3) = 1

6E3. If the quadratic term in

1 − 16E2 is below machine precision ǫ, it cannot be propagated

numerically in the subtraction and leads to a maximum deviation

of E <√

6ǫ ≈ 10−8 at E = 10−8 (M = 10−24).Figure 7 illustrates the ill-posed problem. For e = 1 and very

small M, the cubic approximation provides a very accurate ref-erence. For comparison, we overplot our E55 solution as well ascomputer generated pairs E-sin(E),E. Both exhibit a discreti-sation of 10−8 in E. We conclude that Newton’s, Halley’s, andother methods which directly try to solve Eq. (1) are as wellonly accurate to 10−8 in this corner when operating with doubleprecision! Therefore, the M, E pairs mentioned above were notgenerated with built-in sin()-function but using a Taylor expan-sion of Kepler’s equations

M = E(1 − e) − e

(

−E3

3!+

E5

5!− . . .

)

. (28)

We may call the term in brackets rest function of sine. It dif-fers by a factor of −E−3 compared to its first naming in Stumpff(1959); see also Eq. (32).

We find that the deviations approximately follow the form

δ55(E, e) = |E55 − E| ≈ 10−16 + 10−16 E

1 − e cos E+ 10−16E, (29)

as indicated by the curves in Figs. 5 and 6. For very smallM and E, the constant term 10−16 dominates. Then the mid-dle term, which is related to the derivative of Eq. (1), sets in.Finally, the linear term 10−16E takes over which describes theaccuracy to which E can be stored. The middle term leads to anascending and a descending branch which can be approximatedby 10−16 E

1−eand 10−16 2

Eand forms a local maximum. The two

lines intersect at E ≈√

2(1 − e) and δ55(e) ≈ 10−16

21−e

. For

highest resolvable eccentricity below one, this provides δ(e =

1 − 10−16) ≈√

2 × 10−8.

Article number, page 4 of 8

Page 5: CORDIC-like method for solving Kepler’s equation · 3.2. CORDIC-like Newton method The convergenceof the CORDIC-like algorithms in Sect. 2 and 3.1 is linear, while Newton’s method

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

10-17

10-16

10-15

10-14

10-13

10-12

10-11

10-10

10-9

10-8

0.01 110-25 10-20 10-15 10-10 10-5

abso

lute

dev

iatio

n δ

= |E

55 -

E|

mean anomaly M

e = 1.0e = 0.9999999999

e = 0.5

1

- 10

-14

e =

1 - 1

0-1

2

1 - 1

0-1

0

0.99

9999

990.

9999

99

0.99

99

0.99

3·10-8

10-16

10-16E/(1-e)2·10-16/E

10-16E

Fig. 6. Similar to Fig. 4, but with logarithmic scaling.

0

1·10-8

2·10-8

3·10-8

4·10-8

5·10-8

6·10-8

7·10-8

0 1·10-23 2·10-23 3·10-23 4·10-23 5·10-23

ecce

ntric

ano

mal

y E

mean anomaly M

E-sin(E), E

E55(M)

(6M)1/3

Fig. 7. The ill-posed Kepler equation in the corner M = 0 and e = 1.Comparison of the computer approximations M(E) = E − e sin E (red)and our algorithm E55(M) (blue) with a cubic root (black; from well-posed inversion of a third-order Taylor expansion).

5.2. Accuracy of algorithm variants

We find that the algorithm with positive and negative rotations(E+−n , Sect. 2) provides slightly worse accuracy for all eccentric-ities (Fig. C.2). For instance for e = 0.9, it is limited to 10−15 andit ranges up to 10−5 for e = 1 at M = 10−16 (E = 10−5).

The combination of one final Newton iteration with E29 assuggested in Sect. 3.2 has a similar accuracy as E55 (in particularat e = 1) and provides therefore an alternative shortcut. The com-bination of one Halley iteration with E19 has generally similarperformance, but for high eccentricities it approaches 10−6.

5.3. Performance

Before we discuss some performance results, we should mentionthat a comparison of the algorithm speed is not simple. Depend-ing on the hardware, multiplications might be little or much moreexpensive than additions. Then there is the question about the

level of optimisation done in the code, by the compiler, or inbuilt-in routines.

It can be also instructive to count the number of floatingpoint operations. In Appendix A we see five multiplications,four additions, and one comparison in each iteration. For singleprecision this must be executed 29 times. On the other hand,a Newton iteration in Eq. (2) has one division, two multiplica-tions, four additions, and one sine and cosine term. In case thelatter two terms are computed via Taylor expansion to 17th orderand with a Horner scheme, each will contribute additional ninemultiplications and nine additions. Furthermore, the cosine andsine routines have to check the need for range reduction eachtime. Actually, also an additional final sine and cosine operationshould be loaded to Newton’s methods, since sin E and cos E areusually needed in subsequent calculations.

Given same weight to all listed operations, we count ap-proximately ten operations per CORDIC iteration vs. about per43 operation per Newton iteration. Then we estimate that fourCORDIC iteration are as expensive as one Newton iteration.

We have implemented both CORDIC-like and Newton’smethod in a C program. The Newton’s method used the startguess E0 = M + 0.85e and called cos and sin function from thestandard math.h library. The run time was measured with pythontimeit feature for α29 (10−8) and for 1000 mean anomalies uni-formly sampled from 0 to π. The run-time of the CORDIC-likealgorithm is independent of e, while Newton’s method is fast forsmall eccentricities and slower for high eccentricities. We mea-sured that our CORDIC-like method has double, same, and halfspeed for e = 1, 0.01, and 0, respectively, or that 14 CORDIC-like iteration are as expensive as one Newton iteration.

5.4. Comparison with Fukushima (1997)

There are some similarities between this and the work ofFukushima (1997), and a comparison can be enlightening anduseful to put our method in context. Fukushima (1997) uses a128 long table sampled uniformly in E which provides an accu-racy of 1/128. In contrast, we need a table of length log2 128 = 7to obtain this accuracy and higher accuracy is not limited by ahuge table. While Fukushima (1997) likely needs fewer itera-tions due to the use of discretised Newton method, we have nodivisions.

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 inSects. 3.2 and 3.3 avoids such recomputations by using truncatedTaylor series for sine and cosine for small angles αn. Our methodappears less complex since it needs only three short equations.

5.5. Universal Kepler’s equation

Our algorithms can solve eccentric and hyperbolic Kepler’sequations. The two are very similar and could be unified, similarto the method in Walther (1971). Both algorithm includes alsothe eccentricity e = 1 (which are the limits for rectilinear el-lipse and hyperbola, i.e. radial cases). The question is, therefore,whether our algorithm can also be applied to universal Kepler’sequation.

Following Fukushima (1999) and using the Stumpff functionof third degree, c3 (Stumpff 1959), the universal Kepler’s equa-

Article number, page 5 of 8

Page 6: CORDIC-like method for solving Kepler’s equation · 3.2. CORDIC-like Newton method The convergenceof the CORDIC-like algorithms in Sect. 2 and 3.1 is linear, while Newton’s method

A&A proofs: manuscript no. ke_cordic

tion can be written as

L = G + κG3c3(λG2) (30)

= G + κ

√λG − sin

√λG

√λ3

(31)

= G + κ

[

G3

3!−λG5

5!+λ2G7

7!− . . .

]

(32)

with universal mean anomaly L, universal eccentric anomaly G,Brunnow’s parameter λ = 1−e

1+e, and κ = e

1+e. In parabolic case

(e = 1, λ = 0), it becomes a cubic equation. Using Eq. (31) and

the relations L = 1+λ

2√λ3

M and E =√λG, one can derive Kepler’s

equation, Eq. (1) (see Appendix A in Fukushima 1999).

Equation (31) is expressed with a sine term. However, theapplication of a CORDIC-like algorithm to solve for G seems

not possible due to the scaling term√λ inside the sine function.

6. Summary

We show that the eccentric anomaly E from Kepler’s equationcan be computed by a CORDIC-like approach and is an interest-ing alternative to other existing tools. The start vector E0 = 0◦

and its Cartesian representation (cos E0 = 1 and sin E0 = 0) arerotated using a set of basis angles. The basis angles αn and itstrigonometric function values can be pre-computed and storedin an auxiliary table. Since the table is short and independent ofe, it can be also hard-coded.

Our method provides E, sin E, and cos E without calling anytranscendental function at run-time. The precision is adjustablevia the number of iterations. For instance, single precision is ob-tained with n = 29 iterations. Using double precision arithmetic,

we found the accuracy is limited to ∼ 10−15

21−e

in the extreme

corner (M = 0, e = 1). For accuracy and speed we recommendthe one-sided algorithm described in Sect. 3.1.

Our method is very flexible. As a stand-alone method it canprovide high accuracy, but it can be also serve start value forother refinement routines and coupled with Newton’s and Hal-ley’s method. In this context we proposed in Sects. 3.2 and 3.3to propagate cosine and sine terms simultaneously using smallangle approximations in the trigonometric addition theorems andderived the limits when they can be applied without accuracyloss.

Though the number of iterations appears relatively large, thecomputational load per iteration is small. Indeed, a with simplesoftware implementation we find a performance that is goodcompetition for Newton’s method. However, CORDIC algo-rithms utilise their full potential when implemented in hardware,that is, directly as a digital circuit. So-called field programmablegate arrays (FGPA) might be a possibility to install our algorithmcloser to machine layout. Indeed, hardware oriented approachescan be very successful. This was shown by the GRAvity PipelinE(GRAPE) project (Hut & Makino 1999), which tackled N-bodyproblems. By implementing Newtonian pair-wise forces effi-ciently in hardware, it demonstrated a huge performance boostand solved new astrodynamical problems (Sugimoto 2003).

Though we could not completely transfer the originalCORDIC algorithm to Kepler’s equation, it might benefit fromongoing developments and improvement of CORDIC algorithmswhich is still an active field. In the light of CORDIC, solvingKepler’s equations appears almost as simple as computing a sinefunction.

Acknowledgements. The author thanks the referee Kleomenis Tsiganis for usefulcomments and M. Kürster and S. Reffert for manuscript reading. This work issupported by the Deutsche Forschungsgemeinschaft under DFG RE 1664/12-1and Research Unit FOR2544 "Blue Planets around Red Stars", project no. RE1664/14-1.

References

Boyd, J. P. 2007, Appl. Numer. Math., 57, 12Burkardt, T. M. & Danby, J. M. A. 1983, Celestial Mechanics, 31, 317Colwell, P. 1993, Solving Kepler’s equation over three centuries (Richmond, Va.:

Willmann-Bell, 1993.)Danby, J. M. A. 1987, Celestial Mechanics, 40, 303Feinstein, S. A. & McLaughlin, C. A. 2006, Celestial Mechanics and Dynamical

Astronomy, 96, 49Ford, E. B. 2009, New A, 14, 406Fukushima, T. 1997, Celestial Mechanics and Dynamical Astronomy, 66, 309Fukushima, T. 1999, Celestial Mechanics and Dynamical Astronomy, 75, 201Hachaïchi, Y. & Lahbib, Y. 2016, ArXiv e-prints [arXiv:1606.02468]Hut, P. & Makino, J. 1999, Science, 283, 501Jain, A., Khare, K., & Aggarwal, S. 2013, International Journal of Computer

Applications, 63, 1, full text availableMaharatna, K., Troya, A., Banerjee, S., & Grass, E. 2004, IEE Proceedings -

Computers and Digital Techniques, 151, 448Markley, F. L. 1995, Celestial Mechanics and Dynamical Astronomy, 63, 101Stumpff, K. 1959, Himmelsmechanik.Stumpff, K. 1968, Rep. NASA Tech. Note, NASA-TN-D-4460, 29 p., 4460Sugimoto, D. 2003, in IAU Symposium, Vol. 208, Astrophysical Supercomput-

ing using Particle Simulations, ed. J. Makino & P. Hut, 1Volder, J. E. 1959, IRE Transactions on electronic computers, 330Walther, J. S. 1971, in Proceedings of the May 18-20, 1971, Spring Joint Com-

puter Conference, AFIPS ’71 (Spring) (New York, NY, USA: ACM), 379–385Wisdom, J. & Holman, M. 1991, AJ, 102, 1528

Appendix A: Illustrative python code

The following codes implement the two-sided algorithms de-scribed in Sects. 2 and 4.

Appendix A.1: Elliptic case

1 # initialise cos-sin look-up table

2 from math import pi, cos, sin, floor

3 divtwopi = 1 / (2*pi)

4 acs = [(a, cos(a), sin(a)) for a in \

5 [pi/2**i for i in range(1,60)]]

6

7 def Ecs(M, e, n=29):

8 E = 2 * pi * floor(M*divtwopi+0.5)

9 cosE, sinE = 1., 0.

10 for a,cosa,sina in acs[:n]:

11 if E-e*sinE > M:

12 a, sina = -a, -sina

13 E += a

14 cosE, sinE = cosE*cosa - sinE*sina,\

15 cosE*sina + sinE*cosa

16 return E, cosE, sinE

As an example, calling the function with Ecs(2-sin(2), 1)

should return the triple (1.99999999538762,

-0.4161468323531165, 0.9092974287451092).We note that this pure python code illustrates the function-

ality and low complexity (not performance). An implementationin C (including a wrapper to python) and gnuplot with algorithmvariants (e.g. one-sided for improved accuracy and speed) areavailable online1.

1 https://github.com/mzechmeister/ke

Article number, page 6 of 8

Page 7: CORDIC-like method for solving Kepler’s equation · 3.2. CORDIC-like Newton method The convergenceof the CORDIC-like algorithms in Sect. 2 and 3.1 is linear, while Newton’s method

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

Appendix A.2: Hyperbolic case

1 # initialise cosh-sinh look-up table

2 from math import log, cosh, sinh, frexp, ldexp

3 ln2 = log(2)

4 acsh = [(a, cosh(a), sinh(a)) for a in \

5 [4*ln2/2**i for i in range(1,60)]]

6

7 def Hcs(M, e, n=29):

8 m = max(0, frexp(M/e)[1])

9 if M < 0: m = -m

10 H = m * ln2

11 coshH = ldexp(1, m-1) + ldexp(1, -m-1)

12 sinhH = ldexp(1, m-1) - ldexp(1, -m-1)

13 for a,cosha,sinha in acsh[:n]:

14 if e*sinhH-H > M:

15 a, sinha = -a, -sinha

16 H += a

17 coshH, sinhH = coshH*cosha + sinhH*sinha,\

18 coshH*sinha + sinhH*cosha

19 return H, coshH, sinhH

Calling Hcs(sinh(2)-2, 1) returns the triple(1.9999999991222275, 3.7621956879000753,

3.626860404544669).The function frexp returns the mantissa and the exponent of

a floating point number. Vice versa, the function ldexp needs amantissa and an exponent to create a floating point number.

Appendix B: Comparison with original CORDIC

algorithm for the sine

The CORDIC algorithm for the sine function is in practice fur-ther optimised to work solely with additive operation and bitshiftoperation and without multiplication or even division. The rota-tion angles αn are slightly modified for this purpose such thatnow for n > 1

tanαn =1

2n−2. (B.1)

As in Sect. 2, the first iteration starts with α1 = 90◦ (tanα1 =

∞). The second angle is also the same α2 = 45◦ (tanα2 = 1),while all other angles are somewhat larger than in Eq. (7) andthe convergence range is

∑∞n=1 αn = 1.0549π = 189.9◦. Using

Eq. (B.1) we can write Eq. (12) as(

cn+1

sn+1

)

= cosαn+1

[

1 −σn+11

2n−1

σn+11

2n−1 1

] (

cn

sn

)

. (B.2)

The next move in CORDIC is to take out the factor cosαn

from this equation and to accumulate it separately. This savestwo multiplications, but changes the magnitude of the vector bycosαn in each iteration and after n iteration by

Kn =

n∏

i=2

cosαi =

n∏

i=2

1√

1 + tan2 αi

=

n−2∏

i=0

1√

1 + 4−i. (B.3)

This scale correction factor Kn can be applied once after the lastiteration. The iteration scheme for the scale dependent vectorXn, Yn (where cn = KnXn and sn = KnYn) becomes

Xn+1 = Xn − Yn

σn+1

2n−1(B.4)

Yn+1 = Yn + Xn

σn+1

2n−1. (B.5)

This could be applied for the solution of Eq. (1), too. However,the check for rotation direction in Eq. (6) requires then a scalecorrection (En − eKnXn > M). So one multiplication remains,while one multiplication can still be saved. The additional mul-tiplication with e could be saved by including it into the startvector (i.e. z0 = e exp(iE0) = e = X0 for E0 = 0).

The final move in CORDIC is to map float values to inte-ger values which has the great advantage that the division bya power of two can replaced by bit shifts. But since we couldnot eliminate all multiplications, the full concept of CORDIC isunfortunately not directly applicable to solve Kepler’s equation.Therefore, we call our method CORDIC-like.

However, it should be noted that also scale-free CORDICalgorithms have been developed (Hachaïchi & Lahbib 2016) atthe expense of some additional bitshift operations. Furthermore,the scale correction Kn, which converges to K∞ ≈ 0.607253 ≈1/1.64676, does not change in double precision for n = 26. Thismeans from iteration n = 26 onwards, the concept of CORDICis applicable, when setting the magnitude of the start vector toK26e. Similar, for n = 29, the cosine term becomes one in doubleprecision (cosαn = 1), which also indicates a quasi-constantscale factor and means that we can save this multiplication. InSects. 3.2 and 3.3 we exploit this fact in another way.

Appendix C: Accuracy plots

Appendix D: Starting estimate for the hyperbolic

case

It can be shown that Eq. (27) always provides a lower bound forH in case of M ≥ 0. Starting with Kepler’s equation Eq. (20) andusing the identity sinh x = 1

2[exp x−exp(−x)] and the inequation

exp(−x) ≥ 1 − 2x (for x ≥ 0), we obtain

M = e sinh H − H =e

2

[

exp H − exp(−H) − 2H

e

]

≤ e

2

[

exp H − 1 + 2H

(

1 − 1

e

)]

≤ e

2[exp H − 1].

For large values H, the approximation becomes more accu-rate. Further reforming and introduction of the binary logarithmyields

H ≥ ln

(

2M

e+ 1

)

= ln 2 · log2

(

2M

e+ 1

)

. (D.1)

The right side of Eq. (D.1) is plotted in Fig. 4 as a red dottedline. It belongs to the family of start guesses given in Burkardt& Danby (1983) (H0 = ln( 2M

e+ k)). For M

e= 1

2, e = 1, and

H0 = 0, the deviation is H − H0 ≈ 1.396 = 2.014 ln 2. Since,unfortunately, the pre-factor is slightly larger than two, the con-vergence range is expanded to 4 ln 2 by setting α1 = 2 ln 2 inEq. (22).

For this large convergence range, we can further simply thestart guess by omitting the additive one in Eq. (D.1)

H > ln2M

e= ln 2 · log2

2M

e= ln 2 ·

(

1 + log2

M

e

)

(D.2)

> ln 2 · floor

(

1 + log2

M

e

)

(D.3)

which finally leads to the start value for m in Eq. (27). The rightfunction in Eq. (D.2) is plotted is in Fig. 4 as a blue dashed line.

Article number, page 7 of 8

Page 8: CORDIC-like method for solving Kepler’s equation · 3.2. CORDIC-like Newton method The convergenceof the CORDIC-like algorithms in Sect. 2 and 3.1 is linear, while Newton’s method

A&A proofs: manuscript no. ke_cordic

10-17

10-16

10-15

10-14

10-13

10-12

0 0.5 1 1.5 2 2.5 3

abso

lute

dev

iatio

n δ

= |E

55 -

E|

eccentric anomaly E

e = 1.0e = 0.9999999999

e = 0.53·10-8

10-16

10-16E/(1-e)2·10-16/E

10-16E

10-17

10-16

10-15

10-14

10-13

10-12

10-11

10-10

10-9

10-8

0.01 110-16 10-14 10-12 10-10 10-8 10-6 10-4

abso

lute

dev

iatio

n δ

= |E

55 -

E|

eccentric anomaly E

e = 1.0e = 0.9e = 0.5

1 - 1

0-1

6

1 -

10-1

4e

= 1

- 10

-12

1 - 1

0-1

00.

9999

9999

0.99

9999

0.99

990.

99

3·10-8

10-16

10-16E/(1-e)2·10-16/E

10-16E

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

10-16

10-14

10-12

10-10

10-8

10-6

10-4

0.01 110-16 10-14 10-12 10-10 10-8 10-6 10-4

abso

lute

dev

iatio

n δ

= |E

55 -

E|

eccentric anomaly E

e = 0.9E55

+-

E55E29 + 1xNewton

E19 + 1xHalley10-16/(1-e cos E)+10-16E

10-16E

10-16

10-14

10-12

10-10

10-8

10-6

10-4

0.01 110-16 10-14 10-12 10-10 10-8 10-6 10-4

abso

lute

dev

iatio

n δ

= |E

55 -

E|

eccentric anomaly E

e = 1E55

+-

E55E29 + 1xNewton

E19 + 1xHalley10-16/(1-e cos E)+10-16E

10-16E

Fig. C.2. Accuracy comparison of algorithm variants.

At Me= 1 and e = 1, the hyperbolic anomaly is H ≈ 1.729 =

2.495 ln 2 and thus inside our convergence domain.The simple start guess developed here is valid for all e ≥ 1

and a lower bound for M ≥ 0, meaning that it is suitable forone-sided algorithms.

Article number, page 8 of 8