
    Vpf;                        d dl mZ d dlmZ d dlmZ d dlZd dlmZ d dl	m
Z d dlmZ d dlmZ d dlmZ d%dZd&dZd'dZ	 	 d(d)dZd*dZ	 	 	 	 d+d,d!Z	 	 d(d)d"Z	 	 	 	 d+d,d#Zd&d$ZdS )-    )annotations)Sequence)partialN)lax)canonicalize_axis)promote_dtypes_complex)ArrayNintkr	   returnc                v    t          | |          \  }}t          j        dt          j        z  |z  |z            S )Ny             )r   jnpexppi)r
   r   N_arrs      R/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/scipy/fft.py_W4r      s5    #Aq))(%	"U*	+	++    xaxisc           	         t          j        | d d d|          }t          j        t          j        | dd d|          |f          }t          j        ||g|          S N      )r   slice_in_dimrevconcatenate)r   r   v0v1s       r   _dct_interleaver!      sV    
4q$//"
ws1dAt44tg>>"	"b4	(	((r   outc                d   t          j        t          j        dd| j                  t          j        | j                 dz
  fd| j                  gd          }t          j        |fdt          | j                  D                       }| t          j        || j                 z            z  S )N)r      r   r   r   c                     g | ]
}|k    |S  r&   .0ar   s     r   
<listcomp>z#_dct_ortho_norm.<locals>.<listcomp>&   s    #L#L#L!!t))A)))r   )	r   r   fulldtypeshapeexpand_dimsrangendimsqrt)r"   r   factors    ` r   _dct_ortho_normr3   $   s    ?CHT1ci88#(CIdOVWDWCY[\^a^g:h:hiklmm&?6#L#L#L#LuSX#L#L#LMM&	sx4011	11r   r   typen
int | Nonenorm
str | Nonec                    |dk    rt          d          ||dvrt          d|d          t           j                  Nt	          j         t          j        d j                   fdt           j                  D                         j
                 }t                     }t          j                            |	          }t	          j        t          j        ||j        j        
          fdt           j                  D                       }|t!          ||          z  }	d|	j        z  }	|dk    rt#          |	          }	|	S )a  Computes the discrete cosine transform of the input

  JAX implementation of :func:`scipy.fft.dct`.

  Args:
    x: array
    type: integer, default = 2. Currently only type 2 is supported.
    n: integer, default = x.shape[axis]. The length of the transform.
      If larger than ``x.shape[axis]``, the input will be zero-padded, if
      smaller, the input will be truncated.
    axis: integer, default=-1. The axis along which the dct will be performed.
    norm: string. The normalization mode: one of ``[None, "backward", "ortho"]``.
      The default is ``None``, which is equivalent to ``"backward"``.

  Returns:
    array containing the discrete cosine transform of x

  See Also:
    - :func:`jax.scipy.fft.dctn`: multidimensional DCT
    - :func:`jax.scipy.fft.idct`: inverse DCT
    - :func:`jax.scipy.fft.idctn`: multidimensional inverse DCT

  Examples:
    >>> x = jax.random.normal(jax.random.key(0), (3, 3))
    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jax.scipy.fft.dct(x))
    [[-0.58 -0.33 -1.08]
     [-0.88 -1.01 -1.79]
     [-1.06 -2.43  1.24]]

    When ``n`` smaller than ``x.shape[axis]``

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jax.scipy.fft.dct(x, n=2))
    [[-0.22 -0.9 ]
     [-0.57 -1.68]
     [-2.52 -0.11]]

    When ``n`` smaller than ``x.shape[axis]`` and ``axis=0``

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jax.scipy.fft.dct(x, n=2, axis=0))
    [[-2.22  1.43 -0.67]
     [ 0.52 -0.26 -0.04]]

    When ``n`` larger than ``x.shape[axis]`` and ``axis=1``

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jax.scipy.fft.dct(x, n=4, axis=1))
    [[-0.58 -0.35 -0.64 -1.11]
     [-0.88 -0.9  -1.46 -1.68]
     [-1.06 -2.25 -1.15  1.93]]
  r   Only DCT type 2 is implemented.Nbackwardorthozjax.scipy.fft.dct: norm= is not implementedr   c                F    g | ]}d |k    rj                  z
  nd d fS r   r-   r(   r)   r   r6   r   s     r   r*   zdct.<locals>.<listcomp>l   H     ) ) ) !t))Q&&A> ) ) )r   r   r,   c                     g | ]
}|k    |S r&   r&   r'   s     r   r*   zdct.<locals>.<listcomp>r   s%    9`9`9`VW[_V_V_!V_V_V_r   r>   )NotImplementedError
ValueErrorr   r0   r   padr   arrayr,   r/   r-   r!   fftr.   arangerealr   r3   )
r   r5   r6   r   r8   r
   vVr   r"   s
   ` ``      r   dctrQ   -   si   n 
QYY
?
@
@@	$&;;;
G4GGG
H
HH	4	(	($]39Q(() ) ) ) ) )--) ) )	* 	*A gdm!a!	gkk!$k!	ocj!&,7779`9`9`9`U16]]9`9`9`aa!	C1II#	CH#	W__
#t
$
$C	*r   axesSequence[int]c           	     d  
 t          t          t          | j                  |          \  
