o
    iJC                     @   s   d dl Z d dlZd dlmZ d dlm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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 )    N)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                    s\   | t u rtt | tS t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__ a/home/ubuntu/veenaModal/venv/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__1   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>I   s    z*LinearOperator._matmat.<locals>.<listcomp>)r   hstackTr   Xr   r-   r   r   E   s   zLinearOperator._matmatc                 C   s   |  |ddS )z6Default matrix-vector multiplication handler.
        r!   r(   )matmatr)   r   xr   r   r   r   K   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   r5   MNyr   r   r   r#   P   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(   r6   r   z0invalid shape returned by user-defined rmatvec())r   r   _rmatvecr7   r)   r8   r   r   r   rmatvecd   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#   r4   r   r   r   r<   y   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)r7   r   r   r   r   r2   Yr   r   r   r3      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   rA   r   rB   )r7   r   r   _rmatmatrC   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   r/   r0   r@   r3   r1   r   r-   r   rE      s
   zLinearOperator._rmatmatc                 C   s   | | S Nr   r4   r   r   r   __call__   s   zLinearOperator.__call__c                 C   
   |  |S rG   )dotr4   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_ScaledLinearOperatorr7   r   r#   r3   r   r4   r   r   r   rJ      s   



"


zLinearOperator.dotc                 C      t |r	td| |S Nz0Scalar operands are not allowed, use '*' instead)r   rO   r   rK   r   otherr   r   r   
__matmul__      

zLinearOperator.__matmul__c                 C   rQ   rR   )r   rO   r   __rmul__rS   r   r   r   __rmatmul__   rV   zLinearOperator.__rmatmul__c                 C      t |r
t| |S tS rG   )r   rO   rP   NotImplementedr4   r   r   r   rW         

zLinearOperator.__rmul__c                 C   rY   rG   )r   rO   _PowerLinearOperatorrZ   )r   pr   r   r   __pow__   r[   zLinearOperator.__pow__c                 C   s   t |tr
t| |S tS rG   )rM   r   _SumLinearOperatorrZ   r4   r   r   r   __add__   r[   zLinearOperator.__add__c                 C   s
   t | dS )Nr!   )rP   r-   r   r   r   __neg__   rL   zLinearOperator.__neg__c                 C   s   |  | S rG   )r`   r4   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   r9   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   rh   )z(Transpose this linear operator.
        )
_transposer-   r   r   r   	transpose   rj   zLinearOperator.transposec                 C      t | S )z6Default implementation of _adjoint; defers to rmatvec.)_AdjointLinearOperatorr-   r   r   r   r>         zLinearOperator._adjointc                 C   rm   )z? Default implementation of _transpose; defers to rmatvec + conj)_TransposedLinearOperatorr-   r   r   r   rk      ro   zLinearOperator._transpose)#re   
__module____qualname____doc__r7   r	   r    r%   r   r   r#   r=   r<   r3   rF   rE   rH   rK   rJ   rU   rX   rW   r^   r`   ra   rb   rg   ri   propertyr@   rl   r0   r>   rk   __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                    s<   t t| || d| _|| _|| _|| _|| _|   d S )Nr   )	r   r
   r    r   "_CustomLinearOperator__matvec_impl#_CustomLinearOperator__rmatvec_impl#_CustomLinearOperator__rmatmat_impl"_CustomLinearOperator__matmat_implr%   )r   r   r#   r=   r3   r   rF   r   r   r   r      s   z_CustomLinearOperator.__init__c                    $   | j d ur
|  |S tt| |S rG   )ry   r   r
   r   r1   r   r   r   r        

