o
    jg-                     @   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||| |	  ||
|   }}|| || ||| < ||| |	 < qq|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   Q/var/www/html/zoom/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strNc                    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 |  }	|rt|	 d  }	dg|d  }
td|d D ]}|
|d  |	   |
|< qd}||kr|d || }}td||D ]5}t|D ].}|||  ||| |  |
||   }}||   ||   ||| < ||| | < qq|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 rt|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
	:+
	'	
&
: