§
    OŠtj¡-  ã                   ó,  — d Z ddlmZmZmZ ddlmZ ddlmZm	Z	 ddl
mZmZ ddlmZmZ ddlmZmZ ddlmZ dd
„Zdd„Zdd„Zej         e_         dd„Zd„ Zd„ Zej         e_         dd„Zd„ Zd„ Zej         e_         d„ Zdd„Zdd„Z ej         e _         dS )zd
Discrete Fourier Transform, Number Theoretic Transform,
Walsh Hadamard Transform, Mobius Transform
é    )ÚSÚSymbolÚsympify)Ú
expand_mul)ÚpiÚI)ÚsinÚcos)ÚisprimeÚprimitive_root)ÚibinÚiterable©Úas_intFc           	      ó0  ‡‡‡— t          | ¦  «        st          d¦  «        ‚d„ | D ¦   «         }t          d„ |D ¦   «         ¦  «        rt          d¦  «        ‚t	          |¦  «        Š‰dk     r|S ‰                     ¦   «         dz
  }‰‰dz
  z  r
|dz  }d|z  Š|t          j        g‰t	          |¦  «        z
  z  z  }t          d‰¦  «        D ]H}t          t          ||d¬¦  «        d	d	d
…         d¦  «        }||k     r||         ||         c||<   ||<   ŒI|rdt          z  ‰z  ndt          z  ‰z  Š‰�‰                     ‰dz   ¦  «        Šˆfd„t          ‰dz  ¦  «        D ¦   «         }d}|‰k    r‡|dz  ‰|z  }
}	t          d‰|¦  «        D ]`}t          |	¦  «        D ]N}|||z            t          |||z   |	z            ||
|z           z  ¦  «        }}||z   ||z
  c|||z   <   |||z   |	z   <   ŒOŒa|dz  }|‰k    °‡|r‰�ˆˆfd„|D ¦   «         nˆfd„|D ¦   «         }|S )z3Utility function for the Discrete Fourier TransformzAExpected a sequence of numeric coefficients for Fourier Transformc                 ó,   — g | ]}t          |¦  «        ‘ŒS © ©r   ©Ú.0Úargs     úW/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sympy/discrete/transforms.pyú
<listcomp>z&_fourier_transform.<locals>.<listcomp>   ó   € Ð%Ð%Ð%˜#��‰ŒÐ%Ð%Ð%ó    c              3   óJ   K  — | ]}|                      t          ¦  «        V — Œd S ©N)Úhasr   )r   Úxs     r   ú	<genexpr>z%_fourier_transform.<locals>.<genexpr>   s,   è è € Ð
$Ð
$˜Qˆ1�5Š5•‰=Œ=Ð
$Ð
$Ð
$Ð
$Ð
$Ð
$r   z"Expected non-symbolic coefficientsé   é   T©ÚstrNéÿÿÿÿéþÿÿÿc                 ój   •— g | ]/}t          ‰|z  ¦  «        t          t          ‰|z  ¦  «        z  z   ‘Œ0S r   )r
   r   r	   )r   ÚiÚangs     €r   r   z&_fourier_transform.<locals>.<listcomp>4   s6   ø€ Ð:Ð:Ð: q�ˆS�‰U‰Œ•a�˜C ™E™
œ
‘lÑ	"Ð:Ð:Ð:r   r   c                 ó@   •— g | ]}|‰z                        ‰¦  «        ‘ŒS r   )Úevalf)r   r   ÚdpsÚns     €€r   r   z&_fourier_transform.<locals>.<listcomp>@   s)   ø€ Ð)Ð)Ð) !ˆa�‰c�[Š[˜ÑÔÐ)Ð)Ð)r   c                 ó   •— g | ]}|‰z  ‘ŒS r   r   ©r   r   r-   s     €r   r   z&_fourier_transform.<locals>.<listcomp>A   s   ø€ Ð!1Ð!1Ð!1¨! ! A¡#Ð!1Ð!1Ð!1r   )r   Ú	TypeErrorÚanyÚ
ValueErrorÚlenÚ
bit_lengthr   ÚZeroÚrangeÚintr   r   r+   r   )Úseqr,   ÚinverseÚaÚbr(   ÚjÚwÚhÚhfÚutÚuÚvr)   r-   s    `           @@r   Ú_fourier_transformrC      s¬  øøø€ õ �C‰=Œ=ð 1Ýð 0ñ 1ô 1ð 	1ð 	&Ð% Ð%Ñ%Ô%€AÝ
Ð
$Ð
$ !Ð
$Ñ
$Ô
$Ñ$Ô$ð ?ÝÐ=Ñ>Ô>Ð>åˆA‰Œ€AØˆ1‚u€uØˆà	�Š‰Œ˜Ñ€AØˆ!ˆa‰%�yð Ø	ˆQ‰ˆØˆq‰Dˆà�!Œ&ˆ�1•s˜1‘v”v‘:Ñ	Ñ€AÝ�1�a‰[Œ[ð $ð $ˆÝ•�Q˜˜tÐ$Ñ$Ô$ T T r TÔ*¨AÑ.Ô.ˆØˆqŠ5ˆ5Ø˜1œ˜q œtˆJˆAˆa‰D�!�A‘$øàÐ
(ˆ"�R‰%�‰'ˆ' !¥B¡$ q¡&€Cà
€Ø�iŠi˜˜a™Ñ Ô ˆà:Ð:Ð:Ð:­E°!°q±&©M¬MÐ:Ñ:Ô:€Aà	€AØ
ˆqŠ&ˆ&Ø�a‘˜˜a™ˆBˆÝ�q˜!˜Q‘”ð 	7ð 	7ˆAÝ˜2‘Y”Yð 7ð 7�Ø˜˜Q™”x¥¨A¨a°!©e°b©j¬M¸!¸BÀ¹F¼)Ñ,CÑ!DÔ!D�1�Ø*+¨a©%°°Q±Ð'��!�a‘%‘˜!˜A ™E B™J™-˜-ð7ð 	
ˆQ‰ˆð ˆqŠ&ˆ&ð ð 2Ø-0¨_Ð)Ð)Ð)Ð)Ð) qÐ)Ñ)Ô)Ð)Ø!1Ð!1Ð!1Ð!1¨qÐ!1Ñ!1Ô!1ð 	
ð €Hr   Nc                 ó$   — t          | |¬¦  «        S )am  
    Performs the Discrete Fourier Transform (**DFT**) in the complex domain.

    The sequence is automatically padded to the right with zeros, as the
    *radix-2 FFT* requires the number of sample points to be a power of 2.

    This method should be used with default arguments only for short sequences
    as the complexity of expressions increases with the size of the sequence.

    Parameters
    ==========

    seq : iterable
        The sequence on which **DFT** is to be applied.
    dps : Integer
        Specifies the number of decimal digits for precision.

    Examples
    ========

    >>> from sympy import fft, ifft

    >>> fft([1, 2, 3, 4])
    [10, -2 - 2*I, -2, -2 + 2*I]
    >>> ifft(_)
    [1, 2, 3, 4]

    >>> ifft([1, 2, 3, 4])
    [5/2, -1/2 + I/2, -1/2, -1/2 - I/2]
    >>> fft(_)
    [1, 2, 3, 4]

    >>> ifft([1, 7, 3, 4], dps=15)
    [3.75, -0.5 - 0.75*I, -1.75, -0.5 + 0.75*I]
    >>> fft(_)
    [1.0, 7.0, 3.0, 4.0]

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm
    .. [2] https://mathworld.wolfram.com/FastFourierTransform.html

    )r,   ©rC   ©r8   r,   s     r   ÚfftrG   F   s   € õ\ ˜c sÐ+Ñ+Ô+Ð+r   c                 ó&   — t          | |d¬¦  «        S )NT)r,   r9   rE   rF   s     r   ÚifftrI   w   s   € Ý˜c s°DÐ9Ñ9Ô9Ð9r   c                 ó†  ‡‡— t          | ¦  «        st          d¦  «        ‚t          |¦  «        Št          ‰¦  «        st	          d¦  «        ‚ˆfd„| D ¦   «         }t          |¦  «        }|dk     r|S |                     ¦   «         dz
  }||dz
  z  r
