§
    OŠtjg  ã                  óö   — 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 ddlmZ dd	lmZ dd
lmZ dd„Zdd„Zd d„Zed!d„¦   «         Zed!d„¦   «         Zed!d„¦   «         Zed!d„¦   «         Zd"d„ZdS )#aÛ  A module for special angle formulas for trigonometric functions

TODO
====

This module should be developed in the future to contain direct square 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ÚxÚintÚreturnútuple[tuple[int, ...], int]c                 ó<  ‡— | sdS t          | ¦  «        dk    r
d| d         fS t          | ¦  «        dk    r&t          | d         | d         ¦  «        \  }Š}|‰f|fS t          | dd…         Ž \  }}t          | d         |¦  «        \  }Š}|gˆfd„|D ¦   «         ¢R |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  ó"   •K  — | ]	}‰|z  V — Œ
d S ©Nr   )Ú.0ÚiÚvs     €úo/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sympy/functions/elementary/_trigonometric_special.pyú	<genexpr>zmigcdex.<locals>.<genexpr>S   s'   øè è € Ð"Ð"˜1��Q‘Ð"Ð"Ð"Ð"Ð"Ð"ó    )Úlenr   Úmigcdex)r   ÚuÚhÚyÚgr   s        @r   r   r   .   sÀ   ø€ ð2 ð Øˆuå
ˆ1�v„v�‚{€{Ø�Q�q”TˆzÐå
ˆ1�v„v�‚{€{Ý˜˜1œ˜q œtÑ$Ô$‰ˆˆ1ˆaØ�1ˆv�qˆyÐå�A�a�b�b”Eˆ?�D€A€qÝ�Q�q”T˜1‰oŒo�G€A€qˆ!ØÐ#Ð"Ð"Ð"Ð" Ð"Ñ"Ô"Ð#Ð# QÐ&Ð&r   Údenomsútuple[int, ...]c                 ól   ‡— | sdS dd„}t          || ¦  «        Šˆfd„| D ¦   «         }t          |Ž \  }}|S )	aÞ  Compute 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 existence 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                ó   — | |z  S r   r   )r   r!   s     r   Úmulzipartfrac.<locals>.mul˜   s   € Ø�1‰uˆr   c                ó   •— g | ]}‰|z  ‘ŒS r   r   )r   r   Údenoms     €r   ú
<listcomp>zipartfrac.<locals>.<listcomp>œ   s   ø€ Ð$Ð$Ð$˜ˆ�!‰Ð$Ð$Ð$r   )r   r   r!   r   r   r   )r   r   )r#   r'   Úar    Ú_r)   s        @r   Ú	ipartfracr-   V   sc   ø€ ð~ ð Øˆrðð ð ð õ �3˜ÑÔ€EØ$Ð$Ð$Ð$˜VÐ$Ñ$Ô$€AÝ�Aˆ;�D€A€qØ€Hr   Únúlist[int] | Nonec                óˆ   — g }dD ]<}t          | |¦  «        \  }}|dk    r!|} |                     |¦  «         | dk    r|c S Œ=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.   ÚprimesÚpÚquotientÚ	remainders        r   Úfermat_coordsr;   ¡   sb   € ð
 €FØ#ð ð ˆÝ$ Q¨™lœlÑˆ�)Ø˜Š>ˆ>ØˆAØ�MŠM˜!ÑÔÐØ�AŠvˆvØ���øØˆ4r   r   c                 ó   — t           j        S )z-Computes $\cos \frac{\pi}{3}$ in square roots)r   ÚHalfr   r   r   Úcos_3r>   ±   s   € õ Œ6€Mr   c                 ó,   — t          d¦  «        dz   dz  S )z-Computes $\cos \frac{\pi}{5}$ in square rootsr2   r   é   r
   r   r   r   Úcos_5rA   ·   s   € õ �‰GŒG�a‰K˜1ÑÐr   c                 ó¾  — t          dt          d¦  «        z   dz  t          d¦  «        t          dt          d¦  «        z
  ¦  «        t          t          d¦  «        dt          dt          d¦  «        z   ¦  «        z  dt          d¦  «        z
  t          dt          d¦  «        z
  ¦  «        z  z
  z  dt          d¦  «        z  z   dz   ¦  «        z   z  dz  z   ¦  «        S )	z.Computes $\cos \frac{\pi}{17}$ in square rootsé   r3   é    r   iøÿÿÿr   é   é"   r
   r   r   r   Úcos_17rG   ½   sÓ   € õ Ø	�d�2‰hŒh‰˜"Ñ�t A™wœw­$¨rµD¸±H´H©}Ñ*=Ô*=Ý�T�!‰WŒW˜�T "¥t¨B¡x¤x¡-Ñ0Ô0Ñ0°A½¸R¹¼±LÝ
ˆr•D˜‘H”H‰}Ñ
Ô
ñ4ñ ñ Ø!"¥T¨"¡X¤X¡ñ.Ø02ñ3ñ 	4ô 	4ñ+4ñ  5à79ñ :ñ 	:ñ;ô ;ð ;r   c                 óš  — dd„} dd„} | t           j        t          d¦  «        ¦  «        \  }} | |t          d	¦  «        ¦  «        \  }} | |t          d	¦  «        ¦  «        \  }} | |d
d|z   d|z  z   z  ¦  «        \  }}	 | |d
d|z   d|z  z   z  ¦  «        \  }
} | |d
d|z   d|z  z   z  ¦  «        \  }} | |d
d|z   d|z  z   z  ¦  «        \  }} | |d||z   |z   d|
z  z   z  ¦  «        \  }} | |d||z   |z   d|z  z   z  ¦  «        \  }} | |d||z   |	z   d|z  z   z  ¦  «        \  }} | |d||z   |
z   d|z  z   z  ¦  «        \  }} | |	d||	z   |z   d|z  z   z  ¦  «        \  }} | |
d||
z   |z   d|z  z   z  ¦  «        \  }} | |d||z   |z   d|z  z   z  ¦  «        \  }} | |d||z   |z   d|	z  z   z  ¦  «        \  }} ||d||z   |z   |z   z  ¦  «        }  ||d||z   |z   |z   z  ¦  «        }! ||d||z   |z   |z   z  ¦  «        }" ||d||z   |z   |z   z  ¦  «        }# ||d||z   |z   |z   z  ¦  «        }$ ||d||z   |z   |z   z  ¦  «        }% ||  d|!|"z   z  ¦  «         }& ||# d|$|%z   z  ¦  «         }'d ||& d|'z  ¦  «        z  }(t          t          d¦  «        t          |(d
z   ¦  «        z  dz  t           j        z   ¦  «        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+   r   Úbr   útuple[Expr, Expr]c                ón   — | t          | dz  |z   ¦  «        z   dz  | t          | dz  |z   ¦  «        z
  dz  fS ©Nr   r
   ©r+   rI   s     r   Úf1zcos_257.<locals>.f1Ð   s=   € Ø•D˜˜A™ ™‘N”NÑ" aÑ'¨!­d°1°a±4¸!±8©n¬nÑ*<ÀÑ)AÐAÐAr   c                ó8   — | t          | dz  |z   ¦  «        z
  dz  S rL   r
   rM   s     r   Úf2zcos_257.<locals>.f2Ó   s    € Ø•D˜˜A™ ™‘N”NÑ" AÑ%Ð%r   é   é@   r@   r2   r   éüÿÿÿéþÿÿÿé   )r+   r   rI   r   r   rJ   )r+   r   rI   r   r   r   )r   ÚNegativeOner	   r   r=   ))rN   rP   Út1Út2Úz1Úz3Úz2Úz4Úy1Úy5Úy6Úy2Úy3Úy7Úy8Úy4Úx1Úx9Úx2Úx10Úx3Úx11Úx4Úx12Úx5Úx13Úx6Úx14Úx15Úx7Úx8Úx16Úv1Úv2Úv3Úv4Úv5Úv6Úu1Úu2Úw1s)                                            r   Úcos_257r~   Æ   sÊ  € ðBð Bð Bð Bð&ð &ð &ð &ð ˆR•”�w s™|œ|Ñ,Ô,�F€BˆØˆR�•G˜B‘K”KÑ Ô �F€BˆØˆR�•G˜B‘K”KÑ Ô �F€BˆØˆR��A�q˜2‘v  "¡‘}Ñ%Ñ&Ô&�F€BˆØˆR��A�q˜2‘v  "¡‘}Ñ%Ñ&Ô&�F€BˆØˆR��A�q˜2‘v  "¡‘}Ñ%Ñ&Ô&�F€BˆØˆR��A�q˜2‘v  "¡‘}Ñ%Ñ&Ô&�F€BˆØˆR��B˜˜R™ "™ q¨¡tÑ+Ñ,Ñ-Ô-�F€BˆØˆb��R˜˜b™ 2™¨¨"©Ñ,Ñ-Ñ.Ô.�G€BˆØˆb��R˜˜b™ 2™¨¨"©Ñ,Ñ-Ñ.Ô.�G€BˆØˆb��R˜˜b™ 2™¨¨"©Ñ,Ñ-Ñ.Ô.�G€BˆØˆb��R˜˜b™ 2™¨¨"©Ñ,Ñ-Ñ.Ô.�G€BˆØˆb��R˜˜b™ 2™¨¨"©Ñ,Ñ-Ñ.Ô.�G€BˆØˆb��R˜˜b™ 2™¨¨"©Ñ,Ñ-Ñ.Ô.�G€CˆØˆb��R˜˜b™ 2™¨¨"©Ñ,Ñ-Ñ.Ô.�G€BˆØ	ˆˆB��B˜‘G˜b‘L 2Ñ%Ñ&Ñ	'Ô	'€BØ	ˆˆB��B˜‘G˜b‘L 2Ñ%Ñ&Ñ	'Ô	'€BØ	ˆˆB��B˜‘G˜c‘M CÑ'Ñ(Ñ	)Ô	)€BØ	ˆˆB��B˜‘H˜s‘N SÑ(Ñ)Ñ	*Ô	*€BØ	ˆˆC��S˜3‘Y ‘_ sÑ*Ñ+Ñ	,Ô	,€BØ	ˆˆC��S˜2‘X ‘] RÑ'Ñ(Ñ	)Ô	)€BØ
