a
    CCCfKm                     @   s  d Z ddlZddlZddlmZ ddlmZmZm	Z	m
Z
 ddg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 ) aW  Abstract linear algebra library.

This module defines a class hierarchy that implements a kind of "lazy"
matrix representation, called the ``LinearOperator``. It can be used to do
linear algebra with extremely large sparse or structured matrices, without
representing those explicitly in memory. Such matrices can be added,
multiplied, transposed, etc.

As a motivating example, suppose you want have a matrix where almost all of
the elements have the value one. The standard sparse matrix representation
skips the storage of zeros, but not ones. By contrast, a LinearOperator is
able to represent such matrices efficiently. First, we need a compact way to
represent an all-ones matrix::

    >>> import numpy as np
    >>> from scipy.sparse.linalg._interface import LinearOperator
    >>> class Ones(LinearOperator):
    ...     def __init__(self, shape):
    ...         super().__init__(dtype=None, shape=shape)
    ...     def _matvec(self, x):
    ...         return np.repeat(x.sum(), self.shape[0])

Instances of this class emulate ``np.ones(shape)``, but using a constant
amount of storage, independent of ``shape``. The ``_matvec`` method specifies
how this linear operator multiplies with (operates on) a vector. We can now
add this operator to a sparse matrix that stores only offsets from one::

    >>> from scipy.sparse.linalg._interface import aslinearoperator
    >>> from scipy.sparse import csr_matrix
    >>> offsets = csr_matrix([[1, 0, 2], [0, -1, 0], [0, 0, 3]])
    >>> A = aslinearoperator(offsets) + Ones(offsets.shape)
    >>> A.dot([1, 2, 3])
    array([13,  4, 15])

The result is the same as that given by its dense, explicitly-stored
counterpart::

    >>> (np.ones(A.shape, A.dtype) + offsets.toarray()).dot([1, 2, 3])
    array([13,  4, 15])

Several algorithms in the ``scipy.sparse`` library are able to operate on
``LinearOperator`` instances.
    N)issparse)isshape	isintlikeasmatrixis_pydata_spmatrixLinearOperatoraslinearoperatorc                       s  e Zd 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d0d1 Zd2d3 Zd4d5 ZeeZ d6d7 Z!ee!Z"d8d9 Z#d:d; Z$  Z%S )<r   aj  Common interface for performing matrix vector products

    Many iterative methods (e.g. cg, gmres) do not need to know the
    individual entries of a matrix to solve a linear system A*x=b.
    Such solvers only require the computation of matrix vector
    products, A*v where v is a dense vector.  This class serves as
    an abstract interface between iterative solvers and matrix-like
    objects.

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

    A subclass must implement either one of the methods ``_matvec``
    and ``_matmat``, and the attributes/properties ``shape`` (pair of
    integers) and ``dtype`` (may be None). It may call the ``__init__``
    on this class to have these attributes validated. Implementing
    ``_matvec`` automatically implements ``_matmat`` (using a naive
    algorithm) and vice-versa.

    Optionally, a subclass may implement ``_rmatvec`` or ``_adjoint``
    to implement the Hermitian adjoint (conjugate transpose). As with
    ``_matvec`` and ``_matmat``, implementing either ``_rmatvec`` or
    ``_adjoint`` implements the other automatically. Implementing
    ``_adjoint`` is preferable; ``_rmatvec`` is mostly there for
    backwards compatibility.

    Parameters
    ----------
    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).

    Attributes
    ----------
    args : tuple
        For linear operators describing products etc. of other linear
        operators, the operands of the binary operation.
    ndim : int
        Number of dimensions (this is always 2)

    See Also
    --------
    aslinearoperator : Construct LinearOperators

    Notes
    -----
    The user-defined matvec() function must properly handle the case
    where v has shape (N,) as well as the (N,1) case.  The shape of
    the return type is handled internally by LinearOperator.

    LinearOperator instances can also be multiplied, added with each
    other and exponentiated, all lazily: the result of these operations
    is always a new, composite LinearOperator, that defers linear
    operations to the original operators and combines the results.

    More details regarding how to subclass a LinearOperator and several
    examples of concrete LinearOperator instances can be found in the
    external project `PyLops <https://pylops.readthedocs.io>`_.


    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse.linalg import LinearOperator
    >>> def mv(v):
    ...     return np.array([2*v[0], 3*v[1]])
    ...
    >>> A = LinearOperator((2,2), matvec=mv)
    >>> A
    <2x2 _CustomLinearOperator with dtype=float64>
    >>> A.matvec(np.ones(2))
    array([ 2.,  3.])
    >>> A * np.ones(2)
    array([ 2.,  3.])

       Nc                    sX   | t u rt tS t | }t|jt jkrPt|jt jkrPtjdt	dd |S 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/var/www/html/django/DPS/env/lib/python3.9/site-packages/scipy/sparse/linalg/_interface.pyr      s    zLinearOperator.__new__c                 C   sB   |durt |}t|}t|s2td|d|| _|| _dS )zInitialize this LinearOperator.

        To be called by subclasses. ``dtype`` may be None; ``shape`` should
        be convertible to a length-2 tuple.
        Nzinvalid shape z (must be 2-d))npdtypetupler   
