o
    hE                     @  s  d dl mZ ddlmZmZ ddlmZ ddlmZ ddl	m
Z
 d dlmZ er6dd	lmZmZmZmZmZ d d
lmZ d dlZd dlZG dd deZG dd deZG dd deZG dd deZdddkddZdddld#d$Zdmd%d&Zd d'dnd)d*Zdod+d,Zdmd-d.Z dmd/d0Z!dpd1d2Z"dd3d4dqd8d9Z#drd;d<Z$dd=dsd@dAZ%dmdBdCZ&dpdDdEZ'dd=dsdFdGZ(dHdIdtdLdMZ)dudNdOZ*dPdQ Z+dpdRdSZ,dTdUdvdWdXZ-dwdZd[Z.dd\dxd_d`Z/d d'dndadbZ0dddldcddZ1ddddedydhdiZ2g djZ3dS )z    )annotations   )_floating_dtypes_numeric_dtypes)reshape)Array   )normalize_axis_tuple)TYPE_CHECKING)LiteralOptionalSequenceTupleUnion)
NamedTupleNc                   @     e Zd ZU ded< ded< dS )
EighResultr   eigenvalueseigenvectorsN__name__
__module____qualname____annotations__ r   r   Q/var/www/html/scripts/venv/lib/python3.10/site-packages/numpy/array_api/linalg.pyr         
 r   c                   @  r   )QRResultr   QRNr   r   r   r   r   r      r   r   c                   @  r   )SlogdetResultr   sign	logabsdetNr   r   r   r   r   r       r   r    c                   @  s&   e Zd ZU ded< ded< ded< dS )	SVDResultr   USVhNr   r   r   r   r   r#      s   
 r#   F)upperxr   r'   boolreturnc               C  s:   | j tvr	tdtj| j}|rt|j	S t|S )z
    Array API compatible wrapper for :py:func:`np.linalg.cholesky <numpy.linalg.cholesky>`.

    See its docstring for more information.
    z2Only floating-point dtypes are allowed in cholesky)
dtyper   	TypeErrornplinalgcholesky_arrayr   _newmT)r(   r'   Lr   r   r   r/   %   s   

r/   axisx1x2r6   intc               C  sr   | j tvs
|j tvrtd| j|jkrtd| jdkr!td| j| dkr,tdttj	| j
|j
|dS )zz
    Array API compatible wrapper for :py:func:`np.cross <numpy.cross>`.

    See its docstring for more information.
    z(Only numeric dtypes are allowed in crossz"x1 and x2 must have the same shaper   z/cross() requires arrays of dimension at least 1   zcross() dimension must equal 3r5   )r+   r   r,   shape
ValueErrorndimr   r1   r-   crossr0   )r7   r8   r6   r   r   r   r>   5   s   
r>   c                C  &   | j tvr	tdttj| jS )z
    Array API compatible wrapper for :py:func:`np.linalg.det <numpy.linalg.det>`.

    See its docstring for more information.
    z-Only floating-point dtypes are allowed in det)	r+   r   r,   r   r1   r-   r.   detr0   r(   r   r   r   r@   G   s   
r@   )offsetrB   c               C  s   t tj| j|dddS )z
    Array API compatible wrapper for :py:func:`np.diagonal <numpy.diagonal>`.

    See its docstring for more information.
    r4   rB   axis1axis2)r   r1   r-   diagonalr0   r(   rB   r   r   r   rG   T   s   rG   c                C  ,   | j tvr	tdtttjtj	| j
 S )z
    Array API compatible wrapper for :py:func:`np.linalg.eigh <numpy.linalg.eigh>`.

    See its docstring for more information.
    z.Only floating-point dtypes are allowed in eigh)r+   r   r,   r   mapr   r1   r-   r.   eighr0   rA   r   r   r   rK   _      
rK   c                C  r?   )z
    Array API compatible wrapper for :py:func:`np.linalg.eigvalsh <numpy.linalg.eigvalsh>`.

    See its docstring for more information.
    z2Only floating-point dtypes are allowed in eigvalsh)	r+   r   r,   r   r1   r-   r.   eigvalshr0   rA   r   r   r   rM   o      
rM   c                C  r?   )z
    Array API compatible wrapper for :py:func:`np.linalg.inv <numpy.linalg.inv>`.

    See its docstring for more information.
    z-Only floating-point dtypes are allowed in inv)	r+   r   r,   r   r1   r-   r.   invr0   rA   r   r   r   rO   |   rN   rO   c                C  s2   | j tvs
|j tvrtdtt| j|jS )z|
    Array API compatible wrapper for :py:func:`np.matmul <numpy.matmul>`.

    See its docstring for more information.
    z)Only numeric dtypes are allowed in matmul)r+   r   r,   r   r1   r-   matmulr0   r7   r8   r   r   r   rP      s   rP   fro)keepdimsordrS   rT   2Optional[Union[int, float, Literal['fro', 'nuc']]]c               C  s.   | j tvr	tdttjj| jd||dS )
    Array API compatible wrapper for :py:func:`np.linalg.norm <numpy.linalg.norm>`.

    See its docstring for more information.
    z5Only floating-point dtypes are allowed in matrix_norm)rC   r4   r6   rS   rT   )	r+   r   r,   r   r1   r-   r.   normr0   )r(   rS   rT   r   r   r   matrix_norm   s   
rY   nc                C  s(   | j tvr	tdttj| j|S )z
    Array API compatible wrapper for :py:func:`np.matrix_power <numpy.matrix_power>`.

    See its docstring for more information.
    zMOnly floating-point dtypes are allowed for the first argument of matrix_power)	r+   r   r,   r   r1   r-   r.   matrix_powerr0   )r(   rZ   r   r   r   r[      s   
r[   )rtolr\   Optional[Union[float, Array]]c               C  s   | j dk rtjdtjj| jdd}|du r0|jdddt| jd	d  t|j	j
 }nt|tr8|j}|jdddt|d
tjf  }ttj||kddS )z
    Array API compatible wrapper for :py:func:`np.matrix_rank <numpy.matrix_rank>`.

    See its docstring for more information.
    r   zA1-dimensional array given. Array must be at least two-dimensionalF
compute_uvNr4   T)r6   rS   rC   .r5   )r=   r-   r.   LinAlgErrorsvdr0   maxr;   finfor+   eps
isinstancer   asarraynewaxisr1   count_nonzero)r(   r\   r%   tolr   r   r   matrix_rank   s   
0
"rj   c                C  s(   | j dk r	tdtt| jddS )Nr   z5x must be at least 2-dimensional for matrix_transposer4   rC   )r=   r<   r   r1   r-   swapaxesr0   rA   r   r   r   matrix_transpose   s   
rl   c                C  sN   | j tvs
|j tvrtd| jdks|jdkrtdtt| j	|j	S )zz
    Array API compatible wrapper for :py:func:`np.outer <numpy.outer>`.

    See its docstring for more information.
    z(Only numeric dtypes are allowed in outerr   z/The input arrays to outer must be 1-dimensional)
r+   r   r,   r=   r<   r   r1   r-   outerr0   rQ   r   r   r   rm      s
   rm   c               C  sR   | j tvr	td|du rt| jdd t| j j }t	tj
j| j|dS )z
    Array API compatible wrapper for :py:func:`np.linalg.pinv <numpy.linalg.pinv>`.

    See its docstring for more information.
    z.Only floating-point dtypes are allowed in pinvNrC   )rcond)r+   r   r,   rb   r;   r-   rc   rd   r   r1   r.   pinvr0   )r(   r\   r   r   r   ro      s
   
 ro   reducedmoderr   Literal['reduced', 'complete']c               C  0   | j tvr	tdtttjtjj	| j
|d S )z
    Array API compatible wrapper for :py:func:`np.linalg.qr <numpy.linalg.qr>`.

    See its docstring for more information.
    z,Only floating-point dtypes are allowed in qrrq   )r+   r   r,   r   rJ   r   r1   r-   r.   qrr0   )r(   rr   r   r   r   ru         
ru   c                C  rI   )z
    Array API compatible wrapper for :py:func:`np.linalg.slogdet <numpy.linalg.slogdet>`.

    See its docstring for more information.
    z1Only floating-point dtypes are allowed in slogdet)r+   r   r,   r    rJ   r   r1   r-   r.   slogdetr0   rA   r   r   r   rw     rL   rw   c                 C  s   ddl m}m}m}m}m}m}m} ddlm	}	 || \} }
