Exemple #1
0
def exec_sample():
    n = 10

    matrix = lil_matrix((n, n), dtype='float64')

    for i in range(n - 1):
        matrix[(i, i + 1)] = 1
        matrix[(i + 1, i)] = 1

    # Equivalent to the following n by n matrix
    #      0  1  0  0  ..  0  0
    #      1  0  1  0  ..  0  0
    #      0  1  0  1  ..  0  0
    #      0  0  1  0  ..  0  0
    #      :  :  :  :      :  :
    #      0  0  0  0  ..  0  1
    #      0  0  0  0  ..  1  0
    # Its eigenvalues are 2*cos(k*pi/(n+1)), where k = 1, 2, ... , n.
    # So k = 1 is the largest one and k = n is the smallest one.

    engine = PyLanczos(csr_matrix(matrix),
                       True)  # True to calculate the largest eigenvalue.
    eigval, eigvec, itern = engine.run()
    print("Eigenvalue: {}".format(eigval))
    print("Eigenvector: {}".format(eigvec))
    print("Iteration count: {}".format(itern))
Exemple #2
0
    def test_simple_matrix(self):
        matrix = np.array([[2, 1, 1], [1, 2, 1], [1, 1, 2]], dtype='float64')

        engine = PyLanczos(matrix, True)
        eigval, eigvec, itern = engine.run()

        np.testing.assert_almost_equal(eigval, 4.0)
        sign = np.sign(eigvec[0])
        correct_eigvec = sign / np.sqrt(len(matrix)) * np.array([1, 1, 1])
        np.testing.assert_allclose(eigvec, correct_eigvec)
Exemple #3
0
def exec_sample():
    matrix = np.array([[2.0, 1.0, 1.0],
                       [1.0, 2.0, 1.0],
                       [1.0, 1.0, 2.0]])

    engine = PyLanczos(matrix, True)  # True to calculate the maximum eigenvalue.
    eigval, eigvec, itern = engine.run()
    print("Eigenvalue: {}".format(eigval))
    print("Eigenvector: {}".format(eigvec))
    print("Iteration count: {}".format(itern))
Exemple #4
0
    def test_complex_matrix(self):
        matrix = np.array([[0, 1j, 1], [-1j, 0, 1j], [1, -1j, 0]],
                          dtype='complex128')

        engine = PyLanczos(matrix, False)
        eigval, eigvec, itern = engine.run()

        np.testing.assert_almost_equal(eigval, -2.0)
        phase = np.angle(eigvec[0])
        correct_eigvec = np.array([1, 1j, -1])
        correct_eigvec = np.exp(
            1j * phase) / np.linalg.norm(correct_eigvec) * correct_eigvec
        np.testing.assert_allclose(eigvec, correct_eigvec)
Exemple #5
0
    def test_sparse_matrix_numpy(self):
        n = 10

        matrix = np.zeros((n, n), dtype='float64')
        for i in range(n - 1):
            matrix[i, i + 1] = -1
            matrix[i + 1, i] = -1

        engine = PyLanczos(matrix, False)
        eigval, eigvec, itern = engine.run()

        np.testing.assert_almost_equal(eigval, -2.0 * np.cos(np.pi / (n + 1)))
        sign = np.sign(eigvec[0])

        correct_eigvec = sign * np.sin(
            (1 + np.array(range(n))) * np.pi / (n + 1))
        correct_eigvec /= np.linalg.norm(correct_eigvec)

        np.testing.assert_allclose(eigvec, correct_eigvec)
Exemple #6
0
    def test_sparse_matrix_dynamic(self):
        n = 10

        def mv_mul(v_in, v_out):
            for i in range(n - 1):
                v_out[i] += -1.0 * v_in[i + 1]
                v_out[i + 1] += -1.0 * v_in[i]

        engine = PyLanczos.create_custom(mv_mul, n, 'float64', False)
        eigval, eigvec, itern = engine.run()

        np.testing.assert_almost_equal(eigval, -2.0 * np.cos(np.pi / (n + 1)))
        sign = np.sign(eigvec[0])

        correct_eigvec = sign * np.sin(
            (1 + np.array(range(n))) * np.pi / (n + 1))
        correct_eigvec /= np.linalg.norm(correct_eigvec)

        np.testing.assert_allclose(eigvec, correct_eigvec)
Exemple #7
0
    def test_unsupported_dtype(self):
        matrix = np.array([[2, 1, 1], [1, 2, 1], [1, 1, 2]], dtype='int64')

        with self.assertRaises(Exception):
            PyLanczos(matrix, True)
Exemple #8
0
import numpy as np
from pylanczos import PyLanczos

if __name__ == "__main__":
    matrix = np.array([[2.0, 1.0, 1.0], [1.0, 2.0, 1.0], [1.0, 1.0, 2.0]])

    engine = PyLanczos(matrix,
                       True)  # True to calculate the maximum eigenvalue.
    eigval, eigvec, itern = engine.run()
    print("Eigenvalue: {}".format(eigval))
    print("Eigenvector: {}".format(eigvec))