K i-RdZddlmZmZmZddlmZddlmZm Z ddl m Z m Z ddl mZmZddlmZmZddlmZdd Zdd Zdd Zeje_dd ZdZdZeje_ddZdZdZeje_dZddZddZ eje _y )zd Discrete Fourier Transform, Number Theoretic Transform, Walsh Hadamard Transform, Mobius Transform )SSymbolsympify) expand_mul)piI)sincos)isprimeprimitive_root)ibiniterable)as_intc Jt|s td|Dcgc] }t|}}td|Dr t dt |}|dkr|S|j dz }||dz zr |dz }d|z}|tjg|t |z zz }td|D]7}tt||dddd d}||ks'||||c||<||<9|r d tz|z n dtz|z } || j|dz} t|dzDcgc]&}t| |ztt!| |zzz(} }d} | |krv| dz|| z} } td || D]Q}t| D]A}|||zt#|||z| z| | |zz}}||z||z c|||z<|||z| z<CS| dz} | |krv|r7|"|Dcgc]}||z j|c}n|Dcgc]}||z  c}}|Scc}wcc}wcc}wcc}w) z3Utility function for the Discrete Fourier TransformzAExpected a sequence of numeric coefficients for Fourier Transformc3FK|]}|jtywN)hasr).0xs _/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/discrete/transforms.py z%_fourier_transform..s $Q155= $s!z"Expected non-symbolic coefficientsTstrNr)r TypeErrorrany ValueErrorlen bit_lengthrZerorangeintr revalfr rr r)seqdpsinversearganbijangwhhfutuvrs r_fourier_transformr7sq C=01 1"%%#%A% $! $$=>> AA1u A!a%y Q qD!&&1s1v: A 1a[$ Qt$TrT*A . q51qtJAaD!A$$ "R%'!B$q&C iia ,1!q&M:qSUaCE l ":A: A q&aaBq!Q 7A2Y 7QxAa!ebjM!BF),C!D1*+a%Q'!a%!AEBJ- 7 7 Q q&-0_q )!ac[[  )/0!1!!A#!1  HO &0 ; *!1sH,+HH H Nct||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(r7r'r(s rfftr;Fs\ cs ++ct||dS)NT)r(r)r9r:s rifftr>ws csD 99r<ct|s tdt|}t|s t d|Dcgc]}t||z}}t |}|dkr|S|j dz }||dz zr |dz }d|z}|dz |zr t d|dg|t |z zz }td|D]7}tt||dd d d d} || ks'|| ||c||<|| <9t|} t| |dz |z|} |rt| |dz |} dg|dzz} td|dzD]}| |dz | z|z| |<d} | |krs| dz|| z}}td|| D]N}t|D]>} ||| z||| z|z| || zz}}||z|z||z |zc||| z<||| z|z<@P| dz} | |krs|r%t||dz |}|Dcgc] }||z|z }}|Scc}wcc}w) z3Utility function for the Number Theoretic TransformzJExpected a sequence of integer coefficients for Number Theoretic Transformz5Expected prime modulus for Number Theoretic Transformrrz/Expected prime modulus of the form (m*2**k + 1)rTrNr) rrrr r r!r"r$r%r r pow)r'primer)prr+r,r-r.r/prrtr1r2r3r4r5r6rvs r_number_theoretic_transformrFs C=9: : u A 1:56 6!$$1Q$A$ AA1u A!a%y Q qD A{JKK!a#a&j A 1a[$ Qt$TrT*A . q51qtJAaD!A$$  B R!a%Aq !B QUA  Q!V A 1a1f Qx{Q! A q&aaBq!Q CA2Y CQx1q52:qay!81+,q5A+A{'!a%!AEBJ- C C Q q& AE1  !!QrTAX ! ! HW %R "s G9&G>ct||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 )rArFr'rAs rnttrJsP 's% 88r<ct||dS)NT)rAr)rHrIs rinttrLs &s% FFr<c t|s td|Dcgc] }t|}}t|}|dkr|S||dz zrd|j z}|t j g|t|z zz }d}||kr_|dz}td||D]?}t|D]/}|||z|||z|z} } | | z| | z c|||z<|||z|z<1A|dz}||kr_|r|D cgc]} | |z  }} |Scc}wcc} w)z1Utility function for the Walsh Hadamard Transformz@Expected a sequence of coefficients for Walsh Hadamard Transformrrrrrrr!r"rr#r$) r'r)r*r+r,r2r3r.r/r5r6rs r_walsh_hadamard_transformrOsF C=78 8"%%#%A% AA1u!a%y q||~ !&&1s1v: A A q& !Vq!Q 7A2Y 7Qx1q52:1*+a%Q'!a%!AEBJ- 7 7 Q q& QQqS   H+ && s C;+ Dct|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 rOr's rfwhtrSsH %S ))r<ct|dS)NT)r)rQrRs rifwhtrU:s $S$ 77r<c t|s td|Dcgc] }t|}}t|}|dkr|S||dz zrd|j z}|t j g|t|z zz }|r@d}||kr7t|D]}||zs ||xx||||z zz cc< |dz}||kr7|Sd}||kr7t|D]}||zr ||xx||||z zz cc< |dz}||kr7|Scc}w)z\Utility function for performing Mobius Transform using Yate's Dynamic Programming methodz#Expected a sequence of coefficientsrrrN)r'sgnsubsetr*r+r,r.r/s r_mobius_transformrYFs> C==>>!$%#%A% AA1u!a%y q||~ !&&1s1v: A !e1X )q5aDC!a%L(D ) FA !e H !e1X %q5!Aa!eH $ % FA !e H9 &sDct|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 rrWrXrYr'rXs rmobius_transformr^lsp Sb 88r<ct|d|S)Nrr[r\r]s rinverse_mobius_transformr`s Sb 88r<)Fr)T)!__doc__ sympy.corerrrsympy.core.functionrsympy.core.numbersrr(sympy.functions.elementary.trigonometricr r sympy.ntheoryr r sympy.utilities.iterablesr rsympy.utilities.miscrr7r;r>rFrJrLrOrSrUrYr^r`r<rrjs *)*$=14'. b.,b:{{ 7 t(9VG{{  >$*N8  # L89t9$4#;#; r<