| j        
         | j                 }}t          t          | 
                    }t          j                            ||          }t          j
        t          j        ||j                  
fdt          | j                  D                       }t          j
        t          j        ||j                  fdt          | j                  D                       }t          ||          t          ||          |z  t          ||           t          j        t          j        |          d          z  z   z  }	d	|	j        z  }	|d
k    rt%          t%          |	
                    S |	S )N)num_dims)rR   rF   c                     g | ]
}|k    |S r&   r&   )r(   r)   axis1s     r   r*   z_dct2.<locals>.<listcomp>       ???aAJJJJJr   c                     g | ]
}|k    |S r&   r&   )r(   r)   axis2s     r   r*   z_dct2.<locals>.<listcomp>   rX   r   rE   r   )shiftr   r   r>   )mapr   r   r0   r-   r!   r   rL   fftnr   r.   rM   r,   r/   r   rollfliprN   r3   )r   rR   r8   N1N2rO   rP   k1k2r"   rW   rZ   s             @@r   _dct2rd   z   s~   W.@@@$GG,%75>175>b"oa//77!	gll14l  !
sz"AG444????5==???A A"
sz"AG444????5==???A A"Bs2r{{QR"!RWAXAXAX`ahm8n8n8n)nno#	CH#	W__?366>>>	*r   sSequence[int] | Nonec                    |dk    rt          d          ||dvrt          d|d          t           j                  t	                    dk    r#t           ||d         ndd         |	          S |lt          t          |                     fd
t           j                  D             }t          j	         t          j        d j                  |           t	                    dk    rt           |          S fdt          dt	                    d          D             D ]}t           ||            S )a  Computes the multidimensional discrete cosine transform of the input

  JAX implementation of :func:`scipy.fft.dctn`.

  Args:
    x: array
    type: integer, default = 2. Currently only type 2 is supported.
    s: integer or sequence of integers. Specifies the shape of the result. If not
      specified, it will default to the shape of ``x`` along the specified ``axes``.
    axes: integer or sequence of integers. Specifies the axes along which the
      transform will be computed.
    norm: string. The normalization mode: one of ``[None, "backward", "ortho"]``.
      The default is ``None``, which is equivalent to ``"backward"``.

  Returns:
    array containing the discrete cosine transform of x

  See Also:
    - :func:`jax.scipy.fft.dct`: one-dimensional DCT
    - :func:`jax.scipy.fft.idct`: one-dimensional inverse DCT
    - :func:`jax.scipy.fft.idctn`: multidimensional inverse DCT

  Examples:

    ``jax.scipy.fft.dctn`` computes the transform along both the axes by default
    when ``axes`` argument is ``None``.

    >>> x = jax.random.normal(jax.random.key(0), (3, 3))
    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jax.scipy.fft.dctn(x))
    [[-5.04 -7.54 -3.26]
     [ 0.83  3.64 -4.03]
     [ 0.12 -0.73  3.74]]

    When ``s=[2]``, dimension of the transform along ``axis 0`` will be ``2``
    and dimension along ``axis 1`` will be same as that of input.

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jax.scipy.fft.dctn(x, s=[2]))
    [[-2.92 -2.68 -5.74]
     [ 0.42  0.97  1.  ]]

    When ``s=[2]`` and ``axes=[1]``, dimension of the transform along ``axis 1`` will
    be ``2`` and dimension along ``axis 0`` will  be same as that of input.
    Also when ``axes=[1]``, transform will be computed only along ``axis 1``.

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jax.scipy.fft.dctn(x, s=[2], axes=[1]))
    [[-0.22 -0.9 ]
     [-0.57 -1.68]
     [-2.52 -0.11]]

    When ``s=[2, 4]``, shape of the transform will be ``(2, 4)``.

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jax.scipy.fft.dctn(x, s=[2, 4]))
    [[-2.92 -2.49 -4.21 -5.57]
     [ 0.42  0.79  1.16  0.8 ]]
  r   r;   Nr<   zjax.scipy.fft.dctn: norm=r?   r   r   r6   r   r8   c                N    g | ]!}d |v r|         j         |         z
  nd d f"S rA   rB   r(   r)   nsr   s     r   r*   zdctn.<locals>.<listcomp>   <    PPPQQa2gg1