|dz  }d|z  }‰dz
  |z  rt	          d¦  «        ‚|dg|t          |¦  «        z
  z  z  }t          d|¦  «        D ]H}t          t          ||d¬	¦  «        d
d
d…         d¦  «        }||k     r||         ||         c||<   ||<   ŒIt          ‰¦  «        }t          |‰dz
  |z  ‰¦  «        }	|rt          |	‰dz
  ‰¦  «        }	dg|dz  z  }
t          d|dz  ¦  «        D ]}|
|dz
           |	z  ‰z  |
|<   Œd}||k    r€|dz  ||z  }}t          d||¦  «        D ]Y}t          |¦  «        D ]G}|||z            |||z   |z            |
||z           z  }}||z   ‰z  ||z
  ‰z  c|||z   <   |||z   |z   <   ŒHŒZ|dz  }||k    °€|r#t          |‰dz
  ‰¦  «        Šˆˆfd„|D ¦   «         }|S )z3Utility function for the Number Theoretic TransformzJExpected a sequence of integer coefficients for Number Theoretic Transformz5Expected prime modulus for Number Theoretic Transformc                 ó4   •— g | ]}t          |¦  «        ‰z  ‘ŒS r   r   )r   r   Úps     €r   r   z/_number_theoretic_transform.<locals>.<listcomp>�   s#   ø€ Ð$Ð$Ð$˜1��‰Œ�Q‰Ð$Ð$Ð$r   r"   r!   z/Expected prime modulus of the form (m*2**k + 1)r   Tr#   Nr%   c                 ó    •— g | ]
}|‰z  ‰z  ‘ŒS r   r   )r   r   rL   Úrvs     €€r   r   z/_number_theoretic_transform.<locals>.<listcomp>¸   s!   ø€ Ð!Ð!Ð!˜!ˆQˆr‰T�A‰XÐ!Ð!Ð!r   )r   r0   r   r   r2   r3   r4   r6   r7   r   r   Úpow)r8   Úprimer9   r:   r-   r;   r(   r<   ÚprÚrtr=   r>   r?   r@   rA   rB   rL   rN   s                   @@r   Ú_number_theoretic_transformrS   ƒ   sø  øø€ õ �C‰=Œ=ð :Ýð 9ñ :ô :ð 	:õ 	ˆu‰Œ€AÝ�1‰:Œ:ð 6Ýð 5ñ 6ô 6ð 	6ð 	%Ð$Ð$Ð$ Ð$Ñ$Ô$€AåˆA‰Œ€AØˆ1‚u€uØˆà	�Š‰Œ˜Ñ€AØˆ!ˆa‰%�yð Ø	ˆQ‰ˆØˆq‰Dˆà	ˆA‰��{ð LÝÐJÑKÔKÐKàˆ!ˆˆa•#�a‘&”&‰jÑ	Ñ€AÝ�1�a‰[Œ[ð $ð $ˆÝ•�Q˜˜tÐ$Ñ$Ô$ T T r TÔ*¨AÑ.Ô.ˆØˆqŠ5ˆ5Ø˜1œ˜q œtˆJˆAˆa‰D�!�A‘$øå	˜Ñ	Ô	€Bå	ˆR�!�a‘%˜A‘˜qÑ	!Ô	!€BØð Ý��Q˜‘U˜AÑÔˆà	
ˆˆQ�!‰V‰€AÝ�1�a˜1‘fÑÔð ð ˆØ��Q‘Œx˜‰{˜Q‰ˆˆ!‰ˆà	€AØ
ˆqŠ&ˆ&Ø�a‘˜˜a™ˆBˆÝ�q˜!˜Q‘”ð 	Cð 	CˆAÝ˜2‘Y”Yð Cð C�Ø˜˜Q™”x  1 q¡5¨2¡:¤¨q°°a±¬yÑ!8�1�Ø+,¨q©5°A©+¸¸A¹À±{Ð'��!�a‘%‘˜!˜A ™E B™J™-˜-ðCð 	
ˆQ‰ˆð ˆqŠ&ˆ&ð ð "Ý��A˜‘E˜1ÑÔˆØ!Ð!Ð!Ð!Ð!˜qÐ!Ñ!Ô!ˆà€Hr   c                 ó$   — t          | |¬¦  «        S )aR  
    Performs the Number Theoretic Transform (**NTT**), which specializes the
    Discrete Fourier Transform (**DFT**) over quotient ring `Z/pZ` for prime
    `p` instead of complex numbers `C`.

    The sequence is automatically padded to the right with zeros, as the
    *radix-2 NTT* requires the number of sample points to be a power of 2.

    Parameters
    ==========

    seq : iterable
        The sequence on which **DFT** is to be applied.
    prime : Integer
        Prime modulus of the form `(m 2^k + 1)` to be used for performing
        **NTT** on the sequence.

    Examples
    ========

    >>> from sympy import ntt, intt
    >>> ntt([1, 2, 3, 4], prime=3*2**8 + 1)
    [10, 643, 767, 122]
    >>> intt(_, 3*2**8 + 1)
    [1, 2, 3, 4]
    >>> intt([1, 2, 3, 4], prime=3*2**8 + 1)
    [387, 415, 384, 353]
    >>> ntt(_, prime=3*2**8 + 1)
    [1, 2, 3, 4]

    References
    ==========

    .. [1] http://www.apfloat.org/ntt.html
    .. [2] https://mathworld.wolfram.com/NumberTheoreticTransform.html
    .. [3] https://en.wikipedia.org/wiki/Discrete_Fourier_transform_(general%29

    )rP   ©rS   ©r8   rP   s     r   ÚnttrW   ½   s   € õP ' s°%Ð8Ñ8Ô8Ð8r   c                 ó&   — t          | |d¬¦  «        S )NT)rP   r9   rU   rV   s     r   ÚinttrY   è   s   € Ý& s°%ÀÐFÑFÔFÐFr   c                 ó  ‡	— t          | ¦  «        st          d¦  «        ‚d„ | D ¦   «         }t          |¦  «        Š	‰	dk     r|S ‰	‰	dz
  z  rd‰	                     ¦   «         z  Š	|t          j        g‰	t          |¦  «        z
  z  z  }d}|‰	k    ri|dz  }t          d‰	|¦  «        D ]G}t          |¦  «        D ]5}|||z            |||z   |z            }}||z   ||z
  c|||z   <   |||z   |z   <   Œ6ŒH|dz  }|‰	k    °i|rˆ	fd„|D ¦   «         }|S )z1Utility function for the Walsh Hadamard Transformz@Expected a sequence of coefficients for Walsh Hadamard Transformc                 ó,   — g | ]}t          |¦  «        ‘ŒS r   r   r   s     r   r   z-_walsh_hadamard_transform.<locals>.<listcomp>û   r   r   r!   r"   r   c                 ó   •— g | ]}|‰z  ‘ŒS r   r   r/   s     €r   r   z-_walsh_hadamard_transform.<locals>.<listcomp>  s   ø€ ÐÐÐ�QˆQˆq‰SÐÐÐr   ©r   r0   r3   r4   r   r5   r6   )
