
    `i(                         d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlZd Z ej        dddd           Z	 ej        d	d
dd           Z
ddZd ZddZd ZddZdS )    N)_core)_decomposition)_utilc                 x    t          j        | ||fd          } |t          j        |d          d          }|S )N)F
compute_uvr   axis)cupymoveaxisr   svd)xrow_axiscol_axisopyresults         f/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupy/linalg/_norms.py_multi_svd_normr      sC    a(H-x88AR"1777bAAAFM    
_norm_ord2)z?->lzb->lzB->Lzh->lzH->Lzi->lzI->Lzl->lzL->Lzq->qzQ->Q)ze->e)NNNfloatzf->fzd->d)z	in0 * in0a + bout0 = sqrt(type_out0_raw(a))N_norm_ord2_complex)zF->fzD->d)z1in0.real() * in0.real() + in0.imag() * in0.imag()r   r   NFc                    t          | j        j        t          j                  s|                     t                    } || j        }||dk    r|dk    s
|dk    r|dv r| j        j        dk    rMt          | 
                                          }||z  }t          j        |                                          }n)t          j        | | z                                            }|r|                    d|z            }|S | j        }|t          t!          |                    }nFt#          |t                    s1	 t%          |          }n# t&          $ r t)          d          w xY w|f}t+          |          dk    r|t          j        k    r$t          |                               ||          S |t          j         k    r$t          |                               ||          S |d	k    r8| d	k                        | j        j                                      ||          S |dk    r$t          |                               ||          S ||dk    r4| j        j        dk    rt5          | ||          S t7          | ||          S 	 t          |           n# t(          $ r t9          d
          w xY wt          |           }||z  }|                    ||          }|t          j        ||j                  z  }|S t+          |          dk    r|\  }	}
|	d	k     r|	|z  }	|
d	k     r|
|z  }
d	|	cxk    r|k     rn nd	|
cxk    r|k     sn t9          d|d| j                  |	|
k    rt9          d          |dk    r4t?          j         t          j!        d	          }tE          | |	|
|          }n|dk    r4t?          j         t          j!        d          }tE          | |	|
|          }n|dk    rD|
|	k    r|
dz  }
t          |                               |	                              |
          }n[|t          j        k    rD|	|
k    r|	dz  }	t          |                               |
                              |	          }n|dk    rC|
|	k    r|
dz  }
t          |                               |	                              |
          }n|t          j         k    rC|	|
k    r|	dz  }	t          |                               |
                              |	          }nj|dv r4| j        j        dk    rt5          | |          }nDt7          | |          }n2|dk    rtE          | |	|
t          j                  }nt9          d          |r?tG          | j                  }d||d	         <   d||d         <   |                    |          }|S t9          d          )a  Returns one of matrix norms specified by ``ord`` parameter.

    See numpy.linalg.norm for more detail.

    Args:
        x (cupy.ndarray): Array to take norm. If ``axis`` is None,
            ``x`` must be 1-D or 2-D.
        ord (non-zero int, inf, -inf, 'fro'): Norm type.
        axis (int, 2-tuple of ints, None): 1-D or 2-D norm is cumputed over
            ``axis``.
        keepdims (bool): If this is set ``True``, the axes which are normed
            over are left.

    Returns:
        cupy.ndarray

    N      )ffroc)r   z6'axis' must be None, an integer or a tuple of integersr   keepdimsr   zInvalid norm order for vectors.)dtypezInvalid axis z for an array with shape zDuplicate axes given.)indicesr   r   r   )Nr"   r!   nucz Invalid norm order for matrices.z&Improper number of dimensions to norm.)$
issubclassr&   typenumpyinexactastyper   ndimkindabsravelr   sqrtsumreshapetuplerange
isinstanceint	Exception	TypeErrorleninfmaxminrealr   r   
ValueError
reciprocalshape	functoolspartialtaker   list)r   ordr   r%   r.   sretndabsxr   r   op_maxop_min	ret_shapes                 r   normrO      s"   $ aglEM22 HHUOO |vKDAII#((sl22w|s""		NNQi((iQ.. /kk$+..J 
B|U2YYe$$ 	Lt99DD 	L 	L 	LJL L L	L w
4yyA~~%)q66::4(:;;;UYJq66::4(:;;;AXX F??16<0044H 5 . . .AXXq66::4(:;;;[C1HHw|s"")!$JJJJadX>>>>Dc



 D D D !BCCCD q66DSLD((x(88CDOCsy9999CJ	Ta!(a<<NHa<<NHX"""""""""qH'9'9'9'9r'9'9'9'9*"ddAGG- . . .x4555!88&ty!<<<F!!Xx@@CCBYY&ty"===F!!Xx@@CCAXX(""Aa&&**(*++//X/>>CCEI(""Aa&&**(*++//X/>>CCBYY(""Aa&&**(*++//X/>>CCUYJ(""Aa&&**(*++//X/>>CC&&&w|s""(666 ...E\\!!XxBBCC?@@@ 	)QWI!"Id1g!"Id1g++i((C
ABBBs   >E E(J   J:c                 T    t          |           \  }}|t          j        |          z  S )a  Returns the determinant of an array.

    Args:
        a (cupy.ndarray): The input matrix with dimension ``(..., N, N)``.

    Returns:
        cupy.ndarray: Determinant of ``a``. Its shape is ``a.shape[:-2]``.

    .. seealso:: :func:`numpy.linalg.det`
    )slogdetr   exp)asignlogdets      r   detrV      s(     1::LD&$(6""""r   c                    | j         dk     r0| dk                                                        t                    S t	          j        | d          }|S|                    dd          t          | j        d	d                   z  t          j	        |j
                  j        z  }||k                        dt          j        
          S )aB  Return matrix rank of array using SVD method

    Args:
        M (cupy.ndarray): Input array. Its `ndim` must be less than or equal to
            2.
        tol (None or float): Threshold of singular value of `M`.
            When `tol` is `None`, and `eps` is the epsilon value for datatype
            of `M`, then `tol` is set to `S.max() * max(M.shape) * eps`,
            where `S` is the singular value of `M`.
            It obeys :func:`numpy.linalg.matrix_rank`.

    Returns:
        cupy.ndarray: Rank of `M`.

    .. seealso:: :func:`numpy.linalg.matrix_rank`
    r    r   Fr	   Nr   Tr$   r   )r   r&   )r.   anyr-   r8   r   r   r=   rB   r+   finfor&   epsr3   intp)MtolSs      r   matrix_rankr_      s    " 	vzzQ||~~$$S)))1///A
{uu"tu,,s17233</@/@@{17##'(G==b
=333r   c                    t          j        |            t          j        |            t          j        |           \  }}t	          j        |j                                                  }| j        }|dd         }|d         }| j	        dk    r.t          j        ||          }t          j        ||          }||fS t          j        | |          \  }	}
}t          j        |	dd          }t          j        t          j        |                                        d          }t          j        |
t          j        d|dz             k    d          }|j        dk    r|t          j        |dk     d          z  }|d	z  dz  dz   }|j        d
k    r.|t          j        |t          j        |          z  d          z  }|                    |          }|                    |d          }|dk    }t          j        ||                    d          |                              |          t          j        ||                    d          |                              |          fS )a  Returns sign and logarithm of the determinant of an array.

    It calculates the natural logarithm of the determinant of a given value.

    Args:
        a (cupy.ndarray): The input matrix with dimension ``(..., N, N)``.

    Returns:
        tuple of :class:`~cupy.ndarray`:
            It returns a tuple ``(sign, logdet)``. ``sign`` represents each
            sign of the determinant as a real number ``0``, ``1`` or ``-1``.
            'logdet' represents the natural logarithm of the absolute of the
            determinant.
            If the determinant is zero, ``sign`` will be ``0`` and ``logdet``
            will be ``-inf``.
            The shapes of both ``sign`` and ``logdet`` are equal to
            ``a.shape[:-2]``.

    .. warning::
        This function calls one or more cuSOLVER routine(s) which may yield
        invalid results if input conditions are not met.
        To detect these invalid results, you can set the `linalg`
        configuration to a value that is not `ignore` in
        :func:`cupyx.errstate` or :func:`cupyx.seterr`.

    .. warning::
        To produce the same results as :func:`numpy.linalg.slogdet` for
        singular inputs, set the `linalg` configuration to `raise`.

    .. seealso:: :func:`numpy.linalg.slogdet`
    Nr   r   r   )axis1axis2r   r   r!   r    r#   F)copyz-inf)r   _assert_stacked_2d_assert_stacked_squarelinalg_common_typer+   r&   charlowerrB   sizer   oneszerosr   
_lu_factordiagonallogr0   r3   count_nonzeroaranger/   prodr-   wherer*   r4   )rS   r&   
sign_dtypelogdet_dtypea_shaperB   nrT   rU   luipivdev_infodiagnon_zerosingulars                  r   rQ   rQ      s6   @ 
Q	 ###033E:;z446677LgGCRCLEAv{{y
++E<00V|'21e<<Bh =2R000DXdhtnn%%))r)22F !$$+aQ*?*?"?bIIIHzSD&taxb9999 qLB"DzSditx~~ 5B????;;uD]]<e]44F!|H
8Z__Q//66>>uEE
8\..v66??GGNN r   r   c                 4    |                      |||||          S )a  Returns the sum along the diagonals of an array.

    It computes the sum along the diagonals at ``axis1`` and ``axis2``.

    Args:
        a (cupy.ndarray): Array to take trace.
        offset (int): Index of diagonals. Zero indicates the main diagonal, a
            positive value an upper diagonal, and a negative value a lower
            diagonal.
        axis1 (int): The first axis along which the trace is taken.
        axis2 (int): The second axis along which the trace is taken.
        dtype: Data type specifier of the output.
        out (cupy.ndarray): Output array.

    Returns:
        cupy.ndarray: The trace of ``a`` along axes ``(axis1, axis2)``.

    .. seealso:: :func:`numpy.trace`

    )trace)rS   offsetra   rb   r&   outs         r   r~   r~     s    , 7765%444r   )NNF)N)r   r   r   NN)r+   r   r   cupy.linalgr   r   rC   r   create_reduction_funcr   r   rO   rV   r_   rQ   r~    r   r   <module>r      s&           & & & & & &             )U( B1F F
 1U0567	9 9 ~C ~C ~C ~CH# # #4 4 4 44K K K\5 5 5 5 5 5r   