
    2Vh                     <   d 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l
mZ d Z G d	 d
e	      Z G d de      Z ed      dXd       Z G d de      Z ed      dXd       Z G d de	      Z ed      dYd       Z G d de	      Z ed      d        Z G d de	      Z ed      dXd       Z G d  d!e	      Z ed"      d#        Z G d$ d%e	      Z ed&      d'        Z G d( d)e	      Z ed*      d+        Z G d, d-e	      Z ed.      d/        Z G d0 d1e	      Z  ed2      dZd3       Z! G d4 d5e	      Z" ed6      dZd7       Z# G d8 d9e	      Z$ ed:      	 d[d;       Z% G d< d=e	      Z& ed>      	 	 	 d\d?       Z' G d@ dAe	      Z( edB      dC        Z) G dD dEe	      Z* edF      dG        Z+ G dH dIe	      Z, edJ      dK        Z- G dL dMe	      Z. edNg      dO        Z/ G dP dQe	      Z0 G dR dSe	      Z1 edT      dU        Z2 edV      dW        Z3y)]z4Commonly used math operations not included in NumPy.    )backend)keras_export)KerasTensor)any_symbolic_tensors)	Operation)reduce_shapec                     | j                   }|j                   }t        |      dkD  rt        dt        |       d      |d   $|d   |d   |d   k7  rt        d| d| d      y y y )N   z;Argument `segment_ids` should be an 1-D vector, got shape: z. Consider either flatten input with segment_ids.reshape((-1)) and data.reshape((-1, ) + data.shape[len(segment_ids.shape):]) or vectorize with vmap.r   zJArgument `segment_ids` and `data` should have same leading dimension. Got z v.s. .)shapelen
ValueError)datasegment_ids
data_shapesegment_ids_shapes       B/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/ops/math.py_segment_reduce_validationr      s    J#))
!I$%& '##
 	
 	!(qM%a JqM1/0l!
 	
 2 & 	)    c                   &     e Zd Zd fd	Zd Z xZS )SegmentReductionc                 >    t         |           || _        || _        y N)super__init__num_segmentssorted)selfr   r   	__class__s      r   r   zSegmentReduction.__init__#   s    (r   c                 |    | j                   ft        |j                  dd        z   }t        ||j                        S )Nr
   r   dtype)r   tupler   r   r"   )r   r   _output_shapes       r   compute_output_specz$SegmentReduction.compute_output_spec(   s5    ))+eDJJqrN.CCTZZ@@r   NF)__name__
__module____qualname__r   r&   __classcell__r   s   @r   r   r   "   s    
Ar   r   c                       e Zd Zd Zy)
SegmentSumc                     t        ||       t        j                  j                  ||| j                  | j
                        S Nr   r   )r   r   mathsegment_sumr   r   r   r   r   s      r   callzSegmentSum.call.   >    "45||''**;;	 ( 
 	
r   Nr(   r)   r*   r5    r   r   r.   r.   -       
r   r.   zkeras.ops.segment_sumNc                     t        | |       t        | f      rt        ||      j                  | |      S t        j
                  j                  | |||      S )a  Computes the sum of segments in a tensor.

    Args:
        data: Input tensor.
        segment_ids: A N-D tensor containing segment indices for each
            element in `data`. Num dims for segment ids should be strictly
            smaller or equal to number of dims in data.
        num_segments: An integer representing the total number of
            segments. If not specified, it is inferred from the maximum
            value in `segment_ids`.
        sorted: A boolean indicating whether `segment_ids` is sorted.
            Defaults to `False`.

    Returns:
        A tensor containing the sum of segments, where each element
        represents the sum of the corresponding segment in `data`.

    Example:

    >>> data = keras.ops.convert_to_tensor([1, 2, 10, 20, 100, 200])
    >>> segment_ids = keras.ops.convert_to_tensor([0, 0, 1, 1, 2, 2])
    >>> num_segments = 3
    >>> keras.ops.segment_sum(data, segment_ids,num_segments)
    array([3, 30, 300], dtype=int32)
    r1   )r   r   r.   symbolic_callr   r2   r3   r   r   r   r   s       r   r3   r3   8   sW    6 t[1TG$,/==dKPP<<##kV $  r   c                       e Zd Zd Zy)
SegmentMaxc                     t        ||       t        j                  j                  ||| j                  | j
                        S r0   )r   r   r2   segment_maxr   r   r4   s      r   r5   zSegmentMax.call\   r6   r   Nr7   r8   r   r   r>   r>   [   r9   r   r>   zkeras.ops.segment_maxc                     t        | |       t        | f      rt        ||      j                  | |      S t        j
                  j                  | |||      S )a  Computes the max of segments in a tensor.

    Args:
        data: Input tensor.
        segment_ids: A N-D tensor containing segment indices for each
            element in `data`. data.shape[:len(segment_ids.shape)] should match.
        num_segments: An integer representing the total number of
            segments. If not specified, it is inferred from the maximum
            value in `segment_ids`.
        sorted: A boolean indicating whether `segment_ids` is sorted.
            Defaults to `False`.

    Returns:
        A tensor containing the max of segments, where each element
        represents the max of the corresponding segment in `data`.

    Example:

    >>> data = keras.ops.convert_to_tensor([1, 2, 10, 20, 100, 200])
    >>> segment_ids = keras.ops.convert_to_tensor([0, 0, 1, 1, 2, 2])
    >>> num_segments = 3
    >>> keras.ops.segment_max(data, segment_ids, num_segments)
    array([2, 20, 200], dtype=int32)
    r1   )r   r   r>   r;   r   r2   r@   r<   s       r   r@   r@   f   sW    4 t[1TG$,/==dKPP<<##kV $  r   c                   ,     e Zd Zd fd	Zd Zd Z xZS )TopKc                 >    t         |           || _        || _        y r   )r   r   kr   )r   rE   r   r   s      r   r   zTopK.__init__   s    r   c                     t        |j                        }| j                  |d<   t        ||j                        t        |d      fS )Nr!   int32)listr   rE   r   r"   r   xr%   s      r   r&   zTopK.compute_output_spec   sA    AGG}66R l!'':l':
 	
r   c                 l    t         j                  j                  || j                  | j                        S r   )r   r2   top_krE   r   r   rK   s     r   r5   z	TopK.call   s#    ||!!!TVVT[[99r   )Fr(   r)   r*   r   r&   r5   r+   r,   s   @r   rC   rC      s    