z_CustomLinearOperator._matmatc                 C   rI   rG   )rv   r4   r   r   r   r     rL   z_CustomLinearOperator._matvecc                 C   s    | j }|d u rtd|  |S )Nzrmatvec is not defined)rw   r?   )r   r5   funcr   r   r   r<     s   
z_CustomLinearOperator._rmatvecc                    rz   rG   )rx   r   r
   rE   r1   r   r   r   rE   "  r{   z_CustomLinearOperator._rmatmatc                 C   s.   t | jd | jd f| j| j| j| j| jdS )Nr(   r   )r   r#   r=   r3   rF   r   )r
   r   rw   rv   rx   ry   r   r-   r   r   r   r>   (  s   z_CustomLinearOperator._adjoint)NNNN)re   rq   rr   rs   r    r   r   r<   rE   r>   ru   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 )rn   z$Adjoint of arbitrary Linear Operatorc                    <   |j d |j d f}tt| j|j|d || _|f| _d S Nr(   r   )r   r   )r   r   rn   r    r   Ar   r   r   r   r   r   r   r    4     
z_AdjointLinearOperator.__init__c                 C      | j |S rG   )r   r<   r4   r   r   r   r   ;  rc   z_AdjointLinearOperator._matvecc                 C   r   rG   )r   r   r4   r   r   r   r<   >  rc   z_AdjointLinearOperator._rmatvecc                 C   r   rG   )r   rE   r4   r   r   r   r   A  rc   z_AdjointLinearOperator._matmatc                 C   r   rG   )r   r   r4   r   r   r   rE   D  rc   z_AdjointLinearOperator._rmatmat
re   rq   rr   rs   r    r   r<   r   rE   ru   r   r   r   r   rn   1  s    rn   c                       r}   )rp   z*Transposition of arbitrary Linear Operatorc                    r~   r   )r   r   rp   r    r   r   r   r   r   r   r   r    K  r   z"_TransposedLinearOperator.__init__c                 C      t | jt |S rG   )r   conjr   r<   r4   r   r   r   r   R     z!_TransposedLinearOperator._matvecc                 C   r   rG   )r   r   r   r   r4   r   r   r   r<   V     z"_TransposedLinearOperator._rmatvecc                 C   r   rG   )r   r   r   rE   r4   r   r   r   r   Y  r   z!_TransposedLinearOperator._matmatc                 C   r   rG   )r   r   r   r   r4   r   r   r   rE   ]  r   z"_TransposedLinearOperator._rmatmatr   r   r   r   r   rp   H  s    rp   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_dtypea  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                    sb   t |tr
t |tstd|j|jkrtd||f ||f| _tt| t||g|j d S )N)both operands have to be a LinearOperatorz$cannot add %r and %r: shape mismatch)	rM   r   r   r   r   r   r_   r    r   r   r   Br   r   r   r    k  s   

 z_SumLinearOperator.__init__c                 C       | j d || j d | S Nr   r(   r   r#   r4   r   r   r   r   u      z_SumLinearOperator._matvecc                 C   r   r   r   r=   r4   r   r   r   r<   x  r   z_SumLinearOperator._rmatvecc                 C   r   r   r   rF   r4   r   r   r   rE   {  r   z_SumLinearOperator._rmatmatc                 C   r   r   r   r3   r4   r   r   r   r   ~  r   z_SumLinearOperator._matmatc                 C   s   | j \}}|j|j S rG   r   r@   r   r   r   r   r>        
z_SumLinearOperator._adjoint
re   rq   rr   r    r   r<   rE   r   r>   ru   r   r   r   r   r_   j  s    
r_   c                       r   )rN   c                    sx   t |tr
t |tstd|jd |jd kr td||f tt| t||g|jd |jd f ||f| _d S )Nr   r(   r   z)cannot multiply %r and %r: shape mismatch)	rM   r   r   r   r   rN   r    r   r   r   r   r   r   r      s   
z_ProductLinearOperator.__init__c                 C      | j d | j d |S r   r   r4   r   r   r   r        z_ProductLinearOperator._matvecc                 C   r   Nr(   r   r   r4   r   r   r   r<     r   z_ProductLinearOperator._rmatvecc                 C   r   r   r   r4   r   r   r   rE     r   z_ProductLinearOperator._rmatmatc                 C   r   r   r   r4   r   r   r   r     r   z_ProductLinearOperator._matmatc                 C   s   | j \}}|j|j S rG   r   r   r   r   r   r>     r   z_ProductLinearOperator._adjointr   r   r   r   r   rN     s    rN   c                       r   )rP   c                    sX   t |ts	tdt|stdt|gt|g}tt| 	||j
 ||f| _d S )NLinearOperator expected as Azscalar expected as alpha)rM   r   r   r   rO   r   r   r   rP   r    r   r   )r   r   alphar   r   r   r   r      s   

