o
    ʩZhG                     @  s2  d dl mZ ddlmZmZmZmZmZ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 erFdd
lmZ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doddZ"dddpd$d%Z#dqd&d'Z$d d(drd*d+Z%dsd,d-Z&dqd.d/Z'dqd0d1Z(dtd2d3Z)dd4d5dud9d:Z*dvd<d=Z+dd>dwdAdBZ,dqdCdDZ-dtdEdFZ.dd>dwdGdHZ/dIdJdxdMdNZ0dydOdPZ1dQdR Z2dtdSdTZ3dUdVdzdXdYZ4d{d[d\Z5dd]d|d`daZ6d ddbd}dedfZ7dddpdgdhZ8ddddid~dldmZ9g dnZ:dS )    )annotations   )_floating_dtypes_numeric_dtypesfloat32float64	complex64
complex128)reshape)conj)Array   )normalize_axis_tuple)TYPE_CHECKING)LiteralOptionalSequenceTupleUnionDtype)
NamedTupleNc                   @     e Zd ZU ded< ded< dS )
EighResultr   ZeigenvaluesZeigenvectorsN__name__
__module____qualname____annotations__ r   r   M/var/www/html/lang_env/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   signZ	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SZVhNr   r   r   r   r   r&   &   s   
 r&   F)upperxr   r)   boolreturnc               C  sT   | j tvr	tdtj| j}|r%t|j	}|j t
tfv r#t|}|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   _newZmTr   r	   r   )r*   r)   Lr'   r   r   r   r1   -   s   

r1   axisx1x2r7   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 3r6   )r-   r   r.   shape
ValueErrorndimr   r3   r/   crossr2   )r8   r9   r7   r   r   r   r?   @   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   r3   r/   r0   detr2   r*   r   r   r   rA   R   s   
rA   )offsetrC   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.
    r5   )rC   axis1axis2)r   r3   r/   diagonalr2   )r*   rC   r   r   r   rG   _   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   r3   r/   r0   eighr2   rB   r   r   r   rJ   j      
rJ   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   r3   r/   r0   eigvalshr2   rB   r   r   r   rL   z      
rL   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   r3   r/   r0   invr2   rB   r   r   r   rN      rM   rN   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   r3   r/   matmulr2   r8   r9   r   r   r   rO      s   rO   Zfro)keepdimsordrQ   rR   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)rD   r5   r7   rQ   rR   )	r-   r   r.   r   r3   r/   r0   normr2   )r*   rQ   rR   r   r   r   matrix_norm   s   
rW   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   r3   r/   r0   matrix_powerr2   )r*   rX   r   r   r   rY      s   
rY   )rtolrZ   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Z
compute_uvNr5   T)r7   rQ   rD   .r6   )r>   r/   r0   ZLinAlgErrorsvdr2   maxr<   finfor-   eps
isinstancer   asarrayZnewaxisr3   Zcount_nonzero)r*   rZ   r(   Ztolr   r   r   matrix_rank   s   
0
"rc   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_transposer5   rD   )r>   r=   r   r3   r/   Zswapaxesr2   rB   r   r   r   matrix_transpose   s   
rd   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   r3   r/   outerr2   rP   r   r   r   re      s
   re   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 pinvNrD   )Zrcond)r-   r   r.   r^   r<   r/   r_   r`   r   r3   r0   pinvr2   )r*   rZ   r   r   r   rf      s
   
 rf   Zreducedmoderh   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 qrrg   )r-   r   r.   r!   rI   r   r3   r/   r0   qrr2   )r*   rh   r   r   r   rk        
rk   c                C  rH   )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$   rI   r   r3   r/   r0   slogdetr2   rB   r   r   r   rm     rK   rm   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}tj|ddddd	 || ||d
}W d    n1 scw   Y  ||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callignore)rv   invalidZoverdivideZunder)	signatureF)copy)Zlinalg.linalgrn   ro   rp   rq   rr   rs   rt   r0   ru   r>   Zsolve1solver/   ZerrstateZastype)abrn   ro   rp   rq   rr   rs   rt   ru   _wraptZresult_tZgufuncrz   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   r3   r   r2   rP   r   r   r   r|   K  s   r|   Tfull_matricesr   c               C  rj   )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&   rI   r   r3   r/   r0   r]   r2   )r*   r   r   r   r   r]   X  rl   r]   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   r3   r/   r0   r]   r2   rB   r   r   r   svdvalsi  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   r3   r/   	tensordotr2   )r8   r9   r   r   r   r   r   q  s   r   )rC   r-   r-   Optional[Dtype]c               C  sZ   | j tvr	td|du r| j tkrt}n| j tkrt}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 traceNrD   r5   )rC   rE   rF   r-   )r-   r   r.   r   r   r   r	   r   r3   r/   rb   tracer2   )r*   rC   r-   r   r   r   r   z  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 axisr5   .).N).r   r   )r-   r   r.   r^   r>   tupler<   r=   r/   Zbroadcast_arraysr2   Zmoveaxisr   r3   )	r8   r9   r7   r>   Zx1_shapeZx2_shapeZx1_Zx2_resr   r   r   vecdot  s   r   rU   %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 )rT   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-   )r7   rR   r   )r-   r   r.   r2   Zravelra   r   r   r>   ranger/   Z	transposer
   prodr:   r   r3   r0   rV   listr<   )
r*   r7   rQ   rR   Z_axisrestZnewshaper   r<   r   r   )r}   r   r   vector_norm  s.   

0

r   )r1   r?   rA   rG   rJ   rL   rN   rO   rW   rY   rc   rd   re   rf   rk   rm   r|   r]   r   r   r   r   r   )r*   r   r)   r+   r,   r   )r8   r   r9   r   r7   r:   r,   r   )r*   r   r,   r   )r*   r   rC   r:   r,   r   )r*   r   r,   r   )r8   r   r9   r   r,   r   )r*   r   rQ   r+   rR   rS   r,   r   )r*   r   rX   r:   r,   r   )r*   r   rZ   r[   r,   r   )r*   r   rh   ri   r,   r!   )r*   r   r,   r$   )r*   r   r   r+   r,   r&   )r*   r   r,   r   )r8   r   r9   r   r   r   r,   r   )r*   r   rC   r:   r-   r   r,   r   )
r*   r   r7   r   rQ   r+   rR   r   r,   r   );
__future__r   Z_dtypesr   r   r   r   r   r	   Z_manipulation_functionsr
   Z_elementwise_functionsr   Z_array_objectr   Zcore.numericr   typingr   Z_typingr   r   r   r   r   r   r   Znumpy.linalgnumpyr/   r   r!   r$   r&   r1   r?   rA   rG   rJ   rL   rN   rO   rW   rY   rc   rd   re   rf   rk   rm   r   r|   r]   r   r   r   r   r   __all__r   r   r   r   <module>   sR      










	-