
    wg-                     R   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 _         y
)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_intc           	      J   t        |       st        d      | D cg c]  }t        |       }}t        d |D              rt	        d      t        |      }|dk  r|S |j                         dz
  }||dz
  z  r
|dz  }d|z  }|t        j                  g|t        |      z
  z  z  }t        d|      D ]7  }t        t        ||d      ddd	   d      }||k  s'||   ||   c||<   ||<   9 |rd
t        z  |z  ndt        z  |z  }	||	j                  |dz         }	t        |dz        D cg c]&  }t        |	|z        t        t!        |	|z        z  z   ( }
}d}||k  rv|dz  ||z  }}t        d||      D ]Q  }t        |      D ]A  }|||z      t#        |||z   |z      |
||z     z        }}||z   ||z
  c|||z   <   |||z   |z   <   C S |dz  }||k  rv|r7|"|D cg c]  }||z  j                  |       c}n|D cg c]  }||z  	 c}}|S c c}w c c}w c c}w c c}w )z3Utility function for the Discrete Fourier TransformzAExpected a sequence of numeric coefficients for Fourier Transformc              3   F   K   | ]  }|j                  t                y wN)hasr   ).0xs     ^/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/discrete/transforms.py	<genexpr>z%_fourier_transform.<locals>.<genexpr>   s     
$Q155=
$s   !z"Expected non-symbolic coefficients      TstrNr   )r   	TypeErrorr   any
ValueErrorlen
bit_lengthr   Zerorangeintr   r   evalfr
   r   r	   r   )seqdpsinversearganbijangwhhfutuvr   s                    r   _fourier_transformr7      sq    C= 0 1 	1 "%%#%A%

$!
$$=>>AA1u	A!a%y	QqD!&&1s1v:	A1a[ $Qt$TrT*A.q51qtJAaD!A$$
 "R%'!B$q&C
iia ,1!q&M:qSUaCE
l	":A:	A
q&aaBq!Q 	7A2Y 7QxAa!ebjM!BF),C!D1*+a%Q'!a%!AEBJ-7	7 	
Q q& -0_q)!ac[[)/0!1!!A#!1 	
 HO 	&0 	; *!1s   H,+HH H 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(   r7   r'   r(   s     r   fftr;   F   s    \ cs++    c                     t        | |d      S )NT)r(   r)   r9   r:   s     r   ifftr>   w   s    csD99r<   c                    t        |       st        d      t        |      }t        |      st	        d      | D cg c]  }t        |      |z   }}t        |      }|dk  r|S |j                         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 ]7  }t        t        ||d      d	d	d
   d      }	||	k  s'||	   ||   c||<   ||	<   9 t        |      }
t        |
|dz
  |z  |      }|rt        ||dz
  |      }dg|dz  z  }t        d|dz        D ]  }||dz
     |z  |z  ||<    d}||k  rs|dz  ||z  }}t        d||      D ]N  }t        |      D ]>  }	|||	z      |||	z   |z      |||	z     z  }}||z   |z  ||z
  |z  c|||	z   <   |||	z   |z   <   @ P |dz  }||k  rs|r%t        ||dz
  |      }|D cg c]
  }||z  |z   }}|S c c}w c c}w )z3Utility function for the Number Theoretic TransformzJExpected a sequence of integer coefficients for Number Theoretic Transformz5Expected prime modulus for Number Theoretic Transformr   r   z/Expected prime modulus of the form (m*2**k + 1)r   Tr   Nr   )r   r   r   r   r    r!   r"   r$   r%   r   r   pow)r'   primer)   pr   r+   r,   r-   r.   r/   prrtr1   r2   r3   r4   r5   r6   rvs                      r   _number_theoretic_transformrF      s    C= 9 : 	: 	uA1: 5 6 	6 !$$1Q$A$AA1u	A!a%y	QqD	A{JKK!a#a&j	A1a[ $Qt$TrT*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A2Y CQx1q52:qay!81+,q5A+A{'!a%!AEBJ-C	C 	
Q q& AE1 !!QrTAX!!HW 	%R "s   G9&G>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

    )rA   rF   r'   rA   s     r   nttrJ      s    P 's%88r<   c                     t        | |d      S )NT)rA   r)   rH   rI   s     r   inttrL      s    &s%FFr<   c                 
   t        |       st        d      | D cg c]  }t        |       }}t        |      }|dk  r|S ||dz
  z  rd|j	                         z  }|t
        j                  g|t        |      z
  z  z  }d}||k  r_|dz  }t        d||      D ]?  }t        |      D ]/  }|||z      |||z   |z      }
}	|	|
z   |	|
z
  c|||z   <   |||z   |z   <   1 A |dz  }||k  r_|r|D cg c]  }||z  	 }}|S c c}w c c}w )z1Utility function for the Walsh Hadamard Transformz@Expected a sequence of coefficients for Walsh Hadamard Transformr   r   r   r   r   r   r!   r"   r   r#   r$   )r'   r)   r*   r+   r,   r2   r3   r.   r/   r5   r6   r   s               r   _walsh_hadamard_transformrO      sF    C= 7 8 	8 "%%#%A%AA1u!a%yq||~!&&1s1v:	A	A
q&!Vq!Q 	7A2Y 7Qx1q52:1*+a%Q'!a%!AEBJ-7	7 	
Q q& QQqSH+ 	&& s   C;+D 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

    rO   r'   s    r   fwhtrS     s    H %S))r<   c                     t        | d      S )NT)r)   rQ   rR   s    r   ifwhtrU   :  s    $S$77r<   c                    t        |       st        d      | D cg c]  }t        |       }}t        |      }|dk  r|S ||dz
  z  rd|j	                         z  }|t
        j                  g|t        |      z
  z  z  }|r@d}||k  r7t        |      D ]  }||z  s	||xx   ||||z     z  z  cc<     |dz  }||k  r7|S d}||k  r7t        |      D ]  }||z  r	||xx   ||||z     z  z  cc<     |dz  }||k  r7|S c c}w )z\Utility function for performing Mobius Transform using
    Yate's Dynamic Programming methodz#Expected a sequence of coefficientsr   r   rN   )r'   sgnsubsetr*   r+   r,   r.   r/   s           r   _mobius_transformrY   F  s>    C==>>!$%#%A%AA1u!a%yq||~!&&1s1v:	A!e1X )q5aDC!a%L(D) FA	 !e H !e1X %q5!Aa!eH$% FA !e H9 	&s   Dc                     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   rW   rX   rY   r'   rX   s     r   mobius_transformr^   l  s    p Sb88r<   c                     t        | d|      S )Nr   r[   r\   r]   s     r   inverse_mobius_transformr`     s    Sb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   r7   r;   r>   rF   rJ   rL   rO   rS   rU   rY   r^   r`    r<   r   <module>rj      s   
 * ) * $ = 1 4 '.b.,b: {{7t(9VG {{>$*N8 #L89t9 $4#;#;   r<   