o
    2h'                     @   s   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lmZ dd	 Zd8ddZd8ddZd9ddZdd Zd8ddZd:ddZdd Zdd Zdd Zdd  Z	d!d" Zd;d#d$Zd;d%d&Z	d<d(d)Z		'	d=d*d+Zd,d- Zd.d/ Zd0d1 Zd2d3 Z d>d4d5Z!d6d7 Z"dS )?    N)standardize_dtype)dtypes)fft)fft2)convert_to_tensor)scipyc                 C   s   |d u rt |d }|dk}| | }|| }t|j}||d< |t jkr2t j||jdt j  }	nt j||jd}	|rE|	|	|| |	S t 
|}
||
 }||
 }|	|	|| |	S )N   r   dtype)npamaxlistshapemaximumonesr
   infzerosatargsort)datasegment_idsreduction_methodnum_segmentssortedvalid_indices
valid_datavalid_segment_ids
data_shaperesultsort_indicessorted_segment_idssorted_data r"   W/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/backend/numpy/math.py_segment_reduction_fn   s&   


r$   Fc                 C      t | |tj||S N)r$   r   addr   r   r   r   r"   r"   r#   segment_sum+      r)   c                 C   r%   r&   )r$   r   r   r(   r"   r"   r#   segment_max1   r*   r+   Tc                 C   s   |r,t j| dddd d df }t j| |dd}|dd |f }|dd |f }||fS t j| | ddd| d f }t j| |dd}||fS )Naxis.)r   r   take_along_axisargpartition)xkr   sorted_indicessorted_valuestop_k_valuestop_k_indicesr"   r"   r#   top_k7   s    r7   c                 C   sD   | d d d f } t ||d }tj|| dd}||k}tj|ddS )Nr   r,   r-   )r7   r   r/   any)targetspredictionsr2   topk_valuestargets_valuesmaskr"   r"   r#   in_top_kF   s
   r>   c                 C   s   t jj| ||dS )N)r.   keepdims)r   special	logsumexp)r1   r.   r?   r"   r"   r#   rA   N      rA   reducedc                 C   s&   |dvrt d| tjj| |dS )N>   rC   completez]`mode` argument value not supported. Expected one of {'reduced', 'complete'}. Received: mode=mode)
ValueErrorr   linalgqr)r1   rF   r"   r"   r#   rI   R   s   rI   c                 C   s   | j ^ }}t|}| j d d | j d ||  | |f }| jd d || jd  | jd f }tjjj| ||d} t| g || j dd  R S )Nr,   )r   strides)r   r   rJ   r   libstride_tricks
as_stridedreshape)r1   sequence_lengthsequence_stridebatch_shape_r   rJ   r"   r"   r#   extract_sequences\   s    rT   c                 C   s   t | ttfrt| dkrtd|  | \}}|j|jkr*td|j d|j t|jtj	r:t|jtj	sBtd|  d|d|  }|S )N   zLInput `x` should be a tuple of two tensors - real and imaginary.Received: x=zInput `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 = z, x[1].shape = zBAt least one tensor in input `x` is not of type float.Received: x=.y              ?)

isinstancetupler   lenrG   r   r   
issubdtyper
   floating)r1   realimagcomplex_inputr"   r"   r#   _get_complex_tensor_from_tuplek   s2   r_   c                 C       t | \}}t|t|fS r&   )jax_fftr   arrayr1   r\   r]   r"   r"   r#   r         r   c                 C   r`   r&   )jax_fft2r   rb   rc   r"   r"   r#   r      rd   r   c                 C   s(   t | }tj|}t|t|fS r&   )r_   r   r   ifft2r\   r]   )r1   r^   complex_outputr"   r"   r#   rf      s   rf   c                 C   s8   t jj| |ddd}t || jt || jfS )Nr,   backwardnr.   norm)r   r   rfftr\   astyper
   r]   )r1   
