o
    X۷iA                     @  s   d dl mZ d dlZd dlZd dlmZ d dlmZ G dd dZG dd deZ	G d	d
 d
eZ
G dd deZddd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G dd deZG dd deZG dd deZdd ZdS )     )annotationsN)sparse)_utilc                      s   e Zd ZdZdZ fddZdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 ZeeZd1d2 ZeeZd3d4 Z d5d6 Z!  Z"S )7LinearOperatora  LinearOperator(shape, matvec, rmatvec=None, matmat=None, dtype=None, rmatmat=None)

    Common interface for performing matrix vector products

    To construct a concrete LinearOperator, either pass appropriate callables
    to the constructor of this class, or subclass it.

    Args:
        shape (tuple):  Matrix dimensions ``(M, N)``.
        matvec (callable f(v)):  Returns returns ``A * v``.
        rmatvec (callable f(v)):  Returns ``A^H * v``, where ``A^H`` is the
                                  conjugate transpose of ``A``.
        matmat (callable f(V)):  Returns ``A * V``, where ``V`` is a dense
                                 matrix with dimensions ``(N, K)``.
        dtype (dtype):  Data type of the matrix.
        rmatmat (callable f(V)):  Returns ``A^H * V``, where ``V`` is a dense
                                  matrix with dimensions ``(M, K)``.

    .. seealso:: :class:`scipy.sparse.linalg.LinearOperator`
       c                   sT   | t u r
t tS t | }t|jt jkr(t|jt jkr(tjdt	dd |S )NzMLinearOperator subclass should implement at least one of _matvec and _matmat.r   )category
stacklevel)
r   super__new___CustomLinearOperatortype_matvec_matmatwarningswarnRuntimeWarning)clsargskwargsobj	__class__ Z/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupyx/scipy/sparse/linalg/_interface.pyr
   $   s   zLinearOperator.__new__c                 C  sB   |dur	t |}t|}t|std|f || _|| _dS )z0Initialize this :class:`LinearOperator`
        Nzinvalid shape %r (must be 2-d))cupydtypetupler   isshape
ValueErrorshape)selfr   r   r   r   r   __init__3   s   


zLinearOperator.__init__c                 C  s0   | j du rt| jd }| |j | _ dS dS )zECalled from subclasses at the end of the `__init__` routine.
        N)r   r   zerosr   matvec)r    vr   r   r   _init_dtype@   s   
zLinearOperator._init_dtypec                   s   t  fdd|jD S )z6Default matrix-matrix multiplication handler.
        c                      g | ]}  |d dqS r"      )r$   reshape.0colr    r   r   
<listcomp>K   s    z*LinearOperator._matmat.<locals>.<listcomp>)r   hstackTr    Xr   r.   r   r   G   s   zLinearOperator._matmatc                 C  s   |  |ddS )z6Default matrix-vector multiplication handler.
        r"   r)   )matmatr*   r    xr   r   r   r   M   s   zLinearOperator._matvecc                 C  sp   | j \}}|j |fkr|j |dfkrtd| |}|jdkr'||}|S |jdkr4||d}|S td)z&Matrix-vector multiplication.
        r)   dimension mismatchr   z/invalid shape returned by user-defined matvec())r   r   r   ndimr*   r    r6   MNyr   r   r   r$   R   s   




zLinearOperator.matvecc                 C  sp   | j \}}|j |fkr|j |dfkrtd| |}|jdkr'||}|S |jdkr4||d}|S td)z.Adjoint matrix-vector multiplication.
        r)   r7   r   z0invalid shape returned by user-defined rmatvec())r   r   _rmatvecr8   r*   r9   r   r   r   rmatvecf   s   