r8   r9   r:   r>   r?   r(   r<   rA   rB   r-   s
            @r   Ú_walsh_hadamard_transformr^   ô   sf  ø€ õ �C‰=Œ=ð 8Ýð 7ñ 8ô 8ð 	8ð 	&Ð% Ð%Ñ%Ô%€AÝˆA‰Œ€AØˆ1‚u€uØˆàˆ!ˆa‰%�yð Øˆq�|Š|‰~Œ~Ñˆà�!Œ&ˆ�1•s˜1‘v”v‘:Ñ	Ñ€AØ	€AØ
ˆqŠ&ˆ&Ø�!‰VˆÝ�q˜!˜Q‘”ð 	7ð 	7ˆAÝ˜2‘Y”Yð 7ð 7�Ø˜˜Q™”x  1 q¡5¨2¡:¤�1�Ø*+¨a©%°°Q±Ð'��!�a‘%‘˜!˜A ™E B™J™-˜-ð7ð 	
ˆQ‰ˆð ˆqŠ&ˆ&ð ð ØÐÐÐ˜!ÐÑÔˆà€Hr   c                 ó    — t          | ¦  «        S )aN  
    Performs the Walsh Hadamard Transform (**WHT**), and uses Hadamard
    ordering for the sequence.

    The sequence is automatically padded to the right with zeros, as the
    *radix-2 FWHT* requires the number of sample points to be a power of 2.

    Parameters
    ==========

    seq : iterable
        The sequence on which WHT is to be applied.

    Examples
    ========

    >>> from sympy import fwht, ifwht
    >>> fwht([4, 2, 2, 0, 0, 2, -2, 0])
    [8, 0, 8, 0, 8, 8, 0, 0]
    >>> ifwht(_)
    [4, 2, 2, 0, 0, 2, -2, 0]

    >>> ifwht([19, -1, 11, -9, -7, 13, -15, 5])
    [2, 0, 4, 0, 3, 10, 0, 0]
    >>> fwht(_)
    [19, -1, 11, -9, -7, 13, -15, 5]

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Hadamard_transform
    .. [2] https://en.wikipedia.org/wiki/Fast_Walsh%E2%80%93Hadamard_transform

    ©r^   ©r8   s    r   Úfwhtrb     s   € õH % SÑ)Ô)Ð)r   c                 ó$   — t          | d¬¦  «        S )NT)r9   r`   ra   s    r   Úifwhtrd   :  s   € Ý$ S°$Ð7Ñ7Ô7Ð7r   c                 ó,  — t          | ¦  «        st          d¦  «        ‚d„ | D ¦   «         }t          |¦  «        }|dk     r|S ||dz
  z  rd|                     ¦   «         z  }|t          j        g|t          |¦  «        z
  z  z  }|rGd}||k     r>t          |¦  «        D ]#}||z  r||xx         ||||z           z  z  cc<   Œ$|dz  }||k     °>nGd}||k     r?t          |¦  «        D ]$}||z  rŒ||xx         ||||z           z  z  cc<   Œ%|dz  }||k     °?|S )z\Utility function for performing Mobius Transform using
    Yate's Dynamic Programming methodz#Expected a sequence of coefficientsc                 ó,   — g | ]}t          |¦  «        ‘ŒS r   r   r   s     r   r   z%_mobius_transform.<locals>.<listcomp>M  r   r   r!   r"   r]   )r8   ÚsgnÚsubsetr:   r-   r(   r<   s          r   Ú_mobius_transformri   F  sn  € õ �C‰=Œ=ð ?ÝÐ=Ñ>Ô>Ð>à%Ð% Ð%Ñ%Ô%€AåˆA‰Œ€AØˆ1‚u€uØˆàˆ!ˆa‰%�yð Øˆq�|Š|‰~Œ~Ñˆà�!Œ&ˆ�1•s˜1‘v”v‘:Ñ	Ñ€Aàð ØˆØ�!ŠeˆeÝ˜1‘X”Xð )ð )�Ø�q‘5ð )Ø�a�D�D”D˜C  ! a¡%¤™LÑ(�D�D‘DøØ�‰FˆAð	 �!Šeˆeøð ˆØ�!ŠeˆeÝ˜1‘X”Xð %ð %�Ø�q‘5ð ØØ�!��”˜˜A˜a !™eœH™Ñ$��‘�Ø�‰FˆAð �!Šeˆeð €Hr   Tc                 ó&   — t          | d|¬¦  «        S )a
  
    Performs the Mobius Transform for subset lattice with indices of
    sequence as bitmasks.

    The indices of each argument, considered as bit strings, correspond
    to subsets of a finite set.

    The sequence is automatically padded to the right with zeros, as the
    definition of subset/superset based on bitmasks (indices) requires
    the size of sequence to be a power of 2.

    Parameters
    ==========

    seq : iterable
        The sequence on which Mobius Transform is to be applied.
    subset : bool
        Specifies if Mobius Transform is applied by enumerating subsets
        or supersets of the given set.

    Examples
    ========

    >>> from sympy import symbols
    >>> from sympy import mobius_transform, inverse_mobius_transform
    >>> x, y, z = symbols('x y z')

    >>> mobius_transform([x, y, z])
    [x, x + y, x + z, x + y + z]
    >>> inverse_mobius_transform(_)
    [x, y, z, 0]

    >>> mobius_transform([x, y, z], subset=False)
    [x + y + z, y, z, 0]
    >>> inverse_mobius_transform(_, subset=False)
    [x, y, z, 0]

    >>> mobius_transform([1, 2, 3, 4])
    [1, 3, 4, 10]
    >>> inverse_mobius_transform(_)
    [1, 2, 3, 4]
    >>> mobius_transform([1, 2, 3, 4], subset=False)
    [10, 6, 7, 4]
    >>> inverse_mobius_transform(_, subset=False)
    [1, 2, 3, 4]

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/M%C3%B6bius_inversion_formula
    .. [2] https://people.csail.mit.edu/rrw/presentations/subset-conv.pdf
    .. [3] https://arxiv.org/pdf/1211.0189.pdf

    r"   ©rg   rh   ©ri   ©r8   rh   s     r   Úmobius_transformrn   l  s   € õp ˜S b°Ð8Ñ8Ô8Ð8r   c                 ó&   — t          | d|¬¦  «        S )Nr%   rk   rl   rm   s     r   Úinverse_mobius_transformrp   ¦  s   € Ý˜S b°Ð8Ñ8Ô8Ð8r   )Fr   )T)!Ú__doc__Ú
sympy.corer   r   r   Úsympy.core.functionr   Úsympy.core.numbersr   r   Ú(sympy.functions.elementary.trigonometricr	   r
   Úsympy.ntheoryr   r   Úsympy.utilities.iterablesr   r   Úsympy.utilities.miscr   rC   rG   rI   rS   rW   rY   r^   rb   rd   ri   rn   rp   r   r   r   ú<module>ry      sÉ  ððð ð
 *Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ø *Ð *Ð *Ð *Ð *Ð *Ø $Ð $Ð $Ð $Ð $Ð $Ð $Ð $Ø =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'ð.ð .ð .ð .ðb.,ð .,ð .,ð .,ðb:ð :ð :ð :ð Œ{€„ð7ð 7ð 7ð 7ðt(9ð (9ð (9ðVGð Gð Gð Œ{€„ðð ð ð ð>$*ð $*ð $*ðN8ð 8ð 8ð ”€„ð#ð #ð #ðL89ð 89ð 89ð 89ðt9ð 9ð 9ð 9ð $4Ô#;Ð Ô  Ð  Ð  r   