:r   rC   zkeras.ops.top_kc                     t        | f      rt        ||      j                  |       S t        j                  j                  | ||      S )a  Finds the top-k values and their indices in a tensor.

    Args:
        x: Input tensor.
        k: An integer representing the number of top elements to retrieve.
        sorted: A boolean indicating whether to sort the output in
        descending order. Defaults to `True`.

    Returns:
        A tuple containing two tensors. The first tensor contains the
        top-k values, and the second tensor contains the indices of the
        top-k values in the input tensor.

    Example:

    >>> x = keras.ops.convert_to_tensor([5, 2, 7, 1, 9, 3])
    >>> values, indices = top_k(x, k=3)
    >>> print(values)
    array([9 7 5], shape=(3,), dtype=int32)
    >>> print(indices)
    array([4 2 0], shape=(3,), dtype=int32)

    )r   rC   r;   r   r2   rM   )rK   rE   r   s      r   rM   rM      s>    2 QD!Av,,Q//<<aF++r   c                   *     e Zd Z fdZd Zd Z xZS )InTopKc                 0    t         |           || _        y r   )r   r   rE   )r   rE   r   s     r   r   zInTopK.__init__   s    r   c                 0    t        |j                  d      S )Nboolr!   r   r   r   targetspredictionss      r   r&   zInTopK.compute_output_spec   s    f==r   c                 X    t         j                  j                  ||| j                        S r   )r   r2   in_top_krE   rW   s      r   r5   zInTopK.call   s    ||$$Wk466BBr   rO   r,   s   @r   rR   rR      s    >Cr   rR   zkeras.ops.in_top_kc                     t        | |f      rt        |      j                  | |      S t        j                  j                  | ||      S )a  Checks if the targets are in the top-k predictions.

    Args:
        targets: A tensor of true labels.
        predictions: A tensor of predicted labels.
        k: An integer representing the number of predictions to consider.

    Returns:
        A boolean tensor of the same shape as `targets`, where each element
        indicates whether the corresponding target is in the top-k predictions.

    Example:

    >>> targets = keras.ops.convert_to_tensor([2, 5, 3])
    >>> predictions = keras.ops.convert_to_tensor(
    ... [[0.1, 0.4, 0.6, 0.9, 0.5],
    ...  [0.1, 0.7, 0.9, 0.8, 0.3],
    ...  [0.1, 0.6, 0.9, 0.9, 0.5]])
    >>> in_top_k(targets, predictions, k=3)
    array([ True False  True], shape=(3,), dtype=bool)
    )r   rR   r;   r   r2   r[   )rX   rY   rE   s      r   r[   r[      sA    . Wk23ay&&w<<<<  +q99r   c                   ,     e Zd Zd fd	Zd Zd Z xZS )	Logsumexpc                 >    t         |           || _        || _        y r   )r   r   axiskeepdims)r   r`   ra   r   s      r   r   zLogsumexp.__init__   s    	 r   c                 p    t        |j                  | j                  | j                        }t	        |      S )N)r   )r   r   r`   ra   r   rJ   s      r   r&   zLogsumexp.compute_output_spec   s'    #AGGTYYF..r   c                 n    t         j                  j                  || j                  | j                        S )Nr`   ra   )r   r2   	logsumexpr`   ra   rN   s     r   r5   zLogsumexp.call   s&    ||%%adii$--%PPr   r'   rO   r,   s   @r   r^   r^      s    !
