U
    —9%eM  ã                   @  sò   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlmZ dd	lmZ d
ddœdd„Zd
ddœdd„Zd
ddœdd„Zeddœdd„ƒZeddœdd„ƒZeddœdd„ƒZeddœdd „ƒZd!dœd"d#„Zd$S )%aÛ  A module for special angle forumlas for trigonometric functions

TODO
====

This module should be developed in the future to contain direct squrae root
representation of

.. math
    F(\frac{n}{m} \pi)

for every

- $m \in \{ 3, 5, 17, 257, 65537 \}$
- $n \in \mathbb{N}$, $0 \le n < m$
- $F \in \{\sin, \cos, \tan, \csc, \sec, \cot\}$

Without multi-step rewrites
(e.g. $\tan \to \cos/\sin \to \cos/\sqrt \to \ sqrt$)
or using chebyshev identities
(e.g. $\cos \to \cos + \cos^2 + \cdots \to \sqrt{} + \sqrt{}^2 + \cdots $),
which are trivial to implement in sympy,
and had used to give overly complicated expressions.

The reference can be found below, if anyone may need help implementing them.

References
==========

.. [*] Gottlieb, Christian. (1999). The Simple and straightforward construction
   of the regular 257-gon. The Mathematical Intelligencer. 21. 31-37.
   10.1007/BF03024829.
.. [*] https://resources.wolframcloud.com/FunctionRepository/resources/Cos2PiOverFermatPrime
é    )Úannotations)ÚCallable)Úreduce)ÚExpr)ÚS)ÚigcdexÚInteger©Úsqrt)ÚcacheitÚintztuple[tuple[int, ...], int])ÚxÚreturnc                    s”   | sdS t | ƒdkr d| d fS t | ƒdkrPt| d | d ƒ\}‰ }|ˆ f|fS t| dd… Ž \}}t| d |ƒ\}‰ }|f‡ fdd„|D ƒ˜|fS )	aN  Compute extended gcd for multiple integers.

    Explanation
    ===========

    Given the integers $x_1, \cdots, x_n$ and
    an extended gcd for multiple arguments are defined as a solution
    $(y_1, \cdots, y_n), g$ for the diophantine equation
    $x_1 y_1 + \cdots + x_n y_n = g$ such that
    $g = \gcd(x_1, \cdots, x_n)$.

    Examples
    ========

    >>> from sympy.functions.elementary._trigonometric_special import migcdex
    >>> migcdex()
    ((), 0)
    >>> migcdex(4)
    ((1,), 4)
    >>> migcdex(4, 6)
    ((-1, 1), 2)
    >>> migcdex(6, 10, 15)
    ((1, 1, -1), 1)
    )© r   é   )r   r   é   Nc                 3  s   | ]}ˆ | V  qd S ©Nr   )Ú.0Úi©Úvr   úp/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/functions/elementary/_trigonometric_special.pyÚ	<genexpr>R   s     zmigcdex.<locals>.<genexpr>)Úlenr   Úmigcdex)r   ÚuÚhÚyÚgr   r   r   r   -   s    r   ztuple[int, ...])Údenomsr   c                    sF   | sdS ddddœdd„}t || ƒ‰ ‡ fdd„| D ƒ}t|Ž \}}|S )aâ  Compute the the partial fraction decomposition.

    Explanation
    ===========

    Given a rational number $\frac{1}{q_1 \cdots q_n}$ where all
    $q_1, \cdots, q_n$ are pairwise coprime,

    A partial fraction decomposition is defined as

    .. math::
        \frac{1}{q_1 \cdots q_n} = \frac{p_1}{q_1} + \cdots + \frac{p_n}{q_n}

    And it can be derived from solving the following diophantine equation for
    the $p_1, \cdots, p_n$

    .. math::
        1 = p_1 \prod_{i \ne 1}q_i + \cdots + p_n \prod_{i \ne n}q_i

    Where $q_1, \cdots, q_n$ being pairwise coprime implies
    $\gcd(\prod_{i \ne 1}q_i, \cdots, \prod_{i \ne n}q_i) = 1$,
    which guarantees the existance of the solution.

    It is sufficient to compute partial fraction decomposition only
    for numerator $1$ because partial fraction decomposition for any
    $\frac{n}{q_1 \cdots q_n}$ can be easily computed by multiplying
    the result by $n$ afterwards.

    Parameters
    ==========

    denoms : int
        The pairwise coprime integer denominators $q_i$ which defines the
        rational number $\frac{1}{q_1 \cdots q_n}$

    Returns
    =======

    tuple[int, ...]
        The list of numerators which semantically corresponds to $p_i$ of the
        partial fraction decomposition
        $\frac{1}{q_1 \cdots q_n} = \frac{p_1}{q_1} + \cdots + \frac{p_n}{q_n}$

    Examples
    ========

    >>> from sympy import Rational, Mul
    >>> from sympy.functions.elementary._trigonometric_special import ipartfrac

    >>> denoms = 2, 3, 5
    >>> numers = ipartfrac(2, 3, 5)
    >>> numers
    (1, 7, -14)

    >>> Rational(1, Mul(*denoms))
    1/30
    >>> out = 0
    >>> for n, d in zip(numers, denoms):
    ...    out += Rational(n, d)
    >>> out
    1/30
    r   r   )r   r   r   c                 S  s   | | S r   r   )r   r   r   r   r   Úmul—   s    zipartfrac.<locals>.mulc                   s   g | ]}ˆ | ‘qS r   r   )r   r   ©Údenomr   r   Ú
