o
    !ôj¡-  ã                   @   s  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
d„Zd%dd„Zd%dd„Zej e_ d$dd„Zdd„ Zdd„ Zej e_ d$dd„Zdd„ Zdd„ Zej e_ dd„ Zd&d d!„Zd&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                    s   t | ƒstdƒ‚dd„ | D ƒ}tdd„ |D ƒƒrtdƒ‚t|ƒ‰ˆdk r&|S ˆ ¡ d }ˆˆd @ r:|d7 }d| ‰|tjgˆt|ƒ  7 }tdˆƒD ]"}t	t
||d	d
�ddd… dƒ}||k rm|| || ||< ||< qK|rvdt ˆ ndt ˆ ‰ ˆdur‡ˆ  ˆd ¡‰ ‡ fdd„tˆd ƒD ƒ}d}|ˆkrå|d ˆ| }	}
tdˆ|ƒD ]3}t|	ƒD ],}|||  t||| |	  ||
|   ƒ}}|| || ||| < ||| |	 < q¯q©|d9 }|ˆksš|rþˆdurõ‡‡fdd„|D ƒn‡fdd„|D ƒ}|S )z3Utility function for the Discrete Fourier TransformzAExpected a sequence of numeric coefficients for Fourier Transformc                 S   ó   g | ]}t |ƒ‘qS © ©r   ©Ú.0Úargr   r   úl/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/sympy/discrete/transforms.pyÚ
<listcomp>   ó    z&_fourier_transform.<locals>.<listcomp>c                 s   s   � | ]}|  t¡V  qd S ©N)Úhasr   ©r   Úxr   r   r   Ú	<genexpr>   s   € z%_fourier_transform.<locals>.<genexpr>z"Expected non-symbolic coefficientsé   é   T©ÚstrNéÿÿÿÿéþÿÿÿc                    s(   g | ]}t ˆ | ƒttˆ | ƒ  ‘qS r   )r	   r   r   )r   Úi)Úangr   r   r   4   s   ( r   c                    s   g | ]	}|ˆ   ˆ ¡‘qS r   )Úevalfr   )ÚdpsÚnr   r   r   @   s    c                    ó   g | ]}|ˆ  ‘qS r   r   r   ©r(   r   r   r   A   r   )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%   r'   r(   r   Ú_fourier_transform   sJ   €.(þúÿr>   Nc                 C   ó   t | |d�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'   ©r>   ©r3   r'   r   r   r   ÚfftF   s   .rB   c                 C   ó   t | |dd�S )NT)r'   r4   r@   rA   r   r   r   Úifftw   ó   rD   c                    s0  t | ƒstdƒ‚t|ƒ‰ tˆ ƒstdƒ‚‡ fdd„| D ƒ}t|ƒ}|dk r'|S | ¡ d }||d @ r;|d7 }d| }ˆ d | rEtdƒ‚|dg|t|ƒ  7 }td|ƒD ]"}tt	||d	d
�ddd… dƒ}||k rw|| || ||< ||< qUt
ˆ ƒ}t|ˆ d | ˆ ƒ}	|r�t|	ˆ d ˆ ƒ}	dg|d  }
td|d ƒD ]}|
|d  |	 ˆ  |
|< qžd}||k�r|d || }}td||ƒD ]5}t|ƒD ].}|||  ||| |  |
||   }}|| ˆ  || ˆ  ||| < ||| | < qÉqÃ|d9 }||ks´|�rt|ˆ d ˆ ƒ‰‡ ‡fd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                    s   g | ]}t |ƒˆ  ‘qS r   r   r   )Úpr   r   r   �   ó    z/_number_theoretic_transform.<locals>.<listcomp>r   r   z/Expected prime modulus of the form (m*2**k + 1)r   Tr    Nr"   c                    s   g | ]}|ˆ ˆ  ‘qS r   r   r   ©rF   Úrvr   r   r   ¸   rG   )r   r+   r   r
   r-   r.   r/   r1   r2   r   r   Úpow)r3   Úprimer4   r5   r(   r6   r$   r7   ÚprÚrtr8   r9   r:   r;   r<   r=   r   rH   r   Ú_number_theoretic_transformƒ   sT   €
*0þúrN   c                 C   r?   )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

    )rK   ©rN   ©r3   rK   r   r   r   Úntt½   s   (rQ   c                 C   rC   )NT)rK   r4   rO   rP   r   r   r   Úinttè   rE   rR   c           	         s  t | ƒstdƒ‚dd„ | D ƒ}t|ƒ‰ ˆ dk r|S ˆ ˆ d @ r%dˆ  ¡  ‰ |tjgˆ t|ƒ  7 }d}|ˆ kru|d }tdˆ |ƒD ]+}t|ƒD ]$}|||  ||| |  }}|| || ||| < ||| | < qGqA|d9 }|ˆ ks7|r€‡ fdd„|D ƒ}|S )z1Utility function for the Walsh Hadamard Transformz@Expected a sequence of coefficients for Walsh Hadamard Transformc                 S   r   r   r   r   r   r   r   r   û   r   z-_walsh_hadamard_transform.<locals>.<listcomp>r   r   r   c                    r)   r   r   r   r*   r   r   r     r   ©r   r+   r.   r/   r   r0   r1   )	r3   r4   r5   r9   r:   r$   r7   r<   r=   r   r*   r   Ú_walsh_hadamard_transformô   s,   (þúrT   c                 C   s   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

    ©rT   ©r3   r   r   r   Úfwht  s   $rW   c                 C   s   t | dd�S )NT)r4   rU   rV   r   r   r   Úifwht:  s   rX   c                 C   s  t | ƒstdƒ‚dd„ | D ƒ}t|ƒ}|dk r|S ||d @ r%d| ¡  }|tjg|t|ƒ  7 }|r\d}||k rZt|ƒD ]}||@ rQ||  ||||A   7  < q=|d9 }||k s9|S d}||k r„t|ƒD ]}||@ rmqf||  ||||A   7  < qf|d9 }||k sb|S )z\Utility function for performing Mobius Transform using
    Yate's Dynamic Programming methodz#Expected a sequence of coefficientsc                 S   r   r   r   r   r   r   r   r   M  r   z%_mobius_transform.<locals>.<listcomp>r   r   rS   )r3   ÚsgnÚsubsetr5   r(   r$   r7   r   r   r   Ú_mobius_transformF  s8   €üøûr[   Tc                 C   ó   t | d|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   ©rY   rZ   ©r[   ©r3   rZ   r   r   r   Úmobius_transforml  s   8r`   c                 C   r\   )Nr"   r]   r^   r_   r   r   r   Úinverse_mobius_transform¦  rE   ra   )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   r>   rB   rD   rN   rQ   rR   rT   rW   rX   r[   r`   ra   r   r   r   r   Ú<module>   s0    
	
1
1
	:+
	'	
&
: