def lsmr(A, b, damp=<NUM_LIT:0.0>, atol=<NUM_LIT>, btol=<NUM_LIT>, conlim=<NUM_LIT>,<EOL>maxiter=None, show=False):

A = aslinearoperator(A)<EOL>b = b.squeeze()<EOL>msg = ('<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>')<EOL>hdg1 = '<STR_LIT>''<STR_LIT:r>'<EOL>hdg2 = '<STR_LIT>'<EOL>pfreq = <NUM_LIT:20>   <EOL>pcount = <NUM_LIT:0>   <EOL>m, n = A.shape<EOL>minDim = min([m, n])<EOL>if maxiter is None:<EOL><INDENT>maxiter = minDim<EOL><DEDENT>if show:<EOL><INDENT>print('<STR_LIT:U+0020>')<EOL>print('<STR_LIT>')<EOL>print('<STR_LIT>' % (m, n))<EOL>print('<STR_LIT>' % (damp))<EOL>print('<STR_LIT>' % (atol, conlim))<EOL>print('<STR_LIT>' % (btol, maxiter))<EOL><DEDENT>u = b<EOL>beta = norm(u)<EOL>v = zeros(n)<EOL>alpha = <NUM_LIT:0><EOL>if beta > <NUM_LIT:0>:<EOL><INDENT>u = (<NUM_LIT:1> / beta) * u<EOL>v = A.rmatvec(u)<EOL>alpha = norm(v)<EOL><DEDENT>if alpha > <NUM_LIT:0>:<EOL><INDENT>v = (<NUM_LIT:1> / alpha) * v<EOL><DEDENT>itn = <NUM_LIT:0><EOL>zetabar = alpha * beta<EOL>alphabar = alpha<EOL>rho = <NUM_LIT:1><EOL>rhobar = <NUM_LIT:1><EOL>cbar = <NUM_LIT:1><EOL>sbar = <NUM_LIT:0><EOL>h = v.copy()<EOL>hbar = zeros(n)<EOL>x = zeros(n)<EOL>betadd = beta<EOL>betad = <NUM_LIT:0><EOL>rhodold = <NUM_LIT:1><EOL>tautildeold = <NUM_LIT:0><EOL>thetatilde = <NUM_LIT:0><EOL>zeta = <NUM_LIT:0><EOL>d = <NUM_LIT:0><EOL>normA2 = alpha * alpha<EOL>maxrbar = <NUM_LIT:0><EOL>minrbar = <NUM_LIT><EOL>normA = sqrt(normA2)<EOL>condA = <NUM_LIT:1><EOL>normx = <NUM_LIT:0><EOL>normb = beta<EOL>istop = <NUM_LIT:0><EOL>ctol = <NUM_LIT:0><EOL>if conlim > <NUM_LIT:0>:<EOL><INDENT>ctol = <NUM_LIT:1> / conlim<EOL><DEDENT>normr = beta<EOL>normar = alpha * beta<EOL>if normar == <NUM_LIT:0>:<EOL><INDENT>if show:<EOL><INDENT>print(msg[<NUM_LIT:0>])<EOL><DEDENT>return x, istop, itn, normr, normar, normA, condA, normx<EOL><DEDENT>if show:<EOL><INDENT>print('<STR_LIT:U+0020>')<EOL>print(hdg1, hdg2)<EOL>test1 = <NUM_LIT:1><EOL>test2 = alpha / beta<EOL>str1 = '<STR_LIT>' % (itn, x[<NUM_LIT:0>])<EOL>str2 = '<STR_LIT>' % (normr, normar)<EOL>str3 = '<STR_LIT>' % (test1, test2)<EOL>print('<STR_LIT>'.join([str1, str2, str3]))<EOL><DEDENT>while itn < maxiter:<EOL><INDENT>itn = itn + <NUM_LIT:1><EOL>u = A.matvec(v) - alpha * u<EOL>beta = norm(u)<EOL>if beta > <NUM_LIT:0>:<EOL><INDENT>u = (<NUM_LIT:1> / beta) * u<EOL>v = A.rmatvec(u) - beta * v<EOL>alpha = norm(v)<EOL>if alpha > <NUM_LIT:0>:<EOL><INDENT>v = (<NUM_LIT:1> / alpha) * v<EOL><DEDENT><DEDENT>chat, shat, alphahat = _sym_ortho(alphabar, damp)<EOL>rhoold = rho<EOL>c, s, rho = _sym_ortho(alphahat, beta)<EOL>thetanew = s*alpha<EOL>alphabar = c*alpha<EOL>rhobarold = rhobar<EOL>zetaold = zeta<EOL>thetabar = sbar * rho<EOL>rhotemp = cbar * rho<EOL>cbar, sbar, rhobar = _sym_ortho(cbar * rho, thetanew)<EOL>zeta = cbar * zetabar<EOL>zetabar = - sbar * zetabar<EOL>hbar = h - (thetabar * rho / (rhoold * rhobarold)) * hbar<EOL>x = x + (zeta / (rho * rhobar)) * hbar<EOL>h = v - (thetanew / rho) * h<EOL>betaacute = chat * betadd<EOL>betacheck = -shat * betadd<EOL>betahat = c * betaacute<EOL>betadd = -s * betaacute<EOL>thetatildeold = thetatilde<EOL>ctildeold, stildeold, rhotildeold = _sym_ortho(rhodold, thetabar)<EOL>thetatilde = stildeold * rhobar<EOL>rhodold = ctildeold * rhobar<EOL>betad = - stildeold * betad + ctildeold * betahat<EOL>tautildeold = (zetaold - thetatildeold * tautildeold) / rhotildeold<EOL>taud = (zeta - thetatilde * tautildeold) / rhodold<EOL>d = d + betacheck * betacheck<EOL>normr = sqrt(d + (betad - taud)**<NUM_LIT:2> + betadd * betadd)<EOL>normA2 = normA2 + beta * beta<EOL>normA = sqrt(normA2)<EOL>normA2 = normA2 + alpha * alpha<EOL>maxrbar = max(maxrbar, rhobarold)<EOL>if itn > <NUM_LIT:1>:<EOL><INDENT>minrbar = min(minrbar, rhobarold)<EOL><DEDENT>condA = max(maxrbar, rhotemp) / min(minrbar, rhotemp)<EOL>normar = abs(zetabar)<EOL>normx = norm(x)<EOL>test1 = normr / normb<EOL>if (normA * normr) != <NUM_LIT:0>:<EOL><INDENT>test2 = normar / (normA * normr)<EOL><DEDENT>else:<EOL><INDENT>test2 = infty<EOL><DEDENT>test3 = <NUM_LIT:1> / condA<EOL>t1 = test1 / (<NUM_LIT:1> + normA * normx / normb)<EOL>rtol = btol + atol * normA * normx / normb<EOL>if itn >= maxiter:<EOL><INDENT>istop = <NUM_LIT:7><EOL><DEDENT>if <NUM_LIT:1> + test3 <= <NUM_LIT:1>:<EOL><INDENT>istop = <NUM_LIT:6><EOL><DEDENT>if <NUM_LIT:1> + test2 <= <NUM_LIT:1>:<EOL><INDENT>istop = <NUM_LIT:5><EOL><DEDENT>if <NUM_LIT:1> + t1 <= <NUM_LIT:1>:<EOL><INDENT>istop = <NUM_LIT:4><EOL><DEDENT>if test3 <= ctol:<EOL><INDENT>istop = <NUM_LIT:3><EOL><DEDENT>if test2 <= atol:<EOL><INDENT>istop = <NUM_LIT:2><EOL><DEDENT>if test1 <= rtol:<EOL><INDENT>istop = <NUM_LIT:1><EOL><DEDENT>if show:<EOL><INDENT>if (n <= <NUM_LIT>) or (itn <= <NUM_LIT:10>) or (itn >= maxiter - <NUM_LIT:10>) or(itn % <NUM_LIT:10> == <NUM_LIT:0>) or (test3 <= <NUM_LIT> * ctol) or(test2 <= <NUM_LIT> * atol) or (test1 <= <NUM_LIT> * rtol) or(istop != <NUM_LIT:0>):<EOL><INDENT>if pcount >= pfreq:<EOL><INDENT>pcount = <NUM_LIT:0><EOL>print('<STR_LIT:U+0020>')<EOL>print(hdg1, hdg2)<EOL><DEDENT>pcount = pcount + <NUM_LIT:1><EOL>str1 = '<STR_LIT>' % (itn, x[<NUM_LIT:0>])<EOL>str2 = '<STR_LIT>' % (normr, normar)<EOL>str3 = '<STR_LIT>' % (test1, test2)<EOL>str4 = '<STR_LIT>' % (normA, condA)<EOL>print('<STR_LIT>'.join([str1, str2, str3, str4]))<EOL><DEDENT><DEDENT>if istop > <NUM_LIT:0>:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if show:<EOL><INDENT>print('<STR_LIT:U+0020>')<EOL>print('<STR_LIT>')<EOL>print(msg[istop])<EOL>print('<STR_LIT>' % (istop, normr))<EOL>print('<STR_LIT>' % (normA, normar))<EOL>print('<STR_LIT>' % (itn, condA))<EOL>print('<STR_LIT>' % (normx))<EOL>print(str1, str2)<EOL>print(str3, str4)<EOL><DEDENT>return x, istop, itn, normr, normar, normA, condA, normx<EOL>

Iterative solver for least-squares problems.

    lsmr solves the system of linear equations ``Ax = b``. If the system
    is inconsistent, it solves the least-squares problem ``min ||b - Ax||_2``.
    A is a rectangular matrix of dimension m-by-n, where all cases are
    allowed: m = n, m > n, or m < n. B is a vector of length m.
    The matrix A may be dense or sparse (usually sparse).

    Parameters
    ----------
    A : {matrix, sparse matrix, ndarray, LinearOperator}
        Matrix A in the linear system.
    b : (m,) ndarray
        Vector b in the linear system.
    damp : float
        Damping factor for regularized least-squares. `lsmr` solves
        the regularized least-squares problem::

         min ||(b) - (  A   )x||
             ||(0)   (damp*I) ||_2

        where damp is a scalar.  If damp is None or 0, the system
        is solved without regularization.
    atol, btol : float
        Stopping tolerances. `lsmr` continues iterations until a
        certain backward error estimate is smaller than some quantity
        depending on atol and btol.  Let ``r = b - Ax`` be the
        residual vector for the current approximate solution ``x``.
        If ``Ax = b`` seems to be consistent, ``lsmr`` terminates
        when ``norm(r) <= atol * norm(A) * norm(x) + btol * norm(b)``.
        Otherwise, lsmr terminates when ``norm(A^{T} r) <=
        atol * norm(A) * norm(r)``.  If both tolerances are 1.0e-6 (say),
        the final ``norm(r)`` should be accurate to about 6
        digits. (The final x will usually have fewer correct digits,
        depending on ``cond(A)`` and the size of LAMBDA.)  If `atol`
        or `btol` is None, a default value of 1.0e-6 will be used.
        Ideally, they should be estimates of the relative error in the
        entries of A and B respectively.  For example, if the entries
        of `A` have 7 correct digits, set atol = 1e-7. This prevents
        the algorithm from doing unnecessary work beyond the
        uncertainty of the input data.
    conlim : float
        `lsmr` terminates if an estimate of ``cond(A)`` exceeds
        `conlim`.  For compatible systems ``Ax = b``, conlim could be
        as large as 1.0e+12 (say).  For least-squares problems,
        `conlim` should be less than 1.0e+8. If `conlim` is None, the
        default value is 1e+8.  Maximum precision can be obtained by
        setting ``atol = btol = conlim = 0``, but the number of
        iterations may then be excessive.
    maxiter : int
        `lsmr` terminates if the number of iterations reaches
        `maxiter`.  The default is ``maxiter = min(m, n)``.  For
        ill-conditioned systems, a larger value of `maxiter` may be
        needed.
    show : bool
        Print iterations logs if ``show=True``.

    Returns
    -------
    x : ndarray of float
        Least-square solution returned.
    istop : int
        istop gives the reason for stopping::

          istop   = 0 means x=0 is a solution.
                  = 1 means x is an approximate solution to A*x = B,
                      according to atol and btol.
                  = 2 means x approximately solves the least-squares problem
                      according to atol.
                  = 3 means COND(A) seems to be greater than CONLIM.
                  = 4 is the same as 1 with atol = btol = eps (machine
                      precision)
                  = 5 is the same as 2 with atol = eps.
                  = 6 is the same as 3 with CONLIM = 1/eps.
                  = 7 means ITN reached maxiter before the other stopping
                      conditions were satisfied.

    itn : int
        Number of iterations used.
    normr : float
        ``norm(b-Ax)``
    normar : float
        ``norm(A^T (b - Ax))``
    norma : float
        ``norm(A)``
    conda : float
        Condition number of A.
    normx : float
        ``norm(x)``

    Notes
    -----

    .. versionadded:: 0.11.0

    References
    ----------
    .. [1] D. C.-L. Fong and M. A. Saunders,
           "LSMR: An iterative algorithm for sparse least-squares problems",
           SIAM J. Sci. Comput., vol. 33, pp. 2950-2971, 2011.
           http://arxiv.org/abs/1006.0758
    .. [2] LSMR Software, http://www.stanford.edu/~clfong/lsmr.html


def make_system(A, M, x0, b, xtype=None):

A_ = A<EOL>A = aslinearoperator(A)<EOL>if A.shape[<NUM_LIT:0>] != A.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ValueError('<STR_LIT>' % (A.shape,))<EOL><DEDENT>N = A.shape[<NUM_LIT:0>]<EOL>b = asanyarray(b)<EOL>if not (b.shape == (N,<NUM_LIT:1>) or b.shape == (N,)):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if b.dtype.char not in '<STR_LIT>':<EOL><INDENT>b = b.astype('<STR_LIT:d>')  <EOL><DEDENT>def postprocess(x):<EOL><INDENT>if isinstance(b,matrix):<EOL><INDENT>x = asmatrix(x)<EOL><DEDENT>return x.reshape(b.shape)<EOL><DEDENT>if xtype is None:<EOL><INDENT>if hasattr(A,'<STR_LIT>'):<EOL><INDENT>xtype = A.dtype.char<EOL><DEDENT>else:<EOL><INDENT>xtype = A.matvec(b).dtype.char<EOL><DEDENT>xtype = coerce(xtype, b.dtype.char)<EOL><DEDENT>else:<EOL><INDENT>warn('<STR_LIT>'<EOL>'<STR_LIT>',<EOL>DeprecationWarning)<EOL>if xtype == <NUM_LIT:0>:<EOL><INDENT>xtype = b.dtype.char<EOL><DEDENT>else:<EOL><INDENT>if xtype not in '<STR_LIT>':<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>b = asarray(b,dtype=xtype)  <EOL>b = b.ravel()<EOL>if x0 is None:<EOL><INDENT>x = zeros(N, dtype=xtype)<EOL><DEDENT>else:<EOL><INDENT>x = array(x0, dtype=xtype)<EOL>if not (x.shape == (N,<NUM_LIT:1>) or x.shape == (N,)):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>x = x.ravel()<EOL><DEDENT>if M is None:<EOL><INDENT>if hasattr(A_,'<STR_LIT>'):<EOL><INDENT>psolve = A_.psolve<EOL><DEDENT>else:<EOL><INDENT>psolve = id<EOL><DEDENT>if hasattr(A_,'<STR_LIT>'):<EOL><INDENT>rpsolve = A_.rpsolve<EOL><DEDENT>else:<EOL><INDENT>rpsolve = id<EOL><DEDENT>if psolve is id and rpsolve is id:<EOL><INDENT>M = IdentityOperator(shape=A.shape, dtype=A.dtype)<EOL><DEDENT>else:<EOL><INDENT>M = LinearOperator(A.shape, matvec=psolve, rmatvec=rpsolve,<EOL>dtype=A.dtype)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>M = aslinearoperator(M)<EOL>if A.shape != M.shape:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>return A, M, x, b, postprocess<EOL>