""1a8PPPr   )rR   r8   c                *    g | ]}||d z            S )r   r&   )r(   irR   s     r   r*   zdctn.<locals>.<listcomp>   s%    AAAQT!AaC%[AAAr   )rH   rI   r/   r0   lenrQ   dictzipr   rJ   r   rK   r,   rd   dctn)r   r5   re   rR   r8   pads
axes_blockrk   s   `  `   @r   rr   rr      si   ~ 
QYY
?
@
@@	$&;;;
HDHHH
I
II	\==DYY!^^qAMAaDDt$q'MMMM]	c$ll		BPPPPP%--PPPD39Q(($//AYY!^^D)))) BAAA%3t99a*@*@AAA , ,jQZd+++AA	
(r   c                r    |dk    rt          d          ||dvrt          d|d          t           j                  Nt	          j         t          j        d j                   fdt           j                  D                         j
                 }                     t          j                   ||d	k    rt                      t                      t	          j        t          j        |t          j        
          fdt           j                  D                       }t!          ||          }                     |j                    t!          ||          z    dz  |z   t          j                                        t'           j                  }|S )a  Computes the inverse discrete cosine transform of the input

  JAX implementation of :func:`scipy.fft.idct`.

  Args:
    x: array
    type: integer, default = 2. Currently only type 2 is supported.
    n: integer, default = x.shape[axis]. The length of the transform.
      If larger than ``x.shape[axis]``, the input will be zero-padded, if
      smaller, the input will be truncated.
    axis: integer, default=-1. The axis along which the dct will be performed.
    norm: string. The normalization mode: one of ``[None, "backward", "ortho"]``.
      The default is ``None``, which is equivalent to ``"backward"``.

  Returns:
    array containing the inverse discrete cosine transform of x

  See Also:
    - :func:`jax.scipy.fft.dct`: DCT
    - :func:`jax.scipy.fft.dctn`: multidimensional DCT
    - :func:`jax.scipy.fft.idctn`: multidimensional inverse DCT

  Examples:

    >>> x = jax.random.normal(jax.random.key(0), (3, 3))
    >>> with jnp.printoptions(precision=2, suppress=True):
    ...    print(jax.scipy.fft.idct(x))
    [[-0.02 -0.   -0.17]
     [-0.02 -0.07 -0.28]
     [-0.16 -0.36  0.18]]

    When ``n`` smaller than ``x.shape[axis]``

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...    print(jax.scipy.fft.idct(x, n=2))
    [[ 0.   -0.19]
     [-0.03 -0.34]
     [-0.38  0.04]]

    When ``n`` smaller than ``x.shape[axis]`` and ``axis=0``

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...    print(jax.scipy.fft.idct(x, n=2, axis=0))
    [[-0.35  0.23 -0.1 ]
     [ 0.17 -0.09  0.01]]

    When ``n`` larger than ``x.shape[axis]`` and ``axis=0``

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...    print(jax.scipy.fft.idct(x, n=4, axis=0))
    [[-0.34  0.03  0.07]
     [ 0.    0.18 -0.17]
     [ 0.14  0.09 -0.14]
     [ 0.   -0.18  0.14]]

    ``jax.scipy.fft.idct`` can be used to reconstruct ``x`` from the result
    of ``jax.scipy.fft.dct``

    >>> x_dct = jax.scipy.fft.dct(x)
    >>> jnp.allclose(x, jax.scipy.fft.idct(x_dct))
    Array(True, dtype=bool)
  r   r;   Nr<   zjax.scipy.fft.idct: norm=r?   r   c                F    g | ]}d |k    rj                  z
  nd d fS rA   rB   rC   s     r   r*   zidct.<locals>.<listcomp>*  rD   r   r=   rF   c                     g | ]
}|k    |S r&   r&   r'   s     r   r*   zidct.<locals>.<listcomp>2  s%    8_8_8_qUVZ^U^U^U^U^U^r   rE   )rH   rI   r   r0   r   rJ   r   rK   r,   r/   r-   astypefloat32r3   r.   rM   r   rL   ifft_dct_deinterleaverN   )	r   r5   r6   r   r8   r
   r   w4r"   s	   ` ``     r   idctr}      s   @ 