/Qr   r^   zkeras.ops.logsumexpc                     t        | f      rt        ||      j                  |       S t        j                  j                  | ||      S )a  Computes the logarithm of sum of exponentials of elements in a tensor.

    Args:
        x: Input tensor.
        axis: An integer or a tuple of integers specifying the axis/axes
            along which to compute the sum. If `None`, the sum is computed
            over all elements. Defaults to `None`.
        keepdims: A boolean indicating whether to keep the dimensions of
            the input tensor when computing the sum. Defaults to `False`.

    Returns:
        A tensor containing the logarithm of the sum of exponentials of
        elements in `x`.

    Example:

    >>> x = keras.ops.convert_to_tensor([1., 2., 3.])
    >>> logsumexp(x)
    3.407606
    rd   )r   r^   r;   r   r2   re   )rK   r`   ra   s      r   re   re      sB    , QD!x(66q99<<!!!$!BBr   c                   *     e Zd Z fdZd Zd Z xZS )ExtractSequencesc                 >    t         |           || _        || _        y r   )r   r   sequence_lengthsequence_stride)r   rj   rk   r   s      r   r   zExtractSequences.__init__  s    ..r   c                 H   t        |j                        dk  rt        d|j                         |j                  d   -d|j                  d   | j                  z
  | j                  z  z   }nd }|j                  d d || j                  fz   }t        ||j                        S )Nr
   5Input should have rank >= 1. Received: input.shape = rG   r!   )r   r   r   rj   rk   r   r"   )r   rK   num_sequences	new_shapes       r   r&   z$ExtractSequences.compute_output_spec  s    qww<!++,77)5  772;"QWWR[4#7#77D<P<PPP  !MGGCRLM43G3G#HH	!''::r   c                 n    t         j                  j                  || j                  | j                        S )N)rj   rk   )r   r2   extract_sequencesrj   rk   rN   s     r   r5   zExtractSequences.call  s3    ||-- 00 00 . 
 	
r   rO   r,   s   @r   rh   rh   
  s    /
;
r   rh   zkeras.ops.extract_sequencesc                     t        | f      rt        ||      j                  |       S t        j                  j                  | ||      S )a_  Expands the dimension of last axis into sequences of `sequence_length`.

    Slides a window of size `sequence_length` over the last axis of the input
    with a stride of `sequence_stride`, replacing the last axis with
    `[num_sequences, sequence_length]` sequences.

    If the dimension along the last axis is N, the number of sequences can be
    computed by:

    `num_sequences = 1 + (N - sequence_length) // sequence_stride`

    Args:
        x: Input tensor.
        sequence_length: An integer representing the sequences length.
        sequence_stride: An integer representing the sequences hop size.

    Returns:
        A tensor of sequences with shape [..., num_sequences, sequence_length].

    Example:

    >>> x = keras.ops.convert_to_tensor([1, 2, 3, 4, 5, 6])
    >>> extract_sequences(x, 3, 2)
    array([[1, 2, 3],
       [3, 4, 5]])
    )r   rh   r;   r   r2   rq   )rK   rj   rk   s      r   rq   rq   '  sF    8 QD!AOO
 	
 <<))!_oNNr   c                   ,     e Zd Zd fd	Zd Zd Z xZS )FFTc                 0    t         |           || _        y r   )r   r   r`   )r   r`   r   s     r   r   zFFT.__init__K  s    	r   c                 <   t        |t        t        f      rt        |      dk7  rt	        d|       |\  }}|j
                  |j
                  k7  r%t	        d|j
                   d|j
                         t        |j
                        dk  rt	        d|j
                         |j
                  d   }|%t	        d| j                   d	|j
                         t        |j
                  |j                  
      t        |j
                  |j                  
      fS )N   MInput `x` should be a tuple of two tensors - real and imaginary. Received: x=Input `x` should be a tuple of two tensors - real and imaginary. Both the real and imaginary parts should have the same shape. Received: x[0].shape = , x[1].shape = r
   rm   rG   Input should have its z/th axis fully-defined. Received: input.shape = r!   )	
isinstancer#   rI   r   r   r   r`   r   r"   )r   rK   realimagms        r   r&   zFFT.compute_output_specO  s!   !eT]+s1v{**+. 
 
d::#66:jj\ B  $

