orbix.equations.orbit
=====================

.. py:module:: orbix.equations.orbit

.. autoapi-nested-parse::

   Common equations for orbital mechanics.



Attributes
----------

.. autoapisummary::

   orbix.equations.orbit.two_pi_G
   orbix.equations.orbit._TOL_E
   orbix.equations.orbit._TOL_N
   orbix.equations.orbit._ARC_EPS


Functions
---------

.. autoapisummary::

   orbix.equations.orbit.period_a
   orbix.equations.orbit.period_n
   orbix.equations.orbit.mean_motion
   orbix.equations.orbit.period_to_sma
   orbix.equations.orbit.semi_amplitude
   orbix.equations.orbit.semi_amplitude_reduced
   orbix.equations.orbit.mean_anomaly_t0
   orbix.equations.orbit.mean_anomaly_tp
   orbix.equations.orbit.AB_matrices
   orbix.equations.orbit.AB_matrices_reduced
   orbix.equations.orbit.thiele_innes_constants
   orbix.equations.orbit.thiele_innes_constants_reduced
   orbix.equations.orbit._safe_norm
   orbix.equations.orbit.state_vector_to_keplerian


Module Contents
---------------

.. py:data:: two_pi_G

.. py:function:: period_a(a, mu)

   Orbital period from semi-major axis and standard gravitational parameter.

   :param a: Array
             Semi-major axis
   :param mu: Array
              Standard gravitational parameter

   :returns:

             Array
                 Orbital period
   :rtype: T


.. py:function:: period_n(n)

   Orbital period from mean motion.

   :param n: Array
             Mean motion

   :returns:

             Array
                 Orbital period
   :rtype: T


.. py:function:: mean_motion(a, mu)

   Mean motion from semi-major axis and standard gravitational parameter.

   :param a: Array
             Semi-major axis
   :param mu: Array
              Standard gravitational parameter

   :returns:

             Array
                 Mean motion
   :rtype: n


.. py:function:: period_to_sma(T, Ms)

   Semi-major axis from orbital period via Kepler's third law.

   :param T: Orbital period (days). Scalar or array.
   :param Ms: Stellar mass (kg). Scalar or array.

   :returns: Semi-major axis (AU). Scalar or array.
   :rtype: a


.. py:function:: semi_amplitude(T, Ms, Mp, e, i)

   Semi-amplitude of the radial velocity curve from base quantities.

   :param T: Array
             Orbital period
   :param Ms: Array
              Mass of the star
   :param Mp: Array
              Mass of the planet
   :param e: Array
             Eccentricity
   :param i: Array
             Inclination

   :returns:

             Array
                 Semi-amplitude of the radial velocity curve
   :rtype: K


.. py:function:: semi_amplitude_reduced(T, Ms, minimum_mass, sqrt_one_minus_e2)

   Semi-amplitude of the radial velocity curve from pre-calculated quantities.

   :param T: Array
             Orbital period
   :param Ms: Array
              Mass of the star
   :param minimum_mass: Array
                        Mass of the planet multiplied by sin(i)
   :param sqrt_one_minus_e2: Array
                             Square root of (1 - eccentricity^2)

   :returns:

             Array
                 Semi-amplitude of the radial velocity curve
   :rtype: K


.. py:function:: mean_anomaly_t0(t, n, M0, t0)

   Mean anomaly at time t (can be vector) from epoch.

   Requires that all units are consistent and does NOT clip the mean anomaly
   to the range [0, 2pi).

   :param t: Array
             Time
   :param n: Array
             Mean motion
   :param M0: Array
              Mean anomaly at epoch
   :param t0: Array
              Epoch

   :returns:

             Array
                 Mean anomaly at time t
   :rtype: M


.. py:function:: mean_anomaly_tp(t, n, tp)

   Mean anomaly at time t (can be vector) from periapsis passage.

   :param t: Array
             Time
   :param n: Array
             Mean motion
   :param tp: Array
              Time of periapsis passage

   :returns:

             Array
                 Mean anomaly at time t
   :rtype: M