QYY
?
@
@@	$&;;;
HDHHH
I
II	4	(	($]39Q(() ) ) ) ) )--) ) )	* 	*A gdm!hhs{!	\TZ''4  Aa!	ocj#+6668_8_8_8_E!&MM8_8_8_``!
1Qxx"hhrx!3q!99o!!eai!	gll14l  !!&$''#	*r   c                    |dk    rt          d          ||dvrt          d|d          |t           j                  }t	          |          dk    r#t           ||d         nd|d         |	          S |lt          t          ||                     fd
t           j                  D             }t          j	         t          j        d j                  |           |D ]}t           ||            S )a	  Computes the multidimensional inverse discrete cosine transform of the input

  JAX implementation of :func:`scipy.fft.idctn`.

  Args:
    x: array
    type: integer, default = 2. Currently only type 2 is supported.
    s: integer or sequence of integers. Specifies the shape of the result. If not
      specified, it will default to the shape of ``x`` along the specified ``axes``.
    axes: integer or sequence of integers. Specifies the axes along which the
      transform will be computed.
    norm: string. The normalization mode: one of ``[None, "backward", "ortho"]``.
      The default is ``None``, which is equivalent to ``"backward"``.

  Returns:
    array containing the inverse discrete cosine transform of x

  See Also:
    - :func:`jax.scipy.fft.dct`: one-dimensional DCT
    - :func:`jax.scipy.fft.dctn`: multidimensional DCT
    - :func:`jax.scipy.fft.idct`: one-dimensional inverse DCT

  Examples:

    ``jax.scipy.fft.idctn`` computes the transform along both the axes by default
    when ``axes`` argument is ``None``.

    >>> x = jax.random.normal(jax.random.key(0), (3, 3))
    >>> with jnp.printoptions(precision=2, suppress=True):
    ...    print(jax.scipy.fft.idctn(x))
    [[-0.03 -0.08 -0.08]
     [ 0.05  0.12 -0.09]
     [-0.02 -0.04  0.08]]

    When ``s=[2]``, dimension of the transform along ``axis 0`` will be ``2``
    and dimension along ``axis 1`` will be the same as that of input.

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...  print(jax.scipy.fft.idctn(x, s=[2]))
    [[-0.01 -0.03 -0.14]
     [ 0.    0.03  0.06]]

    When ``s=[2]`` and ``axes=[1]``, dimension of the transform along ``axis 1`` will
    be ``2`` and dimension along ``axis 0`` will  be same as that of input.
    Also when ``axes=[1]``, transform will be computed only along ``axis 1``.

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...  print(jax.scipy.fft.idctn(x, s=[2], axes=[1]))
    [[ 0.   -0.19]
     [-0.03 -0.34]
     [-0.38  0.04]]

    When ``s=[2, 4]``, shape of the transform will be ``(2, 4)``

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...  print(jax.scipy.fft.idctn(x, s=[2, 4]))
    [[-0.01 -0.01 -0.05 -0.11]
     [ 0.    0.01  0.03  0.04]]

    ``jax.scipy.fft.idctn`` can be used to reconstruct ``x`` from the result
    of ``jax.scipy.fft.dctn``

    >>> x_dctn = jax.scipy.fft.dctn(x)
    >>> jnp.allclose(x, jax.scipy.fft.idctn(x_dctn))
    Array(True, dtype=bool)
  r   r;   Nr<   zjax.scipy.fft.idctn: norm=r?   r   r   rh   c                N    g | ]!}d |v r|         j         |         z
  nd d f"S rA   rB   rj   s     r   r*   zidctn.<locals>.<listcomp>  rl   r   )r   r8   )rH   rI   r/   r0   ro   r}   rp   rq   r   rJ   r   rK   r,   )r   r5   re   rR   r8   rs   r   rk   s   `      @r   idctnr   ?  s   L 
