
    \h-                     `   S r SSKJrJrJr  SSKJr  SSKJrJ	r	  SSK
JrJr  SSKJrJr  SSKJrJr  SSKJr  SS	 jrSS jrSS jr\R                   \l         SS jrS rS r\R                   \l         SS jrS rS r\R                   \l         S rSS jrSS jr \R                   \ l         g
)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           	      ~   [        U 5      (       d  [        S5      eU  Vs/ s H  n[        U5      PM     nn[        S U 5       5      (       a  [	        S5      e[        U5      nUS:  a  U$ UR                  5       S-
  nXUS-
  -  (       a
  US-  nSU-  nU[        R                  /U[        U5      -
  -  -  n[        SU5       H1  n[        [        XvSS9SSS	2   S5      nXx:  d  M$  XH   XG   sXG'   XH'   M3     U(       a  S
[        -  U-  OS[        -  U-  n	Ub  U	R                  US-   5      n	[        US-  5       Vs/ s H%  n[        X-  5      [        [!        X-  5      -  -   PM'     n
nSnX::  ap  US-  X[-  p[        SX[5       HL  n[        U5       H:  nXGU-      [#        XGU-   U-      XU-     -  5      pX-   X-
  sXGU-   '   XGU-   U-   '   M<     MN     US-  nX::  a  Mp  U(       a>  Ub%  U Vs/ s H  nUU-  R                  U5      PM     snOU Vs/ s H  nUU-  PM
     snnU$ s  snf s  snf s  snf s  snf )z3Utility function for the Discrete Fourier TransformzAExpected a sequence of numeric coefficients for Fourier Transformc              3   J   #    U  H  oR                  [        5      v   M     g 7fN)hasr   ).0xs     Q/var/www/auris/envauris/lib/python3.13/site-packages/sympy/discrete/transforms.py	<genexpr>%_fourier_transform.<locals>.<genexpr>   s     
$!Q55==!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_transformr8      sE    C== 0 1 	1 "%%#A%

$!
$$$=>>AA1u	Aa%y	QqD!&&1s1v:	A1a[Qt$TrT*A.5qtJAD!$ 
 "R%'!B$q&C
iia ,1!q&M:MqSUaCE
l	"MA:	A
&aBq!A2YQxA!ebjM!F),C!D1*+%'a%!EBJ-    	
Q & -0_q)q!ac[[q)/0!1q!!A#q!1 	
 HO 	&0 	; *!1s   H+?,H02H5H:Nc                     [        XS9$ )a  
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)   r8   r(   r)   s     r   fftr<   F   s    \ c++    c                     [        XSS9$ )NT)r)   r*   r:   r;   s     r   ifftr?   w   s    cD99r=   c           	      6   [        U 5      (       d  [        S5      e[        U5      n[        U5      (       d  [	        S5      eU  Vs/ s H  n[        U5      U-  PM     nn[        U5      nUS:  a  U$ UR                  5       S-
  nXfS-
  -  (       a
  US-  nSU-  nUS-
  U-  (       a  [	        S5      eUS/U[        U5      -
  -  -  n[        SU5       H1  n[        [        XSS9S	S	S
2   S5      n	X:  d  M$  XY   XX   sXX'   XY'   M3     [        U5      n
[        XS-
  U-  U5      nU(       a  [        XS-
  U5      nS/US-  -  n[        SUS-  5       H  nXS-
     U-  U-  X'   M     SnX::  ap  US-  Xm-  p[        SXm5       HL  n[        U5       H:  n	XXU	-      XXU	-   U-      XU	-     -  nnUU-   U-  UU-
  U-  sXXU	-   '   XXU	-   U-   '   M<     MN     US-  nX::  a  Mp  U(       a&  [        XcS-
  U5      nU Vs/ s H
  oDU-  U-  PM     nnU$ s  snf s  snf )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/   r0   prrtr2   r3   r4   r5   r6   r7   rvs                      r   _number_theoretic_transformrG      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[Qt$TrT*A.5qtJAD!$ 
 
	B	Ra%Aq	!BUA	
Q!VA1a1fQx{Q  	
A
&aBq!A2YQxq52:qay!81+,q5A+A{'a%!EBJ-    	
Q & E1 !q!rTAXq!HW 	%R "s   H<Hc                     [        XS9$ )a  
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

)rB   rG   r(   rB   s     r   nttrK      s    P 's88r=   c                     [        XSS9$ )NT)rB   r*   rI   rJ   s     r   inttrM      s    &sFFr=   c                 ,   [        U 5      (       d  [        S5      eU  Vs/ s H  n[        U5      PM     nn[        U5      nUS:  a  U$ XDS-
  -  (       a  SUR	                  5       -  nU[
        R                  /U[        U5      -
  -  -  nSnXT::  a\  US-  n[        SXE5       H;  n[        U5       H)  nX7U-      X7U-   U-      pX-   X-
  sX7U-   '   X7U-   U-   '   M+     M=     US-  nXT::  a  M\  U(       a  U Vs/ s H  oU-  PM	     nnU$ s  snf s  snf )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-   r3   r4   r/   r0   r6   r7   r   s               r   _walsh_hadamard_transformrP      s(    C== 7 8 	8 "%%#A%AA1ua%yq||~!&&1s1v:	A	A
&!Vq!A2YQxq52:1*+%'a%!EBJ-    	
Q & !QqS!H+ 	&& s   D:Dc                     [        U 5      $ )a  
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

rP   r(   s    r   fwhtrT     s    H %S))r=   c                     [        U SS9$ )NT)r*   rR   rS   s    r   ifwhtrV   :  s    $S$77r=   c                 D   [        U 5      (       d  [        S5      eU  Vs/ s H  n[        U5      PM     nn[        U5      nUS:  a  U$ XUS-
  -  (       a  SUR	                  5       -  nU[
        R                  /U[        U5      -
  -  -  nU(       aE  SnXe:  a<  [        U5       H!  nXv-  (       d  M  XG==   XXv-     -  -  ss'   M#     US-  nXe:  a  M<  U$ SnXe:  a<  [        U5       H!  nXv-  (       a  M  XG==   XXv-     -  -  ss'   M#     US-  nXe:  a  M<  U$ s  snf )zXUtility function for performing Mobius Transform using
Yate's Dynamic Programming methodz#Expected a sequence of coefficientsr   r   rO   )r(   sgnsubsetr+   r,   r-   r/   r0   s           r   _mobius_transformrZ   F  s    C===>>!$%#A%AA1ua%yq||~!&&1s1v:	Ae1X55DC!%L(D  FA	 e H e1X5aeH$  FA e H9 	&s   Dc                     [        U SUS9$ )af  
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   rX   rY   rZ   r(   rY   s     r   mobius_transformr_   l  s    p Sb88r=   c                     [        U SUS9$ )Nr   r\   r]   r^   s     r   inverse_mobius_transformra     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   r8   r<   r?   rG   rK   rM   rP   rT   rV   rZ   r_   ra    r=   r   <module>rk      s   
 * ) * $ = 1 4 '.b.,b: {{7t(9VG {{>$*N8 #L89t9 $4#;#;   r=   