
    R1io                     R   d Z ddlmZmZmZmZmZmZmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ  ee      j$                  Z G d de      Z e       Zd Zd Zd	 Zd
 Zd Zeeeef\  ZZZZd Zd Z d Z!d Z"d Z#e!Z$e#Z%d Z&d Z'd Z(d Z)d Z*d Z+	 ddl,m-Z- d Z/y#  ddl.m.Z- Y d Z/yxY w)z@Basic operations that are needed repeatedly throughout Skyfield.    )arcsinarctan2arraycoseinsumfinfofloat64	full_likeloadrollaxissinsqrt)get_data)tauc                       e Zd ZdZeZy)Az8Allow literal NumPy arrays to be spelled ``A[1, 2, 3]``.N)__name__
__module____qualname____doc__r   __getitem__     V/home/cursorai/projects/iching/venv/lib/python3.12/site-packages/skyfield/functions.pyr   r      s
    BKr   r   c                 ,    | |z  j                  d      S )zGiven one or more vectors in `v` and `u`, return their dot products.

    This works whether `v` and `u` each have the shape ``(3,)``, or
    whether they are each whole arrays of corresponding x, y, and z
    coordinates and have shape ``(3, N)``.

    r   axis)sum)vus     r   dotsr!      s     E;;A;r   c                     t        | d      S )z*Swap the first two dimensions of an array.   )r   )Ms    r   Tr%      s    Aq>r   c                     t        d| |      S )z8Matrix times vector: multiply an NxN matrix by a vector.zij...,j...->i...r   )r$   r   s     r   mxvr(      s    $a++r   c                     t        d| |      S )z/Matrix times matrix: multiply two NxN matrices.zij...,jk...->ik...r'   )M1M2s     r   mxmr,   #   s    &B//r   c                     t        d| ||      S )zCMatrix times matrix times matrix: multiply 3 NxN matrices together.zij...,jk...,kl...->il...r'   )r*   r+   M3s      r   mxmxmr/   '   s    ,b"b99r   c                 >    t        | | z  j                  d            S )zGiven a 3-element array |xyz|, return its length.

    The three elements can be simple scalars, or the array can be two
    dimensions and offer three whole series of x, y, and z coordinates.

    r   r   )r   r   )xyzs    r   	length_ofr2   -   s     sQ'((r   c                     | t        |      z  }|t        |       z  }dt        t        ||z
        t        ||z               z  S )a  Given two vectors `v` and `u`, return the radian angle separating them.

    This works whether `v` and `u` each have the shape ``(3,)``, or
    whether they are each whole arrays of corresponding x, y, and z
    coordinates with shape ``(3, N)``. The returned angle will be
    between 0 and tau/2.

    This formula is from Section 12 of:
    https://people.eecs.berkeley.edu/~wkahan/Mindless.pdf

    g       @)r2   r   )r    r   abs       r   angle_betweenr6   6   sD     	
IaLA	IaLA1q5)9QU+;<<<r   c                 ~    t        |       }| \  }}}t        ||t        z   z        }t        ||      t        z  }|||fS )a|  Convert |xyz| to spherical coordinates (r,theta,phi).

    ``r`` - vector length
    ``theta`` - angle above (+) or below (-) the xy-plane
    ``phi`` - angle around the z-axis

    Note that ``theta`` is an elevation angle measured up and down from
    the xy-plane, not a polar angle measured from the z-axis, to match
    the convention for both latitude and declination.

    )r2   r   _AVOID_DIVIDE_BY_ZEROr   r   )r1   rxyzthetaphis          r   to_sphericalr?   F   sI     	#AGAq!11123E
!Q-#
CeS=r   c                 Z   | \  }}}|\  }}}t        |       }t        ||t        z   z        }	t        ||      t        z  }
t        | |      t        |       z  }||z  }||z  }||z   t        z   }||z  |||z  ||z  z   z  z
  |||z  z   t        |      z  z  }||z  ||z  z
  |z  }||	|
