
    Ed-                     ,   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   <   Oa|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     9lib/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,      
$
$Q155==
$
$
$
$
$
$r   z"Expected non-symbolic coefficients      TstrNc                 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1u 	A!a%y 	QqD!&1s1vv:	A1a[[ $ $Qt$$$TTrT*A..q5 	$1qtJAaD!A$
("R%''!B$q&C
 !iia  ::::E!q&MM:::A	A
q& aaBq!Q 	7 	7A2YY 7 7QxAa!ebjM!BF),C!D!D1*+a%Q'!a%!AEBJ--7 	
Q q&   2-0 2)))))q))))!1!1!1!1q!1!1!1 	
 Hr   Nc                 $    t          | |          S )al  
    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] http://mathworld.wolfram.com/FastFourierTransform.html

    )r,   rC   r8   r,   s     r   fftrG   F   s    \ cs++++r   c                 &    t          | |d          S )NT)r,   r9   rE   rF   s     r   ifftrI   w   s    csD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   <   HZ|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!    !!!!QrTAX!!!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1u 	A!a%y 	QqD	A{ LJKKK!a#a&&j	A1a[[ $ $Qt$$$TTrT*A..q5 	$1qtJAaD!A$			B	R!a%Aq	!	!B QUA	
Q!VA1a1f  Qx{Q!	A
q& aaBq!Q 	C 	CA2YY C CQx1q52:qay!81+,q5A+A{'!a%!AEBJ--C 	
Q q&   "AE1!!!!!q!!!Hr   c                 $    t          | |          S )aQ  
    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] http://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   <   6H|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    QQqSr   r   r0   r3   r4   r   r5   r6   )
r8   r9   r:   r>   r?   r(   r<   rA   rB   r-   s
            @r   _walsh_hadamard_transformr^      so    C== 8 7 8 8 	8 	&%%%%AAA1u !a%y q||~~!&1s1vv:	A	A
q& !Vq!Q 	7 	7A2YY 7 7Qx1q52:1*+a%Q'!a%!AEBJ--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  s}    C== ?=>>>%%%%%AAA1u !a%y q||~~!&1s1vv:	A !e 	1XX ) )q5 )aDDDC!a%L(DDDFA	 !e 	 !e 	1XX % %q5 !Aa!eH$FA !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 Sb8888r   c                 &    t          | d|          S )Nr%   rk   rl   rm   s     r   inverse_mobius_transformrp     s    Sb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 (9VG G G {   >$* $* $*N8 8 8 # # #L89 89 89 89t9 9 9 9 $4#;       r   