
    _Mhr                        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          Z G d d	e          Z G d
 de          Z G d de          ZddZ G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Zd ZdS )aU  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_array
    >>> offsets = csr_array([[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                        e Zd ZdZdZdZ fdZd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z ee          Z d Z! ee!          Z"d Z#d Z$ xZ%S ) r   al  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.

    It is highly recommended to explicitly specify the `dtype`, otherwise
    it is determined automatically at the cost of a single matvec application
    on `int8` zero vector using the promoted `dtype` of the output.
    Python `int` could be difficult to automatically cast to numpy integers
    in the definition of the `matvec` so the determination may be inaccurate.
    It is assumed that `matmat`, `rmatvec`, and `rmatmat` would result in
    the same dtype of the output given an `int8` input as `matvec`.

    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=int8>
    >>> A.matvec(np.ones(2))
    array([ 2.,  3.])
    >>> A @ np.ones(2)
    array([ 2.,  3.])

       Nc                 h   | t           u r&t                                          t                    S t                                          |           }t	          |          j        t           j        k    r>t	          |          j        t           j        k    rt          j        dt          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__s       ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/sparse/linalg/_interface.pyr   zLinearOperator.__new__   s    .  77??#8999''//#&&CS		!^%;;;S		)^-CCC F'5!E E E E J    c                     |t          j        |          }t          |          }t          |          st	          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$   s      r   __init__zLinearOperator.__init__   s]     HUOOEeu~~ 	GEeEEEFFF



r   c                 4   | j         t          j        | j        d         t          j                  }	 t          j        |                     |                    }|j         | _         dS # t          $ r" t          j         t                    | _         Y dS w xY wdS )a  Determine the dtype by executing `matvec` on an `int8` test vector.

        In `np.promote_types` hierarchy, the type `int8` is the smallest,
        so we call `matvec` on `int8` and use the promoted dtype of the output
        to set the default `dtype` of the `LinearOperator`.
        We assume that `matmat`, `rmatvec`, and `rmatmat` would result in
        the same dtype of the output given an `int8` input as `matvec`.

        Called from subclasses at the end of the __init__ routine.
        N)r!   )	r!   r    zerosr$   int8asarraymatvecOverflowErrorint)r%   vmatvec_vs      r   _init_dtypezLinearOperator._init_dtype   s     :Brw777A,:dkk!nn55
 &^


	 ! + + +Xc]]



+	 s   'A) )(BBc                 N     t          j         f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                 b    g | ]+}                     |                    d d                    ,S r(      )r,   reshape.0colr%   s     r   
<listcomp>z*LinearOperator._matmat.<locals>.<listcomp>   s3    HHHS$++ckk"Q&7&788HHHr   )r    hstackTr%   Xs   ` r   r   zLinearOperator._matmat   s,     yHHHHACHHHIIIr   c                 T    |                      |                    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(   r5   )matmatr6   r%   xs     r   r   zLinearOperator._matvec   s$     {{199R++,,,r   c                    t          j        |          }| j        \  }}|j        |fk    r|j        |dfk    rt          d          |                     |          }t          |t           j                  rt          |          }nt          j        |          }|j	        dk    r|
                    |          }n1|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.

        r5   dimension mismatchr   z/invalid shape returned by user-defined matvec())r    
asanyarrayr$   r#   r   
isinstancematrixr   r+   ndimr6   r%   rB   MNys        r   r,   zLinearOperator.matvec   s    0 M!j!7qd??qw1Q%//1222LLOOa## 	AA
1A6Q;;		!AAVq[[		!AAANOOOr   c                    t          j        |          }| j        \  }}|j        |fk    r|j        |dfk    rt          d          |                     |          }t          |t           j                  rt          |          }nt          j        |          }|j	        dk    r|
                    |          }n1|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.

        r5   rD   r   z0invalid shape returned by user-defined rmatvec())r    rE   r$   r#   _rmatvecrF   rG   r   r+   rH   r6   rI   s        r   rmatveczLinearOperator.rmatvec  s    0 M!j!7qd??qw1Q%//1222MM!a## 	AA