|||fS )N)r2   r   r8   r   r   r!   r   )r9   r   r:   r;   r<   xdotydotzdotlengthlatlon
range_ratex2y2
x2_plus_y2lat_ratelon_rates                    r   _to_spherical_and_ratesrM   X   s    GAq!D$q\F
f445
6C
!Q-#
Cail*J	
QB	
QBb00JT!ATAH)<$==	ac	T*--/HD4!8#z1H3Z8;;r   c                     | t        |      z  }t        |t        |      z  |t        |      z  | t        |      z  f      S )a  Convert (r,theta,phi) to Cartesian coordinates |xyz|.

    ``r`` - vector length
    ``theta`` - angle in radians above (+) or below (-) the xy-plane
    ``phi`` - angle in radians around the z-axis

    Note that ``theta`` is an elevation angle measured up and down from
    the xy-plane, not a polar angle measured from the z-axis, to match
    the convention for both latitude and declination.

    )r   r   r   )r9   r=   r>   rxys       r   from_sphericalrP   k   s;     c%j.C#C.#C.!c%j.ABBr   c                 r    t        |       }t        |       }| dz  }|dz   }t        |||f||| f|||ff      S N        g      ?r   r   r   r=   cszeroones        r   rot_xrZ      sM    E
AE
A3;D
*C3d#dAr]T1aLABBr   c                 r    t        |       }t        |       }| dz  }|dz   }t        |||f|||f| ||ff      S rR   rT   rU   s        r   rot_yr\      sM    E
AE
A3;D
*C1dA,sD 1QBa=ABBr   c                 r    t        |       }t        |       }| dz  }|dz   }t        || |f|||f|||ff      S rR   rT   rU   s        r   rot_zr^      sM    E
AE
A3;D
*C1qb$-!QdC/@ABBr   c                 L    | \  }}}|dz  }t        || |f||| f| ||ff      S )NrS   )r   )angular_velocity_vectorr:   r;   r<   rX   s        r   angular_velocity_matrixra      s@    %GAq!s7D4!Q-!TA2!Q>??r   c                 b    t        | d      r| S t        | d      rt        |       S t        |       S )aS  Convert plain Python sequences into NumPy arrays.

    This lets users pass plain old Python lists and tuples to Skyfield,
    instead of always having to remember to build NumPy arrays.  We pass
    any kind of generic sequence to the NumPy ``array()`` constructor
    and wrap any other kind of value in a NumPy ``float64`` object.

    shape__len__)hasattrr   r	   )values    r   	_to_arrayrg      s1     ug			"U|u~r   c                    t        | dd      }t        |dd      }||z
  }|dkD  r.|r| xj                  d|z  z  c_        | |fS t        ||       } | |fS |dk  r+|r|xj                  d| z  z  c_        | |fS t        | |      }| |fS )zECoerce two NumPy generics-or-arrays to the same number of dimensions.ndimr   )r#   )getattrrc   r
   )r4   r5   anbn
differences        r   
_reconcilern      s    	FA	B	FA	BbJA~GGtj((G a4K !QA a4K 
aGGtzk))G a4K !QAa4Kr   )BytesIO)StringIOc                 `    t        ddj                  |             }t        t        |            S )z=Load a binary NumPy array file that is bundled with Skyfield.skyfieldzdata/{0})r   formatr   ro   )filenamedatas     r   load_bundled_npyrv      s(    J
 1 1( ;<Dr   N)0r   numpyr   r   r   r   r   r   r	   r
   r   r   r   r   pkgutilr   skyfield.constantsr   tinyr8   objectr   r!   r%   r(   r,   r/   _T_mxv_mxm_mxmxmr2   r6   r?   rM   rP   to_polar
from_polarrZ   r\   r^   ra   rg   rn   ioro   rp   rv   r   r   r   <module>r      s    F     "g++   C,0: Ce+ D$)= $<&C" 
CCC@
 "--,s   B B&