zLinearOperator.rmatvecc                 C  s    t | jtjkr
t| j|S )z?Default implementation of _rmatvec; defers to adjoint.
        )r   _adjointr   NotImplementedErrorHr$   r5   r   r   r   r=   {   s   zLinearOperator._rmatvecc                 C  sN   |j dkrtd|j  |jd | jd kr td| j|jf | |}|S )z&Matrix-matrix multiplication.
        r   (expected 2-d ndarray or matrix, not %d-dr   r)   dimension mismatch: %r, %r)r8   r   r   r   r    r3   Yr   r   r   r4      s   


zLinearOperator.matmatc                 C  sN   |j dkrtd|j  |jd | jd kr td| j|jf | |}|S )z.Adjoint matrix-matrix multiplication.
        r   rB   r   rC   )r8   r   r   _rmatmatrD   r   r   r   rmatmat   s   


zLinearOperator.rmatmatc                   s6   t  jtjkrt fdd|jD S  j|S )z@Default implementation of _rmatmat defers to rmatvec or adjoint.c                   r'   r(   )r>   r*   r+   r.   r   r   r/      s    z+LinearOperator._rmatmat.<locals>.<listcomp>)r   r?   r   r   r0   r1   rA   r4   r2   r   r.   r   rF      s
   zLinearOperator._rmatmatc                 C  s   | | S Nr   r5   r   r   r   __call__   s   zLinearOperator.__call__c                 C  
   |  |S rH   )dotr5   r   r   r   __mul__      
zLinearOperator.__mul__c                 C  st   t |tr
t| |S t|rt| |S |jdks%|jdkr*|jd dkr*| |S |jdkr4| 	|S t
d| )z7Matrix-matrix or matrix-vector multiplication.
        r)   r   z!expected 1-d or 2-d array, got %r)
isinstancer   _ProductLinearOperatorr   isscalar_ScaledLinearOperatorr8   r   r$   r4   r   r5   r   r   r   rK      s   



"


zLinearOperator.dotc                 C     t |r	td| |S Nz0Scalar operands are not allowed, use '*' instead)r   rP   r   rL   r    otherr   r   r   
__matmul__      

zLinearOperator.__matmul__c                 C  rR   rS   )r   rP   r   __rmul__rT   r   r   r   __rmatmul__   rW   zLinearOperator.__rmatmul__c                 C     t |r
t| |S tS rH   )r   rP   rQ   NotImplementedr5   r   r   r   rX         

zLinearOperator.__rmul__c                 C  rZ   rH   )r   rP   _PowerLinearOperatorr[   )r    pr   r   r   __pow__   r\   zLinearOperator.__pow__c                 C  s   t |tr
t| |S tS rH   )rN   r   _SumLinearOperatorr[   r5   r   r   r   __add__   r\   zLinearOperator.__add__c                 C  s
   t | dS )Nr"   )rQ   r.   r   r   r   __neg__   rM   zLinearOperator.__neg__c                 C  s   |  | S rH   )ra   r5   r   r   r   __sub__      zLinearOperator.__sub__c                 C  s<   | j \}}| jd u rd}ndt| j }d||| jj|f S )Nzunspecified dtypezdtype=z<%dx%d %s with %s>)r   r   strr   __name__)r    r:   r;   dtr   r   r   __repr__   s
   

zLinearOperator.__repr__c                 C     |   S )zHermitian adjoint.
        )r?   r.   r   r   r   adjoint      zLinearOperator.adjointc                 C  ri   )z(Transpose this linear operator.
        )
_transposer.   r   r   r   	transpose   rk   zLinearOperator.transposec                 C     t | S )z6Default implementation of _adjoint; defers to rmatvec.)_AdjointLinearOperatorr.   r   r   r   r?         zLinearOperator._adjointc                 C  rn   )z? Default implementation of _transpose; defers to rmatvec + conj)_TransposedLinearOperatorr.   r   r   r   rl      rp   zLinearOperator._transpose)#rf   
__module____qualname____doc__r8   r
   r!   r&   r   r   r$   r>   r=   r4   rG   rF   rI   rL   rK   rV   rY   rX   r_   ra   rb   rc   rh   rj   propertyrA   rm   r1   r?   rl   __classcell__r   r   r   r   r      s>    		r   c                      sV   e Zd ZdZ		d fdd	Z fddZdd Zd	d
 Z fddZdd Z	  Z