1A6Q;;		!AAVq[[		!AAAOPPPr   c                 d   t          |           j        t          j        k    rut          | d          r^t          |           j        t          j        k    r<|                     |                    dd                                        d          S t          | j                            |          S )z6Default implementation of _rmatvec; defers to adjoint._rmatmatr(   r5   )	r   _adjointr   hasattrrQ   r6   NotImplementedErrorHr,   rA   s     r   rN   zLinearOperator._rmatvecA  s    ::."999j)) CT

+~/FFF}}QYYr1%5%566>>rBBB%%6==###r   c                 (   t          |          s#t          |          st          j        |          }|j        dk    rt          d|j         d          |j        d         | j        d         k    rt          d| j         d|j                   	 |                     |          }nA# t          $ r4}t          |          st          |          rt          d          | d	}~ww xY wt          |t          j                  rt          |          }|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   r5   dimension mismatch: , zdUnable to multiply a LinearOperator with a sparse matrix. Wrap the matrix in aslinearoperator first.N)r   r   r    rE   rH   r#   r$   r   	Exception	TypeErrorrF   rG   r   r%   r>   Yes       r   r@   zLinearOperator.matmatM  s-   .  	!1!44 	!a  A6Q;;NAFNNNOOO71:A&&KDJKK!'KKLLL	QAA 	 	 	{{ 033 B   	 a## 	As   B( (
C&2/C!!C&c                 &   t          |          s#t          |          st          j        |          }|j        dk    rt          d|j        z            |j        d         | j        d         k    rt          d| j         d|j                   	 |                     |          }nA# t          $ r4}t          |          st          |          rt          d          | d}~ww xY wt          |t          j                  rt          |          }|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   rW   rX   zfUnable to multiply a LinearOperator with a sparse matrix. Wrap the matrix in aslinearoperator() first.N)r   r   r    rE   rH   r#   r$   rQ   rY   rZ   rF   rG   r   r[   s       r   rmatmatzLinearOperator.rmatmat|  s4   ,  	!1!44 	!a  A6Q;;G v& ' ' ' 71:A&&KDJKK!'KKLLL	a  AA 	 	 	{{ 033 D   	 a## 	As   B' '
C%1/C  C%c                      t                     j        t          j        k    r%t          j         fd|j        D                       S  j                            |          S )z@Default implementation of _rmatmat defers to rmatvec or adjoint.c                 b    g | ]+}                     |                    d d                    ,S r4   )rO   r6   r7   s     r   r:   z+LinearOperator._rmatmat.<locals>.<listcomp>  s3    NNN3dll3;;r1+=+=>>NNNr   )r   rR   r   r    r;   r<   rU   r@   r=   s   ` r   rQ   zLinearOperator._rmatmat  sU    ::."9999NNNN!#NNNOOO6==###r   c                     | |z  S N rA   s     r   __call__zLinearOperator.__call__  s    Avr   c                 ,    |                      |          S rc   )dotrA   s     r   __mul__zLinearOperator.__mul__  s    xx{{r   c                 n    t          j        |          st          d          t          | d|z            S )Nz.Can only divide a linear operator by a scalar.g      ?)r    isscalarr#   _ScaledLinearOperatorr%   others     r   __truediv__zLinearOperator.__truediv__  s8    {5!! 	OMNNN$T3u9555r   c                    t          |t                    rt          | |          S t          j        |          rt          | |          S t          |          s#t          |          st          j        |          }|j	        dk    s|j	        dk    r&|j
        d         dk    r|                     |          S |j	        dk    r|                     |          S t          d|          )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.

        r5   r   )expected 1-d or 2-d array or matrix, got )rF   r   _ProductLinearOperatorr    rj   rk   r   r   r+   rH   r$   r,   r@   r#   rA   s     r   rg   zLinearOperator.dot  s     a(( 	T)$222[^^ 	T(q111A;; "'9!'<'< "JqMMv{{afkkagajAoo{{1~~%1{{1~~% !RQ!R!RSSSr   c                 r    t          j        |          rt          d          |                     |          S Nz0Scalar operands are not allowed, use '*' instead)r    rj   r#   rh   rl   s     r   
__matmul__zLinearOperator.__matmul__  s=    ;u 	0 / 0 0 0||E"""r   c                 r    t          j        |          rt          d          |                     |          S rs   )r    rj   r#   __rmul__rl   s     r   __rmatmul__zLinearOperator.__rmatmul__  s=    ;u 	0 / 0 0 0}}U###r   c                 t    t          j        |          rt          | |          S |                     |          S rc   )r    rj   rk   _rdotrA   s     r   rv   zLinearOperator.__rmul__  s2    ;q>> 	!(q111::a== r   c                    t          |t                    rt          ||           S t          j        |          rt          | |          S t          |          s#t          |          st          j        |          }|j	        dk    s|j	        dk    r5|j
        d         dk    r$| j                            |j                  j        S |j	        dk    r$| j                            |j                  j        S t          d|          )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.
        r5   r   r   rp   )rF   r   rq   r    rj   rk   r   r   r+   rH   r$   r<   r,   r@   r#   rA   s     r   ry   zLinearOperator._rdot  s    $ a(( 	T)!T222[^^ 	T(q111A;; "'9!'<'< "JqMM v{{afkkagajAoov}}QS))++1v}}QS))++ !RQ!R!RSSSr   c                 X    t          j        |          rt          | |          S t          S rc   )r    rj   _PowerLinearOperatorNotImplemented)r%   ps     r   __pow__zLinearOperator.__pow__  s(    ;q>> 	"'a000!!r   c                 Z    t          |t                    rt          | |          S t          S rc   )rF   r   _SumLinearOperatorr}   rA   s     r   __add__zLinearOperator.__add__  s*    a(( 	"%dA...!!r   c                 "    t          | d          S )Nr(   )rk   r%   s    r   __neg__zLinearOperator.__neg__  s    $T2...r   c                 .    |                      |           S rc   )r   rA   s     r   __sub__zLinearOperator.__sub__!  s    ||QBr   c                 ~    | j         \  }}| j        d}ndt          | j                  z   }d||| j        j        |fz  S )Nzunspecified dtypezdtype=z<%dx%d %s with %s>)r$   r!   strr   __name__)r%   rJ   rK   dts       r   __repr__zLinearOperator.__repr__$  sG    j!:$BBC
OO+B#q!T^-Db&IIIr   c                 *    |                                  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.
        )rR   r   s    r   adjointzLinearOperator.adjoint-  s     }}r   c                 *    |                                  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   s    r   	transposezLinearOperator.transpose?  s        r   c                      t          |           S )z6Default implementation of _adjoint; defers to rmatvec.)_AdjointLinearOperatorr   s    r   rR   zLinearOperator._adjointI  s    %d+++r   c                      t          |           S )z? Default implementation of _transpose; defers to rmatvec + conj)_TransposedLinearOperatorr   s    r   r   zLinearOperator._transposeM  s    (...r   )&r   
__module____qualname____doc__rH   __array_ufunc__r   r&   r1   r   r   r,   rO   rN   r@   r_   rQ   re   rh   rn   rg   rt   rw   rv   ry   r   r   r   r   r   r   propertyrU   r   r<   rR   r   __classcell__r   s   @r   r   r   7   s       \ \| DO       , , ,*J J J
- 
- 
-- - -^- - -^
$ 
$ 
$- - -^, , ,\$ $ $    6 6 6T T T># # #$ $ $! ! !"T "T "TH" " "" " "/ / /     J J J    	A! ! ! 	A, , ,/ / / / / / /r   c                   N     e Zd ZdZ	 	 d	 fd	Z fdZd Zd Z fdZd Z	 xZ
S )
r   z>Linear operator defined in terms of user-specified operations.Nc                     t                                          ||           d| _        || _        || _        || _        || _        |                                  d S )Nrd   )r   r&   r   "_CustomLinearOperator__matvec_impl#_CustomLinearOperator__rmatvec_impl#_CustomLinearOperator__rmatmat_impl"_CustomLinearOperator__matmat_implr1   )r%   r$   r,   rO   r@   r!   r_   r   s          r   r&   z_CustomLinearOperator.__init__U  s\    &&&	#%%#r   c                 ~    | j         |                      |          S t                                          |          S rc   )r   r   r   r%   r>   r   s     r   r   z_CustomLinearOperator._matmatb  s6    )%%a(((77??1%%%r   c                 ,    |                      |          S rc   )r   rA   s     r   r   z_CustomLinearOperator._matvech  s    !!!$$$r   c                 \    | j         }|t          d          |                      |          S )Nzrmatvec is not defined)r   rT   )r%   rB   funcs      r   rN   z_CustomLinearOperator._rmatveck  s2    "<%&>???""1%%%r   c                 ~    | j         |                      |          S t                                          |          S rc   )r   r   rQ   r   s     r   rQ   z_CustomLinearOperator._rmatmatq  s8    *&&q)))77##A&&&r   c                     t          | j        d         | j        d         f| j        | j        | j        | j        | j                  S )Nr5   r   )r$   r,   rO   r@   r_   r!   )r   r$   r   r   r   r   r!   r   s    r   rR   z_CustomLinearOperator._adjointw  sI    $DJqM4:a=+I,0,?-1-?,0,?-1-?+/:7 7 7 	7r   )NNNN)r   r   r   r   r&   r   r   rN   rQ   rR   r   r   s   @r   r   r   R  s        HH;?%)     & & & & &% % %& & &' ' ' ' '7 7 7 7 7 7 7r   r   c                   :     e Zd ZdZ fdZd Zd Zd Zd Z xZ	S )r   z$Adjoint of arbitrary Linear Operatorc                     |j         d         |j         d         f}t                                          |j        |           || _        |f| _        d S Nr5   r   )r!   r$   r$   r   r&   r!   Ar   r%   r   r$   r   s      r   r&   z_AdjointLinearOperator.__init__  L    QWQZ(qwe444D			r   c                 6    | j                             |          S rc   )r   rN   rA   s     r   r   z_AdjointLinearOperator._matvec      vq!!!r   c                 6    | j                             |          S rc   )r   r   rA   s     r   rN   z_AdjointLinearOperator._rmatvec      v~~a   r   c                 6    | j                             |          S rc   )r   rQ   rA   s     r   r   z_AdjointLinearOperator._matmat  r   r   c                 6    | j                             |          S rc   )r   r   rA   s     r   rQ   z_AdjointLinearOperator._rmatmat  r   r   
r   r   r   r   r&   r   rN   r   rQ   r   r   s   @r   r   r     sz        ..    " " "! ! !" " "! ! ! ! ! ! !r   r   c                   :     e Zd ZdZ fdZd Zd Zd Zd Z xZ	S )r   z*Transposition of arbitrary Linear Operatorc                     |j         d         |j         d         f}t                                          |j        |           || _        |f| _        d S r   r   r   s      r   r&   z"_TransposedLinearOperator.__init__  r   r   c                 ~    t          j        | j                            t          j        |                              S rc   )r    conjr   rN   rA   s     r   r   z!_TransposedLinearOperator._matvec  (    wtvrwqzz22333r   c                 ~    t          j        | j                            t          j        |                              S rc   )r    r   r   r   rA   s     r   rN   z"_TransposedLinearOperator._rmatvec  (    wtv~~bgajj11222r   c                 ~    t          j        | j                            t          j        |                              S rc   )r    r   r   rQ   rA   s     r   r   z!_TransposedLinearOperator._matmat  r   r   c                 ~    t          j        | j                            t          j        |                              S rc   )r    r   r   r   rA   s     r   rQ   z"_TransposedLinearOperator._rmatmat  r   r   r   r   s   @r   r   r     sz        44    4 4 43 3 34 4 43 3 3 3 3 3 3r   r   c                     |g }| D ].}|*t          |d          r|                    |j                   /t          j        | S )Nr!   )rS   appendr!   r    result_type)	operatorsdtypesr   s      r   
_get_dtyper     sP    ~ % %?wsG44?MM#)$$$>6""r   c                   <     e Zd Z fdZd Zd Zd Zd Zd Z xZ	S )r   c                 D   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)rF   r   r#   r$   r   r   r&   r   r%   r   Br   s      r   r&   z_SumLinearOperator.__init__  s    !^,, 	Jq.11	JHIII7agF1FF1FFFGGGF	QF++QW55555r   c                     | j         d                             |          | j         d                             |          z   S Nr   r5   r   r,   rA   s     r   r   z_SumLinearOperator._matvec  5    y|""1%%	!(;(;A(>(>>>r   c                     | j         d                             |          | j         d                             |          z   S r   r   rO   rA   s     r   rN   z_SumLinearOperator._rmatvec  5    y|##A&&1)=)=a)@)@@@r   c                     | j         d                             |          | j         d                             |          z   S r   r   r_   rA   s     r   rQ   z_SumLinearOperator._rmatmat  r   r   c                     | j         d                             |          | j         d                             |          z   S r   r   r@   rA   s     r   r   z_SumLinearOperator._matmat  r   r   c                 4    | j         \  }}|j        |j        z   S rc   r   rU   r%   r   r   s      r   rR   z_SumLinearOperator._adjoint      y1sQSyr   
r   r   r   r&   r   rN   rQ   r   rR   r   r   s   @r   r   r     s        6 6 6 6 6? ? ?A A AA A A? ? ?      r   r   c                   <     e Zd Z fdZd Zd Zd Zd Zd Z xZ	S )rq   c                    t          |t                    rt          |t                    st          d          |j        d         |j        d         k    rt          d| d| d          t	                                          t          ||g          |j        d         |j        d         f           ||f| _        d S )Nr   r5   r   zcannot multiply r   r   )rF   r   r#   r$   r   r&   r   r   r   s      r   r&   z_ProductLinearOperator.__init__  s    !^,, 	Jq.11	JHIII71:##KKKKKKLLLQF++67gaj!'!*5M	O 	O 	OF			r   c                 ~    | j         d                             | j         d                             |                    S r   r   rA   s     r   r   z_ProductLinearOperator._matvec  0    y|""49Q<#6#6q#9#9:::r   c                 ~    | j         d                             | j         d                             |                    S Nr5   r   r   rA   s     r   rN   z_ProductLinearOperator._rmatvec  0    y|##DIaL$8$8$;$;<<<r   c                 ~    | j         d                             | j         d                             |                    S r   r   rA   s     r   rQ   z_ProductLinearOperator._rmatmat  r   r   c                 ~    | j         d                             | j         d                             |                    S r   r   rA   s     r   r   z_ProductLinearOperator._matmat  r   r   c                 4    | j         \  }}|j        |j        z  S rc   r   r   s      r   rR   z_ProductLinearOperator._adjoint  r   r   r   r   s   @r   rq   rq     s            ; ; ;= = == = =; ; ;      r   rq   c                   <     e Zd Z fdZd Zd Zd Zd Zd Z xZ	S )rk   c                 |   t          |t                    st          d          t          j        |          st          d          t          |t
                    r|j        \  }}||z  }t          |gt          |          g          }t                      
                    ||j                   ||f| _        d S )NLinearOperator expected as Azscalar expected as alpha)rF   r   r#   r    rj   rk   r   r   r   r   r&   r$   )r%   r   alphaalpha_originalr!   r   s        r   r&   z_ScaledLinearOperator.__init__  s    !^,, 	=;<<<{5!! 	97888a.// 	+ !A~ N*EA3e..(((J			r   c                 ^    | j         d         | j         d                             |          z  S r   r   rA   s     r   r   z_ScaledLinearOperator._matvec  '    y|dil11!4444r   c                     t          j        | j        d                   | j        d                             |          z  S r   )r    r   r   rO   rA   s     r   rN   z_ScaledLinearOperator._rmatvec   1    wty|$$ty|';';A'>'>>>r   c                     t          j        | j        d                   | j        d                             |          z  S r   )r    r   r   r_   rA   s     r   rQ   z_ScaledLinearOperator._rmatmat  r   r   c                 ^    | j         d         | j         d                             |          z  S r   r   rA   s     r   r   z_ScaledLinearOperator._matmat  r   r   c                 N    | j         \  }}|j        t          j        |          z  S rc   )r   rU   r    r   )r%   r   r   s      r   rR   z_ScaledLinearOperator._adjoint	  s"    95sRWU^^##r   r   r   s   @r   rk   rk     s             5 5 5? ? ?? ? ?5 5 5$ $ $ $ $ $ $r   rk   c                   B     e Zd Z fdZd Zd Zd Zd Zd Zd Z	 xZ
S )r|   c                 p   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   r5   z$square LinearOperator expected, got z"non-negative integer expected as p)	rF   r   r#   r$   r   r   r&   r   r   )r%   r   r~   r   s      r   r&   z_PowerLinearOperator.__init__  s    !^,, 	=;<<<71:##IAIIJJJ|| 	Cq1uuABBBQC!'222F			r   c                     t          j        |d          }t          | j        d                   D ]} ||          }|S )NT)copyr5   )r    arrayranger   )r%   funrB   resis        r   _powerz_PowerLinearOperator._power  sG    hqt$$$ty|$$ 	 	A#c((CC
r   c                 N    |                      | j        d         j        |          S Nr   )r   r   r,   rA   s     r   r   z_PowerLinearOperator._matvec       {{49Q<.222r   c                 N    |                      | j        d         j        |          S r   )r   r   rO   rA   s     r   rN   z_PowerLinearOperator._rmatvec#      {{49Q</333r   c                 N    |                      | j        d         j        |          S r   )r   r   r_   rA   s     r   rQ   z_PowerLinearOperator._rmatmat&  r  r   c                 N    |                      | j        d         j        |          S r   )r   r   r@   rA   s     r   r   z_PowerLinearOperator._matmat)  r   r   c                 *    | j         \  }}|j        |z  S rc   r   )r%   r   r~   s      r   rR   z_PowerLinearOperator._adjoint,  s    y1saxr   )r   r   r   r&   r   r   rN   rQ   r   rR   r   r   s   @r   r|   r|     s        	 	 	 	 	  3 3 34 4 44 4 43 3 3      r   r|   c                   *     e Zd Z fdZd Zd Z xZS )MatrixLinearOperatorc                     t                                          |j        |j                   || _        d | _        |f| _        d S rc   )r   r&   r!   r$   r   _MatrixLinearOperator__adjr   )r%   r   r   s     r   r&   zMatrixLinearOperator.__init__2  s<    !'***
D			r   c                 6    | j                             |          S rc   )r   rg   r=   s     r   r   zMatrixLinearOperator._matmat8  s    vzz!}}r   c                 P    | j         t          | j                  | _         | j         S rc   )r	  _AdjointMatrixOperatorr   r   s    r   rR   zMatrixLinearOperator._adjoint;  s#    :/77DJzr   )r   r   r   r&   r   rR   r   r   s   @r   r  r  1  sV                    r   r  c                   0    e Zd Zd Zed             Zd ZdS )r  c                     |j                                         | _        |f| _        |j        d         |j        d         f| _        d S r   )r<   r   r   r   r$   )r%   adjoint_arrays     r   r&   z_AdjointMatrixOperator.__init__B  sA    %%''"$	"(+]-@-CC


r   c                 &    | j         d         j        S r   )r   r!   r   s    r   r!   z_AdjointMatrixOperator.dtypeG  s    y|!!r   c                 6    t          | j        d                   S r   )r  r   r   s    r   rR   z_AdjointMatrixOperator._adjointK  s    #DIaL111r   N)r   r   r   r&   r   r!   rR   rd   r   r   r  r  A  sP        D D D
 " " X"2 2 2 2 2r   r  c                   >     e Zd Zd fd	Zd Zd Zd Zd Zd Z xZ	S )	IdentityOperatorNc                 L    t                                          ||           d S rc   )r   r&   )r%   r$   r!   r   s      r   r&   zIdentityOperator.__init__P  s#    &&&&&r   c                     |S rc   rd   rA   s     r   r   zIdentityOperator._matvecS      r   c                     |S rc   rd   rA   s     r   rN   zIdentityOperator._rmatvecV  r  r   c                     |S rc   rd   rA   s     r   rQ   zIdentityOperator._rmatmatY  r  r   c                     |S rc   rd   rA   s     r   r   zIdentityOperator._matmat\  r  r   c                     | S rc   rd   r   s    r   rR   zIdentityOperator._adjoint_  s    r   rc   r   r   s   @r   r  r  O  s        ' ' ' ' ' '              r   r  c                    t          | t                    r| S t          | t          j                  st          | t          j                  rO| j        dk    rt          d          t          j        t          j        |                     } t          |           S t          |           st          |           rt          |           S t          | d          ryt          | d          rid}d}d}t          | d          r| j        }t          | d          r| j        }t          | d          r| j        }t          | j        | j        |||	          S t%          d
          )a  Return A as a LinearOperator.

    'A' may be any of the following types:
     - ndarray
     - matrix
     - sparse array (e.g. csr_array, lil_array, 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,   NrO   r_   r!   )rO   r_   r!   ztype not understood)rF   r   r    ndarrayrG   rH   r#   
atleast_2dr+   r  r   r   rS   rO   r_   r!   r$   r,   rZ   )r   rO   r_   r!   s       r   r	   r	   c  sg   4 !^$$ 3	Arz	"	" 3jBI&>&> 36A::8999M"*Q--((#A&&&	! 3*1-- 3#A&&& 1g 	371h#7#7 	3GGEq)$$ $)q)$$ $)q'""  !!'18W*1@ @ @ @ 1222r   rc   )r   r   numpyr    scipy.sparser   scipy.sparse._sputilsr   r   r   r   __all__r   r   r   r   r   r   rq   rk   r|   r  r  r  r	   rd   r   r   <module>r"     sn  * *X      ! ! ! ! ! ! R R R R R R R R R R R R/
0X/ X/ X/ X/ X/ X/ X/ X/v+7 +7 +7 +7 +7N +7 +7 +7\! ! ! ! !^ ! ! !*3 3 3 3 3 3 3 3.# # # #       6    ^   8$ $ $ $ $N $ $ $D         >      F    >    2 2 2 2 21 2 2 2    ~   (63 63 63 63 63r   