ValueErrorshape)selfr   r!   r   r   r   __init__   s    
zLinearOperator.__init__c                 C   s2   | j du r.t| jd }t| |j | _ dS )zCCalled from subclasses at the end of the __init__ routine.
        N)r   r   Zzerosr!   asarraymatvec)r"   vr   r   r   _init_dtype   s    
zLinearOperator._init_dtypec                    s   t  fdd|jD S )zDefault matrix-matrix multiplication handler.

        Falls back on the user-defined _matvec method, so defining that will
        define matrix multiplication (though in a very suboptimal way).
        c                    s   g | ]}  |d dqS r$      )r&   reshape.0colr"   r   r   
<listcomp>       z*LinearOperator._matmat.<locals>.<listcomp>)r   hstackTr"   Xr   r/   r   r      s    zLinearOperator._matmatc                 C   s   |  |ddS )ay  Default matrix-vector multiplication handler.

        If self is a linear operator of shape (M, N), then this method will
        be called on a shape (N,) or (N, 1) ndarray, and should return a
        shape (M,) or (M, 1) ndarray.

        This default implementation falls back on _matmat, so defining that
        will define matrix-vector multiplication as well.
        r$   r*   )matmatr+   r"   xr   r   r   r      s    
zLinearOperator._matvecc                 C   s   t |}| j\}}|j|fkr6|j|dfkr6td| |}t|t jrVt|}n
t |}|j	dkrv|
|}n |j	dkr|
|d}ntd|S )ax  Matrix-vector multiplication.

        Performs the operation y=A*x where A is an MxN linear
        operator and x is a column vector or 1-d array.

        Parameters
        ----------
        x : {matrix, ndarray}
            An array with shape (N,) or (N,1).

        Returns
        -------
        y : {matrix, ndarray}
            A matrix or ndarray with shape (M,) or (M,1) depending
            on the type and shape of the x argument.

        Notes
        -----
        This matvec wraps the user-specified matvec routine or overridden
        _matvec method to ensure that y has the correct shape and type.

        r*   dimension mismatchr	   z/invalid shape returned by user-defined matvec())r   
asanyarrayr!   r    r   
isinstancematrixr   r%   ndimr+   r"   r8   MNyr   r   r   r&      s    






zLinearOperator.matvecc                 C   s   t |}| j\}}|j|fkr6|j|dfkr6td| |}t|t jrVt|}n
t |}|j	dkrv|
|}n |j	dkr|
|d}ntd|S )a  Adjoint matrix-vector multiplication.

        Performs the operation y = A^H * x where A is an MxN linear
        operator and x is a column vector or 1-d array.

        Parameters
        ----------
        x : {matrix, ndarray}
            An array with shape (M,) or (M,1).

        Returns
        -------
        y : {matrix, ndarray}
            A matrix or ndarray with shape (N,) or (N,1) depending
            on the type and shape of the x argument.

        Notes
        -----
        This rmatvec wraps the user-specified rmatvec routine or overridden
        _rmatvec method to ensure that y has the correct shape and type.

        r*   r9   r	   z0invalid shape returned by user-defined rmatvec())r   r:   r!   r    _rmatvecr;   r<   r   r%   r=   r+   r>   r   r   r   rmatvec   s    






zLinearOperator.rmatvecc                 C   s&   t | jtjkrtn| j|S dS )z6Default implementation of _rmatvec; defers to adjoint.N)r   _adjointr   NotImplementedErrorHr&   r7   r   r   r   rB   +  s    zLinearOperator._rmatvecc              
   C   s   t |st|st|}|jdkr6td|j d|jd | jd krbtd| j d|j z| |}W n@ ty } z(t |st|rt	d| W Y d	}~n
