<commit_before>import numpy as np
from numba import njit as jit

from poliastro.core.angles import E_to_M, nu_to_E, E_to_nu
from poliastro.core.elements import coe2rv, rv2coe


@jit
def recSeries_coe(k, p, ecc, inc, raan, argp, nu, tof, numiter=20, order=8):

    semi_axis_a = p / (1 - ecc ** 2)
    n = np.sqrt(k / np.abs(semi_axis_a) ** 3)

    
    if ecc == 0:
        # Solving for circular orbit
        
        M0 = nu
        M = M0 + n * tof
        nu = M - 2 * np.pi * np.floor(M / 2 / np.pi)
        return nu

    elif ecc < 1.0:
        # Solving for elliptical orbit
        
        M0 = E_to_M(nu_to_E(nu, ecc), ecc)
        M = M0 + n * tof
        M = M - 2 * np.pi * np.floor(M / 2 / np.pi)
        
        E = M
        for i in range(0,order):
            E = M + ecc*np.sin(E)
        
        return E_to_nu(E,ecc)
        
        
    else:
        raise ValueError("Parabolic/Hyperbolic orbits not supported.")

    return nu


@jit
def recSeries(k, r0, v0, tof, numiter=20, order=8):
    """Kepler solver for elliptical orbits with recursive series approximation
    method. The order of the series is a user defined parameter.

    Parameters
    ----------
    k : float
        Standard gravitational parameter of the attractor.
    r0 : numpy.ndarray
        Position vector.
    v0 : numpy.ndarray
        Velocity vector.
    tof : float
        Time of flight.
    numiter : int, optional
        Number of iterations, defaults to 20.
    order : int, optional
        Order of recursion, defaults to 8.

    Returns
    -------
    rr : numpy.ndarray
        Final position vector.
    vv : numpy.ndarray
        Final velocity vector.

    Notes
    -----
    This algorithm uses series developed in the paper *Recursive solution to
    Kepler’s problem for elliptical orbits - application in robust 
    Newton-Raphson and co-planar closest approach estimation* 
    with DOI: http://dx.doi.org/10.13140/RG.2.2.18578.58563/1
    """

    # Solve first for eccentricity and mean anomaly
    p, ecc, inc, raan, argp, nu = rv2coe(k, r0, v0)
    nu = recSeries_coe(k, p, ecc, inc, raan, argp, nu, tof, numiter, order)

    return coe2rv(k, p, ecc, inc, raan, argp, nu)<commit_msg>Corrections and Comments<commit_after>import numpy as np
from numba import njit as jit

from poliastro.core.angles import E_to_M, nu_to_E, E_to_nu
from poliastro.core.elements import coe2rv, rv2coe


@jit
def recSeries_coe(k, p, ecc, inc, raan, argp, nu, tof, order=8):

    # semi-major axis
    semi_axis_a = p / (1 - ecc ** 2)
    # mean angular motion
    n = np.sqrt(k / np.abs(semi_axis_a) ** 3)
    
    if ecc == 0:
        # Solving for circular orbit
        
        # compute initial mean anoamly
        M0 = nu # For circular orbit (M = E = nu)
        # final mean anaomaly
        M = M0 + n * tof
        # snapping anomaly to [0,pi] range
        nu = M - 2 * np.pi * np.floor(M / 2 / np.pi)
        
        return nu

    elif ecc < 1.0:
        # Solving for elliptical orbit
        
        # compute initial mean anoamly
        M0 = E_to_M(nu_to_E(nu, ecc), ecc)
        # final mean anaomaly
        M = M0 + n * tof
        # snapping anomaly to [0,pi] range
        M = M - 2 * np.pi * np.floor(M / 2 / np.pi)
        
        # compute eccentric anomaly through recursive series
        E = M
        for i in range(0,order):
            E = M + ecc*np.sin(E)
        
        return E_to_nu(E,ecc)

    else:
        # Parabolic/Hyperbolic orbits are not supported
        raise ValueError("Parabolic/Hyperbolic orbits not supported.")

    return nu


@jit
def recSeries(k, r0, v0, tof, order=8):
    """Kepler solver for elliptical orbits with recursive series approximation
    method. The order of the series is a user defined parameter.

    Parameters
    ----------
    k : float
        Standard gravitational parameter of the attractor.
    r0 : numpy.ndarray
        Position vector.
    v0 : numpy.ndarray
        Velocity vector.
    tof : float
        Time of flight.
    order : int, optional
        Order of recursion, defaults to 8.

    Returns
    -------
    rr : numpy.ndarray
        Final position vector.
    vv : numpy.ndarray
        Final velocity vector.

    Notes
    -----
    This algorithm uses series discussed in the paper *Recursive solution to
    Kepler’s problem for elliptical orbits - application in robust 
    Newton-Raphson and co-planar closest approach estimation* 
    with DOI: http://dx.doi.org/10.13140/RG.2.2.18578.58563/1
    """

    # Solve first for eccentricity and mean anomaly
    p, ecc, inc, raan, argp, nu = rv2coe(k, r0, v0)
    nu = recSeries_coe(k, p, ecc, inc, raan, argp, nu, tof, order)

    return coe2rv(k, p, ecc, inc, raan, argp, nu)
{'fraction_non_alphanumeric': 0.0968013468013468, 'fraction_numerical': 0.021885521885521887, 'mean_word_length': 1.9613707165109033, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4322327', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1549, 'n_words': 531}