|-  tzz?Q++/::,8  JJrN9( 4++/::,8  djj

;djj

;
 	
r   c                 @    t         j                  j                  |      S r   )r   r2   fftrN   s     r   r5   zFFT.callt      ||""r   )rG   rO   r,   s   @r   rt   rt   J  s    #
J#r   rt   zkeras.ops.fftc                     t        |       rt               j                  |       S t        j                  j                  |       S )a,  Computes the Fast Fourier Transform along last axis of input.

    Args:
        x: Tuple of the real and imaginary parts of the input tensor. Both
            tensors in the tuple should be of floating type.

    Returns:
        A tuple containing two tensors - the real and imaginary parts of the
        output tensor.

    Example:

    >>> x = (
    ...     keras.ops.convert_to_tensor([1., 2.]),
    ...     keras.ops.convert_to_tensor([0., 1.]),
    ... )
    >>> fft(x)
    (array([ 3., -1.], dtype=float32), array([ 1., -1.], dtype=float32))
    )r   rt   r;   r   r2   r   rK   s    r   r   r   x  s4    * Au""1%%<<Ar   c                   *     e Zd Z fdZd Zd Z xZS )FFT2c                 0    t         |           d| _        y N)rG   r   r   axesr   r   s    r   r   zFFT2.__init__      	r   c                    t        |t        t        f      rt        |      dk7  rt	        d|       |\  }}|j
                  |j
                  k7  r%t	        d|j
                   d|j
                         t        |j
                        dk  rt	        d|j
                         |j
                  | j                  d      }|j
                  | j                  d      }||%t	        d| j                   d	|j
                         t        |j
                  |j                  
      t        |j
                  |j                  
      fS Nrw   rx   ry   rz   5Input should have rank >= 2. Received: input.shape = r   r
   r{   z- axes fully-defined. Received: input.shape = r!   	r|   r#   rI   r   r   r   r   r   r"   r   rK   r}   r~   r   ns         r   r&   zFFT2.compute_output_spec  D   !eT]+s1v{**+. 
 
d::#66:jj\ B  $

|-  tzz?Q++/::,8  JJtyy|$JJtyy|$9	( 4++/::,8  djj

;djj

;
 	
r   c                 @    t         j                  j                  |      S r   )r   r2   fft2rN   s     r   r5   z	FFT2.call  s    ||  ##r   rO   r,   s   @r   r   r     s    #
J$r   r   zkeras.ops.fft2c                     t        |       rt               j                  |       S t        j                  j                  |       S )au  Computes the 2D Fast Fourier Transform along the last two axes of input.

    Args:
        x: Tuple of the real and imaginary parts of the input tensor. Both
            tensors in the tuple should be of floating type.

    Returns:
        A tuple containing two tensors - the real and imaginary parts of the
        output.

    Example:

    >>> x = (
    ...     keras.ops.convert_to_tensor([[1., 2.], [2., 1.]]),
    ...     keras.ops.convert_to_tensor([[0., 1.], [1., 0.]]),
    ... )
    >>> fft2(x)
    (array([[ 6.,  0.],
        [ 0., -2.]], dtype=float32), array([[ 2.,  0.],
        [ 0., -2.]], dtype=float32))
    )r   r   r;   r   r2   r   r   s    r   r   r     s4    . Av##A&&<<Qr   c                   *     e Zd Z fdZd Zd Z xZS )IFFT2c                 0    t         |           d| _        y r   r   r   s    r   r   zIFFT2.__init__  r   r   c                    t        |t        t        f      rt        |      dk7  rt	        d|       |\  }}|j
                  |j
                  k7  r%t	        d|j
                   d|j
                         t        |j
                        dk  rt	        d|j
                         |j
                  | j                  d      }|j
                  | j                  d      }||%t	        d| j                   d	|j
                         t        |j
                  |j                  
      t        |j
                  |j                  
      fS r   r   r   s         r   r&   zIFFT2.compute_output_spec  r   r   c                 @    t         j                  j                  |      S r   )r   r2   ifft2rN   s     r   r5   z
IFFT2.call  s    ||!!!$$r   rO   r,   s   @r   r   r     s    #
J%r   r   zkeras.ops.ifft2c                     t        |       rt               j                  |       S t        j                  j                  |       S )a  Computes the 2D Inverse Fast Fourier Transform along the last two axes of
        input.

    Args:
        x: Tuple of the real and imaginary parts of the input tensor. Both
            tensors in the tuple should be of floating type.

    Returns:
        A tuple containing two tensors - the real and imaginary parts of the
        output.

    Example:

    >>> x = (
    ...     keras.ops.convert_to_tensor([[1., 2.], [2., 1.]]),
    ...     keras.ops.convert_to_tensor([[0., 1.], [1., 0.]]),
    ... )
    >>> ifft2(x)
    (array([[ 6.,  0.],
        [ 0., -2.]], dtype=float32), array([[ 2.,  0.],
        [ 0., -2.]], dtype=float32))
    )r   r   r;   r   r2   r   r   s    r   r   r   
  s4    0 Aw$$Q''<<a  r   c                   ,     e Zd Zd fd	Zd Zd Z xZS )RFFTc                 0    t         |           || _        y r   r   r   