d	}~0 0 t
|tjrt|}|S )
aP  Matrix-matrix multiplication.

        Performs the operation y=A*X where A is an MxN linear
        operator and X dense N*K matrix or ndarray.

        Parameters
        ----------
        X : {matrix, ndarray}
            An array with shape (N,K).

        Returns
        -------
        Y : {matrix, ndarray}
            A matrix or ndarray with shape (M,K) depending on
            the type of the X argument.

        Notes
        -----
        This matmat wraps any user-specified matmat routine or overridden
        _matmat method to ensure that y has the correct type.

        r	   z$expected 2-d ndarray or matrix, not z-dr   r*   dimension mismatch: , zdUnable to multiply a LinearOperator with a sparse matrix. Wrap the matrix in aslinearoperator first.N)r   r   r   r:   r=   r    r!   r   	Exception	TypeErrorr;   r<   r   r"   r5   Yer   r   r   r6   3  s&    

zLinearOperator.matmatc              
   C   s   t |st|st|}|jdkr2td|j |jd | jd kr^td| j d|j z| |}W n@ ty } z(t |st|rt	d| W Y d}~n
d}~0 0 t
|tjrt|}|S )a;  Adjoint matrix-matrix multiplication.

        Performs the operation y = A^H * x where A is an MxN linear
        operator and x is a column vector or 1-d array, or 2-d array.
        The default implementation defers to the adjoint.

        Parameters
        ----------
        X : {matrix, ndarray}
            A matrix or 2D array.

        Returns
        -------
        Y : {matrix, ndarray}
            A matrix or 2D array depending on the type of the input.

        Notes
        -----
        This rmatmat wraps the user-specified rmatmat routine.

        r	   z(expected 2-d ndarray or matrix, not %d-dr   rG   rH   zfUnable to multiply a LinearOperator with a sparse matrix. Wrap the matrix in aslinearoperator() first.N)r   r   r   r:   r=   r    r!   _rmatmatrI   rJ   r;   r<   r   rK   r   r   r   rmatmatb  s*    

zLinearOperator.rmatmatc                    s:   t  jtjkr*t fdd|jD S  j|S dS )z@Default implementation of _rmatmat defers to rmatvec or adjoint.c                    s   g | ]}  |d dqS r)   )rC   r+   r,   r/   r   r   r0     r1   z+LinearOperator._rmatmat.<locals>.<listcomp>N)r   rD   r   r   r2   r3   rF   r6   r4   r   r/   r   rN     s    zLinearOperator._rmatmatc                 C   s   | | S Nr   r7   r   r   r   __call__  s    zLinearOperator.__call__c                 C   s
   |  |S rP   )dotr7   r   r   r   __mul__  s    zLinearOperator.__mul__c                 C   s    t |stdt| d| S )Nz.Can only divide a linear operator by a scalar.g      ?)r   isscalarr    _ScaledLinearOperatorr"   otherr   r   r   __truediv__  s    
zLinearOperator.__truediv__c                 C   s   t |trt| |S t|r(t| |S t|sBt|sBt|}|j	dksd|j	dkrn|j
d dkrn| |S |j	dkr| |S td| dS )ar  Matrix-matrix or matrix-vector multiplication.

        Parameters
        ----------
        x : array_like
            1-d or 2-d array, representing a vector or matrix.

        Returns
        -------
        Ax : array
            1-d or 2-d array (depending on the shape of x) that represents
            the result of applying this linear operator on x.

        r*   r	   +expected 1-d or 2-d array or matrix, got %rN)r;   r   _ProductLinearOperatorr   rT   rU   r   r   r%   r=   r!   r&   r6   r    r7   r   r   r   rR     s    




"


zLinearOperator.dotc                 C   s   t |rtd| |S Nz0Scalar operands are not allowed, use '*' instead)r   rT   r    rS   rV   r   r   r   
__matmul__  s    
zLinearOperator.__matmul__c                 C   s   t |rtd| |S r[   )r   rT   r    __rmul__rV   r   r   r   __rmatmul__  s    
zLinearOperator.__rmatmul__c                 C   s"   t |rt| |S | |S d S rP   )r   rT   rU   _rdotr7   r   r   r   r]     s    