ˆ"ˆbˆS�"�b˜2‘g‘,Ñ
Ô
Ð	€BØ
ˆ"ˆbˆS�"�b˜2‘g‘,Ñ
Ô
Ð	€BØ	ˆBˆB�ˆs�B�r‘E‰NŒNÑ	€BÝ•�Q‘”�˜R !™V™œÑ$ QÑ&­¬Ñ/Ñ0Ô0Ð0r   údict[int, Callable[[], Expr]]c                 ó8   — 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
    )r1   r2   r3   r4   )r>   rA   rG   r~   r   r   r   Ú	cos_tabler�   ñ   s   € õ  ÝÝÝð	ð ð r   N)r   r   r   r   )r#   r   r   r$   )r.   r   r   r/   )r   r   )r   r   )Ú__doc__Ú
__future__r   Útypingr   Ú	functoolsr   Úsympy.core.exprr   Úsympy.core.singletonr   Úsympy.core.intfuncr   Úsympy.core.numbersr	   Ú(sympy.functions.elementary.miscellaneousr   Úsympy.core.cacher   r   r-   r;   r>   rA   rG   r~   r�   r   r   r   ú<module>rŒ      s•  ðð!ð !ðD #Ð "Ð "Ð "Ð "Ð "Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  Ø "Ð "Ð "Ð "Ð "Ð "Ø %Ð %Ð %Ð %Ð %Ð %Ø &Ð &Ð &Ð &Ð &Ð &Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø $Ð $Ð $Ð $Ð $Ð $ð%'ð %'ð %'ð %'ðPHð Hð Hð HðVð ð ð ð  	ðð ð ñ 	„ðð
 	ðð ð ñ 	„ðð
 	ð;ð ;ð ;ñ 	„ð;ð 	ð'1ð '1ð '1ñ 	„ð'1ðTð ð ð ð ð r   