fft_lengthr   r   r   s     r   r   zRFFT.__init__(      $r   c                 p   t        |j                        dk  rt        d|j                         | j                  | j                  dz  dz   }n'|j                  d   |j                  d   dz  dz   }nd }|j                  d d |fz   }t	        ||j
                        t	        ||j
                        fS )Nr
   rm   rw   rG   r!   )r   r   r   r   r   r"   )r   rK   new_last_dimensionro   s       r   r&   zRFFT.compute_output_spec,  s    qww<!++,77)5 
 ??&!%A!5!9wwr{&%&WWR[A%5%9"%)"GGCRL$6#88	 iqww7iqww7
 	
r   c                 X    t         j                  j                  || j                        S N)r   )r   r2   rfftr   rN   s     r   r5   z	RFFT.callB  s     ||  t ??r   r   rO   r,   s   @r   r   r   '  s    %
,@r   r   zkeras.ops.rfftc                     t        | f      rt        |      j                  |       S t        j                  j                  | |      S )a  Real-valued Fast Fourier Transform along the last axis of the input.

    Computes the 1D Discrete Fourier Transform of a real-valued signal over the
    inner-most dimension of input.

    Since the Discrete Fourier Transform of a real-valued signal is
    Hermitian-symmetric, RFFT only returns the `fft_length / 2 + 1` unique
    components of the FFT: the zero-frequency term, followed by the
    `fft_length / 2` positive-frequency terms.

    Along the axis RFFT is computed on, if `fft_length` is smaller than the
    corresponding dimension of the input, the dimension is cropped. If it is
    larger, the dimension is padded with zeros.

    Args:
        x: Input tensor.
        fft_length: An integer representing the number of the fft length. If not
            specified, it is inferred from the length of the last axis of `x`.
            Defaults to `None`.

    Returns:
        A tuple containing two tensors - the real and imaginary parts of the
        output.

    Examples:

    >>> x = keras.ops.convert_to_tensor([0.0, 1.0, 2.0, 3.0, 4.0])
    >>> rfft(x)
    (array([10.0, -2.5, -2.5]), array([0.0, 3.4409548, 0.81229924]))

    >>> rfft(x, 3)
    (array([3.0, -1.5]), array([0.0, 0.8660254]))
    )r   r   r;   r   r2   r   rK   r   s     r   r   r   F  s<    F QD!J--a00<<Q
++r   c                   ,     e Zd Zd fd	Zd Zd Z xZS )IRFFTc                 0    t         |           || _        y r   r   r   s     r   r   zIRFFT.__init__o  r   r   c                     t        |t        t        f      rt        |      dk7  rt	        d|       |\  }}|j
                  |j
                  k7  r%t	        d|j
                   d|j
                         t        |j
                        dk  rt	        d|j
                         | j                  | j                  }n'|j
                  d   d|j
                  d   dz
  z  }nd }|j
                  d d |fz   }t        ||j                        S )	Nrw   rx   ry   rz   r
   rm   rG   r!   )	r|   r#   rI   r   r   r   r   r   r"   )r   rK   r}   r~   r   ro   s         r   r&   zIRFFT.compute_output_specs  s   !eT]+s1v{**+.  
d::#66:jj\ B  $

|-  tzz?Q++/::,8 
 ??&!%zz"~)%&$**R.1*<%="%)"JJsO'9&;;	$**==r   c                 X    t         j                  j                  || j                        S r   )r   r2   irfftr   rN   s     r   r5   z
IRFFT.call  s     ||!!!!@@r   r   rO   r,   s   @r   r   r   n  s    %>@Ar   r   zkeras.ops.irfftc                     t        |       rt        |      j                  |       S t        j                  j                  | |      S )a3  Inverse real-valued Fast Fourier transform along the last axis.

    Computes the inverse 1D Discrete Fourier Transform of a real-valued signal
    over the inner-most dimension of input.

    The inner-most dimension of the input is assumed to be the result of RFFT:
    the `fft_length / 2 + 1` unique components of the DFT of a real-valued
    signal. If `fft_length` is not provided, it is computed from the size of the
    inner-most dimension of the input `(fft_length = 2 * (inner - 1))`. If the
    FFT length used to compute is odd, it should be provided since it cannot
    be inferred properly.

    Along the axis IRFFT is computed on, if `fft_length / 2 + 1` is smaller than
    the corresponding dimension of the input, the dimension is cropped. If it is
    larger, the dimension is padded with zeros.

    Args:
        x: Tuple of the real and imaginary parts of the input tensor. Both
            tensors in the tuple should be of floating type.
        fft_length: An integer representing the number of the fft length. If not
            specified, it is inferred from the length of the last axis of `x`.
            Defaults to `None`.

    Returns:
        A tensor containing the inverse real-valued Fast Fourier Transform
        along the last axis of `x`.

    Examples:

    >>> real = keras.ops.convert_to_tensor([0.0, 1.0, 2.0, 3.0, 4.0])
    >>> imag = keras.ops.convert_to_tensor([0.0, 1.0, 2.0, 3.0, 4.0])
    >>> irfft((real, imag))
    array([0.66666667, -0.9106836, 0.24401694])

    >>> irfft(rfft(real, 5), 5)
    array([0.0, 1.0, 2.0, 3.0, 4.0])
    )r   r   r;   r   r2   r   r   s     r   r   r     s:    N AZ ..q11<<a,,r   c                   0     e Zd Z	 	 d fd	Zd Zd Z xZS )STFTc                 h    t         |           || _        || _        || _        || _        || _        y r   )r   r   rj   rk   r   windowcenter)r   rj   rk   r   r   r   r   s         r   r   zSTFT.__init__  s5     	..$r   c                 l   |j                   d   R| j                  du rdn| j                  dz  dz  }d|j                   d   |z   | j                  z
  | j                  z  z   }nd }|j                   d d || j                  dz  dz   fz   }t	        ||j
                        t	        ||j
                        fS )NrG   Fr   rw   r
   r!   )r   r   r   rk   r   r"   )r   rK   paddedrn   ro   s        r   r&   zSTFT.compute_output_spec  s    772;"++.QT__5IQ4NF772;'$//9''((  !MGGCRLM4??a3G!3K#LL	iqww7iqww7
 	