fft_lengthrg   r"   r"   r#   rl      s   rl   c                 C   s(   t | }tjj||ddd| d jS )Nr,   rh   ri   r   )r_   r   r   irfftrm   r
   )r1   rn   r^   r"   r"   r#   ro      s   ro   hannc              
   C   s  t | jdvrtd| j ||k rtd| d| t|tr-|dvr-td| t| } | j}|rTdd tt| j	D }|d	 |d	 f|d
< t
j| |dd} || d	 }|| | }	|d urt|trvttj||| jd}
nt|| jd}
t|
j	dks|
j	d
 |krtd|
j	 t
|
||	gg}
nt
j|| |	 | jd}
tjj| d|
|| |	 || |	 | |d ddd
 } | t
d|
 d	   } t
| dd
} t
| |t
| |fS )N>   float32float64zJInvalid input type. Expected `float32` or `float64`. Received: input type=zT`fft_length` must equal or larger than `sequence_length`. Received: sequence_length=z, fft_length=>   rp   hammingz]If a string is passed to `window`, it must be one of `"hann"`, `"hamming"`. Received: window=c                 S   s   g | ]}d qS ))r   r   r"   ).0rS   r"   r"   r#   
<listcomp>   s    zstft.<locals>.<listcomp>rU   r,   reflectrE   r	   r   PThe shape of `window` must be equal to [sequence_length].Received: window shape=      ?F)fswindownpersegnoverlapnfftboundarypaddedrK   )r   r
   	TypeErrorrG   rW   strr   rangerY   r   r   padr   signal
get_windowr   stftsqrtsumswapaxesr\   rm   r]   )r1   rP   rQ   rn   rz   center	ori_dtype	pad_widthl_padr_padwinr"   r"   r#   r      st   


	 r   c                 C   sn  t | } t| j}||| jd d   }|| d }	|| |	 }
|d ur\t|tr6ttj	
|||d}nt||d}t|jdksJ|jd |krRtd|j t||	|
gg}ntj||	 |
 |d}tj	j| d|||	 |
 ||	 |
 | |dddd		d } |d ur| |  n| | } |du rd
n|d }|d ur|| }n|du r|d  }n|}| d||f S )NrK   r   rU   r	   r,   rw   rx   F)ry   rz   r{   r|   r}   r~   	time_axis	freq_axisr   T.)r_   r   r\   r
   r   rW   r   r   r   r   r   rY   rG   r   r   istftr   )r1   rP   rQ   rn   lengthrz   r   r
   expected_output_lenr   r   r   startendr"   r"   r#   r      sP   	



r   c                 C   s   dt |  S )Nrx   )r   r   r1   r"   r"   r#   rsqrt!  s   r   c                 C      t tj| S r&   )r   rb   r   r@   erfr   r"   r"   r#   r   %  rB   r   c                 C   r   r&   )r   rb   r   r@   erfinvr   r"   r"   r#   r   )  rB   r   c                 C   s   t | } t |}tj| |S r&   )r   r   rH   solve)abr"   r"   r#   r   -  s   r   c                 C   sJ   t | } t| j}d|v s|dkrt| jd}tjj| |||d|S )Nintboolrq   )ordr.   r?   )	r   r   r
   r   result_typer   rH   rk   rm   )r1   r   r.   r?   r
   r"   r"   r#   rk   3  s   
rk   c                 C   s   ddl m} || d S )Nr   )slogdetr   )keras.src.backend.numpy.numpyr   )r1   r   r"   r"   r#   logdet=  s   r   )NF)T)rC   r&   )rp   T)Nrp   T)NNF)#numpyr   keras.src.backendr   keras.src.backend.commonr   keras.src.backend.jax.mathr   ra   r   re   keras.src.backend.numpy.corer   keras.src.utils.module_utilsr   r$   r)   r+   r7   r>   rA   rI   rT   r_   rf   rl   ro   r   r   r   r   r   r   rk   r   r"   r"   r"   r#   <module>   sB    
 






		
E
9

