U
    b@                     @  s  d dl mZ ddlmZmZ ddlmZ d dlmZ erTd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ddddddZdddddddddZddddd Zd d!dddd"d#d$Zdd
dd%d&Zdddd'd(Zdddd)d*Zdddd+d,d-Zdd.d/ddd0dd1d2d3Zdddd4d5d6Z dd7dd8dd9d:d;Z!dddd<d=Z"dddd+d>d?Z#dd7dd8dd9d@dAZ$dBdCddDddEdFdGZ%ddddHdIZ&dJdK Z'dddd+dLdMZ(dNdOddddPdQdRZ)ddSddTdUZ*dVdWdddXddYdZd[Z+d d!dddd"d\d]Z,dddddddd^d_Z-dddVd`ddaddbddcdddeZ.ddd d$d&d(d*d-d3d6d;d=d?dAdGdIdMdRdUd[d]d_degZ/dS )f    )annotations   )_floating_dtypes_numeric_dtypes)Array)TYPE_CHECKING)LiteralOptionalSequenceTupleUnion)
NamedTupleNc                   @  s   e Zd ZU ded< ded< dS )
EighResultr   ZeigenvaluesZeigenvectorsN__name__
__module____qualname____annotations__ r   r   :/tmp/pip-unpacked-wheel-cxuynqjx/numpy/array_api/linalg.pyr      s   
r   c                   @  s   e Zd ZU ded< ded< dS )QRResultr   QRNr   r   r   r   r   r      s   
r   c                   @  s   e Zd ZU ded< ded< dS )SlogdetResultr   signZ	logabsdetNr   r   r   r   r   r      s   
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)upperr   bool)xr   returnc               C  s:   | j tkrtdtj| j}|r0t|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   _newZmT)r    r   Lr   r   r   r&   "   s    
r&   axisint)x1x2r,   r!   c               C  sr   | j tks|j tkrtd| j|jkr0td| jdkrBtd| j| dkrXt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 3r+   )r"   r   r#   shape
ValueErrorndimr   r(   r$   crossr'   r.   r/   r,   r   r   r   r4   2   s    
r4   )r    r!   c                C  s&   | j tk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   r(   r$   r%   detr'   r    r   r   r   r6   D   s    
r6   )offset)r    r8   r!   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.
    r*   r8   Zaxis1Zaxis2)r   r(   r$   diagonalr'   r    r8   r   r   r   r;   Q   s    r;   c                C  s,   | j tk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   r(   r$   r%   eighr'   r7   r   r   r   r>   \   s    
r>   c                C  s&   | j tkrtdttj| jS )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   r(   r$   r%   eigvalshr'   r7   r   r   r   r?   l   s    
r?   c                C  s&   | j tkrtdttj| jS )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   r(   r$   r%   invr'   r7   r   r   r   r@   y   s    
r@   )r.   r/   r!   c                C  s2   | j tks|j tk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   r(   r$   matmulr'   r.   r/   r   r   r   rA      s    rA   Zfro)keepdimsordz4Optional[Union[int, float, Literal[('fro', 'nuc')]]])r    rC   rD   r!   c               C  s.   | j tk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)r9   r*   r,   rC   rD   )	r"   r   r#   r   r(   r$   r%   normr'   )r    rC   rD   r   r   r   matrix_norm   s    
rH   )r    nr!   c                C  s(   | j tk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   r(   r$   r%   matrix_powerr'   )r    rI   r   r   r   rJ      s    
rJ   )rtolzOptional[Union[float, Array]])r    rK   r!   c               C  s   | j dk rtjdtjj| jdd}|dkr`|jdddt| jd	d  t|j	j
 }n2t|trp|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.
       zA1-dimensional array given. Array must be at least two-dimensionalFZ
compute_uvNr*   T)r,   rC   r9   .r+   )r3   r$   r%   ZLinAlgErrorsvdr'   maxr1   finfor"   eps
isinstancer   asarrayZnewaxisr(   Zcount_nonzero)r    rK   r   Ztolr   r   r   matrix_rank   s    
0
"rT   c                C  s(   | j dk rtdtt| jddS )NrL   z5x must be at least 2-dimensional for matrix_transposer*   r9   )r3   r2   r   r(   r$   Zswapaxesr'   r7   r   r   r   matrix_transpose   s    
rU   c                C  sN   | j tks|j tkrtd| jdks0|jdkr8t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#   r3   r2   r   r(   r$   outerr'   rB   r   r   r   rV      s
    rV   c               C  sR   | j tkrtd|dk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 pinvNr9   )Zrcond)r"   r   r#   rO   r1   r$   rP   rQ   r   r(   r%   pinvr'   )r    rK   r   r   r   rW      s
    
 rW   Zreducedmodez Literal[('reduced', 'complete')])r    rY   r!   c               C  s0   | j tk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 qrrX   )r"   r   r#   r   r=   r   r(   r$   r%   qrr'   )r    rY   r   r   r   rZ      s    
rZ   c                C  s,   | j tkrtdtttjtj	| j
 S )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   r=   r   r(   r$   r%   slogdetr'   r7   r   r   r   r[   	  s    
r[   c                 C  s   ddl m}m}m}m}m}m}m} ddlm	}	 || \} }
||  ||  ||\}}|| |\}}|j
dkrx|	j}n|	j}||rdnd}||}|| |||d}||j|dd	S )
NrL   )
_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)Zlinalg.linalgr\   r]   r^   r_   r`   ra   rb   r%   rc   r3   Zsolve1solveZastype)abr\   r]   r^   r_   r`   ra   rb   rc   _wraptZresult_tZgufuncrd   re   rr   r   r   _solve!  s    $
rn   c                C  s0   | j tks|j tk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   r(   rn   r'   rB   r   r   r   rg   <  s    rg   Tfull_matrices)r    rp   r!   c               C  s0   | j tkrtdtttjtjj	| j
|d S )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 svdro   )r"   r   r#   r   r=   r   r(   r$   r%   rN   r'   )r    rp   r   r   r   rN   I  s    
rN   zUnion[Array, Tuple[Array, ...]]c                C  s*   | j tkrtdttjj| jddS )Nz1Only floating-point dtypes are allowed in svdvalsFrM   )	r"   r   r#   r   r(   r$   r%   rN   r'   r7   r   r   r   svdvalsZ  s    
rq   rL   axesz/Union[int, Tuple[Sequence[int], Sequence[int]]])r.   r/   rs   r!   c               C  s6   | j tks|j tkrtdttj| j|j|dS )Nz,Only numeric dtypes are allowed in tensordotrr   )r"   r   r#   r   r(   r$   	tensordotr'   )r.   r/   rs   r   r   r   rt   b  s    rt   c            
   C  s2   | j tk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 tracer9   r*   r:   )	r"   r   r#   r   r(   r$   rS   tracer'   r<   r   r   r   ru   k  s    
ru   c               C  s2   | j tks|j tkrtdt| ||f|ffdS )Nz)Only numeric dtypes are allowed in vecdotrr   )r"   r   r#   rt   r5   r   r   r   vecdotx  s    rv   rF   z%Optional[Union[int, Tuple[int, ...]]]zOptional[Union[int, float]])r    r,   rC   rD   r!   c                 s   | j tkrtd| j dkr.   dnlttrtfddt jD }| }t	
 |t	 fddD f fdd|D  dtt	jj ||dS )	rE   z.Only floating-point dtypes are allowed in normNr   c                 3  s   | ]}| kr|V  qd S )Nr   .0ir+   r   r   	<genexpr>  s      zvector_norm.<locals>.<genexpr>c                   s   g | ]} j | qS r   )r1   rw   )rh   r   r   
<listcomp>  s     zvector_norm.<locals>.<listcomp>rF   )r"   r   r#   r'   flattenrR   tupleranger3   r$   Z	transposeZreshapeprodr   r(   r%   rG   )r    r,   rC   rD   restZnewshaper   )rh   r,   r   vector_norm  s    

:r   )0
__future__r   Z_dtypesr   r   Z_array_objectr   typingr   _typingr   r	   r
   r   r   r   Znumpy.linalgZnumpyr$   r   r   r   r   r&   r4   r6   r;   r>   r?   r@   rA   rH   rJ   rT   rU   rV   rW   rZ   r[   rn   rg   rN   rq   rt   ru   rv   r   __all__r   r   r   r   <module>   sJ   	 