r   c                     t         j                  j                  || j                  | j                  | j
                  | j                  | j                        S )Nrj   rk   r   r   r   )r   r2   stftrj   rk   r   r   r   rN   s     r   r5   z	STFT.call  sH    ||   00 00;;;; ! 
 	
r   hannTrO   r,   s   @r   r   r     s     
 
r   r   zkeras.ops.stftc                     t        | f      rt        |||||      j                  |       S t        j                  j                  | |||||      S )a  Short-Time Fourier Transform along the last axis of the input.

    The STFT computes the Fourier transform of short overlapping windows of the
    input. This giving frequency components of the signal as they change over
    time.

    Args:
        x: Input tensor.
        sequence_length: An integer representing the sequence length.
        sequence_stride: An integer representing the sequence hop size.
        fft_length: An integer representing the size of the FFT to apply. If not
            specified, uses the smallest power of 2 enclosing `sequence_length`.
        window: A string, a tensor of the window or `None`. If `window` is a
            string, available values are `"hann"` and `"hamming"`. If `window`
            is a tensor, it will be used directly as the window and its length
            must be `sequence_length`. If `window` is `None`, no windowing is
            used. Defaults to `"hann"`.
        center: Whether to pad `x` on both sides so that the t-th sequence is
            centered at time `t * sequence_stride`. Otherwise, the t-th sequence
            begins at time `t * sequence_stride`. Defaults to `True`.

    Returns:
        A tuple containing two tensors - the real and imaginary parts of the
        STFT output.

    Example:

    >>> x = keras.ops.convert_to_tensor([0.0, 1.0, 2.0, 3.0, 4.0])
    >>> stft(x, 3, 2, 3)
    (array([[0.75, -0.375],
       [3.75, -1.875],
       [5.25, -2.625]]), array([[0.0, 0.64951905],
       [0.0, 0.64951905],
       [0.0, -0.64951905]]))
    r   )r   r   r;   r   r2   r   )rK   rj   rk   r   r   r   s         r   r   r     sg    N QD!++!
 -
	 <<	''   r   c                   2     e Zd Z	 	 	 d fd	Zd Zd Z xZS )ISTFTc                 v    t         |           || _        || _        || _        || _        || _        || _        y r   )r   r   rj   rk   r   lengthr   r   )r   rj   rk   r   r   r   r   r   s          r   r   zISTFT.__init__(  s<     	..$r   c                    t        |t        t        f      rt        |      dk7  rt	        d|       |\  }}|j
                  |j
                  k7  r%t	        d|j
                   d|j
                         t        |j
                        dk  rt	        d|j
                         |j
                  d   g|j
                  d   dz
  | j                  z  | j                  z   }| j                  | j                  }n$| j                  r|| j                  dz  dz  z
  }nd }|j
                  d d |fz   }t        ||j                        S )	Nrw   rx   ry   rz   r   r   r
   r!   )r|   r#   rI   r   r   r   rk   r   r   r   r   r"   )r   rK   r}   r~   output_sizero   s         r   r&   zISTFT.compute_output_spec9  sN   !eT]+s1v{**+.  
d::#66:jj\ B  $

|-  tzz?Q++/::,8  ::b>%

2"$$%'+7K {{&"kk)T__-AQ,FFKJJsO{n4	$**==r   c           	          t         j                  j                  || j                  | j                  | j
                  | j                  | j                  | j                        S )Nrj   rk   r   r   r   r   )	r   r2   istftrj   rk   r   r   r   r   rN   s     r   r5   z
ISTFT.callZ  sO    ||!! 00 00;;;;;; " 
 	