S )r   z>Linear operator defined in terms of user-specified operations.Nc                   s8   t  || d| _|| _|| _|| _|| _|   d S )Nr   )r	   r!   r   "_CustomLinearOperator__matvec_impl#_CustomLinearOperator__rmatvec_impl#_CustomLinearOperator__rmatmat_impl"_CustomLinearOperator__matmat_implr&   )r    r   r$   r>   r4   r   rG   r   r   r   r!     s   z_CustomLinearOperator.__init__c                       | j d ur
|  |S t |S rH   )rz   r	   r   r2   r   r   r   r        

z_CustomLinearOperator._matmatc                 C  rJ   rH   )rw   r5   r   r   r   r     rM   z_CustomLinearOperator._matvecc                 C  s    | j }|d u rtd|  |S )Nzrmatvec is not defined)rx   r@   )r    r6   funcr   r   r   r=     s   
z_CustomLinearOperator._rmatvecc                   r{   rH   )ry   r	   rF   r2   r   r   r   rF   $  r|   z_CustomLinearOperator._rmatmatc                 C  s.   t | jd | jd f| j| j| j| j| jdS )Nr)   r   )r   r$   r>   r4   rG   r   )r   r   rx   rw   ry   rz   r   r.   r   r   r   r?   *  s   z_CustomLinearOperator._adjoint)NNNN)rf   rr   rs   rt   r!   r   r   r=   rF   r?   rv   r   r   r   r   r     s    r   c                      @   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Z  Z	S )ro   z$Adjoint of arbitrary Linear Operatorc                   8   |j d |j d f}t j|j|d || _|f| _d S Nr)   r   )r   r   r   r	   r!   r   Ar   r    r   r   r   r   r   r!   6     z_AdjointLinearOperator.__init__c                 C     | j |S rH   )r   r=   r5   r   r   r   r   =  rd   z_AdjointLinearOperator._matvecc                 C  r   rH   )r   r   r5   r   r   r   r=   @  rd   z_AdjointLinearOperator._rmatvecc                 C  r   rH   )r   rF   r5   r   r   r   r   C  rd   z_AdjointLinearOperator._matmatc                 C  r   rH   )r   r   r5   r   r   r   rF   F  rd   z_AdjointLinearOperator._rmatmat
rf   rr   rs   rt   r!   r   r=   r   rF   rv   r   r   r   r   ro   3  s    ro   c                      r~   )rq   z*Transposition of arbitrary Linear Operatorc                   r   r   r   r   r   r   r   r!   M  r   z"_TransposedLinearOperator.__init__c                 C     t | jt |S rH   )r   conjr   r=   r5   r   r   r   r   T     z!_TransposedLinearOperator._matvecc                 C  r   rH   )r   r   r   r   r5   r   r   r   r=   X     z"_TransposedLinearOperator._rmatvecc                 C  r   rH   )r   r   r   rF   r5   r   r   r   r   [  r   z!_TransposedLinearOperator._matmatc                 C  r   rH   )r   r   r   r   r5   r   r   r   rF   _  r   z"_TransposedLinearOperator._rmatmatr   r   r   r   r   rq   J  s    rq   c                 C  s>   |d u rg }| D ]}|d urt |dr||j qtj| S )Nr   )hasattrappendr   r   result_type)	operatorsdtypesr   r   r   r   
_get_dtypec  s   
r   c                      D   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Z  Z	S )r`   c                   s^   t |tr
t |tstd|j|jkrtd||f ||f| _t t||g|j d S )N)both operands have to be a LinearOperatorz$cannot add %r and %r: shape mismatch)rN   r   r   r   r   r	   r!   r   r    r   Br   r   r   r!   m  s   

z_SumLinearOperator.__init__c                 C      | j d || j d | S Nr   r)   r   r$   r5   r   r   r   r   w      z_SumLinearOperator._matvecc                 C  r   r   r   r>   r5   r   r   r   r=   z  r   z_SumLinearOperator._rmatvecc                 C  r   r   r   rG   r5   r   r   r   rF   }  r   z_SumLinearOperator._rmatmatc                 C  r   r   r   r4   r5   r   r   r   r     r   z_SumLinearOperator._matmatc                 C  s   | j \}}|j|j S rH   r   rA   r   r   r   r   r?        
z_SumLinearOperator._adjoint
rf   rr   rs   r!   r   r=   rF   r   r?   rv   r   r   r   r   r`   l  s    
r`   c                      r   )rO   c                   st   t |tr
t |tstd|jd |jd kr td||f t t||g|jd |jd f ||f| _d S )Nr   r)   r   z)cannot multiply %r and %r: shape mismatch)rN   r   r   r   r	   r!   r   r   r   r   r   r   r!     s   
z_ProductLinearOperator.__init__c                 C     | j d | j d |S r   r   r5   r   r   r   r        z_ProductLinearOperator._matvecc                 C  r   Nr)   r   r   r5   r   r   r   r=     r   z_ProductLinearOperator._rmatvecc                 C  r   r   r   r5   r   r   r   rF     r   z_ProductLinearOperator._rmatmatc                 C  r   r   r   r5   r   r   r   r     r   z_ProductLinearOperator._matmatc                 C  s   | j \}}|j|j S rH   r   r   r   r   r   r?     r   z_ProductLinearOperator._adjointr   r   r   r   r   rO     s    rO   c                      r   )rQ   c                   sT   t |ts	tdt|stdt|gt|g}t ||j	 ||f| _
d S )NLinearOperator expected as Azscalar expected as alpha)rN   r   r   r   rP   r   r   r	   r!   r   r   )r    r   alphar   r   r   r   r!     s   

z_ScaledLinearOperator.__init__c                 C     | j d | j d | S r   r   r5   r   r   r   r        z_ScaledLinearOperator._matvecc                 C      t | jd | jd | S r   )r   r   r   r>   r5   r   r   r   r=     r   z_ScaledLinearOperator._rmatvecc                 C  r   r   )r   r   r   rG   r5   r   r   r   rF     r   z_ScaledLinearOperator._rmatmatc                 C  r   r   r   r5   r   r   r   r     r   z_ScaledLinearOperator._matmatc                 C  s   | j \}}|jt| S rH   )r   rA   r   r   )r    r   r   r   r   r   r?     s   
z_ScaledLinearOperator._adjointr   r   r   r   r   rQ     s    	rQ   c                      sL   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Zdd Z	  Z
S )r]   c                   sp   t |ts	td|jd |jd krtd| t|r"|dk r&tdt t|g|j ||f| _	d S )Nr   r   r)   z&square LinearOperator expected, got %rz"non-negative integer expected as p)
rN   r   r   r   r   	isintliker	   r!   r   r   r    r   r^   r   r   r   r!     s   
z_PowerLinearOperator.__init__c                 C  s.   t j|dd}t| jd D ]}||}q|S )NT)copyr)   )r   arrayranger   )r    funr6   resir   r   r   _power  s   
z_PowerLinearOperator._powerc                 C     |  | jd j|S Nr   )r   r   r$   r5   r   r   r   r        z_PowerLinearOperator._matvecc                 C  r   r   )r   r   r>   r5   r   r   r   r=     r   z_PowerLinearOperator._rmatvecc                 C  r   r   )r   r   rG   r5   r   r   r   rF     r   z_PowerLinearOperator._rmatmatc                 C  r   r   )r   r   r4   r5   r   r   r   r     r   z_PowerLinearOperator._matmatc                 C  s   | j \}}|j| S rH   r   r   r   r   r   r?     s   

z_PowerLinearOperator._adjoint)rf   rr   rs   r!   r   r   r=   rF   r   r?   rv   r   r   r   r   r]     s    r]   c                      s,   e Zd Z fddZdd Zdd Z  ZS )MatrixLinearOperatorc                   s*   t  |j|j || _d | _|f| _d S rH   )r	   r!   r   r   r   _MatrixLinearOperator__adjr   )r    r   r   r   r   r!     s   zMatrixLinearOperator.__init__c                 C  r   rH   )r   rK   r2   r   r   r   r     rd   zMatrixLinearOperator._matmatc                 C  s   | j d u r
t| | _ | j S rH   )r   _AdjointMatrixOperatorr.   r   r   r   r?     s   

zMatrixLinearOperator._adjoint)rf   rr   rs   r!   r   r?   rv   r   r   r   r   r     s    r   c                   @  s(   e Zd Zdd Zedd Zdd ZdS )r   c                 C  s6   |j j | _ || _|f| _|jd |jd f| _d S r   )r   r1   r   _AdjointMatrixOperator__adjointr   r   )r    rj   r   r   r   r!     s   z_AdjointMatrixOperator.__init__c                 C  s   | j jS rH   )r   r   r.   r   r   r   r     rp   z_AdjointMatrixOperator.dtypec                 C  s   | j S rH   )r   r.   r   r   r   r?     s   z_AdjointMatrixOperator._adjointN)rf   rr   rs   r!   ru   r   r?   r   r   r   r   r     s
    
r   c                      sF   e Zd Zd fdd	Zdd Zdd Zdd	 Zd
d Zdd Z  Z	S )IdentityOperatorNc                   s   t  || d S rH   )r	   r!   )r    r   r   r   r   r   r!     s   zIdentityOperator.__init__c                 C     |S rH   r   r5   r   r   r   r        zIdentityOperator._matvecc                 C  r   rH   r   r5   r   r   r   r=   	  r   zIdentityOperator._rmatvecc                 C  r   rH   r   r5   r   r   r   rF     r   zIdentityOperator._rmatmatc                 C  r   rH   r   r5   r   r   r   r     r   zIdentityOperator._matmatc                 C  s   | S rH   r   r.   r   r   r   r?     r   zIdentityOperator._adjointrH   r   r   r   r   r   r     s    r   c                 C  s   t | tr| S t | tjr| jdkrtdt| } t| S t	| r(t| S t
| dr[t
| dr[d}d}d}t
| dr@| j}t
| drH| j}t
| drP| j}t| j| j|||d	S td
)aM  Return `A` as a LinearOperator.

    Args:
        A (array-like):
            The input array to be converted to a `LinearOperator` object.
            It may be any of the following types:

               * :class:`cupy.ndarray`
               * sparse matrix (e.g. ``csr_matrix``, ``coo_matrix``, etc.)
               * :class:`cupyx.scipy.sparse.linalg.LinearOperator`
               * object with ``.shape`` and ``.matvec`` attributes

    Returns:
        cupyx.scipy.sparse.linalg.LinearOperator: `LinearOperator` object

    .. seealso:: :func:`scipy.sparse.aslinearoperator``
    r   zarray must have ndim <= 2r   r$   Nr>   rG   r   )r>   rG   r   ztype not understood)rN   r   r   ndarrayr8   r   
atleast_2dr   r   
isspmatrixr   r>   rG   r   r   r$   	TypeError)r   r>   rG   r   r   r   r   aslinearoperator  s.   






r   rH   )
__future__r   r   r   cupyx.scipyr   cupyx.scipy.sparser   r   r   ro   rq   r   r`   rO   rQ   r]   r   r   r   r   r   r   r   r   <module>   s&     {.
	#