<commit_before>import krypy
import krypy.tests.test_utils as test_utils
import numpy
import scipy.linalg
import itertools
from numpy.testing import assert_almost_equal, assert_array_almost_equal, \
    assert_array_equal, assert_equal


def test_Arnoldifyer():
    vs = [numpy.ones((10, 1)),
          numpy.r_[numpy.ones((3, 1)), numpy.zeros((7, 1))]
          ]
    for matrix in test_utils.get_matrices():
        evecs, evals = numpy.linalg.eig(matrix)
        sort = numpy.argsort(numpy.abs(evals))
        evecs = evecs[:, sort]
        Us = [numpy.zeros((10, 0)),
              evecs[:, -2:],
              evecs[:, -2:] + 1e-5*numpy.random.rand(10, 2)
              ]
        Wt_sels = ['none', 'smallest', 'largest']
        for A, v, U, Wt_sel in \
                itertools.product(test_utils.get_operators(matrix),
                                  vs, Us, Wt_sels):
            yield run_Arnoldifyer, A, v, U, 5, Wt_sel


def run_Arnoldifyer(A, v, U, maxiter, Wt_sel):
    N, d = U.shape
    # orthonormalize if U is not zero-dim
    if d > 0:
        U, _ = numpy.linalg.qr(U)

    # build projection
    AU = A.dot(U)
    P = krypy.utils.Projection(AU, U).operator_complement()

    # run Arnoldi
    V, H = krypy.utils.arnoldi(P*A, P*v, maxiter=maxiter)
    n = H.shape[1]

    # build matrices used in Arnoldifyer
    B_ = V.T.conj().dot(AU)
    C = U.T.conj().dot(A.dot(V[:, :n]))
    E = U.T.conj().dot(AU)

    VU = numpy.c_[V[:, :n], U]
    M = VU.T.conj().dot(A.dot(VU))
    rvals, rvecs = numpy.linalg.eig(M)
    sort = numpy.argsort(numpy.abs(rvals))
    rvecs = rvecs[sort]
    if Wt_sel == 'none':
        Wt = numpy.zeros((n+d, 0))
    elif Wt_sel == 'smallest':
        Wt = rvecs[:, :2]
    elif Wt_sel == 'largest':
        Wt = rvecs[:, -2:]

    # get Arnoldifyer instance
    arnoldifyer = krypy.deflation.Arnoldifyer(V, U, AU, H, B_, C, E,
                                              numpy.linalg.norm(P*v, 2),
                                              U.T.conj().dot(v))
    # arnoldify given Wt
    Hh, Rh, Vh, F = arnoldifyer.get(Wt, full=True)

    # perform checks
    W = VU.dot(Wt)
    PW = krypy.utils.Projection(A.dot(W), W).operator_complement()
    A = krypy.utils.get_linearoperator((N, N), A)
    At = PW*A
    F = krypy.utils.get_linearoperator((N, N), F)
    assert_almost_equal(numpy.linalg.norm((At+F).dot(Vh) - Vh.dot(Hh), 2)
                        / numpy.linalg.norm(M, 2),
                        0, 12)


if __name__ == '__main__':
    import nose
    nose.main()
<commit_msg>[deflation] numpy.linalg.eig -> scipy.linalg.eig<commit_after>import krypy
import krypy.tests.test_utils as test_utils
import numpy
import scipy.linalg
import itertools
from numpy.testing import assert_almost_equal, assert_array_almost_equal, \
    assert_array_equal, assert_equal


def test_Arnoldifyer():
    vs = [numpy.ones((10, 1)),
          numpy.r_[numpy.ones((3, 1)), numpy.zeros((7, 1))]
          ]
    for matrix in test_utils.get_matrices():
        evecs, evals = scipy.linalg.eig(matrix)
        sort = numpy.argsort(numpy.abs(evals))
        evecs = evecs[:, sort]
        Us = [numpy.zeros((10, 0)),
              evecs[:, -2:],
              evecs[:, -2:] + 1e-5*numpy.random.rand(10, 2)
              ]
        Wt_sels = ['none', 'smallest', 'largest']
        for A, v, U, Wt_sel in \
                itertools.product(test_utils.get_operators(matrix),
                                  vs, Us, Wt_sels):
            yield run_Arnoldifyer, A, v, U, 5, Wt_sel


def run_Arnoldifyer(A, v, U, maxiter, Wt_sel):
    N, d = U.shape
    # orthonormalize if U is not zero-dim
    if d > 0:
        U, _ = numpy.linalg.qr(U)

    # build projection
    AU = A.dot(U)
    P = krypy.utils.Projection(AU, U).operator_complement()

    # run Arnoldi
    V, H = krypy.utils.arnoldi(P*A, P*v, maxiter=maxiter)
    n = H.shape[1]

    # build matrices used in Arnoldifyer
    B_ = V.T.conj().dot(AU)
    C = U.T.conj().dot(A.dot(V[:, :n]))
    E = U.T.conj().dot(AU)

    VU = numpy.c_[V[:, :n], U]
    M = VU.T.conj().dot(A.dot(VU))
    rvals, rvecs = scipy.linalg.eig(M)
    sort = numpy.argsort(numpy.abs(rvals))
    rvecs = rvecs[sort]
    if Wt_sel == 'none':
        Wt = numpy.zeros((n+d, 0))
    elif Wt_sel == 'smallest':
        Wt = rvecs[:, :2]
    elif Wt_sel == 'largest':
        Wt = rvecs[:, -2:]

    # get Arnoldifyer instance
    arnoldifyer = krypy.deflation.Arnoldifyer(V, U, AU, H, B_, C, E,
                                              numpy.linalg.norm(P*v, 2),
                                              U.T.conj().dot(v))
    # arnoldify given Wt
    Hh, Rh, Vh, F = arnoldifyer.get(Wt, full=True)

    # perform checks
    W = VU.dot(Wt)
    PW = krypy.utils.Projection(A.dot(W), W).operator_complement()
    A = krypy.utils.get_linearoperator((N, N), A)
    At = PW*A
    F = krypy.utils.get_linearoperator((N, N), F)
    assert_almost_equal(numpy.linalg.norm((At+F).dot(Vh) - Vh.dot(Hh), 2)
                        / numpy.linalg.norm(M, 2),
                        0, 12)


if __name__ == '__main__':
    import nose
    nose.main()

{'fraction_non_alphanumeric': 0.15255231762174848, 'fraction_numerical': 0.011343633874437709, 'mean_word_length': 2.0386215092097446, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3718738', 'n_tokens_mistral': 2088, 'n_tokens_neox': 2014, 'n_words': 406}