r   Nr   TrO   r,   s   @r   r   r   '  s     ">B	
r   r   zkeras.ops.istftc           	          t        |       rt        |||||      j                  |       S t        j                  j                  | ||||||      S )a  Inverse Short-Time Fourier Transform along the last axis of the input.

    To reconstruct an original waveform, the parameters should be the same in
    `stft`.

    Args:
        x: Tuple of the real and imaginary parts of the input tensor. Both
            tensors in the tuple should be of floating type.
        sequence_length: An integer representing the sequence length.
        sequence_stride: An integer representing the sequence hop size.
        fft_length: An integer representing the size of the FFT that produced
            `stft`. Should be of type `int32`.
        length: An integer representing the output is clipped to exactly length.
            If not specified, no padding or clipping take place. Defaults to
            `None`.
        window: A string, a tensor of the window or `None`. If `window` is a
            string, available values are `"hann"` and `"hamming"`. If `window`
            is a tensor, it will be used directly as the window and its length
            must be `sequence_length`. If `window` is `None`, no windowing is
            used. Defaults to `"hann"`.
        center: Whether `x` was padded on both sides so that the t-th sequence
            is centered at time `t * sequence_stride`. Defaults to `True`.

    Returns:
        A tensor containing the inverse Short-Time Fourier Transform along the
        last axis of `x`.

    Example:

    >>> x = keras.ops.convert_to_tensor([0.0, 1.0, 2.0, 3.0, 4.0])
    >>> istft(stft(x, 1, 1, 1), 1, 1, 1)
    array([0.0, 1.0, 2.0, 3.0, 4.0])
    r   r   )r   r   r;   r   r2   r   )rK   rj   rk   r   r   r   r   s          r   r   r   f  sh    V A++!
 -
	 <<	''   r   c                       e Zd Zd Zd Zy)Rsqrtc                 j    t        j                  |      }t         j                  j                  |      S r   )r   convert_to_tensorr2   rsqrtrN   s     r   r5   z
Rsqrt.call  s'    %%a(||!!!$$r   c                 D    t        |j                  |j                        S )Nr"   r   r   r"   rN   s     r   r&   zRsqrt.compute_output_spec  s    177!''22r   Nr(   r)   r*   r5   r&   r8   r   r   r   r     s    %3r   r   zkeras.ops.rsqrtc                     t        | f      rt               j                  |       S t        j                  |       } t        j
                  j                  |       S )a/  Computes reciprocal of square root of x element-wise.

    Args:
        x: input tensor

    Returns:
        A tensor with the same dtype as `x`.

    Example:

    >>> x = keras.ops.convert_to_tensor([1.0, 10.0, 100.0])
    >>> keras.ops.rsqrt(x)
    array([1.0, 0.31622776, 0.1], dtype=float32)
    )r   r   r;   r   r   r2   r   r   s    r   r   r     sF      QD!w$$Q''!!!$A<<a  r   c                       e Zd Zd Zd Zy)Erfc                 D    t        |j                  |j                        S Nr!   r   rN   s     r   r&   zErf.compute_output_spec      88r   c                 @    t         j                  j                  |      S r   )r   r2   erfrN   s     r   r5   zErf.call  r   r   Nr(   r)   r*   r&   r5   r8   r   r   r   r     s    9#r   r   zkeras.ops.erfc                     t        | f      rt               j                  |       S t        j                  |       } t        j
                  j                  |       S )a;  Computes the error function of `x`, element-wise.

    Args:
        x: Input tensor.

    Returns:
        A tensor with the same dtype as `x`.

    Example:

    >>> x = np.array([-3.0, -2.0, -1.0, 0.0, 1.0])
    >>> keras.ops.erf(x)
    array([-0.99998 , -0.99532, -0.842701,  0.,  0.842701], dtype=float32)
    )r   r   r;   r   r   r2   r   r   s    r   r   r     sF      QD!u""1%%!!!$A<<Ar   c                       e Zd Zd Zd Zy)Erfinvc                 D    t        |j                  |j                        S r   r   rN   s     r   r&   zErfinv.compute_output_spec  r   r   c                 @    t         j                  j                  |      S r   )r   r2   erfinvrN   s     r   r5   zErfinv.call      ||""1%%r   Nr   r8   r   r   r   r     s    9&r   r   zkeras.ops.erfinvc                     t        | f      rt               j                  |       S t        j                  |       } t        j
                  j                  |       S )aD  Computes the inverse error function of `x`, element-wise.

    Args:
        x: Input tensor.

    Returns:
        A tensor with the same dtype as `x`.

    Example:

    >>> x = np.array([-0.5, -0.2, -0.1, 0.0, 0.3])
    >>> keras.ops.erfinv(x)
    array([-0.47694, -0.17914, -0.08886,  0. ,  0.27246], dtype=float32)
    )r   r   r;   r   r   r2   r   r   s    r   r   r     sF      QD!x%%a((!!!$A<<q!!r   c                   *     e Zd Z fdZd Zd Z xZS )Logdetc                 "    t         |           y r   )r   r   r   s    r   r   zLogdet.__init__   s    r   c                 @    t         j                  j                  |      S r   )r   r2   logdetrN   s     r   r5   zLogdet.call  r   r   c                 J    t        |j                  d d |j                        S )Nr   r   r   rN   s     r   r&   zLogdet.compute_output_spec  s    1773B<qww77r   )r(   r)   r*   r   r5   r&   r+   r,   s   @r   r   r     s    &8r   r   zkeras.ops.logdetc                     t        | f      rt               j                  |       S t        j                  j                  |       S )zComputes log of the determinant of a hermitian positive definite matrix.

    Args:
        x: Input matrix. It must 2D and square.

    Returns:
        The natural log of the determinant of matrix.
    )r   r   r;   r   r2   r   r   s    r   r   r   
  s6     QD!x%%a((<<q!!r   c                       e Zd Zd Zd Zy)ViewAsComplexc                     t        j                  |      }t        |j                        dk  s|j                  d   dk7  rt	        d      |d   d|d   z  z   S )Nr
   rG   rw   z=Input tensor's last dimension must be 2 (real and imaginary)..r                 ?.r
   )r   r   r   r   r   rN   s     r   r5   zViewAsComplex.call  sZ    %%a(qww<!qwwr{a/O  y2&	>))r   c                 6    t        |j                  d d d      S )NrG   	complex64r!   rV   rN   s     r   r&   z!ViewAsComplex.compute_output_spec"  s    "[AAr   Nr   r8   r   r   r   r     s    *Br   r   c                       e Zd Zd Zd Zy)