Make a linear system Ax=b

    Parameters
    ----------
    A : LinearOperator
        sparse or dense matrix (or any valid input to aslinearoperator)
    M : {LinearOperator, Nones}
        preconditioner
        sparse or dense matrix (or any valid input to aslinearoperator)
    x0 : {array_like, None}
        initial guess to iterative method
    b : array_like
        right hand side
    xtype : {'f', 'd', 'F', 'D', None}
        dtype of the x vector

    Returns
    -------
    (A, M, x, b, postprocess)
        A : LinearOperator
            matrix of the linear system
        M : LinearOperator
            preconditioner
        x : rank 1 ndarray
            initial guess
        b : rank 1 ndarray
            right hand side
        postprocess : function
            converts the solution vector to the appropriate
            type and dimensions (e.g. (N,1) matrix)


def assert_allclose_cc(actual, desired, **kw):

try:<EOL><INDENT>assert_allclose(actual, desired, **kw)<EOL><DEDENT>except:<EOL><INDENT>assert_allclose(actual, conj(desired), **kw)<EOL><DEDENT>

Almost equal or complex conjugates almost equal


def argsort_which(eval, typ, k, which,<EOL>sigma=None, OPpart=None, mode=None):

if sigma is None:<EOL><INDENT>reval = np.round(eval, decimals=_ndigits[typ])<EOL><DEDENT>else:<EOL><INDENT>if mode is None or mode == '<STR_LIT>':<EOL><INDENT>if OPpart is None:<EOL><INDENT>reval = <NUM_LIT:1.> / (eval - sigma)<EOL><DEDENT>elif OPpart == '<STR_LIT:r>':<EOL><INDENT>reval = <NUM_LIT:0.5> * (<NUM_LIT:1.> / (eval - sigma)<EOL>+ <NUM_LIT:1.> / (eval - np.conj(sigma)))<EOL><DEDENT>elif OPpart == '<STR_LIT:i>':<EOL><INDENT>reval = -<NUM_LIT> * (<NUM_LIT:1.> / (eval - sigma)<EOL>- <NUM_LIT:1.> / (eval - np.conj(sigma)))<EOL><DEDENT><DEDENT>elif mode == '<STR_LIT>':<EOL><INDENT>reval = (eval + sigma) / (eval - sigma)<EOL><DEDENT>elif mode == '<STR_LIT>':<EOL><INDENT>reval = eval / (eval - sigma)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>" % mode)<EOL><DEDENT>reval = np.round(reval, decimals=_ndigits[typ])<EOL><DEDENT>if which in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>ind = np.argsort(abs(reval))<EOL><DEDENT>elif which in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>ind = np.argsort(np.real(reval))<EOL><DEDENT>elif which in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>if typ.islower():<EOL><INDENT>ind = np.argsort(abs(np.imag(reval)))<EOL><DEDENT>else:<EOL><INDENT>ind = np.argsort(np.imag(reval))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>" % which)<EOL><DEDENT>if which in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>return ind[-k:]<EOL><DEDENT>elif which in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>return ind[:k]<EOL><DEDENT>elif which == '<STR_LIT>':<EOL><INDENT>return np.concatenate((ind[:k//<NUM_LIT:2>], ind[k//<NUM_LIT:2>-k:]))<EOL><DEDENT>

Return sorted indices of eigenvalues using the "which" keyword
    from eigs and eigsh


def eigs(A, k=<NUM_LIT:6>, M=None, sigma=None, which='<STR_LIT>', v0=None,<EOL>ncv=None, maxiter=None, tol=<NUM_LIT:0>, return_eigenvectors=True,<EOL>Minv=None, OPinv=None, OPpart=None):

if A.shape[<NUM_LIT:0>] != A.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ValueError('<STR_LIT>' % (A.shape,))<EOL><DEDENT>if M is not None:<EOL><INDENT>if M.shape != A.shape:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>% (M.shape, A.shape))<EOL><DEDENT>if np.dtype(M.dtype).char.lower() != np.dtype(A.dtype).char.lower():<EOL><INDENT>import warnings<EOL>warnings.warn('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>n = A.shape[<NUM_LIT:0>]<EOL>if k <= <NUM_LIT:0> or k >= n:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>% (k, n - <NUM_LIT:1>))<EOL><DEDENT>if sigma is None:<EOL><INDENT>matvec = _aslinearoperator_with_dtype(A).matvec<EOL>if OPinv is not None:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if OPpart is not None:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if M is None:<EOL><INDENT>mode = <NUM_LIT:1><EOL>M_matvec = None<EOL>Minv_matvec = None<EOL>if Minv is not None:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>mode = <NUM_LIT:2><EOL>if Minv is None:<EOL><INDENT>Minv_matvec = get_inv_matvec(M, symmetric=True, tol=tol)<EOL><DEDENT>else:<EOL><INDENT>Minv = _aslinearoperator_with_dtype(Minv)<EOL>Minv_matvec = Minv.matvec<EOL><DEDENT>M_matvec = _aslinearoperator_with_dtype(M).matvec<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if np.issubdtype(A.dtype, np.complexfloating):<EOL><INDENT>if OPpart is not None:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>mode = <NUM_LIT:3><EOL><DEDENT>elif OPpart is None or OPpart.lower() == '<STR_LIT:r>':<EOL><INDENT>mode = <NUM_LIT:3><EOL><DEDENT>elif OPpart.lower() == '<STR_LIT:i>':<EOL><INDENT>if np.imag(sigma) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>mode = <NUM_LIT:4><EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>matvec = _aslinearoperator_with_dtype(A).matvec<EOL>if Minv is not None:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if OPinv is None:<EOL><INDENT>Minv_matvec = get_OPinv_matvec(A, M, sigma,<EOL>symmetric=False, tol=tol)<EOL><DEDENT>else:<EOL><INDENT>OPinv = _aslinearoperator_with_dtype(OPinv)<EOL>Minv_matvec = OPinv.matvec<EOL><DEDENT>if M is None:<EOL><INDENT>M_matvec = None<EOL><DEDENT>else:<EOL><INDENT>M_matvec = _aslinearoperator_with_dtype(M).matvec<EOL><DEDENT><DEDENT>params = _UnsymmetricArpackParams(n, k, A.dtype.char, matvec, mode,<EOL>M_matvec, Minv_matvec, sigma,<EOL>ncv, v0, maxiter, which, tol)<EOL>while not params.converged:<EOL><INDENT>params.iterate()<EOL><DEDENT>return params.extract(return_eigenvectors)<EOL>

Find k eigenvalues and eigenvectors of the square matrix A.

Solves ``A * x[i] = w[i] * x[i]``, the standard eigenvalue problem
for w[i] eigenvalues with corresponding eigenvectors x[i].

If M is specified, solves ``A * x[i] = w[i] * M * x[i]``, the
generalized eigenvalue problem for w[i] eigenvalues
with corresponding eigenvectors x[i]

Parameters
----------
A : ndarray, sparse matrix or LinearOperator
    An array, sparse matrix, or LinearOperator representing
    the operation ``A * x``, where A is a real or complex square matrix.
k : int, optional
    The number of eigenvalues and eigenvectors desired.
    `k` must be smaller than N. It is not possible to compute all
    eigenvectors of a matrix.
M : ndarray, sparse matrix or LinearOperator, optional
    An array, sparse matrix, or LinearOperator representing
    the operation M*x for the generalized eigenvalue problem

        A * x = w * M * x.

    M must represent a real, symmetric matrix if A is real, and must
    represent a complex, hermitian matrix if A is complex. For best
    results, the data type of M should be the same as that of A.
    Additionally:

        If `sigma` is None, M is positive definite

        If sigma is specified, M is positive semi-definite

    If sigma is None, eigs requires an operator to compute the solution
    of the linear equation ``M * x = b``.  This is done internally via a
    (sparse) LU decomposition for an explicit matrix M, or via an
    iterative solver for a general linear operator.  Alternatively,
    the user can supply the matrix or operator Minv, which gives
    ``x = Minv * b = M^-1 * b``.
sigma : real or complex, optional
    Find eigenvalues near sigma using shift-invert mode.  This requires
    an operator to compute the solution of the linear system
    ``[A - sigma * M] * x = b``, where M is the identity matrix if
    unspecified. This is computed internally via a (sparse) LU
    decomposition for explicit matrices A & M, or via an iterative
    solver if either A or M is a general linear operator.
    Alternatively, the user can supply the matrix or operator OPinv,
    which gives ``x = OPinv * b = [A - sigma * M]^-1 * b``.
    For a real matrix A, shift-invert can either be done in imaginary
    mode or real mode, specified by the parameter OPpart ('r' or 'i').
    Note that when sigma is specified, the keyword 'which' (below)
    refers to the shifted eigenvalues ``w'[i]`` where:

        If A is real and OPpart == 'r' (default),
          ``w'[i] = 1/2 * [1/(w[i]-sigma) + 1/(w[i]-conj(sigma))]``.

        If A is real and OPpart == 'i',
          ``w'[i] = 1/2i * [1/(w[i]-sigma) - 1/(w[i]-conj(sigma))]``.

        If A is complex, ``w'[i] = 1/(w[i]-sigma)``.

v0 : ndarray, optional
    Starting vector for iteration.
    Default: random
ncv : int, optional
    The number of Lanczos vectors generated
    `ncv` must be greater than `k`; it is recommended that ``ncv > 2*k``.
    Default: ``min(n, 2*k + 1)``
which : str, ['LM' | 'SM' | 'LR' | 'SR' | 'LI' | 'SI'], optional
    Which `k` eigenvectors and eigenvalues to find:

        'LM' : largest magnitude

        'SM' : smallest magnitude

        'LR' : largest real part

        'SR' : smallest real part

        'LI' : largest imaginary part

        'SI' : smallest imaginary part

    When sigma != None, 'which' refers to the shifted eigenvalues w'[i]
    (see discussion in 'sigma', above).  ARPACK is generally better
    at finding large values than small values.  If small eigenvalues are
    desired, consider using shift-invert mode for better performance.
maxiter : int, optional
    Maximum number of Arnoldi update iterations allowed
    Default: ``n*10``
tol : float, optional
    Relative accuracy for eigenvalues (stopping criterion)
    The default value of 0 implies machine precision.
return_eigenvectors : bool, optional
    Return eigenvectors (True) in addition to eigenvalues
Minv : ndarray, sparse matrix or LinearOperator, optional
    See notes in M, above.
OPinv : ndarray, sparse matrix or LinearOperator, optional
    See notes in sigma, above.
OPpart : {'r' or 'i'}, optional
    See notes in sigma, above

Returns
-------
w : ndarray
    Array of k eigenvalues.
v : ndarray
    An array of `k` eigenvectors.
    ``v[:, i]`` is the eigenvector corresponding to the eigenvalue w[i].

Raises
------
ArpackNoConvergence
    When the requested convergence is not obtained.
    The currently converged eigenvalues and eigenvectors can be found
    as ``eigenvalues`` and ``eigenvectors`` attributes of the exception
    object.

See Also
--------
eigsh : eigenvalues and eigenvectors for symmetric matrix A
svds : singular value decomposition for a matrix A

Notes
-----
This function is a wrapper to the ARPACK [1]_ SNEUPD, DNEUPD, CNEUPD,
ZNEUPD, functions which use the Implicitly Restarted Arnoldi Method to
find the eigenvalues and eigenvectors [2]_.

References
----------
.. [1] ARPACK Software, http://www.caam.rice.edu/software/ARPACK/
.. [2] R. B. Lehoucq, D. C. Sorensen, and C. Yang,  ARPACK USERS GUIDE:
   Solution of Large Scale Eigenvalue Problems by Implicitly Restarted
   Arnoldi Methods. SIAM, Philadelphia, PA, 1998.

Examples
--------
Find 6 eigenvectors of the identity matrix:

>>> import scipy.sparse as sparse
>>> id = np.eye(13)
>>> vals, vecs = sparse.linalg.eigs(id, k=6)
>>> vals
array([ 1.+0.j,  1.+0.j,  1.+0.j,  1.+0.j,  1.+0.j,  1.+0.j])
>>> vecs.shape
(13, 6)


def eigsh(A, k=<NUM_LIT:6>, M=None, sigma=None, which='<STR_LIT>', v0=None,<EOL>ncv=None, maxiter=None, tol=<NUM_LIT:0>, return_eigenvectors=True,<EOL>Minv=None, OPinv=None, mode='<STR_LIT>'):

<EOL>if np.issubdtype(A.dtype, np.complexfloating):<EOL><INDENT>if mode != '<STR_LIT>':<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>" % mode)<EOL><DEDENT>if which == '<STR_LIT>':<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>elif which == '<STR_LIT>':<EOL><INDENT>which = '<STR_LIT>'<EOL><DEDENT>elif which == '<STR_LIT>':<EOL><INDENT>which = '<STR_LIT>'<EOL><DEDENT>ret = eigs(A, k, M=M, sigma=sigma, which=which, v0=v0,<EOL>ncv=ncv, maxiter=maxiter, tol=tol,<EOL>return_eigenvectors=return_eigenvectors, Minv=Minv,<EOL>OPinv=OPinv)<EOL>if return_eigenvectors:<EOL><INDENT>return ret[<NUM_LIT:0>].real, ret[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>return ret.real<EOL><DEDENT><DEDENT>if A.shape[<NUM_LIT:0>] != A.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ValueError('<STR_LIT>' % (A.shape,))<EOL><DEDENT>if M is not None:<EOL><INDENT>if M.shape != A.shape:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>% (M.shape, A.shape))<EOL><DEDENT>if np.dtype(M.dtype).char.lower() != np.dtype(A.dtype).char.lower():<EOL><INDENT>import warnings<EOL>warnings.warn('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>n = A.shape[<NUM_LIT:0>]<EOL>if k <= <NUM_LIT:0> or k >= n:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if sigma is None:<EOL><INDENT>A = _aslinearoperator_with_dtype(A)<EOL>matvec = A.matvec<EOL>if OPinv is not None:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if M is None:<EOL><INDENT>mode = <NUM_LIT:1><EOL>M_matvec = None<EOL>Minv_matvec = None<EOL>if Minv is not None:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>mode = <NUM_LIT:2><EOL>if Minv is None:<EOL><INDENT>Minv_matvec = get_inv_matvec(M, symmetric=True, tol=tol)<EOL><DEDENT>else:<EOL><INDENT>Minv = _aslinearoperator_with_dtype(Minv)<EOL>Minv_matvec = Minv.matvec<EOL><DEDENT>M_matvec = _aslinearoperator_with_dtype(M).matvec<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if Minv is not None:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if mode == '<STR_LIT>':<EOL><INDENT>mode = <NUM_LIT:3><EOL>matvec = None<EOL>if OPinv is None:<EOL><INDENT>Minv_matvec = get_OPinv_matvec(A, M, sigma,<EOL>symmetric=True, tol=tol)<EOL><DEDENT>else:<EOL><INDENT>OPinv = _aslinearoperator_with_dtype(OPinv)<EOL>Minv_matvec = OPinv.matvec<EOL><DEDENT>if M is None:<EOL><INDENT>M_matvec = None<EOL><DEDENT>else:<EOL><INDENT>M = _aslinearoperator_with_dtype(M)<EOL>M_matvec = M.matvec<EOL><DEDENT><DEDENT>elif mode == '<STR_LIT>':<EOL><INDENT>mode = <NUM_LIT:4><EOL>if OPinv is None:<EOL><INDENT>Minv_matvec = get_OPinv_matvec(A, M, sigma,<EOL>symmetric=True, tol=tol)<EOL><DEDENT>else:<EOL><INDENT>Minv_matvec = _aslinearoperator_with_dtype(OPinv).matvec<EOL><DEDENT>matvec = _aslinearoperator_with_dtype(A).matvec<EOL>M_matvec = None<EOL><DEDENT>elif mode == '<STR_LIT>':<EOL><INDENT>mode = <NUM_LIT:5><EOL>matvec = _aslinearoperator_with_dtype(A).matvec<EOL>if OPinv is None:<EOL><INDENT>Minv_matvec = get_OPinv_matvec(A, M, sigma,<EOL>symmetric=True, tol=tol)<EOL><DEDENT>else:<EOL><INDENT>Minv_matvec = _aslinearoperator_with_dtype(OPinv).matvec<EOL><DEDENT>if M is None:<EOL><INDENT>M_matvec = None<EOL><DEDENT>else:<EOL><INDENT>M_matvec = _aslinearoperator_with_dtype(M).matvec<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>" % mode)<EOL><DEDENT><DEDENT>params = _SymmetricArpackParams(n, k, A.dtype.char, matvec, mode,<EOL>M_matvec, Minv_matvec, sigma,<EOL>ncv, v0, maxiter, which, tol)<EOL>while not params.converged:<EOL><INDENT>params.iterate()<EOL><DEDENT>return params.extract(return_eigenvectors)<EOL>

Find k eigenvalues and eigenvectors of the real symmetric square matrix
or complex hermitian matrix A.

Solves ``A * x[i] = w[i] * x[i]``, the standard eigenvalue problem for
w[i] eigenvalues with corresponding eigenvectors x[i].

If M is specified, solves ``A * x[i] = w[i] * M * x[i]``, the
generalized eigenvalue problem for w[i] eigenvalues
with corresponding eigenvectors x[i]

Parameters
----------
A : An N x N matrix, array, sparse matrix, or LinearOperator representing
    the operation A * x, where A is a real symmetric matrix
    For buckling mode (see below) A must additionally be positive-definite
k : int
    The number of eigenvalues and eigenvectors desired.
    `k` must be smaller than N. It is not possible to compute all
    eigenvectors of a matrix.

Returns
-------
w : array
    Array of k eigenvalues
v : array
    An array representing the `k` eigenvectors.  The column ``v[:, i]`` is
    the eigenvector corresponding to the eigenvalue ``w[i]``.

Other Parameters
----------------
M : An N x N matrix, array, sparse matrix, or linear operator representing
    the operation M * x for the generalized eigenvalue problem

        A * x = w * M * x.

    M must represent a real, symmetric matrix if A is real, and must
    represent a complex, hermitian matrix if A is complex. For best
    results, the data type of M should be the same as that of A.
    Additionally:

        If sigma is None, M is symmetric positive definite

        If sigma is specified, M is symmetric positive semi-definite

        In buckling mode, M is symmetric indefinite.

    If sigma is None, eigsh requires an operator to compute the solution
    of the linear equation ``M * x = b``. This is done internally via a
    (sparse) LU decomposition for an explicit matrix M, or via an
    iterative solver for a general linear operator.  Alternatively,
    the user can supply the matrix or operator Minv, which gives
    ``x = Minv * b = M^-1 * b``.
sigma : real
    Find eigenvalues near sigma using shift-invert mode.  This requires
    an operator to compute the solution of the linear system
    `[A - sigma * M] x = b`, where M is the identity matrix if
    unspecified.  This is computed internally via a (sparse) LU
    decomposition for explicit matrices A & M, or via an iterative
    solver if either A or M is a general linear operator.
    Alternatively, the user can supply the matrix or operator OPinv,
    which gives ``x = OPinv * b = [A - sigma * M]^-1 * b``.
    Note that when sigma is specified, the keyword 'which' refers to
    the shifted eigenvalues ``w'[i]`` where:

        if mode == 'normal', ``w'[i] = 1 / (w[i] - sigma)``.

        if mode == 'cayley', ``w'[i] = (w[i] + sigma) / (w[i] - sigma)``.

        if mode == 'buckling', ``w'[i] = w[i] / (w[i] - sigma)``.

    (see further discussion in 'mode' below)
v0 : ndarray, optional
    Starting vector for iteration.
    Default: random
ncv : int, optional
    The number of Lanczos vectors generated ncv must be greater than k and
    smaller than n; it is recommended that ``ncv > 2*k``.
    Default: ``min(n, 2*k + 1)``
which : str ['LM' | 'SM' | 'LA' | 'SA' | 'BE']
    If A is a complex hermitian matrix, 'BE' is invalid.
    Which `k` eigenvectors and eigenvalues to find:

        'LM' : Largest (in magnitude) eigenvalues

        'SM' : Smallest (in magnitude) eigenvalues

        'LA' : Largest (algebraic) eigenvalues

        'SA' : Smallest (algebraic) eigenvalues

        'BE' : Half (k/2) from each end of the spectrum

    When k is odd, return one more (k/2+1) from the high end.
    When sigma != None, 'which' refers to the shifted eigenvalues ``w'[i]``
    (see discussion in 'sigma', above).  ARPACK is generally better
    at finding large values than small values.  If small eigenvalues are
    desired, consider using shift-invert mode for better performance.
maxiter : int, optional
    Maximum number of Arnoldi update iterations allowed
    Default: ``n*10``
tol : float
    Relative accuracy for eigenvalues (stopping criterion).
    The default value of 0 implies machine precision.
Minv : N x N matrix, array, sparse matrix, or LinearOperator
    See notes in M, above
OPinv : N x N matrix, array, sparse matrix, or LinearOperator
    See notes in sigma, above.
return_eigenvectors : bool
    Return eigenvectors (True) in addition to eigenvalues
mode : string ['normal' | 'buckling' | 'cayley']
    Specify strategy to use for shift-invert mode.  This argument applies
    only for real-valued A and sigma != None.  For shift-invert mode,
    ARPACK internally solves the eigenvalue problem
    ``OP * x'[i] = w'[i] * B * x'[i]``
    and transforms the resulting Ritz vectors x'[i] and Ritz values w'[i]
    into the desired eigenvectors and eigenvalues of the problem
    ``A * x[i] = w[i] * M * x[i]``.
    The modes are as follows:

        'normal' :
            OP = [A - sigma * M]^-1 * M,
            B = M,
            w'[i] = 1 / (w[i] - sigma)

        'buckling' :
            OP = [A - sigma * M]^-1 * A,
            B = A,
            w'[i] = w[i] / (w[i] - sigma)

        'cayley' :
            OP = [A - sigma * M]^-1 * [A + sigma * M],
            B = M,
            w'[i] = (w[i] + sigma) / (w[i] - sigma)

    The choice of mode will affect which eigenvalues are selected by
    the keyword 'which', and can also impact the stability of
    convergence (see [2] for a discussion)

Raises
------
ArpackNoConvergence
    When the requested convergence is not obtained.

    The currently converged eigenvalues and eigenvectors can be found
    as ``eigenvalues`` and ``eigenvectors`` attributes of the exception
    object.

See Also
--------
eigs : eigenvalues and eigenvectors for a general (nonsymmetric) matrix A
svds : singular value decomposition for a matrix A

Notes
-----
This function is a wrapper to the ARPACK [1]_ SSEUPD and DSEUPD
functions which use the Implicitly Restarted Lanczos Method to
find the eigenvalues and eigenvectors [2]_.

References
----------
.. [1] ARPACK Software, http://www.caam.rice.edu/software/ARPACK/
.. [2] R. B. Lehoucq, D. C. Sorensen, and C. Yang,  ARPACK USERS GUIDE:
   Solution of Large Scale Eigenvalue Problems by Implicitly Restarted
   Arnoldi Methods. SIAM, Philadelphia, PA, 1998.

Examples
--------
>>> import scipy.sparse as sparse
>>> id = np.eye(13)
>>> vals, vecs = sparse.linalg.eigsh(id, k=6)
>>> vals
array([ 1.+0.j,  1.+0.j,  1.+0.j,  1.+0.j,  1.+0.j,  1.+0.j])
>>> vecs.shape
(13, 6)


def svds(A, k=<NUM_LIT:6>, ncv=None, tol=<NUM_LIT:0>, which='<STR_LIT>', v0=None,<EOL>maxiter=None, return_singular_vectors=True):

if not (isinstance(A, LinearOperator) or isspmatrix(A)):<EOL><INDENT>A = np.asarray(A)<EOL><DEDENT>n, m = A.shape<EOL>if isinstance(A, LinearOperator):<EOL><INDENT>if n > m:<EOL><INDENT>X_dot = A.matvec<EOL>X_matmat = A.matmat<EOL>XH_dot = A.rmatvec<EOL><DEDENT>else:<EOL><INDENT>X_dot = A.rmatvec<EOL>XH_dot = A.matvec<EOL>dtype = getattr(A, '<STR_LIT>', None)<EOL>if dtype is None:<EOL><INDENT>dtype = A.dot(np.zeros([m,<NUM_LIT:1>])).dtype<EOL><DEDENT>def X_matmat(V):<EOL><INDENT>out = np.empty((V.shape[<NUM_LIT:1>], m), dtype=dtype)<EOL>for i, col in enumerate(V.T):<EOL><INDENT>out[i, :] = A.rmatvec(col.reshape(-<NUM_LIT:1>, <NUM_LIT:1>)).T<EOL><DEDENT>return out.T<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>if n > m:<EOL><INDENT>X_dot = X_matmat = A.dot<EOL>XH_dot = _herm(A).dot<EOL><DEDENT>else:<EOL><INDENT>XH_dot = A.dot<EOL>X_dot = X_matmat = _herm(A).dot<EOL><DEDENT><DEDENT>def matvec_XH_X(x):<EOL><INDENT>return XH_dot(X_dot(x))<EOL><DEDENT>XH_X = LinearOperator(matvec=matvec_XH_X, dtype=A.dtype,<EOL>shape=(min(A.shape), min(A.shape)))<EOL>eigvals, eigvec = eigsh(XH_X, k=k, tol=tol ** <NUM_LIT:2>, maxiter=maxiter,<EOL>ncv=ncv, which=which, v0=v0)<EOL>if which == '<STR_LIT>':<EOL><INDENT>eigvals = np.maximum(eigvals.real, <NUM_LIT:0>)<EOL>t = eigvec.dtype.char.lower()<EOL>factor = {'<STR_LIT:f>': <NUM_LIT>, '<STR_LIT:d>': <NUM_LIT>}<EOL>cond = factor[t] * np.finfo(t).eps<EOL>cutoff = cond * np.max(eigvals)<EOL>above_cutoff = (eigvals > cutoff)<EOL>nlarge = above_cutoff.sum()<EOL>nsmall = k - nlarge<EOL>slarge = np.sqrt(eigvals[above_cutoff])<EOL>s = np.zeros_like(eigvals)<EOL>s[:nlarge] = slarge<EOL>if not return_singular_vectors:<EOL><INDENT>return s<EOL><DEDENT>if n > m:<EOL><INDENT>vlarge = eigvec[:, above_cutoff]<EOL>ularge = X_matmat(vlarge) / slarge<EOL>vhlarge = _herm(vlarge)<EOL><DEDENT>else:<EOL><INDENT>ularge = eigvec[:, above_cutoff]<EOL>vhlarge = _herm(X_matmat(ularge) / slarge)<EOL><DEDENT>u = _augmented_orthonormal_cols(ularge, nsmall)<EOL>vh = _augmented_orthonormal_rows(vhlarge, nsmall)<EOL><DEDENT>else:<EOL><INDENT>s = np.sqrt(eigvals)<EOL>if not return_singular_vectors:<EOL><INDENT>return s<EOL><DEDENT>if n > m:<EOL><INDENT>v = eigvec<EOL>u = X_matmat(v) / s<EOL>vh = _herm(v)<EOL><DEDENT>else:<EOL><INDENT>u = eigvec<EOL>vh = _herm(X_matmat(u) / s)<EOL><DEDENT><DEDENT>return u, s, vh<EOL>

Compute the largest k singular values/vectors for a sparse matrix.

    Parameters
    ----------
    A : {sparse matrix, LinearOperator}
        Array to compute the SVD on, of shape (M, N)
    k : int, optional
        Number of singular values and vectors to compute.
    ncv : int, optional
        The number of Lanczos vectors generated
        ncv must be greater than k+1 and smaller than n;
        it is recommended that ncv > 2*k
        Default: ``min(n, 2*k + 1)``
    tol : float, optional
        Tolerance for singular values. Zero (default) means machine precision.
    which : str, ['LM' | 'SM'], optional
        Which `k` singular values to find:

            - 'LM' : largest singular values
            - 'SM' : smallest singular values

        .. versionadded:: 0.12.0
    v0 : ndarray, optional
        Starting vector for iteration, of length min(A.shape). Should be an
        (approximate) right singular vector if N > M and a right singular vector
        otherwise.
        Default: random

        .. versionadded:: 0.12.0
    maxiter: int, optional
        Maximum number of iterations.

        .. versionadded:: 0.12.0
    return_singular_vectors : bool, optional
        Return singular vectors (True) in addition to singular values

        .. versionadded:: 0.12.0

    Returns
    -------
    u : ndarray, shape=(M, k)
        Unitary matrix having left singular vectors as columns.
    s : ndarray, shape=(k,)
        The singular values.
    vt : ndarray, shape=(k, N)
        Unitary matrix having right singular vectors as rows.

    Notes
    -----
    This is a naive implementation using ARPACK as an eigensolver
    on A.H * A or A * A.H, depending on which one is more efficient.


A = scipy.sparse.eye(n, n)<EOL>d0 = array(r_[<NUM_LIT:5>,<NUM_LIT:6>*ones(n-<NUM_LIT:2>),<NUM_LIT:5>])<EOL>d1 = -<NUM_LIT:4>*ones(n)<EOL>d2 = ones(n)<EOL>B = scipy.sparse.spdiags([d2,d1,d0,d1,d2],[-<NUM_LIT:2>,-<NUM_LIT:1>,<NUM_LIT:0>,<NUM_LIT:1>,<NUM_LIT:2>],n,n)<EOL>k = arange(<NUM_LIT:1>,n+<NUM_LIT:1>)<EOL>w_ex = sort(<NUM_LIT:1.>/(<NUM_LIT>*pow(cos(<NUM_LIT:0.5>*k*pi/(n+<NUM_LIT:1>)),<NUM_LIT:4>)))  <EOL>return A, B, w_ex<EOL>

Example taken from
        T. Sakurai, H. Tadano, Y. Inadomi and U. Nagashima
        A moment-based method for large-scale generalized eigenvalue problems
        Appl. Num. Anal. Comp. Math. Vol. 1 No. 2 (2004)


if ar.ndim == <NUM_LIT:2>:<EOL><INDENT>return ar<EOL><DEDENT>else:  <EOL><INDENT>aux = np.array(ar, copy=False)<EOL>aux.shape = (ar.shape[<NUM_LIT:0>], <NUM_LIT:1>)<EOL>return aux<EOL><DEDENT>

If the input array is 2D return it, if it is 1D, append a dimension,
making it a column vector.


if operatorInput is None:<EOL><INDENT>def ident(x):<EOL><INDENT>return x<EOL><DEDENT>operator = LinearOperator(expectedShape, ident, matmat=ident)<EOL><DEDENT>else:<EOL><INDENT>operator = aslinearoperator(operatorInput)<EOL><DEDENT>if operator.shape != expectedShape:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if sys.version_info[<NUM_LIT:0>] >= <NUM_LIT:3>:<EOL><INDENT>operator.__class__ = CallableLinearOperator<EOL><DEDENT>else:<EOL><INDENT>operator.__call__ = operator.matmat<EOL><DEDENT>return operator<EOL>

Takes a dense numpy array or a sparse matrix or
    a function and makes an operator performing matrix * blockvector
    products.

    Examples
    --------
    >>> A = _makeOperator( arrayA, (n, n) )
    >>> vectorB = A( vectorX )


def _applyConstraints(blockVectorV, factYBY, blockVectorBY, blockVectorY):

gramYBV = np.dot(blockVectorBY.T, blockVectorV)<EOL>tmp = cho_solve(factYBY, gramYBV)<EOL>blockVectorV -= np.dot(blockVectorY, tmp)<EOL>


def lobpcg(A, X,<EOL>B=None, M=None, Y=None,<EOL>tol=None, maxiter=<NUM_LIT:20>,<EOL>largest=True, verbosityLevel=<NUM_LIT:0>,<EOL>retLambdaHistory=False, retResidualNormsHistory=False):

failureFlag = True<EOL>blockVectorX = X<EOL>blockVectorY = Y<EOL>residualTolerance = tol<EOL>maxIterations = maxiter<EOL>if blockVectorY is not None:<EOL><INDENT>sizeY = blockVectorY.shape[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>sizeY = <NUM_LIT:0><EOL><DEDENT>if len(blockVectorX.shape) != <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>n, sizeX = blockVectorX.shape<EOL>if sizeX > n:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>A = _makeOperator(A, (n,n))<EOL>B = _makeOperator(B, (n,n))<EOL>M = _makeOperator(M, (n,n))<EOL>if (n - sizeY) < (<NUM_LIT:5> * sizeX):<EOL><INDENT>if blockVectorY is not None:<EOL><INDENT>raise NotImplementedError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>if largest:<EOL><INDENT>eigvals = (n - sizeX, n-<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>eigvals = (<NUM_LIT:0>, sizeX-<NUM_LIT:1>)<EOL><DEDENT>A_dense = A(np.eye(n))<EOL>B_dense = None if B is None else B(np.eye(n))<EOL>return eigh(A_dense, B_dense, eigvals=eigvals, check_finite=False)<EOL><DEDENT>if residualTolerance is None:<EOL><INDENT>residualTolerance = np.sqrt(<NUM_LIT>) * n<EOL><DEDENT>maxIterations = min(n, maxIterations)<EOL>if verbosityLevel:<EOL><INDENT>aux = "<STR_LIT>"<EOL>if B is None:<EOL><INDENT>aux += "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>aux += "<STR_LIT>"<EOL><DEDENT>aux += "<STR_LIT>"<EOL>if M is None:<EOL><INDENT>aux += "<STR_LIT>"<EOL><DEDENT>aux += "<STR_LIT>"<EOL>aux += "<STR_LIT>" % n<EOL>aux += "<STR_LIT>" % sizeX<EOL>if blockVectorY is None:<EOL><INDENT>aux += "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>if sizeY > <NUM_LIT:1>:<EOL><INDENT>aux += "<STR_LIT>" % sizeY<EOL><DEDENT>else:<EOL><INDENT>aux += "<STR_LIT>" % sizeY<EOL><DEDENT><DEDENT>print(aux)<EOL><DEDENT>if blockVectorY is not None:<EOL><INDENT>if B is not None:<EOL><INDENT>blockVectorBY = B(blockVectorY)<EOL><DEDENT>else:<EOL><INDENT>blockVectorBY = blockVectorY<EOL><DEDENT>gramYBY = np.dot(blockVectorY.T, blockVectorBY)<EOL>try:<EOL><INDENT>gramYBY = cho_factor(gramYBY)<EOL><DEDENT>except:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>_applyConstraints(blockVectorX, gramYBY, blockVectorBY, blockVectorY)<EOL><DEDENT>blockVectorX, blockVectorBX = _b_orthonormalize(B, blockVectorX)<EOL>blockVectorAX = A(blockVectorX)<EOL>gramXAX = np.dot(blockVectorX.T, blockVectorAX)<EOL>gramXBX = np.dot(blockVectorX.T, blockVectorX)<EOL>_lambda, eigBlockVector = eigh(gramXAX, check_finite=False)<EOL>ii = np.argsort(_lambda)[:sizeX]<EOL>if largest:<EOL><INDENT>ii = ii[::-<NUM_LIT:1>]<EOL><DEDENT>_lambda = _lambda[ii]<EOL>eigBlockVector = np.asarray(eigBlockVector[:,ii])<EOL>blockVectorX = np.dot(blockVectorX, eigBlockVector)<EOL>blockVectorAX = np.dot(blockVectorAX, eigBlockVector)<EOL>if B is not None:<EOL><INDENT>blockVectorBX = np.dot(blockVectorBX, eigBlockVector)<EOL><DEDENT>activeMask = np.ones((sizeX,), dtype=np.bool)<EOL>lambdaHistory = [_lambda]<EOL>residualNormsHistory = []<EOL>previousBlockSize = sizeX<EOL>ident = np.eye(sizeX, dtype=A.dtype)<EOL>ident0 = np.eye(sizeX, dtype=A.dtype)<EOL>blockVectorP = None  <EOL>blockVectorAP = None<EOL>blockVectorBP = None<EOL>for iterationNumber in xrange(maxIterations):<EOL><INDENT>if verbosityLevel > <NUM_LIT:0>:<EOL><INDENT>print('<STR_LIT>' % iterationNumber)<EOL><DEDENT>aux = blockVectorBX * _lambda[np.newaxis,:]<EOL>blockVectorR = blockVectorAX - aux<EOL>aux = np.sum(blockVectorR.conjugate() * blockVectorR, <NUM_LIT:0>)<EOL>residualNorms = np.sqrt(aux)<EOL>residualNormsHistory.append(residualNorms)<EOL>ii = np.where(residualNorms > residualTolerance, True, False)<EOL>activeMask = activeMask & ii<EOL>if verbosityLevel > <NUM_LIT:2>:<EOL><INDENT>print(activeMask)<EOL><DEDENT>currentBlockSize = activeMask.sum()<EOL>if currentBlockSize != previousBlockSize:<EOL><INDENT>previousBlockSize = currentBlockSize<EOL>ident = np.eye(currentBlockSize, dtype=A.dtype)<EOL><DEDENT>if currentBlockSize == <NUM_LIT:0>:<EOL><INDENT>failureFlag = False  <EOL>break<EOL><DEDENT>if verbosityLevel > <NUM_LIT:0>:<EOL><INDENT>print('<STR_LIT>', currentBlockSize)<EOL>print('<STR_LIT>', _lambda)<EOL>print('<STR_LIT>', residualNorms)<EOL><DEDENT>if verbosityLevel > <NUM_LIT:10>:<EOL><INDENT>print(eigBlockVector)<EOL><DEDENT>activeBlockVectorR = as2d(blockVectorR[:,activeMask])<EOL>if iterationNumber > <NUM_LIT:0>:<EOL><INDENT>activeBlockVectorP = as2d(blockVectorP[:,activeMask])<EOL>activeBlockVectorAP = as2d(blockVectorAP[:,activeMask])<EOL>activeBlockVectorBP = as2d(blockVectorBP[:,activeMask])<EOL><DEDENT>if M is not None:<EOL><INDENT>activeBlockVectorR = M(activeBlockVectorR)<EOL><DEDENT>if blockVectorY is not None:<EOL><INDENT>_applyConstraints(activeBlockVectorR,<EOL>gramYBY, blockVectorBY, blockVectorY)<EOL><DEDENT>aux = _b_orthonormalize(B, activeBlockVectorR)<EOL>activeBlockVectorR, activeBlockVectorBR = aux<EOL>activeBlockVectorAR = A(activeBlockVectorR)<EOL>if iterationNumber > <NUM_LIT:0>:<EOL><INDENT>aux = _b_orthonormalize(B, activeBlockVectorP,<EOL>activeBlockVectorBP, retInvR=True)<EOL>activeBlockVectorP, activeBlockVectorBP, invR = aux<EOL>activeBlockVectorAP = np.dot(activeBlockVectorAP, invR)<EOL><DEDENT>xaw = np.dot(blockVectorX.T, activeBlockVectorAR)<EOL>waw = np.dot(activeBlockVectorR.T, activeBlockVectorAR)<EOL>xbw = np.dot(blockVectorX.T, activeBlockVectorBR)<EOL>if iterationNumber > <NUM_LIT:0>:<EOL><INDENT>xap = np.dot(blockVectorX.T, activeBlockVectorAP)<EOL>wap = np.dot(activeBlockVectorR.T, activeBlockVectorAP)<EOL>pap = np.dot(activeBlockVectorP.T, activeBlockVectorAP)<EOL>xbp = np.dot(blockVectorX.T, activeBlockVectorBP)<EOL>wbp = np.dot(activeBlockVectorR.T, activeBlockVectorBP)<EOL>gramA = np.bmat([[np.diag(_lambda), xaw, xap],<EOL>[xaw.T, waw, wap],<EOL>[xap.T, wap.T, pap]])<EOL>gramB = np.bmat([[ident0, xbw, xbp],<EOL>[xbw.T, ident, wbp],<EOL>[xbp.T, wbp.T, ident]])<EOL><DEDENT>else:<EOL><INDENT>gramA = np.bmat([[np.diag(_lambda), xaw],<EOL>[xaw.T, waw]])<EOL>gramB = np.bmat([[ident0, xbw],<EOL>[xbw.T, ident]])<EOL><DEDENT>_assert_symmetric(gramA)<EOL>_assert_symmetric(gramB)<EOL>if verbosityLevel > <NUM_LIT:10>:<EOL><INDENT>save(gramA, '<STR_LIT>')<EOL>save(gramB, '<STR_LIT>')<EOL><DEDENT>_lambda, eigBlockVector = eigh(gramA, gramB, check_finite=False)<EOL>ii = np.argsort(_lambda)[:sizeX]<EOL>if largest:<EOL><INDENT>ii = ii[::-<NUM_LIT:1>]<EOL><DEDENT>if verbosityLevel > <NUM_LIT:10>:<EOL><INDENT>print(ii)<EOL><DEDENT>_lambda = _lambda[ii].astype(np.float64)<EOL>eigBlockVector = np.asarray(eigBlockVector[:,ii].astype(np.float64))<EOL>lambdaHistory.append(_lambda)<EOL>if verbosityLevel > <NUM_LIT:10>:<EOL><INDENT>print('<STR_LIT>', _lambda)<EOL>

Locally Optimal Block Preconditioned Conjugate Gradient Method (LOBPCG)

    LOBPCG is a preconditioned eigensolver for large symmetric positive
    definite (SPD) generalized eigenproblems.

    Parameters
    ----------
    A : {sparse matrix, dense matrix, LinearOperator}
        The symmetric linear operator of the problem, usually a
        sparse matrix.  Often called the "stiffness matrix".
    X : array_like
        Initial approximation to the k eigenvectors. If A has
        shape=(n,n) then X should have shape shape=(n,k).
    B : {dense matrix, sparse matrix, LinearOperator}, optional
        the right hand side operator in a generalized eigenproblem.
        by default, B = Identity
        often called the "mass matrix"
    M : {dense matrix, sparse matrix, LinearOperator}, optional
        preconditioner to A; by default M = Identity
        M should approximate the inverse of A
    Y : array_like, optional
        n-by-sizeY matrix of constraints, sizeY < n
        The iterations will be performed in the B-orthogonal complement
        of the column-space of Y. Y must be full rank.

    Returns
    -------
    w : array
        Array of k eigenvalues
    v : array
        An array of k eigenvectors.  V has the same shape as X.

    Other Parameters
    ----------------
    tol : scalar, optional
        Solver tolerance (stopping criterion)
        by default: tol=n*sqrt(eps)
    maxiter : integer, optional
        maximum number of iterations
        by default: maxiter=min(n,20)
    largest : boolean, optional
        when True, solve for the largest eigenvalues, otherwise the smallest
    verbosityLevel : integer, optional
        controls solver output.  default: verbosityLevel = 0.
    retLambdaHistory : boolean, optional
        whether to return eigenvalue history
    retResidualNormsHistory : boolean, optional
        whether to return history of residual norms

    Examples
    --------
    >>> # Solve A x = lambda B x with constraints and preconditioning.
    >>> n = 100
    >>> vals = [nm.arange( n, dtype = nm.float64 ) + 1]
    >>> # Matrix A.
    >>> operatorA = spdiags( vals, 0, n, n )
    >>> # Matrix B
    >>> operatorB = nm.eye( n, n )
    >>> # Constraints.
    >>> Y = nm.eye( n, 3 )
    >>> # Initial guess for eigenvectors, should have linearly independent
    >>> # columns. Column dimension = number of requested eigenvalues.
    >>> X = sc.rand( n, 3 )
    >>> # Preconditioner - inverse of A.
    >>> ivals = [1./vals[0]]
    >>> def precond( x ):
        invA = spdiags( ivals, 0, n, n )
        y = invA  * x
        if sp.issparse( y ):
            y = y.toarray()

        return as2d( y )

    >>> # Alternative way of providing the same preconditioner.
    >>> #precond = spdiags( ivals, 0, n, n )

    >>> tt = time.clock()
    >>> eigs, vecs = lobpcg(X, operatorA, operatorB, blockVectorY=Y,
    >>>                     operatorT=precond,
    >>>                     residualTolerance=1e-4, maxIterations=40,
    >>>                     largest=False, verbosityLevel=1)
    >>> print 'solution time:', time.clock() - tt
    >>> print eigs

    Notes
    -----
    If both retLambdaHistory and retResidualNormsHistory are True,
    the return tuple has the following format
    (lambda, V, lambda history, residual norms history).

    In the following ``n`` denotes the matrix size and ``m`` the number
    of required eigenvalues (smallest or largest).

    The LOBPCG code internally solves eigenproblems of the size 3``m`` on every
    iteration by calling the "standard" dense eigensolver, so if ``m`` is not
    small enough compared to ``n``, it does not make sense to call the LOBPCG
    code, but rather one should use the "standard" eigensolver,
    e.g. numpy or scipy function in this case.
    If one calls the LOBPCG algorithm for 5``m``>``n``,
    it will most likely break internally, so the code tries to call the standard
    function instead.

    It is not that n should be large for the LOBPCG to work, but rather the
    ratio ``n``/``m`` should be large. It you call the LOBPCG code with ``m``=1
    and ``n``=10, it should work, though ``n`` is small. The method is intended
    for extremely large ``n``/``m``, see e.g., reference [28] in
    http://arxiv.org/abs/0705.2626

    The convergence speed depends basically on two factors:

    1.  How well relatively separated the seeking eigenvalues are
        from the rest of the eigenvalues.
        One can try to vary ``m`` to make this better.

    2.  How well conditioned the problem is. This can be changed by using proper
        preconditioning. For example, a rod vibration test problem (under tests
        directory) is ill-conditioned for large ``n``, so convergence will be
        slow, unless efficient preconditioning is used.
        For this specific problem, a good simple preconditioner function would
        be a linear solve for A, which is easy to code since A is tridiagonal.

    *Acknowledgements*

    lobpcg.py code was written by Robert Cimrman.
    Many thanks belong to Andrew Knyazev, the author of the algorithm,
    for lots of advice and support.

    References
    ----------
    .. [1] A. V. Knyazev (2001),
           Toward the Optimal Preconditioned Eigensolver: Locally Optimal
           Block Preconditioned Conjugate Gradient Method.
           SIAM Journal on Scientific Computing 23, no. 2,
           pp. 517-541. http://dx.doi.org/10.1137/S1064827500366124

    .. [2] A. V. Knyazev, I. Lashuk, M. E. Argentati, and E. Ovchinnikov (2007),
           Block Locally Optimal Preconditioned Eigenvalue Xolvers (BLOPEX)
           in hypre and PETSc.  http://arxiv.org/abs/0705.2626

    .. [3] A. V. Knyazev's C and MATLAB implementations:
           http://www-math.cudenver.edu/~aknyazev/software/BLOPEX/


if isinstance(A, LinearOperator):<EOL><INDENT>return A<EOL><DEDENT>elif isinstance(A, np.ndarray) or isinstance(A, np.matrix):<EOL><INDENT>if A.ndim > <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>A = np.atleast_2d(np.asarray(A))<EOL>return MatrixLinearOperator(A)<EOL><DEDENT>elif isspmatrix(A):<EOL><INDENT>return MatrixLinearOperator(A)<EOL><DEDENT>else:<EOL><INDENT>if hasattr(A, '<STR_LIT>') and hasattr(A, '<STR_LIT>'):<EOL><INDENT>rmatvec = None<EOL>dtype = None<EOL>if hasattr(A, '<STR_LIT>'):<EOL><INDENT>rmatvec = A.rmatvec<EOL><DEDENT>if hasattr(A, '<STR_LIT>'):<EOL><INDENT>dtype = A.dtype<EOL><DEDENT>return LinearOperator(A.shape, A.matvec,<EOL>rmatvec=rmatvec, dtype=dtype)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT><DEDENT>

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.

    Examples
    --------
    >>> from scipy import matrix
    >>> M = matrix( [[1,2,3],[4,5,6]], dtype='int32' )
    >>> aslinearoperator( M )
    <2x3 LinearOperator with dtype=int32>


return np.hstack([self.matvec(col.reshape(-<NUM_LIT:1>,<NUM_LIT:1>)) for col in X.T])<EOL>

Default matrix-matrix multiplication handler.  Falls back on
        the user-defined matvec() routine, which is always provided.


x = np.asanyarray(x)<EOL>M,N = self.shape<EOL>if x.shape != (N,) and x.shape != (N,<NUM_LIT:1>):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>y = self._matvec(x)<EOL>if isinstance(x, np.matrix):<EOL><INDENT>y = np.asmatrix(y)<EOL><DEDENT>else:<EOL><INDENT>y = np.asarray(y)<EOL><DEDENT>if x.ndim == <NUM_LIT:1>:<EOL><INDENT>y = y.reshape(M)<EOL><DEDENT>elif x.ndim == <NUM_LIT:2>:<EOL><INDENT>y = y.reshape(M,<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return y<EOL>

Matrix-vector multiplication

        Performs the operation y=A*x where A is an MxN linear
        operator and x is a column vector or rank-1 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 to ensure that
        y has the correct shape and type.


X = np.asanyarray(X)<EOL>if X.ndim != <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>M,N = self.shape<EOL>if X.shape[<NUM_LIT:0>] != N:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>Y = self._matmat(X)<EOL>if isinstance(Y, np.matrix):<EOL><INDENT>Y = np.asmatrix(Y)<EOL><DEDENT>return Y<EOL>

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 to ensure that
        y has the correct type.


if '<STR_LIT>' in kwargs:<EOL><INDENT>globals()['<STR_LIT>'] = kwargs['<STR_LIT>']<EOL><DEDENT>

Valid keyword arguments with defaults (other ignored)::

  useUmfpack = True
  assumeSortedIndices = False

The default sparse solver is umfpack when available. This can be changed by
passing useUmfpack = False, which then causes the always present SuperLU
based solver to be used.

Umfpack requires a CSR/CSC matrix to have sorted column/row indices. If
sure that the matrix fulfills this, pass ``assumeSortedIndices=True``
to gain some speed.


def spsolve(A, b, permc_spec=None, use_umfpack=True):

if not (isspmatrix_csc(A) or isspmatrix_csr(A)):<EOL><INDENT>A = csc_matrix(A)<EOL>warn('<STR_LIT>',<EOL>SparseEfficiencyWarning)<EOL><DEDENT>b_is_sparse = isspmatrix(b)<EOL>if not b_is_sparse:<EOL><INDENT>b = asarray(b)<EOL><DEDENT>b_is_vector = ((b.ndim == <NUM_LIT:1>) or (b.ndim == <NUM_LIT:2> and b.shape[<NUM_LIT:1>] == <NUM_LIT:1>))<EOL>A.sort_indices()<EOL>A = A.asfptype()  <EOL>M, N = A.shape<EOL>if (M != N):<EOL><INDENT>raise ValueError("<STR_LIT>" % ((M, N),))<EOL><DEDENT>if M != b.shape[<NUM_LIT:0>]:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>% (A.shape, b.shape[<NUM_LIT:0>]))<EOL><DEDENT>use_umfpack = use_umfpack and useUmfpack<EOL>if b_is_vector and use_umfpack:<EOL><INDENT>if b_is_sparse:<EOL><INDENT>b_vec = b.toarray()<EOL><DEDENT>else:<EOL><INDENT>b_vec = b<EOL><DEDENT>b_vec = asarray(b_vec, dtype=A.dtype).ravel()<EOL>if noScikit:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>if A.dtype.char not in '<STR_LIT>':<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>family = {'<STR_LIT:d>': '<STR_LIT>', '<STR_LIT:D>': '<STR_LIT>'}<EOL>umf = umfpack.UmfpackContext(family[A.dtype.char])<EOL>x = umf.linsolve(umfpack.UMFPACK_A, A, b_vec,<EOL>autoTranspose=True)<EOL><DEDENT>else:<EOL><INDENT>if b_is_vector and b_is_sparse:<EOL><INDENT>b = b.toarray()<EOL>b_is_sparse = False<EOL><DEDENT>if not b_is_sparse:<EOL><INDENT>if isspmatrix_csc(A):<EOL><INDENT>flag = <NUM_LIT:1>  <EOL><DEDENT>else:<EOL><INDENT>flag = <NUM_LIT:0>  <EOL><DEDENT>options = dict(ColPerm=permc_spec)<EOL>x, info = _superlu.gssv(N, A.nnz, A.data, A.indices, A.indptr,<EOL>b, flag, options=options)<EOL>if info != <NUM_LIT:0>:<EOL><INDENT>warn("<STR_LIT>", MatrixRankWarning)<EOL>x.fill(np.nan)<EOL><DEDENT>if b_is_vector:<EOL><INDENT>x = x.ravel()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>Afactsolve = factorized(A)<EOL>tempj = empty(M, dtype=int)<EOL>x = A.__class__(b.shape)<EOL>if not (isspmatrix_csr(b) or isspmatrix_csc(b)):<EOL><INDENT>warn('<STR_LIT>',<EOL>SparseEfficiencyWarning)<EOL>b = csc_matrix(b)<EOL><DEDENT>for j in range(b.shape[<NUM_LIT:1>]):<EOL><INDENT>col = b[:, j].toarray()<EOL>xj = Afactsolve(ravel(col))<EOL>w = nonzero(xj)[<NUM_LIT:0>]<EOL>tempj.fill(j)<EOL>x = x + A.__class__((xj[w], (w, tempj[:len(w)])),<EOL>shape=b.shape, dtype=A.dtype)<EOL><DEDENT><DEDENT><DEDENT>return x<EOL>

Solve the sparse linear system Ax=b, where b may be a vector or a matrix.

    Parameters
    ----------
    A : ndarray or sparse matrix
        The square matrix A will be converted into CSC or CSR form
    b : ndarray or sparse matrix
        The matrix or vector representing the right hand side of the equation.
        If a vector, b.size must
    permc_spec : str, optional
        How to permute the columns of the matrix for sparsity preservation.
        (default: 'COLAMD')

        - ``NATURAL``: natural ordering.
        - ``MMD_ATA``: minimum degree ordering on the structure of A^T A.
        - ``MMD_AT_PLUS_A``: minimum degree ordering on the structure of A^T+A.
        - ``COLAMD``: approximate minimum degree column ordering
    use_umfpack : bool (optional)
        if True (default) then use umfpack for the solution.  This is
        only referenced if b is a vector.

    Returns
    -------
    x : ndarray or sparse matrix
        the solution of the sparse linear equation.
        If b is a vector, then x is a vector of size A.shape[1]
        If b is a matrix, then x is a matrix of size (A.shape[1], b.shape[1])

    Notes
    -----
    For solving the matrix expression AX = B, this solver assumes the resulting
    matrix X is sparse, as is often the case for very sparse inputs.  If the
    resulting X is dense, the construction of this sparse result will be
    relatively expensive.  In that case, consider converting A to a dense
    matrix and using scipy.linalg.solve or its variants.


def splu(A, permc_spec=None, diag_pivot_thresh=None,<EOL>drop_tol=None, relax=None, panel_size=None, options=dict()):

if not isspmatrix_csc(A):<EOL><INDENT>A = csc_matrix(A)<EOL>warn('<STR_LIT>', SparseEfficiencyWarning)<EOL><DEDENT>A.sort_indices()<EOL>A = A.asfptype()  <EOL>M, N = A.shape<EOL>if (M != N):<EOL><INDENT>raise ValueError("<STR_LIT>")  <EOL><DEDENT>_options = dict(DiagPivotThresh=diag_pivot_thresh, ColPerm=permc_spec,<EOL>PanelSize=panel_size, Relax=relax)<EOL>if options is not None:<EOL><INDENT>_options.update(options)<EOL><DEDENT>return _superlu.gstrf(N, A.nnz, A.data, A.indices, A.indptr,<EOL>ilu=False, options=_options)<EOL>

Compute the LU decomposition of a sparse, square matrix.

Parameters
----------
A : sparse matrix
    Sparse matrix to factorize. Should be in CSR or CSC format.
permc_spec : str, optional
    How to permute the columns of the matrix for sparsity preservation.
    (default: 'COLAMD')

    - ``NATURAL``: natural ordering.
    - ``MMD_ATA``: minimum degree ordering on the structure of A^T A.
    - ``MMD_AT_PLUS_A``: minimum degree ordering on the structure of A^T+A.
    - ``COLAMD``: approximate minimum degree column ordering

diag_pivot_thresh : float, optional
    Threshold used for a diagonal entry to be an acceptable pivot.
    See SuperLU user's guide for details [SLU]_
drop_tol : float, optional
    (deprecated) No effect.
relax : int, optional
    Expert option for customizing the degree of relaxing supernodes.
    See SuperLU user's guide for details [SLU]_
panel_size : int, optional
    Expert option for customizing the panel size.
    See SuperLU user's guide for details [SLU]_
options : dict, optional
    Dictionary containing additional expert options to SuperLU.
    See SuperLU user guide [SLU]_ (section 2.4 on the 'Options' argument)
    for more details. For example, you can specify
    ``options=dict(Equil=False, IterRefine='SINGLE'))``
    to turn equilibration off and perform a single iterative refinement.

Returns
-------
invA : scipy.sparse.linalg.SuperLU
    Object, which has a ``solve`` method.

See also
--------
spilu : incomplete LU decomposition

Notes
-----
This function uses the SuperLU library.

References
----------
.. [SLU] SuperLU http://crd.lbl.gov/~xiaoye/SuperLU/


def spilu(A, drop_tol=None, fill_factor=None, drop_rule=None, permc_spec=None,<EOL>diag_pivot_thresh=None, relax=None, panel_size=None, options=None):

if not isspmatrix_csc(A):<EOL><INDENT>A = csc_matrix(A)<EOL>warn('<STR_LIT>', SparseEfficiencyWarning)<EOL><DEDENT>A.sort_indices()<EOL>A = A.asfptype()  <EOL>M, N = A.shape<EOL>if (M != N):<EOL><INDENT>raise ValueError("<STR_LIT>")  <EOL><DEDENT>_options = dict(ILU_DropRule=drop_rule, ILU_DropTol=drop_tol,<EOL>ILU_FillFactor=fill_factor,<EOL>DiagPivotThresh=diag_pivot_thresh, ColPerm=permc_spec,<EOL>PanelSize=panel_size, Relax=relax)<EOL>if options is not None:<EOL><INDENT>_options.update(options)<EOL><DEDENT>return _superlu.gstrf(N, A.nnz, A.data, A.indices, A.indptr,<EOL>ilu=True, options=_options)<EOL>

Compute an incomplete LU decomposition for a sparse, square matrix.

The resulting object is an approximation to the inverse of `A`.

Parameters
----------
A : (N, N) array_like
    Sparse matrix to factorize
drop_tol : float, optional
    Drop tolerance (0 <= tol <= 1) for an incomplete LU decomposition.
    (default: 1e-4)
fill_factor : float, optional
    Specifies the fill ratio upper bound (>= 1.0) for ILU. (default: 10)
drop_rule : str, optional
    Comma-separated string of drop rules to use.
    Available rules: ``basic``, ``prows``, ``column``, ``area``,
    ``secondary``, ``dynamic``, ``interp``. (Default: ``basic,area``)

    See SuperLU documentation for details.
milu : str, optional
    Which version of modified ILU to use. (Choices: ``silu``,
    ``smilu_1``, ``smilu_2`` (default), ``smilu_3``.)

Remaining other options
    Same as for `splu`

Returns
-------
invA_approx : scipy.sparse.linalg.SuperLU
    Object, which has a ``solve`` method.

See also
--------
splu : complete LU decomposition

Notes
-----
To improve the better approximation to the inverse, you may need to
increase `fill_factor` AND decrease `drop_tol`.

This function uses the SuperLU library.


if useUmfpack:<EOL><INDENT>if noScikit:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>if not isspmatrix_csc(A):<EOL><INDENT>A = csc_matrix(A)<EOL>warn('<STR_LIT>', SparseEfficiencyWarning)<EOL><DEDENT>A.sort_indices()<EOL>A = A.asfptype()  <EOL>if A.dtype.char not in '<STR_LIT>':<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>family = {'<STR_LIT:d>': '<STR_LIT>', '<STR_LIT:D>': '<STR_LIT>'}<EOL>umf = umfpack.UmfpackContext(family[A.dtype.char])<EOL>umf.numeric(A)<EOL>def solve(b):<EOL><INDENT>return umf.solve(umfpack.UMFPACK_A, A, b, autoTranspose=True)<EOL><DEDENT>return solve<EOL><DEDENT>else:<EOL><INDENT>return splu(A).solve<EOL><DEDENT>

Return a fuction for solving a sparse linear system, with A pre-factorized.

Parameters
----------
A : (N, N) array_like
    Input.

Returns
-------
solve : callable
    To solve the linear system of equations given in `A`, the `solve`
    callable should be passed an ndarray of shape (N,).

Examples
--------
>>> A = np.array([[ 3. ,  2. , -1. ],
                  [ 2. , -2. ,  4. ],
                  [-1. ,  0.5, -1. ]])

>>> solve = factorized( A ) # Makes LU decomposition.

>>> rhs1 = np.array([1,-2,0])
>>> x1 = solve( rhs1 ) # Uses the LU factors.
array([ 1., -2., -2.])


I = speye(A.shape[<NUM_LIT:0>], A.shape[<NUM_LIT:1>], dtype=A.dtype, format=A.format)<EOL>Ainv = spsolve(A, I)<EOL>return Ainv<EOL>

Compute the inverse of a sparse matrix

Parameters
----------
A : (M,M) ndarray or sparse matrix
    square matrix to be inverted

Returns
-------
Ainv : (M,M) ndarray or sparse matrix
    inverse of `A`

Notes
-----
This computes the sparse inverse of `A`.  If the inverse of `A` is expected
to be non-sparse, it will likely be faster to convert `A` to dense and use
scipy.linalg.inv.

.. versionadded:: 0.12.0


<EOL>if int(p) != p or p < <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>p = int(p)<EOL>if len(A.shape) != <NUM_LIT:2> or A.shape[<NUM_LIT:0>] != A.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>v = np.ones((A.shape[<NUM_LIT:0>], <NUM_LIT:1>), dtype=float)<EOL>M = A.T<EOL>for i in range(p):<EOL><INDENT>v = M.dot(v)<EOL><DEDENT>return max(v)<EOL>

Compute the 1-norm of a non-negative integer power of a non-negative matrix.

Parameters
----------
A : a square ndarray or matrix or sparse matrix
    Input matrix with non-negative entries.
p : non-negative integer
    The power to which the matrix is to be raised.

Returns
-------
out : float
    The 1-norm of the matrix power p of A.


def _smart_matrix_product(A, B, alpha=None, structure=None):

if len(A.shape) != <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if len(B.shape) != <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>f = None<EOL>if structure == UPPER_TRIANGULAR:<EOL><INDENT>if not isspmatrix(A) and not isspmatrix(B):<EOL><INDENT>f, = scipy.linalg.get_blas_funcs(('<STR_LIT>',), (A, B))<EOL><DEDENT><DEDENT>if f is not None:<EOL><INDENT>if alpha is None:<EOL><INDENT>alpha = <NUM_LIT:1.><EOL><DEDENT>out = f(alpha, A, B)<EOL><DEDENT>else:<EOL><INDENT>if alpha is None:<EOL><INDENT>out = A.dot(B)<EOL><DEDENT>else:<EOL><INDENT>out = alpha * A.dot(B)<EOL><DEDENT><DEDENT>return out<EOL>

A matrix product that knows about sparse and structured matrices.

Parameters
----------
A : 2d ndarray
    First matrix.
B : 2d ndarray
    Second matrix.
alpha : float
    The matrix product will be scaled by this constant.
structure : str, optional
    A string describing the structure of both matrices `A` and `B`.
    Only `upper_triangular` is currently supported.

Returns
-------
M : 2d ndarray
    Matrix product of A and B.


def _onenormest_matrix_power(A, p,<EOL>t=<NUM_LIT:2>, itmax=<NUM_LIT:5>, compute_v=False, compute_w=False, structure=None):

return scipy.sparse.linalg.onenormest(<EOL>MatrixPowerOperator(A, p, structure=structure))<EOL>

Efficiently estimate the 1-norm of A^p.

Parameters
----------
A : ndarray
    Matrix whose 1-norm of a power is to be computed.
p : int
    Non-negative integer power.
t : int, optional
    A positive parameter controlling the tradeoff between
    accuracy versus time and memory usage.
    Larger values take longer and use more memory
    but give more accurate output.
itmax : int, optional
    Use at most this many iterations.
compute_v : bool, optional
    Request a norm-maximizing linear operator input vector if True.
compute_w : bool, optional
    Request a norm-maximizing linear operator output vector if True.

Returns
-------
est : float
    An underestimate of the 1-norm of the sparse matrix.
v : ndarray, optional
    The vector such that ||Av||_1 == est*||v||_1.
    It can be thought of as an input to the linear operator
    that gives an output with particularly large norm.
w : ndarray, optional
    The vector Av which has relatively large 1-norm.
    It can be thought of as an output of the linear operator
    that is relatively large in norm compared to the input.


def _onenormest_product(operator_seq,<EOL>t=<NUM_LIT:2>, itmax=<NUM_LIT:5>, compute_v=False, compute_w=False, structure=None):

return scipy.sparse.linalg.onenormest(<EOL>ProductOperator(*operator_seq, structure=structure))<EOL>

Efficiently estimate the 1-norm of the matrix product of the args.

Parameters
----------
operator_seq : linear operator sequence
    Matrices whose 1-norm of product is to be computed.
t : int, optional
    A positive parameter controlling the tradeoff between
    accuracy versus time and memory usage.
    Larger values take longer and use more memory
    but give more accurate output.
itmax : int, optional
    Use at most this many iterations.
compute_v : bool, optional
    Request a norm-maximizing linear operator input vector if True.
compute_w : bool, optional
    Request a norm-maximizing linear operator output vector if True.
structure : str, optional
    A string describing the structure of all operators.
    Only `upper_triangular` is currently supported.

Returns
-------
est : float
    An underestimate of the 1-norm of the sparse matrix.
v : ndarray, optional
    The vector such that ||Av||_1 == est*||v||_1.
    It can be thought of as an input to the linear operator
    that gives an output with particularly large norm.
w : ndarray, optional
    The vector Av which has relatively large 1-norm.
    It can be thought of as an output of the linear operator
    that is relatively large in norm compared to the input.


<EOL>if isinstance(A, (list, tuple)):<EOL><INDENT>A = np.asarray(A)<EOL><DEDENT>if len(A.shape) != <NUM_LIT:2> or A.shape[<NUM_LIT:0>] != A.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>structure = UPPER_TRIANGULAR if _is_upper_triangular(A) else None<EOL>use_exact_onenorm = A.shape[<NUM_LIT:0>] < <NUM_LIT:200><EOL>h = _ExpmPadeHelper(<EOL>A, structure=structure, use_exact_onenorm=use_exact_onenorm)<EOL>eta_1 = max(h.d4_loose, h.d6_loose)<EOL>if eta_1 < <NUM_LIT> and _ell(h.A, <NUM_LIT:3>) == <NUM_LIT:0>:<EOL><INDENT>U, V = h.pade3()<EOL>return _solve_P_Q(U, V, structure=structure)<EOL><DEDENT>eta_2 = max(h.d4_tight, h.d6_loose)<EOL>if eta_2 < <NUM_LIT> and _ell(h.A, <NUM_LIT:5>) == <NUM_LIT:0>:<EOL><INDENT>U, V = h.pade5()<EOL>return _solve_P_Q(U, V, structure=structure)<EOL><DEDENT>eta_3 = max(h.d6_tight, h.d8_loose)<EOL>if eta_3 < <NUM_LIT> and _ell(h.A, <NUM_LIT:7>) == <NUM_LIT:0>:<EOL><INDENT>U, V = h.pade7()<EOL>return _solve_P_Q(U, V, structure=structure)<EOL><DEDENT>if eta_3 < <NUM_LIT> and _ell(h.A, <NUM_LIT:9>) == <NUM_LIT:0>:<EOL><INDENT>U, V = h.pade9()<EOL>return _solve_P_Q(U, V, structure=structure)<EOL><DEDENT>eta_4 = max(h.d8_loose, h.d10_loose)<EOL>eta_5 = min(eta_3, eta_4)<EOL>theta_13 = <NUM_LIT><EOL>s = max(int(np.ceil(np.log2(eta_5 / theta_13))), <NUM_LIT:0>)<EOL>s = s + _ell(<NUM_LIT:2>**-s * h.A, <NUM_LIT>)<EOL>U, V = h.pade13_scaled(s)<EOL>X = _solve_P_Q(U, V, structure=structure)<EOL>if structure == UPPER_TRIANGULAR:<EOL><INDENT>X = _fragment_2_1(X, h.A, s)<EOL><DEDENT>else:<EOL><INDENT>for i in range(s):<EOL><INDENT>X = X.dot(X)<EOL><DEDENT><DEDENT>return X<EOL>

Compute the matrix exponential using Pade approximation.

Parameters
----------
A : (M,M) array_like or sparse matrix
    2D Array or Matrix (sparse or dense) to be exponentiated

Returns
-------
expA : (M,M) ndarray
    Matrix exponential of `A`

Notes
-----
This is algorithm (6.1) which is a simplification of algorithm (5.1).

.. versionadded:: 0.12.0

References
----------
.. [1] Awad H. Al-Mohy and Nicholas J. Higham (2009)
       "A New Scaling and Squaring Algorithm for the Matrix Exponential."
       SIAM Journal on Matrix Analysis and Applications.
       31 (3). pp. 970-989. ISSN 1095-7162


P = U + V<EOL>Q = -U + V<EOL>if isspmatrix(U):<EOL><INDENT>return spsolve(Q, P)<EOL><DEDENT>elif structure is None:<EOL><INDENT>return solve(Q, P)<EOL><DEDENT>elif structure == UPPER_TRIANGULAR:<EOL><INDENT>return solve_triangular(Q, P)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>' + str(structure))<EOL><DEDENT>

A helper function for expm_2009.

Parameters
----------
U : ndarray
    Pade numerator.
V : ndarray
    Pade denominator.
structure : str, optional
    A string describing the structure of both matrices `U` and `V`.
    Only `upper_triangular` is currently supported.

Notes
-----
The `structure` argument is inspired by similar args
for theano and cvxopt functions.


<EOL>x2 = x*x<EOL>if abs(x) < <NUM_LIT>:<EOL><INDENT>return <NUM_LIT:1> + (x2/<NUM_LIT>)*(<NUM_LIT:1> + (x2/<NUM_LIT>)*(<NUM_LIT:1> + (x2/<NUM_LIT>)))<EOL><DEDENT>else:<EOL><INDENT>return np.sinh(x) / x<EOL><DEDENT>

Stably evaluate sinch.

Notes
-----
The strategy of falling back to a sixth order Taylor expansion
was suggested by the Spallation Neutron Source docs
which was found on the internet by google search.
http://www.ornl.gov/~t6p/resources/xal/javadoc/gov/sns/tools/math/ElementaryFunction.html
The details of the cutoff point and the Horner-like evaluation
was picked without reference to anything in particular.

Note that sinch is not currently implemented in scipy.special,
whereas the "engineer's" definition of sinc is implemented.
The implementation of sinc involves a scaling factor of pi
that distinguishes it from the "mathematician's" version of sinc.


<EOL>a = <NUM_LIT:0.5> * (lam_1 + lam_2)<EOL>b = <NUM_LIT:0.5> * (lam_1 - lam_2)<EOL>return t_12 * np.exp(a) * _sinch(b)<EOL>

Equation (10.42) of Functions of Matrices: Theory and Computation.

Notes
-----
This is a helper function for _fragment_2_1 of expm_2009.
Equation (10.42) is on page 251 in the section on Schur algorithms.
In particular, section 10.4.3 explains the Schur-Parlett algorithm.
expm([[lam_1, t_12], [0, lam_1])
=
[[exp(lam_1), t_12*exp((lam_1 + lam_2)/2)*sinch((lam_1 - lam_2)/2)],
[0, exp(lam_2)]


<EOL>n = X.shape[<NUM_LIT:0>]<EOL>diag_T = T.diagonal().copy()<EOL>scale = <NUM_LIT:2> ** -s<EOL>exp_diag = np.exp(scale * diag_T)<EOL>for k in range(n):<EOL><INDENT>X[k, k] = exp_diag[k]<EOL><DEDENT>for i in range(s-<NUM_LIT:1>, -<NUM_LIT:1>, -<NUM_LIT:1>):<EOL><INDENT>X = X.dot(X)<EOL>scale = <NUM_LIT:2> ** -i<EOL>exp_diag = np.exp(scale * diag_T)<EOL>for k in range(n):<EOL><INDENT>X[k, k] = exp_diag[k]<EOL><DEDENT>for k in range(n-<NUM_LIT:1>):<EOL><INDENT>lam_1 = scale * diag_T[k]<EOL>lam_2 = scale * diag_T[k+<NUM_LIT:1>]<EOL>t_12 = scale * T[k, k+<NUM_LIT:1>]<EOL>value = _eq_10_42(lam_1, lam_2, t_12)<EOL>X[k, k+<NUM_LIT:1>] = value<EOL><DEDENT><DEDENT>return X<EOL>

A helper function for expm_2009.

Notes
-----
The argument X is modified in-place, but this modification is not the same
as the returned value of the function.
This function also takes pains to do things in ways that are compatible
with sparse matrices, for example by avoiding fancy indexing
and by using methods of the matrices whenever possible instead of
using functions of the numpy or scipy libraries themselves.


if len(A.shape) != <NUM_LIT:2> or A.shape[<NUM_LIT:0>] != A.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>p = <NUM_LIT:2>*m + <NUM_LIT:1><EOL>choose_2p_p = scipy.misc.comb(<NUM_LIT:2>*p, p, exact=True)<EOL>abs_c_recip = float(choose_2p_p * math.factorial(<NUM_LIT:2>*p + <NUM_LIT:1>))<EOL>u = <NUM_LIT:2>**-<NUM_LIT><EOL>A_abs_onenorm = _onenorm_matrix_power_nnm(abs(A), p)<EOL>if not A_abs_onenorm:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>alpha = A_abs_onenorm / (_onenorm(A) * abs_c_recip)<EOL>log2_alpha_div_u = np.log2(alpha/u)<EOL>value = int(np.ceil(log2_alpha_div_u / (<NUM_LIT:2> * m)))<EOL>return max(value, <NUM_LIT:0>)<EOL>

A helper function for expm_2009.

Parameters
----------
A : linear operator
    A linear operator whose norm of power we care about.
m : int
    The power of the linear operator

Returns
-------
value : int
    A value related to a bound.


def __init__(self, A, structure=None, use_exact_onenorm=False):

self.A = A<EOL>self._A2 = None<EOL>self._A4 = None<EOL>self._A6 = None<EOL>self._A8 = None<EOL>self._A10 = None<EOL>self._d4_exact = None<EOL>self._d6_exact = None<EOL>self._d8_exact = None<EOL>self._d10_exact = None<EOL>self._d4_approx = None<EOL>self._d6_approx = None<EOL>self._d8_approx = None<EOL>self._d10_approx = None<EOL>self.ident = _ident_like(A)<EOL>self.structure = structure<EOL>self.use_exact_onenorm = use_exact_onenorm<EOL>

Initialize the object.

Parameters
----------
A : a dense or sparse square numpy matrix or ndarray
    The matrix to be exponentiated.
structure : str, optional
    A string describing the structure of matrix `A`.
    Only `upper_triangular` is currently supported.
use_exact_onenorm : bool, optional
    If True then only the exact one-norm of matrix powers and products
    will be used. Otherwise, the one-norm of powers and products
    may initially be estimated.


def onenormest(A, t=<NUM_LIT:2>, itmax=<NUM_LIT:5>, compute_v=False, compute_w=False):

<EOL>if len(A.shape) != <NUM_LIT:2> or A.shape[<NUM_LIT:0>] != A.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>n = A.shape[<NUM_LIT:1>]<EOL>if t >= n:<EOL><INDENT>A_explicit = np.asarray(aslinearoperator(A).matmat(np.identity(n)))<EOL>if A_explicit.shape != (n, n):<EOL><INDENT>raise Exception('<STR_LIT>',<EOL>'<STR_LIT>' + str(A_explicit.shape))<EOL><DEDENT>col_abs_sums = abs(A_explicit).sum(axis=<NUM_LIT:0>)<EOL>if col_abs_sums.shape != (n, ):<EOL><INDENT>raise Exception('<STR_LIT>',<EOL>'<STR_LIT>' + str(col_abs_sums.shape))<EOL><DEDENT>argmax_j = np.argmax(col_abs_sums)<EOL>v = elementary_vector(n, argmax_j)<EOL>w = A_explicit[:, argmax_j]<EOL>est = col_abs_sums[argmax_j]<EOL><DEDENT>else:<EOL><INDENT>est, v, w, nmults, nresamples = _onenormest_core(A, A.T, t, itmax)<EOL><DEDENT>if compute_v or compute_w:<EOL><INDENT>result = (est,)<EOL>if compute_v:<EOL><INDENT>result += (v,)<EOL><DEDENT>if compute_w:<EOL><INDENT>result += (w,)<EOL><DEDENT>return result<EOL><DEDENT>else:<EOL><INDENT>return est<EOL><DEDENT>

Compute a lower bound of the 1-norm of a sparse matrix.

Parameters
----------
A : ndarray or other linear operator
    A linear operator that can be transposed and that can
    produce matrix products.
t : int, optional
    A positive parameter controlling the tradeoff between
    accuracy versus time and memory usage.
    Larger values take longer and use more memory
    but give more accurate output.
itmax : int, optional
    Use at most this many iterations.
compute_v : bool, optional
    Request a norm-maximizing linear operator input vector if True.
compute_w : bool, optional
    Request a norm-maximizing linear operator output vector if True.

Returns
-------
est : float
    An underestimate of the 1-norm of the sparse matrix.
v : ndarray, optional
    The vector such that ||Av||_1 == est*||v||_1.
    It can be thought of as an input to the linear operator
    that gives an output with particularly large norm.
w : ndarray, optional
    The vector Av which has relatively large 1-norm.
    It can be thought of as an output of the linear operator
    that is relatively large in norm compared to the input.

Notes
-----
This is algorithm 2.4 of [1].

In [2] it is described as follows.
"This algorithm typically requires the evaluation of
about 4t matrix-vector products and almost invariably
produces a norm estimate (which is, in fact, a lower
bound on the norm) correct to within a factor 3."

.. versionadded:: 0.13.0

References
----------
.. [1] Nicholas J. Higham and Francoise Tisseur (2000),
       "A Block Algorithm for Matrix 1-Norm Estimation,
       with an Application to 1-Norm Pseudospectra."
       SIAM J. Matrix Anal. Appl. Vol. 21, No. 4, pp. 1185-1201.

.. [2] Awad H. Al-Mohy and Nicholas J. Higham (2009),
       "A new scaling and squaring algorithm for the matrix exponential."
       SIAM J. Matrix Anal. Appl. Vol. 31, No. 3, pp. 970-989.


A_linear_operator = aslinearoperator(A)<EOL>AT_linear_operator = aslinearoperator(AT)<EOL>n = A_linear_operator.shape[<NUM_LIT:0>]<EOL>X = np.ones((n, t))<EOL>if t > <NUM_LIT:1>:<EOL><INDENT>X[:, <NUM_LIT:1>:] = np.random.randint(<NUM_LIT:0>, <NUM_LIT:2>, size=(n, t-<NUM_LIT:1>))*<NUM_LIT:2> - <NUM_LIT:1><EOL><DEDENT>X /= float(n)<EOL>g_prev = None<EOL>h_prev = None<EOL>k = <NUM_LIT:1><EOL>ind = range(t)<EOL>while True:<EOL><INDENT>Y = np.asarray(A_linear_operator.matmat(X))<EOL>g = [norm_1d_1(Y[:, j]) for j in range(t)]<EOL>best_j = np.argmax(g)<EOL>g = sorted(g, reverse=True)<EOL>S = sign_round_up(Y)<EOL>Z = np.asarray(AT_linear_operator.matmat(S))<EOL>h = [norm_1d_inf(row) for row in Z]<EOL>if k >= <NUM_LIT:2>:<EOL><INDENT>if less_than_or_close(max(h), np.dot(Z[:, best_j], X[:, best_j])):<EOL><INDENT>break<EOL><DEDENT><DEDENT>h_i_pairs = zip(h, range(n))<EOL>h, ind = zip(*sorted(h_i_pairs, reverse=True)[:t])<EOL>for j in range(t):<EOL><INDENT>X[:, j] = elementary_vector(n, ind[j])<EOL><DEDENT>if k >= <NUM_LIT:2>:<EOL><INDENT>if not less_than_or_close(g_prev[<NUM_LIT:0>], h_prev[<NUM_LIT:0>]):<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>if not less_than_or_close(h_prev[<NUM_LIT:0>], g[<NUM_LIT:0>]):<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT><DEDENT>if k >= <NUM_LIT:3>:<EOL><INDENT>for j in range(t):<EOL><INDENT>if not less_than_or_close(g[j], g_prev[j]):<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT><DEDENT><DEDENT>g_prev = g<EOL>h_prev = h<EOL>k += <NUM_LIT:1><EOL><DEDENT>return g, ind<EOL>

This is Algorithm 2.2.

Parameters
----------
A : ndarray or other linear operator
    A linear operator that can produce matrix products.
AT : ndarray or other linear operator
    The transpose of A.
t : int, optional
    A positive parameter controlling the tradeoff between
    accuracy versus time and memory usage.

Returns
-------
g : sequence
    A non-negative decreasing vector
    such that g[j] is a lower bound for the 1-norm
    of the column of A of jth largest 1-norm.
    The first entry of this vector is therefore a lower bound
    on the 1-norm of the linear operator A.
    This sequence has length t.
ind : sequence
    The ith entry of ind is the index of the column A whose 1-norm
    is given by g[i].
    This sequence of indices has length t, and its entries are
    chosen from range(n), possibly with repetition,
    where n is the order of the operator A.

Notes
-----
This algorithm is mainly for testing.
It uses the 'ind' array in a way that is similar to
its usage in algorithm 2.4.  This algorithm 2.2 may be easier to test,
so it gives a chance of uncovering bugs related to indexing
which could have propagated less noticeably to algorithm 2.4.


<EOL>A_linear_operator = aslinearoperator(A)<EOL>AT_linear_operator = aslinearoperator(AT)<EOL>if itmax < <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if t < <NUM_LIT:1>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>n = A.shape[<NUM_LIT:0>]<EOL>if t >= n:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>nmults = <NUM_LIT:0><EOL>nresamples = <NUM_LIT:0><EOL>X = np.ones((n, t), dtype=float)<EOL>if t > <NUM_LIT:1>:<EOL><INDENT>for i in range(<NUM_LIT:1>, t):<EOL><INDENT>resample_column(i, X)<EOL><DEDENT>for i in range(t):<EOL><INDENT>while column_needs_resampling(i, X):<EOL><INDENT>resample_column(i, X)<EOL>nresamples += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>X /= float(n)<EOL>ind_hist = set()<EOL>est_old = <NUM_LIT:0><EOL>S = np.zeros((n, t), dtype=float)<EOL>k = <NUM_LIT:1><EOL>ind = None<EOL>while True:<EOL><INDENT>Y = np.asarray(A_linear_operator.matmat(X))<EOL>nmults += <NUM_LIT:1><EOL>mags = [norm_1d_1(Y[:, j]) for j in range(t)]<EOL>est = np.max(mags)<EOL>best_j = np.argmax(mags)<EOL>if est > est_old or k == <NUM_LIT:2>:<EOL><INDENT>if k >= <NUM_LIT:2>:<EOL><INDENT>ind_best = ind[best_j]<EOL><DEDENT>w = Y[:, best_j]<EOL><DEDENT>if k >= <NUM_LIT:2> and est <= est_old:<EOL><INDENT>est = est_old<EOL>break<EOL><DEDENT>est_old = est<EOL>S_old = S<EOL>if k > itmax:<EOL><INDENT>break<EOL><DEDENT>S = sign_round_up(Y)<EOL>if every_col_of_X_is_parallel_to_a_col_of_Y(S, S_old):<EOL><INDENT>break<EOL><DEDENT>if t > <NUM_LIT:1>:<EOL><INDENT>for i in range(t):<EOL><INDENT>while column_needs_resampling(i, S, S_old):<EOL><INDENT>resample_column(i, S)<EOL>nresamples += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>Z = np.asarray(AT_linear_operator.matmat(S))<EOL>nmults += <NUM_LIT:1><EOL>h = [norm_1d_inf(row) for row in Z]<EOL>if k >= <NUM_LIT:2> and max(h) == h[ind_best]:<EOL><INDENT>break<EOL><DEDENT>h_i_pairs = zip(h, range(n))<EOL>h, ind = zip(*sorted(h_i_pairs, reverse=True))<EOL>if t > <NUM_LIT:1>:<EOL><INDENT>if set(ind[:t]) <= ind_hist:<EOL><INDENT>break<EOL><DEDENT>unused_entries = [i for i in ind if i not in ind_hist]<EOL>used_entries = [i for i in ind if i in ind_hist]<EOL>ind = unused_entries + used_entries<EOL><DEDENT>for j in range(t):<EOL><INDENT>X[:, j] = elementary_vector(n, ind[j])<EOL><DEDENT>ind_hist.update(ind[:t])<EOL>k += <NUM_LIT:1><EOL><DEDENT>v = elementary_vector(n, ind_best)<EOL>return est, v, w, nmults, nresamples<EOL>

Compute a lower bound of the 1-norm of a sparse matrix.

Parameters
----------
A : ndarray or other linear operator
    A linear operator that can produce matrix products.
AT : ndarray or other linear operator
    The transpose of A.
t : int, optional
    A positive parameter controlling the tradeoff between
    accuracy versus time and memory usage.
itmax : int, optional
    Use at most this many iterations.

Returns
-------
est : float
    An underestimate of the 1-norm of the sparse matrix.
v : ndarray, optional
    The vector such that ||Av||_1 == est*||v||_1.
    It can be thought of as an input to the linear operator
    that gives an output with particularly large norm.
w : ndarray, optional
    The vector Av which has relatively large 1-norm.
    It can be thought of as an output of the linear operator
    that is relatively large in norm compared to the input.
nmults : int, optional
    The number of matrix products that were computed.
nresamples : int, optional
    The number of times a parallel column was observed,
    necessitating a re-randomization of the column.

Notes
-----
This is algorithm 2.4.


if axis is None:<EOL><INDENT>return int(self.indptr[-<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>if axis < <NUM_LIT:0>:<EOL><INDENT>axis += <NUM_LIT:2><EOL><DEDENT>axis, _ = self._swap((axis, <NUM_LIT:1> - axis))<EOL>_, N = self._swap(self.shape)<EOL>if axis == <NUM_LIT:0>:<EOL><INDENT>return _compat_bincount(downcast_intp_index(self.indices),<EOL>minlength=N)<EOL><DEDENT>elif axis == <NUM_LIT:1>:<EOL><INDENT>return np.diff(self.indptr)<EOL><DEDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>

Get the count of explicitly-stored values (nonzeros)

        Parameters
        ----------
        axis : None, 0, or 1
            Select between the number of values across the whole matrix, in
            each column, or in each row.


if copy:<EOL><INDENT>other = other.copy()<EOL><DEDENT>self.data = other.data<EOL>self.indices = other.indices<EOL>self.indptr = other.indptr<EOL>self.shape = other.shape<EOL>

take the member variables of other and assign them to self


<EOL>major_name,minor_name = self._swap(('<STR_LIT>','<STR_LIT>'))<EOL>major_dim,minor_dim = self._swap(self.shape)<EOL>if self.indptr.dtype.kind != '<STR_LIT:i>':<EOL><INDENT>warn("<STR_LIT>"<EOL>% self.indptr.dtype.name)<EOL><DEDENT>if self.indices.dtype.kind != '<STR_LIT:i>':<EOL><INDENT>warn("<STR_LIT>"<EOL>% self.indices.dtype.name)<EOL><DEDENT>idx_dtype = get_index_dtype((self.indptr, self.indices))<EOL>self.indptr = np.asarray(self.indptr, dtype=idx_dtype)<EOL>self.indices = np.asarray(self.indices, dtype=idx_dtype)<EOL>self.data = to_native(self.data)<EOL>if self.data.ndim != <NUM_LIT:1> or self.indices.ndim != <NUM_LIT:1> or self.indptr.ndim != <NUM_LIT:1>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if (len(self.indptr) != major_dim + <NUM_LIT:1>):<EOL><INDENT>raise ValueError("<STR_LIT>" %<EOL>(len(self.indptr), major_dim + <NUM_LIT:1>))<EOL><DEDENT>if (self.indptr[<NUM_LIT:0>] != <NUM_LIT:0>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if (len(self.indices) != len(self.data)):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if (self.indptr[-<NUM_LIT:1>] > len(self.indices)):<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>self.prune()<EOL>if full_check:<EOL><INDENT>if self.nnz > <NUM_LIT:0>:<EOL><INDENT>if self.indices.max() >= minor_dim:<EOL><INDENT>raise ValueError("<STR_LIT>" %<EOL>(minor_name,minor_dim))<EOL><DEDENT>if self.indices.min() < <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>" %<EOL>minor_name)<EOL><DEDENT>if np.diff(self.indptr).min() < <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>

check whether the matrix format is valid

        Parameters
        ==========

            - full_check : {bool}
                - True  - rigorous check, O(N) operations : default
                - False - basic check, O(1) operations


try:<EOL><INDENT>self.sum_duplicates()<EOL><DEDENT>except NotImplementedError:<EOL><INDENT>pass<EOL><DEDENT>data = np.empty(self.data.shape, dtype=np.asarray(const).dtype)<EOL>data.fill(const)<EOL>return self.__class__((data, self.indices, self.indptr), shape=self.shape)<EOL>

Copy data, with all nonzeros replaced with constant for binopt

        Adopts the dtype of const to avoid removing sign or magnitude before
        comparison.

        Warning: does not make a copy of indices and indptr


<EOL>if isscalarlike(other):<EOL><INDENT>return self.__mul__(other)<EOL><DEDENT>if isspmatrix(other):<EOL><INDENT>if self.shape == other.shape:<EOL><INDENT>other = self.__class__(other)<EOL>return self._binopt(other, '<STR_LIT>')<EOL><DEDENT>elif other.shape == (<NUM_LIT:1>,<NUM_LIT:1>):<EOL><INDENT>return self.__mul__(other.tocsc().data[<NUM_LIT:0>])<EOL><DEDENT>elif self.shape == (<NUM_LIT:1>,<NUM_LIT:1>):<EOL><INDENT>return other.__mul__(self.tocsc().data[<NUM_LIT:0>])<EOL><DEDENT>elif self.shape[<NUM_LIT:1>] == other.shape[<NUM_LIT:0>] and self.shape[<NUM_LIT:1>] == <NUM_LIT:1>:<EOL><INDENT>return self._mul_sparse_matrix(other.tocsc())<EOL><DEDENT>elif self.shape[<NUM_LIT:0>] == other.shape[<NUM_LIT:1>] and self.shape[<NUM_LIT:0>] == <NUM_LIT:1>:<EOL><INDENT>return other._mul_sparse_matrix(self.tocsc())<EOL><DEDENT>elif other.shape[<NUM_LIT:0>] == <NUM_LIT:1> and self.shape[<NUM_LIT:1>] == other.shape[<NUM_LIT:1>]:<EOL><INDENT>other = dia_matrix((other.toarray().ravel(), [<NUM_LIT:0>]),<EOL>shape=(other.shape[<NUM_LIT:1>], other.shape[<NUM_LIT:1>]))<EOL>return self._mul_sparse_matrix(other)<EOL><DEDENT>elif self.shape[<NUM_LIT:0>] == <NUM_LIT:1> and self.shape[<NUM_LIT:1>] == other.shape[<NUM_LIT:1>]:<EOL><INDENT>copy = dia_matrix((self.toarray().ravel(), [<NUM_LIT:0>]),<EOL>shape=(self.shape[<NUM_LIT:1>], self.shape[<NUM_LIT:1>]))<EOL>return other._mul_sparse_matrix(copy)<EOL><DEDENT>elif other.shape[<NUM_LIT:1>] == <NUM_LIT:1> and self.shape[<NUM_LIT:0>] == other.shape[<NUM_LIT:0>]:<EOL><INDENT>other = dia_matrix((other.toarray().ravel(), [<NUM_LIT:0>]),<EOL>shape=(other.shape[<NUM_LIT:0>], other.shape[<NUM_LIT:0>]))<EOL>return other._mul_sparse_matrix(self)<EOL><DEDENT>elif self.shape[<NUM_LIT:1>] == <NUM_LIT:1> and self.shape[<NUM_LIT:0>] == other.shape[<NUM_LIT:0>]:<EOL><INDENT>copy = dia_matrix((self.toarray().ravel(), [<NUM_LIT:0>]),<EOL>shape=(self.shape[<NUM_LIT:0>], self.shape[<NUM_LIT:0>]))<EOL>return copy._mul_sparse_matrix(other)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>return np.multiply(self.todense(), other)<EOL>

Point-wise multiplication by another matrix, vector, or
        scalar.


<EOL>fn = getattr(_sparsetools, self.format + "<STR_LIT>")<EOL>y = np.empty(min(self.shape), dtype=upcast(self.dtype))<EOL>fn(self.shape[<NUM_LIT:0>], self.shape[<NUM_LIT:1>], self.indptr, self.indices, self.data, y)<EOL>return y<EOL>


<EOL>if axis is None:<EOL><INDENT>return self.data.sum()<EOL><DEDENT>elif (not hasattr(self, '<STR_LIT>') and<EOL>axis in self._swap(((<NUM_LIT:1>, -<NUM_LIT:1>), (<NUM_LIT:0>, <NUM_LIT:2>)))[<NUM_LIT:0>]):<EOL><INDENT>dtype = self.dtype<EOL>if np.issubdtype(dtype, np.bool_):<EOL><INDENT>dtype = np.int_<EOL><DEDENT>ret = np.zeros(len(self.indptr) - <NUM_LIT:1>, dtype=dtype)<EOL>major_index, value = self._minor_reduce(np.add)<EOL>ret[major_index] = value<EOL>ret = np.asmatrix(ret)<EOL>if axis % <NUM_LIT:2> == <NUM_LIT:1>:<EOL><INDENT>ret = ret.T<EOL><DEDENT>return ret<EOL><DEDENT>else:<EOL><INDENT>return spmatrix.sum(self, axis)<EOL><DEDENT>

Sum the matrix over the given axis.  If the axis is None, sum
        over both rows and columns, returning a scalar.


major_index = np.flatnonzero(np.diff(self.indptr))<EOL>if self.data.size == <NUM_LIT:0> and major_index.size == <NUM_LIT:0>:<EOL><INDENT>value = np.zeros_like(self.data)<EOL><DEDENT>else:<EOL><INDENT>value = ufunc.reduceat(self.data,<EOL>downcast_intp_index(self.indptr[major_index]))<EOL><DEDENT>return major_index, value<EOL>

Reduce nonzeros with a ufunc over the minor axis when non-empty

        Warning: this does not call sum_duplicates()

        Returns
        -------
        major_index : array of ints
            Major indices where nonzero

        value : array of self.dtype
            Reduce result for nonzeros in each major_index


M, N = self._swap(self.shape)<EOL>def check_bounds(indices, bound):<EOL><INDENT>idx = indices.max()<EOL>if idx >= bound:<EOL><INDENT>raise IndexError('<STR_LIT>' %<EOL>(idx, bound))<EOL><DEDENT>idx = indices.min()<EOL>if idx < -bound:<EOL><INDENT>raise IndexError('<STR_LIT>' %<EOL>(idx, bound))<EOL><DEDENT><DEDENT>check_bounds(i, M)<EOL>check_bounds(j, N)<EOL>i = np.asarray(i, dtype=self.indices.dtype)<EOL>j = np.asarray(j, dtype=self.indices.dtype)<EOL>n_samples = len(x)<EOL>offsets = np.empty(n_samples, dtype=self.indices.dtype)<EOL>ret = _sparsetools.csr_sample_offsets(M, N, self.indptr, self.indices,<EOL>n_samples, i, j, offsets)<EOL>if ret == <NUM_LIT:1>:<EOL><INDENT>self.sum_duplicates()<EOL>_sparsetools.csr_sample_offsets(M, N, self.indptr,<EOL>self.indices, n_samples, i, j,<EOL>offsets)<EOL><DEDENT>if -<NUM_LIT:1> not in offsets:<EOL><INDENT>self.data[offsets] = x<EOL>return<EOL><DEDENT>else:<EOL><INDENT>warn("<STR_LIT>"<EOL>"<STR_LIT>" % self.format,<EOL>SparseEfficiencyWarning)<EOL>mask = offsets > -<NUM_LIT:1><EOL>self.data[offsets[mask]] = x[mask]<EOL>mask = ~mask<EOL>i = i[mask]<EOL>i[i < <NUM_LIT:0>] += M<EOL>j = j[mask]<EOL>j[j < <NUM_LIT:0>] += N<EOL>self._insert_many(i, j, x[mask])<EOL><DEDENT>

Sets value at each (i, j) to x

        Here (i,j) index major and minor respectively.


order = np.argsort(i, kind='<STR_LIT>')  <EOL>i = i.take(order, mode='<STR_LIT>')<EOL>j = j.take(order, mode='<STR_LIT>')<EOL>x = x.take(order, mode='<STR_LIT>')<EOL>do_sort = self.has_sorted_indices<EOL>idx_dtype = get_index_dtype((self.indices, self.indptr),<EOL>maxval=(self.indptr[-<NUM_LIT:1>] + x.size))<EOL>self.indptr = np.asarray(self.indptr, dtype=idx_dtype)<EOL>self.indices = np.asarray(self.indices, dtype=idx_dtype)<EOL>i = np.asarray(i, dtype=idx_dtype)<EOL>j = np.asarray(j, dtype=idx_dtype)<EOL>indices_parts = []<EOL>data_parts = []<EOL>ui, ui_indptr = _compat_unique(i, return_index=True)<EOL>ui_indptr = np.append(ui_indptr, len(j))<EOL>new_nnzs = np.diff(ui_indptr)<EOL>prev = <NUM_LIT:0><EOL>for c, (ii, js, je) in enumerate(izip(ui, ui_indptr, ui_indptr[<NUM_LIT:1>:])):<EOL><INDENT>start = self.indptr[prev]<EOL>stop = self.indptr[ii]<EOL>indices_parts.append(self.indices[start:stop])<EOL>data_parts.append(self.data[start:stop])<EOL>uj, uj_indptr = _compat_unique(j[js:je][::-<NUM_LIT:1>], return_index=True)<EOL>if len(uj) == je - js:<EOL><INDENT>indices_parts.append(j[js:je])<EOL>data_parts.append(x[js:je])<EOL><DEDENT>else:<EOL><INDENT>indices_parts.append(j[js:je][::-<NUM_LIT:1>][uj_indptr])<EOL>data_parts.append(x[js:je][::-<NUM_LIT:1>][uj_indptr])<EOL>new_nnzs[c] = len(uj)<EOL><DEDENT>prev = ii<EOL><DEDENT>start = self.indptr[ii]<EOL>indices_parts.append(self.indices[start:])<EOL>data_parts.append(self.data[start:])<EOL>self.indices = np.concatenate(indices_parts)<EOL>self.data = np.concatenate(data_parts)<EOL>nnzs = np.asarray(np.ediff1d(self.indptr, to_begin=<NUM_LIT:0>), dtype=idx_dtype)<EOL>nnzs[<NUM_LIT:1>:][ui] += new_nnzs<EOL>self.indptr = np.cumsum(nnzs, out=nnzs)<EOL>if do_sort:<EOL><INDENT>self.has_sorted_indices = False<EOL>self.sort_indices()<EOL><DEDENT>self.check_format(full_check=False)<EOL>

Inserts new nonzero at each (i, j) with value x

        Here (i,j) index major and minor respectively.
        i, j and x must be non-empty, 1d arrays.
        Inserts each major group (e.g. all entries per row) at a time.
        Maintains has_sorted_indices property.
        Modifies i, j, x in place.


def _get_slice(self, i, start, stop, stride, shape):

if stride != <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if stop <= start:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>indices = []<EOL>for ind in xrange(self.indptr[i], self.indptr[i+<NUM_LIT:1>]):<EOL><INDENT>if self.indices[ind] >= start and self.indices[ind] < stop:<EOL><INDENT>indices.append(ind)<EOL><DEDENT><DEDENT>index = self.indices[indices] - start<EOL>data = self.data[indices]<EOL>indptr = np.array([<NUM_LIT:0>, len(indices)])<EOL>return self.__class__((data, index, indptr), shape=shape,<EOL>dtype=self.dtype)<EOL>

Returns a copy of the elements
            [i, start:stop:string] for row-oriented matrices
            [start:stop:string, i] for column-oriented matrices