z_ScaledLinearOperator.__init__c                 C      | j d | j d | S r   r   r4   r   r   r   r        z_ScaledLinearOperator._matvecc                 C       t | jd | jd | S r   )r   r   r   r=   r4   r   r   r   r<     r   z_ScaledLinearOperator._rmatvecc                 C   r   r   )r   r   r   rF   r4   r   r   r   rE     r   z_ScaledLinearOperator._rmatmatc                 C   r   r   r   r4   r   r   r   r     r   z_ScaledLinearOperator._matmatc                 C   s   | j \}}|jt| S rG   )r   r@   r   r   )r   r   r   r   r   r   r>     s   
z_ScaledLinearOperator._adjointr   r   r   r   r   rP     s    	rP   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                    st   t |ts	td|jd |jd krtd| t|r"|dk r&tdtt| t	|g|j ||f| _
d S )Nr   r   r(   z&square LinearOperator expected, got %rz"non-negative integer expected as p)rM   r   r   r   r   	isintliker   r\   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   funr5   resir   r   r   _power  s   
z_PowerLinearOperator._powerc                 C      |  | jd j|S Nr   )r   r   r#   r4   r   r   r   r        z_PowerLinearOperator._matvecc                 C   r   r   )r   r   r=   r4   r   r   r   r<     r   z_PowerLinearOperator._rmatvecc                 C   r   r   )r   r   rF   r4   r   r   r   rE     r   z_PowerLinearOperator._rmatmatc                 C   r   r   )r   r   r3   r4   r   r   r   r     r   z_PowerLinearOperator._matmatc                 C   s   | j \}}|j| S rG   r   r   r   r   r   r>     s   

z_PowerLinearOperator._adjoint)re   rq   rr   r    r   r   r<   rE   r   r>   ru   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 t| |j|j || _d | _|f| _d S rG   )r   r   r    r   r   r   _MatrixLinearOperator__adjr   )r   r   r   r   r   r      s   zMatrixLinearOperator.__init__c                 C   r   rG   )r   rJ   r1   r   r   r   r     rc   zMatrixLinearOperator._matmatc                 C   s   | j d u r
t| | _ | j S rG   )r   _AdjointMatrixOperatorr-   r   r   r   r>     s   

zMatrixLinearOperator._adjoint)re   rq   rr   r    r   r>   ru   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   r0   r   _AdjointMatrixOperator__adjointr   r   )r   ri   r   r   r   r      s   z_AdjointMatrixOperator.__init__c                 C   s   | j jS rG   )r   r   r-   r   r   r   r     ro   z_AdjointMatrixOperator.dtypec                 C   s   | j S rG   )r   r-   r   r   r   r>     s   z_AdjointMatrixOperator._adjointN)re   rq   rr   r    rt   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 t| || d S rG   )r   r   r    )r   r   r   r   r   r   r      s   zIdentityOperator.__init__c                 C      |S rG   r   r4   r   r   r   r        zIdentityOperator._matvecc                 C   r   rG   r   r4   r   r   r   r<     r   zIdentityOperator._rmatvecc                 C   r   rG   r   r4   r   r   r   rE   
  r   zIdentityOperator._rmatmatc                 C   r   rG   r   r4   r   r   r   r     r   zIdentityOperator._matmatc                 C   s   | S rG   r   r-   r   r   r   r>     r   zIdentityOperator._adjointrG   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=   rF   r   )r=   rF   r   ztype not understood)rM   r   r   ndarrayr7   r   
atleast_2dr   r   
isspmatrixr   r=   rF   r   r   r#   	TypeError)r   r=   rF   r   r   r   r   aslinearoperator  s.   






r   rG   )r   r   cupyx.scipyr   cupyx.scipy.sparser   objectr   r
   rn   rp   r   r_   rN   rP   r\   r   r   r   r   r   r   r   r   <module>   s$     {.
	#