<listcomp>›   s     zipartfrac.<locals>.<listcomp>)r   r   )r   r    Úar   Ú_r   r!   r   Ú	ipartfracU   s    ?
r&   zlist[int] | None)Únr   c                 C  sF   g }dD ]8}t | |ƒ\}}|dkr|} | |¡ | dkr|  S qdS )z}If n can be factored in terms of Fermat primes with
    multiplicity of each being 1, return those primes, else
    None
    )é   é   é   é  i  r   r   N)ÚdivmodÚappend)r'   ZprimesÚpZquotientÚ	remainderr   r   r   Úfermat_coords    s    

r0   r   )r   c                   C  s   t jS )z-Computes $\cos \frac{\pi}{3}$ in square roots)r   ÚHalfr   r   r   r   Úcos_3°   s    r2   c                   C  s   t dƒd d S )z-Computes $\cos \frac{\pi}{5}$ in square rootsr)   r   é   r	   r   r   r   r   Úcos_5¶   s    r4   c                   C  s|   t dt dƒ d t dƒt dt dƒ ƒt t dƒdt dt dƒ ƒ dt dƒ t dt dƒ ƒ   dt dƒ  d ƒ  d  ƒS )	z.Computes $\cos \frac{\pi}{17}$ in square rootsé   r*   é    r   iøÿÿÿr   é   é"   r	   r   r   r   r   Úcos_17¼   s    "$ÿ
ÿÿÿþÿr9   c            )      C  sâ  ddddœdd„} ddddœdd„}| t jtdƒƒ\}}| |td	ƒƒ\}}| |td	ƒƒ\}}| |d
d| d|   ƒ\}}	| |d
d| d|   ƒ\}
}| |d
d| d|   ƒ\}}| |d
d| d|   ƒ\}}| |d|| | d|
   ƒ\}}| |d|| | d|   ƒ\}}| |d|| |	 d|   ƒ\}}| |d|| |
 d|   ƒ\}}| |	d||	 | d|   ƒ\}}| |
d||
 | d|   ƒ\}}| |d|| | d|   ƒ\}}| |d|| | d|	   ƒ\}}||d|| | |  ƒ} ||d|| | |  ƒ}!||d|| | |  ƒ}"||d|| | |  ƒ}#||d|| | |  ƒ}$||d|| | |  ƒ}%||  d|!|"  ƒ }&||# d|$|%  ƒ }'d||& d|' ƒ }(ttdƒt|(d
 ƒ d t j ƒS )a  Computes $\cos \frac{\pi}{257}$ in square roots

    References
    ==========

    .. [*] https://math.stackexchange.com/questions/516142/how-does-cos2-pi-257-look-like-in-real-radicals
    .. [*] https://r-knott.surrey.ac.uk/Fibonacci/simpleTrig.html
    r   ztuple[Expr, Expr])r$   Úbr   c                 S  s0   | t | d | ƒ d | t | d | ƒ d fS ©Nr   r	   ©r$   r:   r   r   r   Úf1Ï   s    zcos_257.<locals>.f1c                 S  s   | t | d | ƒ d S r;   r	   r<   r   r   r   Úf2Ò   s    zcos_257.<locals>.f2é   é@   r3   r)   r   éüÿÿÿéþÿÿÿé   )r   ZNegativeOner   r
   r1   ))r=   r>   Út1Út2Zz1Zz3Zz2Zz4Úy1Zy5Zy6Úy2Zy3Zy7Zy8Zy4Úx1Zx9Zx2Úx10Zx3Zx11Zx4Zx12Zx5Zx13Zx6Zx14Zx15Zx7Zx8Zx16Zv1Zv2Zv3Zv4Zv5Zv6Úu1Úu2Zw1r   r   r   Úcos_257Å   s6    
""""""""rL   zdict[int, Callable[[], Expr]]c                   C  s   t tttdœS )ag  Lazily evaluated table for $\cos \frac{\pi}{n}$ in square roots for
    $n \in \{3, 5, 17, 257, 65537\}$.

    Notes
    =====

    65537 is the only other known Fermat prime and it is nearly impossible to
    build in the current SymPy due to performance issues.

    References
    ==========

    https://r-knott.surrey.ac.uk/Fibonacci/simpleTrig.html
    )r(   r)   r*   r+   )r2   r4   r9   rL   r   r   r   r   Ú	cos_tableð   s
    ürM   N)Ú__doc__Ú
__future__r   Útypingr   Ú	functoolsr   Zsympy.core.exprr   Zsympy.core.singletonr   Zsympy.core.numbersr   r   Z(sympy.functions.elementary.miscellaneousr
   Zsympy.core.cacher   r   r&   r0   r2   r4   r9   rL   rM   r   r   r   r   Ú<module>   s(   "(K*