zLinearOperator.__rmul__c                 C   s   t |trt|| S t|r(t| |S t|sBt|sBt|}|j	dksd|j	dkrt|j
d dkrt| j|jjS |j	dkr| j|jjS td| dS )a  Matrix-matrix or matrix-vector multiplication from the right.

        Parameters
        ----------
        x : array_like
            1-d or 2-d array, representing a vector or matrix.

        Returns
        -------
        xA : array
            1-d or 2-d array (depending on the shape of x) that represents
            the result of applying this linear operator on x from the right.

        Notes
        -----
        This is copied from dot to implement right multiplication.
        r*   r	   r   rY   N)r;   r   rZ   r   rT   rU   r   r   r%   r=   r!   r3   r&   r6   r    r7   r   r   r   r_     s    




"
zLinearOperator._rdotc                 C   s   t |rt| |S tS d S rP   )r   rT   _PowerLinearOperatorNotImplemented)r"   pr   r   r   __pow__  s    

zLinearOperator.__pow__c                 C   s   t |trt| |S tS d S rP   )r;   r   _SumLinearOperatorra   r7   r   r   r   __add__   s    

zLinearOperator.__add__c                 C   s
   t | dS )Nr$   )rU   r/   r   r   r   __neg__  s    zLinearOperator.__neg__c                 C   s   |  | S rP   )re   r7   r   r   r   __sub__	  s    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   |   S )a  Hermitian adjoint.

        Returns the Hermitian adjoint of self, aka the Hermitian
        conjugate or Hermitian transpose. For a complex matrix, the
        Hermitian adjoint is equal to the conjugate transpose.

        Can be abbreviated self.H instead of self.adjoint().

        Returns
        -------
        A_H : LinearOperator
            Hermitian adjoint of self.
        )rD   r/   r   r   r   adjoint  s    zLinearOperator.adjointc                 C   s   |   S )zTranspose this linear operator.

        Returns a LinearOperator that represents the transpose of this one.
        Can be abbreviated self.T instead of self.transpose().
        )
_transposer/   r   r   r   	transpose'  s    zLinearOperator.transposec                 C   s   t | S )z6Default implementation of _adjoint; defers to rmatvec.)_AdjointLinearOperatorr/   r   r   r   rD   1  s    zLinearOperator._adjointc                 C   s   t | S )z? Default implementation of _transpose; defers to rmatvec + conj)_TransposedLinearOperatorr/   r   r   r   rm   5  s    zLinearOperator._transpose)&ri   
__module____qualname____doc__r=   Z__array_ufunc__r   r#   r(   r   r   r&   rC   rB   r6   rO   rN   rQ   rS   rX   rR   r\   r^   r]   r_   rc   re   rf   rg   rk   rl   propertyrF   rn   r3   rD   rm   __classcell__r   r   r   r   r   7   sB   V	///. %	c                       sR   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&   rC   r6   r   rO   r   r   r   r#   =  s    z_CustomLinearOperator.__init__c                    s$   | j d ur|  |S t |S d S rP   )ry   r   r   r4   r   r   r   r   J  s    

z_CustomLinearOperator._matmatc                 C   s
   |  |S rP   )rv   r7   r   r   r   r   P  s    z_CustomLinearOperator._matvecc                 C   s    | j }|d u rtd|  |S )Nzrmatvec is not defined)rw   rE   )r"   r8   funcr   r   r   rB   S  s    z_CustomLinearOperator._rmatvecc                    s$   | j d ur|  |S t |S d S rP   )rx   r   rN   r4   r   r   r   rN   Y  s    