ViewAsRealc                     t        j                  |      }t         j                  j                  |      }t         j                  j	                  |      }t         j                  j                  ||fd      S )NrG   r`   )r   r   numpyr}   r~   stack)r   rK   	real_part	imag_parts       r   r5   zViewAsReal.call'  sY    %%a(MM&&q)	MM&&q)	}}""Iy#9"CCr   c                 6    t        |j                  dz   d      S )N)rw   float32r!   rV   rN   s     r   r&   zViewAsReal.compute_output_spec-  s    4yAAr   Nr   r8   r   r   r   r   &  s    DBr   r   zkeras.ops.view_as_complexc                 r   t        | f      rt               j                  |       S t        j                  |       } t        | j                        dk  s| j                  d   dk7  rt        d| j                         | d   }| d   }t        j                  |d      d	t        j                  |d      z  z   S )
a)  Converts a real tensor with shape `(..., 2)` to a complex tensor,
    where the last dimension represents the real and imaginary components
    of a complex tensor.

    Args:
        x: A real tensor with last dimension of size 2.

    Returns:
        A complex tensor with shape `x.shape[:-1]`.

    Example:

    ```
    >>> import numpy as np
    >>> from keras import ops

    >>> real_imag = np.array([[1.0, 2.0], [3.0, 4.0]])
    >>> complex_tensor = ops.view_as_complex(real_imag)
    >>> complex_tensor
    array([1.+2.j, 3.+4.j])
    ```
    r
   rG   rw   zMLast dimension of input must be size 2 (real and imaginary). Received shape: r   r   r   r   r   )	r   r   r;   r   r   r   r   r   castrK   r   r   s      r   view_as_complexr  1  s    0 QD!,,Q//!!!$A
177|a1772;!+ wwi)
 	
 &	I&	I<<	5W\\> 9  r   zkeras.ops.view_as_realc                 8   t        | f      rt               j                  |       S t        j                  |       } t        j
                  j                  |       }t        j
                  j                  |       }t        j
                  j                  ||fd      S )a  Converts a complex tensor to a real tensor with shape `(..., 2)`,
    where the last dimension represents the real and imaginary components.

    Args:
        x: A complex tensor.

    Returns:
        A real tensor where the last dimension contains the
        real and imaginary parts.

    Example:
    ```
    >>> import numpy as np
    >>> from keras import ops

    >>> complex_tensor = np.array([1 + 2j, 3 + 4j])
    >>> real = ops.view_as_real(complex_tensor)
    >>> real
    array([[1., 2.],
           [3., 4.]])
    ```
    rG   r   )	r   r   r;   r   r   r   r}   r~   r   r  s      r   view_as_realr  Z  sx    0 QD!|))!,,!!!$A""1%I""1%I==	95B??r   r'   )Tr   r   r   )4__doc__	keras.srcr   keras.src.api_exportr   keras.src.backendr   r   keras.src.ops.operationr   keras.src.ops.operation_utilsr   r   r   r.   r3   r>   r@   rC   rM   rR   r[   r^   re   rh   rq   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r8   r   r   <module>r     s0   :  - ) 2 - 6
.Ay A
! 
 %& 'D
! 
 %& 'B:9 :&  , !,:	CY 	C "#: $:6Q	 Q #$C %C4
y 
: +,O -OD+#) +#\ o 2+$9 +$\     6+%I +%\  ! !!8@9 @> $,  $,N&AI &AR  (- !(-V(
9 (
V KO5  5p<
I <
~   : !:z3I 3  ! !!*#) # o *&Y &  !" ""*8Y 8 !"#" $"
BI 
BB B )*% +%P &'@ (@r   