||  ||  ||\}}|| |\}}|j
dkr<|	j}n|	j}||rEdnd}||}|| |||d}||j|dd	S )
Nr   )
_makearray_assert_stacked_2d_assert_stacked_square_commonTypeisComplexTypeget_linalg_error_extobj_raise_linalgerror_singular)_umath_linalgr   zDD->Dzdd->d)	signatureextobjF)copy)linalg.linalgrx   ry   rz   r{   r|   r}   r~   r.   r   r=   solve1solveastype)abrx   ry   rz   r{   r|   r}   r~   r   _wraptresult_tgufuncr   r   rr   r   r   _solve$  s   $
r   c                C  s0   | j tvs
|j tvrtdtt| j|jS )z
    Array API compatible wrapper for :py:func:`np.linalg.solve <numpy.linalg.solve>`.

    See its docstring for more information.
    z/Only floating-point dtypes are allowed in solve)r+   r   r,   r   r1   r   r0   rQ   r   r   r   r   ?  s   r   Tfull_matricesr   c               C  rt   )z
    Array API compatible wrapper for :py:func:`np.linalg.svd <numpy.linalg.svd>`.

    See its docstring for more information.
    z-Only floating-point dtypes are allowed in svdr   )r+   r   r,   r#   rJ   r   r1   r-   r.   ra   r0   )r(   r   r   r   r   ra   L  rv   ra   Union[Array, Tuple[Array, ...]]c                C  s*   | j tvr	tdttjj| jddS )Nz1Only floating-point dtypes are allowed in svdvalsFr^   )	r+   r   r,   r   r1   r-   r.   ra   r0   rA   r   r   r   svdvals]  s   
r   axesr   /Union[int, Tuple[Sequence[int], Sequence[int]]]c               C  s6   | j tvs
|j tvrtdttj| j|j|dS )Nz,Only numeric dtypes are allowed in tensordotr   )r+   r   r,   r   r1   r-   	tensordotr0   )r7   r8   r   r   r   r   r   e  s   r   c            
   C  s2   | j tvr	tdtttj| j|dddS )zz
    Array API compatible wrapper for :py:func:`np.trace <numpy.trace>`.

    See its docstring for more information.
    z(Only numeric dtypes are allowed in tracerC   r4   rD   )	r+   r   r,   r   r1   r-   rf   tracer0   rH   r   r   r   r   n  s   
 r   c         	      C  s   | j tvs
|j tvrtdt| j|j}d|| j  t| j }d||j  t|j }|| || kr9tdt	| j
|j
\}}t||d}t||d}|dd d d f |d  }t|d S )Nz)Only numeric dtypes are allowed in vecdot)r   z6x1 and x2 must have the same size along the given axisr4   .).N).r   r   )r+   r   r,   rb   r=   tupler;   r<   r-   broadcast_arraysr0   moveaxisr   r1   )	r7   r8   r6   r=   x1_shapex2_shapex1_x2_resr   r   r   vecdot{  s   r   rW   %Optional[Union[int, Tuple[int, ...]]]Optional[Union[int, float]]c         
        s  | j tvr	td| j |du r   d}nBt|trWt|| jtfddt	 jD }|| }t
 |t
j fdd|D tdg fdd|D R  d}n|}tt
jj ||d	}|rt| j}t|du rvt	| jn|| j}|D ]}	d
||	< q}t|t|}|S )rV   z.Only floating-point dtypes are allowed in normNr   c                 3  s    | ]	}| vr|V  qd S )Nr   .0i)normalized_axisr   r   	<genexpr>  s    zvector_norm.<locals>.<genexpr>c                   s   g | ]} j | qS r   )r;   r   )r   r   r   
<listcomp>  s    zvector_norm.<locals>.<listcomp>)r+   )r6   rT   r   )r+   r   r,   r0   ravelre   r   r	   r=   ranger-   	transposer   prodr9   r   r1   r.   rX   listr;   )
r(   r6   rS   rT   _axisrestnewshaper   r;   r   r   )r   r   r   vector_norm  s.   

0

r   )r/   r>   r@   rG   rK   rM   rO   rP   rY   r[   rj   rl   rm   ro   ru   rw   r   ra   r   r   r   r   r   )r(   r   r'   r)   r*   r   )r7   r   r8   r   r6   r9   r*   r   )r(   r   r*   r   )r(   r   rB   r9   r*   r   )r(   r   r*   r   )r7   r   r8   r   r*   r   )r(   r   rS   r)   rT   rU   r*   r   )r(   r   rZ   r9   r*   r   )r(   r   r\   r]   r*   r   )r(   r   rr   rs   r*   r   )r(   r   r*   r    )r(   r   r   r)   r*   r#   )r(   r   r*   r   )r7   r   r8   r   r   r   r*   r   )
r(   r   r6   r   rS   r)   rT   r   r*   r   )4
__future__r   _dtypesr   r   _manipulation_functionsr   _array_objectr   core.numericr	   typingr
   _typingr   r   r   r   r   r   numpy.linalgnumpyr-   r   r   r    r#   r/   r>   r@   rG   rK   rM   rO   rP   rY   r[   rj   rl   rm   ro   ru   rw   r   r   ra   r   r   r   r   r   __all__r   r   r   r   <module>   sP    










	-