z_CustomLinearOperator._rmatmatc                 C   s.   t | jd | jd f| j| j| j| j| jdS )Nr*   r   )r!   r&   rC   r6   rO   r   )r   r!   rw   rv   rx   ry   r   r/   r   r   r   rD   _  s    z_CustomLinearOperator._adjoint)NNNN)ri   rq   rr   rs   r#   r   r   rB   rN   rD   ru   r   r   r   r   r   :  s     r   c                       s@   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                    s8   |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#   k  s    z_AdjointLinearOperator.__init__c                 C   s   | j |S rP   )r}   rB   r7   r   r   r   r   q  s    z_AdjointLinearOperator._matvecc                 C   s   | j |S rP   )r}   r   r7   r   r   r   rB   t  s    z_AdjointLinearOperator._rmatvecc                 C   s   | j |S rP   )r}   rN   r7   r   r   r   r   w  s    z_AdjointLinearOperator._matmatc                 C   s   | j |S rP   )r}   r   r7   r   r   r   rN   z  s    z_AdjointLinearOperator._rmatmat
ri   rq   rr   rs   r#   r   rB   r   rN   ru   r   r   r   r   ro   h  s   ro   c                       s@   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Z  Z	S )rp   z*Transposition of arbitrary Linear Operatorc                    s8   |j d |j d f}t j|j|d || _|f| _d S r{   r|   r~   r   r   r   r#     s    z"_TransposedLinearOperator.__init__c                 C   s   t | jt |S rP   )r   conjr}   rB   r7   r   r   r   r     s    z!_TransposedLinearOperator._matvecc                 C   s   t | jt |S rP   )r   r   r}   r   r7   r   r   r   rB     s    z"_TransposedLinearOperator._rmatvecc                 C   s   t | jt |S rP   )r   r   r}   rN   r7   r   r   r   r     s    z!_TransposedLinearOperator._matmatc                 C   s   t | jt |S rP   )r   r   r}   r   r7   r   r   r   rN     s    z"_TransposedLinearOperator._rmatmatr   r   r   r   r   rp   }  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   Zresult_type)	operatorsZdtypesr   r   r   r   
_get_dtype  s    r   c                       sD   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 )rd   c                    sd   t |trt |tstd|j|jkr>td| d| d||f| _t t||g|j d S )N)both operands have to be a LinearOperatorzcannot add  and : shape mismatch)r;   r   r    r!   r   r   r#   r   r"   r}   Br   r   r   r#     s    

z_SumLinearOperator.__init__c                 C   s    | j d || j d | S Nr   r*   r   r&   r7   r   r   r   r     s    z_SumLinearOperator._matvecc                 C   s    | j d || j d | S r   r   rC   r7   r   r   r   rB     s    z_SumLinearOperator._rmatvecc                 C   s    | j d || j d | S r   r   rO   r7   r   r   r   rN     s    z_SumLinearOperator._rmatmatc                 C   s    | j d || j d | S r   r   r6   r7   r   r   r   r     s    z_SumLinearOperator._matmatc                 C   s   | j \}}|j|j S rP   r   rF   r   r   r   r   rD     s    
z_SumLinearOperator._adjoint
ri   rq   rr   r#   r   rB   rN   r   rD   ru   r   r   r   r   rd     s   	rd   c                       sD   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 )rZ   c                    sz   t |trt |tstd|jd |jd krFtd| d| dt t||g|jd |jd f ||f| _d S )Nr   r*   r   zcannot multiply r   r   )r;   r   r    r!   r   r#   r   r   r   r   r   r   r#     s    
z_ProductLinearOperator.__init__c                 C   s   | j d | j d |S r   r   r7   r   r   r   r     s    z_ProductLinearOperator._matvecc                 C   s   | j d | j d |S Nr*   r   r   r7   r   r   r   rB     s    z_ProductLinearOperator._rmatvecc                 C   s   | j d | j d |S r   r   r7   r   r   r   rN     s    z_ProductLinearOperator._rmatmatc                 C   s   | j d | j d |S r   r   r7   r   r   r   r     s    z_ProductLinearOperator._matmatc                 C   s   | j \}}|j|j S rP   r   r   r   r   r   rD     s    
z_ProductLinearOperator._adjointr   r   r   r   r   rZ     s   
rZ   c                       sD   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 )rU   c                    sp   t |tstdt|s$tdt |tr@|j\}}|| }t|gt|g}t	 
||j ||f| _d S )NLinearOperator expected as Azscalar expected as alpha)r;   r   r    r   rT   rU   r   r   r   r   r#   r!   )r"   r}   alphaZalpha_originalr   r   r   r   r#     s    