.. py:function:: AB_matrices(a, e, i, W, w)

   Compute the A and B matrices for a given set of orbital elements.

   In keplertools Dmitry defines these as:
   "inertial frame components of perifocal frame unit vectors scaled
   by orbit semi-major and semi-minor axes."
   and I wouldn't dare disagree with him on this.

   :param a: Array
             Semi-major axis
   :param e: Array
             Eccentricity
   :param i: Array
             Inclination
   :param W: Array
             Longitude of the ascending node
   :param w: Array
             Argument of periapsis

   :returns:

             jnp.ndarray
                 A matrix
             B: jnp.ndarray
                 B matrix
   :rtype: A


.. py:function:: AB_matrices_reduced(a, sqrt_one_minus_e2, sini, cosi, sinW, cosW, sinw, cosw)

   Compute the A and B matrices from the trig values of the orbital elements.

   :param a: Semi-major axis
   :param sqrt_one_minus_e2: Square root of (1 - eccentricity^2)
   :param sini: Sine of the inclination
   :param cosi: Cosine of the inclination
   :param sinW: Sine of the longitude of the ascending node
   :param cosW: Cosine of the longitude of the ascending node
   :param sinw: Sine of the argument of periapsis
   :param cosw: Cosine of the argument of periapsis

   :returns:

             jnp.ndarray
                 A matrix
             B: jnp.ndarray
                 B matrix
   :rtype: A


.. py:function:: thiele_innes_constants(W, i, w)

   Compute the Thiele-Innes constants from the orbital angles.

   :param W: Longitude of the ascending node
   :param i: Inclination
   :param w: Argument of periapsis

   :returns: A constant
             B: B constant
             F: F constant
             G: G constant
   :rtype: A


.. py:function:: thiele_innes_constants_reduced(sinW, cosW, sinw, cosw, sinwcosi, coswcosi)

   Compute the Thiele-Innes constants from the orbital angles.

   :param sinW: Sine of the longitude of the ascending node
   :param cosW: Cosine of the longitude of the ascending node
   :param sinw: Sine of the argument of periapsis
   :param cosw: Cosine of the argument of periapsis
   :param sinwcosi: Sine of the argument of periapsis times cosine of the inclination
   :param coswcosi: Cosine of the argument of periapsis times cosine of the inclination

   :returns: A constant
             B: B constant
             F: F constant
             G: G constant
   :rtype: A


.. py:data:: _TOL_E
   :value: 1e-09


.. py:data:: _TOL_N
   :value: 1e-09


.. py:data:: _ARC_EPS
   :value: 1e-07


.. py:function:: _safe_norm(x)

   Vector norm with a finite (zero) gradient at ``x == 0``.

   ``jnp.linalg.norm`` has a NaN gradient at exactly the zero vector
   (``x / norm(x)`` is ``0 / 0`` there). This computes the same value
   but evaluates the norm on a nonzero stand-in whenever ``x`` is zero,
   so the local derivative is finite; the ``jnp.where`` then zeroes the
   corresponding cotangent, giving a well-defined (zero) gradient.


.. py:function:: state_vector_to_keplerian(r, v, mu)

   Convert state vectors (r, v) to Keplerian elements using JAX.

   Robust implementation handling edge cases (circular, equatorial,
   and non-bound orbits) using ``jnp.where`` for JIT compatibility.

   Unit-agnostic: ``r``, ``v``, and ``mu`` must be expressed in one
   consistent unit system (e.g. meters / m/s / m^3 s^-2, or the AU / day
   units used elsewhere in this library); the function does not enforce
   or convert any particular convention, and ``a`` is returned in the
   same length unit as ``r``.

   :param r: Stellar-centric position vector ``(3,)``.
   :param v: Stellar-centric velocity vector ``(3,)``.
   :param mu: Gravitational parameter ``G * M_total``.

   :returns:

             ``(a, e, i, W, w, M)`` -- semi-major axis (same length
                 unit as ``r``), eccentricity, inclination [rad], longitude of
                 ascending node [rad], argument of periapsis [rad], mean
                 anomaly [rad].
   :rtype: tuple


