
    (phr                     D   S r SSKrSSKrSSKJr  SSKJrJrJ	r	J
r
  SS/r " S S5      r " S S	\5      r " S
 S\5      r " S S\5      rSS jr " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rS rg)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                      ^  \ rS rSrSrSrSrU 4S jrS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS r\ " \5      r!S r"\ " \"5      r#S r$S  r%S!r&U =r'$ )"r   7   a4  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                 .  > U [         L a  [        TU ]	  [        5      $ [        TU ]	  U 5      n[	        U5      R
                  [         R
                  :X  aA  [	        U5      R                  [         R                  :X  a  [        R                  " S[        SS9  U$ )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       Q/var/www/html/venv/lib/python3.13/site-packages/scipy/sparse/linalg/_interface.pyr   LinearOperator.__new__   sx    . 7?#899'/#&CS	!!^%;%;;S	))^-C-CC F'5!E J    c                     Ub  [         R                  " U5      n[        U5      n[        U5      (       d  [	        SU< S35      eXl        X l        g)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__LinearOperator.__init__   sG     HHUOEeu~~~eYnEFF

r    c                 L   U R                   ch  [        R                  " U R                  S   [        R                  S9n [        R
                  " U R                  U5      5      nUR                   U l         gg! [         a"    [        R                   " [        5      U l          gf = f)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_dtypeLinearOperator._init_dtype   sw     ::Brww7A,::dkk!n5
 &^^
  ! +XXc]
+s   %A7 7)B#"B#c                     [         R                  " UR                   Vs/ s H#  o R                  UR	                  SS5      5      PM%     sn5      $ s  snf )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).
r+      )r"   hstackTr/   reshaper'   Xcols      r   r   LinearOperator._matmat   s;     yyACCHCS++ckk"Q&78CHIIHs   *Ac                 D    U R                  UR                  SS5      5      $ )aI  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+   r7   )matmatr:   r'   xs     r   r   LinearOperator._matvec   s     {{199R+,,r    c                    [         R                  " U5      nU R                  u  p#UR                  U4:w  a  UR                  US4:w  a  [        S5      eU R	                  U5      n[        U[         R                  5      (       a  [        U5      nO[         R                  " U5      nUR                  S:X  a  UR                  U5      nU$ UR                  S:X  a  UR                  US5      nU$ [        S5      e)a  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.

r7   dimension mismatchr   z/invalid shape returned by user-defined matvec())r"   
asanyarrayr&   r%   r   
isinstancematrixr   r.   ndimr:   r'   rB   MNys        r   r/   LinearOperator.matvec   s    0 MM!jj77qd?qww1Q%/122LLOa##A

1A66Q;		!A  VVq[		!AA  NOOr    c                    [         R                  " U5      nU R                  u  p#UR                  U4:w  a  UR                  US4:w  a  [        S5      eU R	                  U5      n[        U[         R                  5      (       a  [        U5      nO[         R                  " U5      nUR                  S:X  a  UR                  U5      nU$ UR                  S:X  a  UR                  US5      nU$ [        S5      e)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.

r7   rE   r   z0invalid shape returned by user-defined rmatvec())r"   rF   r&   r%   _rmatvecrG   rH   r   r.   rI   r:   rJ   s        r   rmatvecLinearOperator.rmatvec  s    0 MM!jj77qd?qww1Q%/122MM!a##A

1A66Q;		!A  VVq[		!AA  OPPr    c                 b   [        U 5      R                  [        R                  :X  an  [        U S5      (       aW  [        U 5      R                  [        R                  :w  a0  U R	                  UR                  SS5      5      R                  S5      $ [        eU R                  R                  U5      $ )z6Default implementation of _rmatvec; defers to adjoint._rmatmatr+   r7   )	r   _adjointr   hasattrrT   r:   NotImplementedErrorHr/   rA   s     r   rP   LinearOperator._rmatvecA  s}    :."9"99j))T
++~/F/FF}}QYYr1%56>>rBB%%66==##r    c                 D   [        U5      (       d&  [        U5      (       d  [        R                  " U5      nUR                  S:w  a  [        SUR                   S35      eUR                  S   U R                  S   :w  a%  [        SU R                   SUR                   35      e U R                  U5      n[        U[        R                  5      (       a  [        U5      nU$ ! [         a2  n[        U5      (       d  [        U5      (       a  [        S5      Uee S	nAff = f)
a  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   r7   dimension mismatch: , zdUnable to multiply a LinearOperator with a sparse matrix. Wrap the matrix in aslinearoperator first.N)r   r   r"   rF   rI   r%   r&   r   	Exception	TypeErrorrG   rH   r   r'   r<   Yes       r   r@   LinearOperator.matmatM  s    . 1!44a A66Q;CAFF82NOO771:A&3DJJ<r!''KLL	QA a##A  	{{033B  	s   &C# #
D--DDc                 B   [        U5      (       d&  [        U5      (       d  [        R                  " U5      nUR                  S:w  a  [        SUR                  -  5      eUR                  S   U R                  S   :w  a%  [        SU R                   SUR                   35      e U R                  U5      n[        U[        R                  5      (       a  [        U5      nU$ ! [         a2  n[        U5      (       d  [        U5      (       a  [        S5      Uee SnAff = f)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   r[   r\   zfUnable to multiply a LinearOperator with a sparse matrix. Wrap the matrix in aslinearoperator() first.N)r   r   r"   rF   rI   r%   r&   rT   r]   r^   rG   rH   r   r_   s       r   rmatmatLinearOperator.rmatmat|  s    , 1!44a A66Q;G vv& ' ' 771:A&3DJJ<r!''KLL	a A a##A  	{{033D  	s   %C" "
D,-DDc                 .   [        U 5      R                  [        R                  :X  aO  [        R                  " UR
                   Vs/ s H#  o R                  UR                  SS5      5      PM%     sn5      $ U R                  R                  U5      $ s  snf )z@Default implementation of _rmatmat defers to rmatvec or adjoint.r+   r7   )
r   rU   r   r"   r8   r9   rQ   r:   rX   r@   r;   s      r   rT   LinearOperator._rmatmat  sg    :."9"9999!##N#3ll3;;r1+=>#NOO66==## Os   *Bc                 
    X-  $ N rA   s     r   __call__LinearOperator.__call__  s	    vr    c                 $    U R                  U5      $ ri   )dotrA   s     r   __mul__LinearOperator.__mul__  s    xx{r    c                 l    [         R                  " U5      (       d  [        S5      e[        U SU-  5      $ )Nz.Can only divide a linear operator by a scalar.g      ?)r"   isscalarr%   _ScaledLinearOperatorr'   others     r   __truediv__LinearOperator.__truediv__  s.    {{5!!MNN$T3u955r    c                    [        U[        5      (       a  [        X5      $ [        R                  " U5      (       a  [        X5      $ [        U5      (       d&  [        U5      (       d  [        R                  " U5      nUR                  S:X  d#  UR                  S:X  a$  UR                  S   S:X  a  U R                  U5      $ UR                  S:X  a  U R                  U5      $ [        SU< 35      e)a"  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.

r7   r   )expected 1-d or 2-d array or matrix, got )rG   r   _ProductLinearOperatorr"   rr   rs   r   r   r.   rI   r&   r/   r@   r%   rA   s     r   rn   LinearOperator.dot  s     a(()$22[[^^(11A;;'9!'<'<JJqMvv{affkaggajAo{{1~%1{{1~% #LQE!RSSr    c                 p    [         R                  " U5      (       a  [        S5      eU R                  U5      $ Nz0Scalar operands are not allowed, use '*' instead)r"   rr   r%   ro   rt   s     r   
__matmul__LinearOperator.__matmul__  s2    ;;u / 0 0||E""r    c                 p    [         R                  " U5      (       a  [        S5      eU R                  U5      $ r}   )r"   rr   r%   __rmul__rt   s     r   __rmatmul__LinearOperator.__rmatmul__  s2    ;;u / 0 0}}U##r    c                 p    [         R                  " U5      (       a  [        X5      $ U R                  U5      $ ri   )r"   rr   rs   _rdotrA   s     r   r   LinearOperator.__rmul__  s(    ;;q>>(11::a= r    c                 Z   [        U[        5      (       a  [        X5      $ [        R                  " U5      (       a  [        X5      $ [        U5      (       d&  [        U5      (       d  [        R                  " U5      nUR                  S:X  d#  UR                  S:X  aB  UR                  S   S:X  a/  U R                  R                  UR                  5      R                  $ UR                  S:X  a/  U R                  R                  UR                  5      R                  $ [        SU< 35      e)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.
r7   r   r   ry   )rG   r   rz   r"   rr   rs   r   r   r.   rI   r&   r9   r/   r@   r%   rA   s     r   r   LinearOperator._rdot  s    $ a(()!22[[^^(11A;;'9!'<'<JJqM vv{affkaggajAovv}}QSS)+++1vv}}QSS)+++ #LQE!RSSr    c                 Z    [         R                  " U5      (       a  [        X5      $ [        $ ri   )r"   rr   _PowerLinearOperatorNotImplemented)r'   ps     r   __pow__LinearOperator.__pow__  s     ;;q>>'00!!r    c                 N    [        U[        5      (       a  [        X5      $ [        $ ri   )rG   r   _SumLinearOperatorr   rA   s     r   __add__LinearOperator.__add__  s     a((%d..!!r    c                     [        U S5      $ )Nr+   )rs   r'   s    r   __neg__LinearOperator.__neg__  s    $T2..r    c                 &    U R                  U* 5      $ ri   )r   rA   s     r   __sub__LinearOperator.__sub__!  s    ||QBr    c                     U R                   u  pU R                  c  SnOS[        U R                  5      -   nSXU R                  R                  U4-  $ )Nzunspecified dtypezdtype=z<%dx%d %s with %s>)r&   r#   strr   __name__)r'   rK   rL   dts       r   __repr__LinearOperator.__repr__$  sJ    jj::$BC

O+B#qT^^-D-Db&IIIr    c                 "    U R                  5       $ )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.
)rU   r   s    r   adjointLinearOperator.adjoint-  s     }}r    c                 "    U R                  5       $ )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   	transposeLinearOperator.transpose?  s       r    c                     [        U 5      $ )z6Default implementation of _adjoint; defers to rmatvec.)_AdjointLinearOperatorr   s    r   rU   LinearOperator._adjointI  s    %d++r    c                     [        U 5      $ )z>Default implementation of _transpose; defers to rmatvec + conj)_TransposedLinearOperatorr   s    r   r   LinearOperator._transposeM  s    (..r    r#   r&   )(r   
__module____qualname____firstlineno____doc__rI   __array_ufunc__r   r(   r4   r   r   r/   rQ   rP   r@   rd   rT   rk   ro   rv   rn   r~   r   r   r   r   r   r   r   r   r   propertyrX   r   r9   rU   r   __static_attributes____classcell__r   s   @r   r   r   7   s    \| DO ,*J
--^-^
$-^,\$6T>#$!"TH""/ J  	A! 	A,/ /r    c                   ^   ^  \ rS rSrSr  S
U 4S jjrU 4S jrS rS rU 4S jr	S r
S	rU =r$ )r   iR  z>Linear operator defined in terms of user-specified operations.c                    > [         TU ]  XQ5        SU l        X l        X0l        X`l        X@l        U R                  5         g )Nrj   )r   r(   r   "_CustomLinearOperator__matvec_impl#_CustomLinearOperator__rmatvec_impl#_CustomLinearOperator__rmatmat_impl"_CustomLinearOperator__matmat_implr4   )r'   r&   r/   rQ   r@   r#   rd   r   s          r   r(   _CustomLinearOperator.__init__U  s;    &	#%%#r    c                 ^   > U R                   b  U R                  U5      $ [        TU ]	  U5      $ ri   )r   r   r   r'   r<   r   s     r   r   _CustomLinearOperator._matmatb  s/    )%%a((7?1%%r    c                 $    U R                  U5      $ ri   )r   rA   s     r   r   _CustomLinearOperator._matvech  s    !!!$$r    c                 X    U R                   nUc  [        S5      eU R                  U5      $ )Nzrmatvec is not defined)r   rW   )r'   rB   funcs      r   rP   _CustomLinearOperator._rmatveck  s/    ""<%&>??""1%%r    c                 ^   > U R                   b  U R                  U5      $ [        TU ]	  U5      $ ri   )r   r   rT   r   s     r   rT   _CustomLinearOperator._rmatmatq  s0    *&&q))7#A&&r    c           	          [        U R                  S   U R                  S   4U R                  U R                  U R                  U R
                  U R                  S9$ )Nr7   r   )r&   r/   rQ   r@   rd   r#   )r   r&   r   r   r   r   r#   r   s    r   rU   _CustomLinearOperator._adjointw  sQ    $DJJqM4::a=+I,0,?,?-1-?-?,0,?,?-1-?-?+/::7 	7r    )__matmat_impl__matvec_impl__rmatmat_impl__rmatvec_implr   )NNNN)r   r   r   r   r   r(   r   r   rP   rT   rU   r   r   r   s   @r   r   r   R  s/    H;?%)&%&'7 7r    r   c                   D   ^  \ rS rSrSrU 4S jrS rS rS rS r	Sr
U =r$ )	r   i  z$Adjoint of arbitrary Linear Operatorc                    > UR                   S   UR                   S   4n[        TU ]	  UR                  US9  Xl        U4U l        g Nr7   r   r   r&   r   r(   r#   Ar   r'   r   r&   r   s      r   r(   _AdjointLinearOperator.__init__  A    QWWQZ(qwwe4D	r    c                 8    U R                   R                  U5      $ ri   )r   rP   rA   s     r   r   _AdjointLinearOperator._matvec      vvq!!r    c                 8    U R                   R                  U5      $ ri   )r   r   rA   s     r   rP   _AdjointLinearOperator._rmatvec      vv~~a  r    c                 8    U R                   R                  U5      $ ri   )r   rT   rA   s     r   r   _AdjointLinearOperator._matmat  r   r    c                 8    U R                   R                  U5      $ ri   )r   r   rA   s     r   rT   _AdjointLinearOperator._rmatmat  r   r    r   r   r   r   r   r   r   r(   r   rP   r   rT   r   r   r   s   @r   r   r     s$    ."!"! !r    r   c                   D   ^  \ rS rSrSrU 4S jrS rS rS rS r	Sr
U =r$ )	r   i  z*Transposition of arbitrary Linear Operatorc                    > UR                   S   UR                   S   4n[        TU ]	  UR                  US9  Xl        U4U l        g r   r   r   s      r   r(   "_TransposedLinearOperator.__init__  r   r    c                     [         R                  " U R                  R                  [         R                  " U5      5      5      $ ri   )r"   conjr   rP   rA   s     r   r   !_TransposedLinearOperator._matvec  &    wwtvvrwwqz233r    c                     [         R                  " U R                  R                  [         R                  " U5      5      5      $ ri   )r"   r   r   r   rA   s     r   rP   "_TransposedLinearOperator._rmatvec  &    wwtvv~~bggaj122r    c                     [         R                  " U R                  R                  [         R                  " U5      5      5      $ ri   )r"   r   r   rT   rA   s     r   r   !_TransposedLinearOperator._matmat  r   r    c                     [         R                  " U R                  R                  [         R                  " U5      5      5      $ ri   )r"   r   r   r   rA   s     r   rT   "_TransposedLinearOperator._rmatmat  r   r    r   r   r   s   @r   r   r     s$    44343 3r    r   c                     Uc  / nU  H6  nUc  M  [        US5      (       d  M  UR                  UR                  5        M8     [        R                  " U6 $ )Nr#   )rV   appendr#   r"   result_type)	operatorsdtypesr   s      r   
_get_dtyper     sH    ~?wsG44MM#))$  >>6""r    c                   F   ^  \ rS rSrU 4S jrS rS rS rS rS r	Sr
U =r$ )	r   i  c                    > [        U[        5      (       a  [        U[        5      (       d  [        S5      eUR                  UR                  :w  a  [        SU SU S35      eX4U l        [
        TU ]  [        X/5      UR                  5        g )N)both operands have to be a LinearOperatorzcannot add  and : shape mismatch)rG   r   r%   r&   r   r   r(   r   r'   r   Br   s      r   r(   _SumLinearOperator.__init__  sw    !^,,q.11HII77agg{1#U1#5EFGGF	QF+QWW5r    c                 |    U R                   S   R                  U5      U R                   S   R                  U5      -   $ Nr   r7   r   r/   rA   s     r   r   _SumLinearOperator._matvec  3    yy|""1%		!(;(;A(>>>r    c                 |    U R                   S   R                  U5      U R                   S   R                  U5      -   $ r   r   rQ   rA   s     r   rP   _SumLinearOperator._rmatvec  3    yy|##A&1)=)=a)@@@r    c                 |    U R                   S   R                  U5      U R                   S   R                  U5      -   $ r   r   rd   rA   s     r   rT   _SumLinearOperator._rmatmat  r  r    c                 |    U R                   S   R                  U5      U R                   S   R                  U5      -   $ r   r   r@   rA   s     r   r   _SumLinearOperator._matmat  r  r    c                 P    U R                   u  pUR                  UR                  -   $ ri   r   rX   r'   r   r   s      r   rU   _SumLinearOperator._adjoint      yyssQSSyr    r   r   r   r   r   r(   r   rP   rT   r   rU   r   r   r   s   @r   r   r     s(    6?AA? r    r   c                   F   ^  \ rS rSrU 4S jrS rS rS rS rS r	Sr
U =r$ )	rz   i  c                 P  > [        U[        5      (       a  [        U[        5      (       d  [        S5      eUR                  S   UR                  S   :w  a  [        SU SU S35      e[        TU ]  [        X/5      UR                  S   UR                  S   45        X4U l        g )Nr   r7   r   zcannot multiply r   r   )rG   r   r%   r&   r   r(   r   r   r   s      r   r(   _ProductLinearOperator.__init__  s    !^,,q.11HII771:#/s%s:JKLLQF+67ggaj!''!*5M	OF	r    c                 v    U R                   S   R                  U R                   S   R                  U5      5      $ r   r   rA   s     r   r   _ProductLinearOperator._matvec  .    yy|""499Q<#6#6q#9::r    c                 v    U R                   S   R                  U R                   S   R                  U5      5      $ Nr7   r   r  rA   s     r   rP   _ProductLinearOperator._rmatvec  .    yy|##DIIaL$8$8$;<<r    c                 v    U R                   S   R                  U R                   S   R                  U5      5      $ r  r  rA   s     r   rT   _ProductLinearOperator._rmatmat  r  r    c                 v    U R                   S   R                  U R                   S   R                  U5      5      $ r   r  rA   s     r   r   _ProductLinearOperator._matmat  r  r    c                 P    U R                   u  pUR                  UR                  -  $ ri   r  r  s      r   rU   _ProductLinearOperator._adjoint  r  r    r  r  r   s   @r   rz   rz     s&    ;==; r    rz   c                   F   ^  \ rS rSrU 4S jrS rS rS rS rS r	Sr
U =r$ )	rs   i  c                 P  > [        U[        5      (       d  [        S5      e[        R                  " U5      (       d  [        S5      e[        U[
        5      (       a  UR                  u  pX#-  n[        U/[        U5      /5      n[        TU ])  XAR                  5        X4U l        g )NLinearOperator expected as Azscalar expected as alpha)rG   r   r%   r"   rr   rs   r   r   r   r   r(   r&   )r'   r   alphaalpha_originalr#   r   s        r   r(   _ScaledLinearOperator.__init__  s    !^,,;<<{{5!!788a.// !A *EA3e.(J	r    c                 ^    U R                   S   U R                   S   R                  U5      -  $ r  r   rA   s     r   r   _ScaledLinearOperator._matvec  (    yy|diil11!444r    c                     [         R                  " U R                  S   5      U R                  S   R                  U5      -  $ r  )r"   r   r   rQ   rA   s     r   rP   _ScaledLinearOperator._rmatvec   1    wwtyy|$tyy|';';A'>>>r    c                     [         R                  " U R                  S   5      U R                  S   R                  U5      -  $ r  )r"   r   r   rd   rA   s     r   rT   _ScaledLinearOperator._rmatmat  r/  r    c                 ^    U R                   S   U R                   S   R                  U5      -  $ r  r  rA   s     r   r   _ScaledLinearOperator._matmat  r,  r    c                 d    U R                   u  pUR                  [        R                  " U5      -  $ ri   )r   rX   r"   r   )r'   r   r'  s      r   rU   _ScaledLinearOperator._adjoint	  s$    99ssRWWU^##r    r  r  r   s   @r   rs   rs     s&     5??5$ $r    rs   c                   L   ^  \ rS rSrU 4S jrS rS rS rS rS r	S r
S	rU =r$ )
r   i  c                 >  > [        U[        5      (       d  [        S5      eUR                  S   UR                  S   :w  a  [        SU< 35      e[	        U5      (       a  US:  a  [        S5      e[
        TU ]  [        U/5      UR                  5        X4U l        g )Nr&  r   r7   z$square LinearOperator expected, got z"non-negative integer expected as p)	rG   r   r%   r&   r   r   r(   r   r   )r'   r   r   r   s      r   r(   _PowerLinearOperator.__init__  s    !^,,;<<771:#CA5IJJ||q1uABBQC!''2F	r    c                 ~    [         R                  " USS9n[        U R                  S   5       H  nU" U5      nM     U$ )NT)copyr7   )r"   arrayranger   )r'   funrB   resis        r   _power_PowerLinearOperator._power  s7    hhqt$tyy|$Ac(C %
r    c                 T    U R                  U R                  S   R                  U5      $ Nr   )r@  r   r/   rA   s     r   r   _PowerLinearOperator._matvec   !    {{499Q<..22r    c                 T    U R                  U R                  S   R                  U5      $ rC  )r@  r   rQ   rA   s     r   rP   _PowerLinearOperator._rmatvec#  !    {{499Q<//33r    c                 T    U R                  U R                  S   R                  U5      $ rC  )r@  r   rd   rA   s     r   rT   _PowerLinearOperator._rmatmat&  rH  r    c                 T    U R                  U R                  S   R                  U5      $ rC  )r@  r   r@   rA   s     r   r   _PowerLinearOperator._matmat)  rE  r    c                 <    U R                   u  pUR                  U-  $ ri   r  )r'   r   r   s      r   rU   _PowerLinearOperator._adjoint,  s    yyssaxr    r  )r   r   r   r   r(   r@  r   rP   rT   r   rU   r   r   r   s   @r   r   r     s+    	3443 r    r   c                   4   ^  \ rS rSrU 4S jrS rS rSrU =r$ )MatrixLinearOperatori1  c                 x   > [         TU ]  UR                  UR                  5        Xl        S U l        U4U l        g ri   )r   r(   r#   r&   r   _MatrixLinearOperator__adjr   )r'   r   r   s     r   r(   MatrixLinearOperator.__init__2  s/    !''*
D	r    c                 8    U R                   R                  U5      $ ri   )r   rn   )r'   r<   s     r   r   MatrixLinearOperator._matmat8  s    vvzz!}r    c                 h    U R                   c  [        U R                  5      U l         U R                   $ ri   )rR  _AdjointMatrixOperatorr   r   s    r   rU   MatrixLinearOperator._adjoint;  s&    ::/7DJzzr    )r   __adjr   )	r   r   r   r   r(   r   rU   r   r   r   s   @r   rP  rP  1  s     r    rP  c                   0    \ rS rSrS r\S 5       rS rSrg)rW  iA  c                     UR                   R                  5       U l        U4U l        UR                  S   UR                  S   4U l        g r  )r9   r   r   r   r&   )r'   adjoint_arrays     r   r(   _AdjointMatrixOperator.__init__B  sB    %%'"$	"((+]-@-@-CC
r    c                 4    U R                   S   R                  $ rC  )r   r#   r   s    r   r#   _AdjointMatrixOperator.dtypeG  s    yy|!!!r    c                 2    [        U R                  S   5      $ rC  )rP  r   r   s    r   rU   _AdjointMatrixOperator._adjointK  s    #DIIaL11r    )r   r   r&   N)	r   r   r   r   r(   r   r#   rU   r   rj   r    r   rW  rW  A  s!    D
 " "2r    rW  c                   J   ^  \ rS rSrS	U 4S jjrS rS rS rS rS r	Sr
U =r$ )
IdentityOperatoriO  c                 $   > [         TU ]  X!5        g ri   )r   r(   )r'   r&   r#   r   s      r   r(   IdentityOperator.__init__P  s    &r    c                     U$ ri   rj   rA   s     r   r   IdentityOperator._matvecS      r    c                     U$ ri   rj   rA   s     r   rP   IdentityOperator._rmatvecV  rh  r    c                     U$ ri   rj   rA   s     r   rT   IdentityOperator._rmatmatY  rh  r    c                     U$ ri   rj   rA   s     r   r   IdentityOperator._matmat\  rh  r    c                     U $ ri   rj   r   s    r   rU   IdentityOperator._adjoint_  s    r    rj   ri   r  r   s   @r   rc  rc  O  s&    ' r    rc  c                    [        U [        5      (       a  U $ [        U [        R                  5      (       d  [        U [        R                  5      (       aP  U R
                  S:  a  [        S5      e[        R                  " [        R                  " U 5      5      n [        U 5      $ [        U 5      (       d  [        U 5      (       a  [        U 5      $ [        U S5      (       a  [        U S5      (       a}  SnSnSn[        U S5      (       a  U R                  n[        U S5      (       a  U R                  n[        U S5      (       a  U R                  n[        U R                   U R"                  UX#S	9$ [%        S
5      e)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/   NrQ   rd   r#   )rQ   rd   r#   ztype not understood)rG   r   r"   ndarrayrH   rI   r%   
atleast_2dr.   rP  r   r   rV   rQ   rd   r#   r&   r/   r^   )r   rQ   rd   r#   s       r   r	   r	   c  s(   4 !^$$	Arzz	"	"jBII&>&>66A:899MM"**Q-(#A&&	!*1--#A&& 1g71h#7#7GGEq)$$))q)$$))q'""!!''188W*1@ @ 122r    ri   )r   r   numpyr"   scipy.sparser   scipy.sparse._sputilsr   r   r   r   __all__r   r   r   r   r   r   rz   rs   r   rP  rW  rc  r	   rj   r    r   <module>rx     s   *X   ! R R/
0X/ X/v+7N +7\!^ !*3 3.# 6^ 8$N $D >  F>  21 2~ (63r    