QYY
?
@
@@	$&;;;
ITIII
J
JJ	\==DYY!^^Q]QqTT47NNNN]	c$ll		BPPPPP%--PPPD39Q(($//A  & &dQT%%%AA	
(r   c                   	 t          d d d           	t          	 fdt          t           j                            D                       }t          	 fdt          t           j                            D                       } |         }t          j         |         f          }t          j         j         j	                  }t          	fdt          t           j                            D                       }t          	fdt          t           j                            D                       }|j
        |                             |          }|j
        |                             |          }|S )Nc              3     K   | ]=}|k    r1t          d t          j        j                 dz            d          nV  >d S r   slicemathceilr-   r(   rn   r   empty_slicer   s     r   	<genexpr>z$_dct_deinterleave.<locals>.<genexpr>  sd       $ $
 56IIeD$)AGDM!O,,a000;$ $ $ $ $ $r   c              3     K   | ]=}|k    r1t          t          j        j                 d z            dd          nV  >dS )r   Nr   r   r   s     r   r   z$_dct_deinterleave.<locals>.<genexpr>  sd       $ $
 56IIeDIagdmAo&&a000;$ $ $ $ $ $r   rF   c              3  J   K   | ]}|k    rt          d d d          nV  d S )Nr   r   r(   rn   r   r   s     r   r   z$_dct_deinterleave.<locals>.<genexpr>  sU       W W=>a4iieD$[W W W W W Wr   c              3  J   K   | ]}|k    rt          d dd          nV  dS )r   Nr   r   r   s     r   r   z$_dct_deinterleave.<locals>.<genexpr>  sU       T T:;199eAtQ+T T T T T Tr   )r   tupler/   ro   r-   r   r   r   zerosr,   atset)
r   r   ix0ix1r   r    r"   evensoddsr   s
   ``       @r   r{   r{     s   dD$''+ $ $ $ $ $ $S\\""$ $ $ 	$ 	$# 
 $ $ $ $ $ $S\\""$ $ $ 
$ 
$# 	v"
wqvw"	!')))#
 W W W W WBGAGBUBUW W W W W%	 T T T T T?DS\\?R?RT T T 
T 
T$			2		#t#	*r   )r
   r   r   r	   r   r	   )r   r	   r   r   r   r	   )r"   r	   r   r   r   r	   )r   Nr4   N)r   r	   r5   r   r6   r7   r   r   r8   r9   r   r	   )r   r	   rR   rS   r8   r9   r   r	   )r   NNN)r   r	   r5   r   re   rf   rR   rf   r8   r9   r   r	   )
__future__r   collections.abcr   	functoolsr   r   jaxr   	jax.numpynumpyr   jax._src.utilr   jax._src.numpy.utilr   jax._src.typingr	   r   r!   r3   rQ   rd   rr   r}   r   r{   r&   r   r   <module>r      s   # " " " " " $ $ $ $ $ $                    + + + + + + 6 6 6 6 6 6 ! ! ! ! ! !, , , ,) ) ) )
2 2 2 2 26+/J J J J JZ      !!%&* U U U U Up 37+/Z Z Z Z Zz !""&'+!Y Y Y Y Yx     r   