z_ScaledLinearOperator.__init__c                 C   s   | j d | j d | S r   r   r7   r   r   r   r     s    z_ScaledLinearOperator._matvecc                 C   s    t | jd | jd | S r   )r   r   r   rC   r7   r   r   r   rB     s    z_ScaledLinearOperator._rmatvecc                 C   s    t | jd | jd | S r   )r   r   r   rO   r7   r   r   r   rN     s    z_ScaledLinearOperator._rmatmatc                 C   s   | j d | j d | S r   r   r7   r   r   r   r     s    z_ScaledLinearOperator._matmatc                 C   s   | j \}}|jt| S rP   )r   rF   r   r   )r"   r}   r   r   r   r   rD     s    
z_ScaledLinearOperator._adjointr   r   r   r   r   rU     s   rU   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                    sn   t |tstd|jd |jd kr2td| t|rB|dk rJtdt t|g|j ||f| _d S )Nr   r   r*   z&square LinearOperator expected, got %rz"non-negative integer expected as p)	r;   r   r    r!   r   r   r#   r   r   r"   r}   rb   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"   Zfunr8   resir   r   r   _power  s    
z_PowerLinearOperator._powerc                 C   s   |  | jd j|S Nr   )r   r   r&   r7   r   r   r   r     s    z_PowerLinearOperator._matvecc                 C   s   |  | jd j|S r   )r   r   rC   r7   r   r   r   rB   
  s    z_PowerLinearOperator._rmatvecc                 C   s   |  | jd j|S r   )r   r   rO   r7   r   r   r   rN     s    z_PowerLinearOperator._rmatmatc                 C   s   |  | jd j|S r   )r   r   r6   r7   r   r   r   r     s    z_PowerLinearOperator._matmatc                 C   s   | j \}}|j| S rP   r   r   r   r   r   rD     s    
z_PowerLinearOperator._adjoint)ri   rq   rr   r#   r   r   rB   rN   r   rD   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  |j|j || _d | _|f| _d S rP   )r   r#   r   r!   r}   _MatrixLinearOperator__adjr   )r"   r}   r   r   r   r#     s    zMatrixLinearOperator.__init__c                 C   s   | j |S rP   )r}   rR   r4   r   r   r   r     s    zMatrixLinearOperator._matmatc                 C   s   | j d u rt| | _ | j S rP   )r   _AdjointMatrixOperatorr/   r   r   r   rD   "  s    

zMatrixLinearOperator._adjoint)ri   rq   rr   r#   r   rD   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}   r3   r   _AdjointMatrixOperator__adjointr   r!   )r"   rl   r   r   r   r#   (  s    z_AdjointMatrixOperator.__init__c                 C   s   | j jS rP   )r   r   r/   r   r   r   r   .  s    z_AdjointMatrixOperator.dtypec                 C   s   | j S rP   )r   r/   r   r   r   rD   2  s    z_AdjointMatrixOperator._adjointN)ri   rq   rr   r#   rt   r   rD   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 rP   )r   r#   )r"   r!   r   r   r   r   r#   7  s    zIdentityOperator.__init__c                 C   s   |S rP   r   r7   r   r   r   r   :  s    zIdentityOperator._matvecc                 C   s   |S rP   r   r7   r   r   r   rB   =  s    zIdentityOperator._rmatvecc                 C   s   |S rP   r   r7   r   r   r   rN   @  s    zIdentityOperator._rmatmatc                 C   s   |S rP   r   r7   r   r   r   r   C  s    zIdentityOperator._matmatc                 C   s   | S rP   r   r/   r   r   r   rD   F  s    zIdentityOperator._adjoint)Nr   r   r   r   r   r   6  s   r   c                 C   s   t | tr| S t | tjs&t | tjrP| jdkr8tdtt| } t	| S t
| s`t| rht	| S t| drt| drd}d}d}t| dr| j}t| dr| j}t| dr| j}t| j| j|||d	S td
dS )a  Return A as a LinearOperator.

    'A' may be any of the following types:
     - ndarray
     - matrix
     - sparse matrix (e.g. csr_matrix, lil_matrix, etc.)
     - LinearOperator
     - An object with .shape and .matvec attributes

    See the LinearOperator documentation for additional information.

    Notes
    -----
    If 'A' has no .dtype attribute, the data type is determined by calling
    :func:`LinearOperator.matvec()` - set the .dtype attribute to prevent this
    call upon the linear operator creation.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse.linalg import aslinearoperator
    >>> M = np.array([[1,2,3],[4,5,6]], dtype=np.int32)
    >>> aslinearoperator(M)
    <2x3 MatrixLinearOperator with dtype=int32>
    r	   zarray must have ndim <= 2r!   r&   NrC   rO   r   )rC   rO   r   ztype not understood)r;   r   r   Zndarrayr<   r=   r    Z
atleast_2dr%   r   r   r   r   rC   rO   r   r!   r&   rJ   )r}   rC   rO   r   r   r   r   r   J  s.    




)N)rs   r   numpyr   Zscipy.sparser   Zscipy.sparse._sputilsr   r   r   r   __all__r   r   ro   rp   r   rd   rZ   rU   r`   r   r   r   r   r   r   r   r   <module>   s,   ,    .
	!#