Ejemplo n.º 1
0
    def test_jacobi_method(self):
        A = [[-1.0, -0.5, -0.2],
             [-0.5, -2.0, -1.0],
             [-0.2, -1.0, -3.0]]

        lamda1, x1 = ea.jacobi_method (A)
        self.assertEqual(len(A), len(lamda1))
        self.assertEqual(len(A[0]), len(lamda1[0]))
        self.assertEqual(len(A), len(x1))
        self.assertEqual(len(A[0]), len(x1[0]))

        Ax1 = la.multiply_matrix_matrix(A, x1)
        # check A V = Lambda V
        for k_pivot in xrange(len(A)):
            # diagonal term
            lambda_i = lamda1[k_pivot][k_pivot]

            # off diagonal
            for i_row in xrange(len(A)):
                self.assertAlmostEqual(Ax1[i_row][k_pivot],lambda_i * x1[i_row][k_pivot])

        # check VT A V = Lambda
        x1TAx1 = la.multiply_matrix_matrix(zip(*x1), Ax1)

        for i_row in xrange(0, len(A) - 1):
            # check diagonal
            self.assertAlmostEqual(x1TAx1[i_row][i_row], lamda1[i_row][i_row])
            # check off-diagonal
            for j_column in xrange(i_row + 1, len(A)):
                self.assertAlmostEqual(x1TAx1[i_row][j_column], 0.0)
Ejemplo n.º 2
0
    def test_jacobi_method(self):
        A = [[-1.0, -0.5, -0.2], [-0.5, -2.0, -1.0], [-0.2, -1.0, -3.0]]

        lamda1, x1 = ea.jacobi_method(A)
        self.assertEqual(len(A), len(lamda1))
        self.assertEqual(len(A[0]), len(lamda1[0]))
        self.assertEqual(len(A), len(x1))
        self.assertEqual(len(A[0]), len(x1[0]))

        Ax1 = la.multiply_matrix_matrix(A, x1)
        # check A V = Lambda V
        for k_pivot in xrange(len(A)):
            # diagonal term
            lambda_i = lamda1[k_pivot][k_pivot]

            # off diagonal
            for i_row in xrange(len(A)):
                self.assertAlmostEqual(Ax1[i_row][k_pivot],
                                       lambda_i * x1[i_row][k_pivot])

        # check VT A V = Lambda
        x1TAx1 = la.multiply_matrix_matrix(zip(*x1), Ax1)

        for i_row in xrange(0, len(A) - 1):
            # check diagonal
            self.assertAlmostEqual(x1TAx1[i_row][i_row], lamda1[i_row][i_row])
            # check off-diagonal
            for j_column in xrange(i_row + 1, len(A)):
                self.assertAlmostEqual(x1TAx1[i_row][j_column], 0.0)
Ejemplo n.º 3
0
def general_eigenproblem_symmetric(A, B):
    """
    Solve Az = lambda Bz using Cholesky decomposition
    Let
        B = L LT
    and
        z = LT**(-1)y
    then
        A LT**(-1)y = lambda L LT LT**(-1)y = lambda L y
    Mutiplying L**(-1) gives
        L**(-1) A LT**(-1)y = lambda L**(-1) L y = lambda y
    So let
        C = L**(-1) A LT**(-1)
    and find eigenvalues and eigenvectors of C.
    Later
        Z = LT**(-1)Y

    ref: Susan Blackford, Generalized Symmetric Definite Eigenproblems, http://www.netlib.org/lapack/lug/node54.html,
            1999 Oct 01 (accessed 2015 Nov 30).

    :param A: n x n matrix
    :param B: n x n matrix
    :return w: 1 x n eigenvalue vector
    :return Z: n x n eigenvector matrix
    """

    L = cholesky_decomposition(B)
    LT = zip(*L)

    L_inv = gj.gauss_jordan(L)
    LT_inv = gj.gauss_jordan(LT)

    del L[:], LT[:]
    del L, LT

    L_inv_A = la.multiply_matrix_matrix(L_inv, A)

    del L_inv[:]
    del L_inv

    C = la.multiply_matrix_matrix(L_inv_A, LT_inv)

    W, Y = jacobi_method(C)

    del C[:]
    del C

    # diagonal elements
    w = [Wi[i] for i, Wi in enumerate(W)]

    del W[:]
    del W

    Z = la.multiply_matrix_matrix(LT_inv, Y)

    del Y[:]
    del Y

    return w, Z
Ejemplo n.º 4
0
def general_eigenproblem_symmetric(A, B):
    """
    Solve Az = lambda Bz using Cholesky decomposition
    Let
        B = L LT
    and
        z = LT**(-1)y
    then
        A LT**(-1)y = lambda L LT LT**(-1)y = lambda L y
    Mutiplying L**(-1) gives
        L**(-1) A LT**(-1)y = lambda L**(-1) L y = lambda y
    So let
        C = L**(-1) A LT**(-1)
    and find eigenvalues and eigenvectors of C.
    Later
        Z = LT**(-1)Y

    ref: Susan Blackford, Generalized Symmetric Definite Eigenproblems, http://www.netlib.org/lapack/lug/node54.html,
            1999 Oct 01 (accessed 2015 Nov 30).

    :param A: n x n matrix
    :param B: n x n matrix
    :return w: 1 x n eigenvalue vector
    :return Z: n x n eigenvector matrix
    """

    L = cholesky_decomposition(B)
    LT = zip(*L)

    L_inv = gj.gauss_jordan(L)
    LT_inv = gj.gauss_jordan(LT)

    del L[:], LT[:]
    del L, LT

    L_inv_A = la.multiply_matrix_matrix(L_inv, A)

    del L_inv[:]
    del L_inv

    C = la.multiply_matrix_matrix(L_inv_A, LT_inv)

    W, Y = jacobi_method(C)

    del C[:]
    del C

    # diagonal elements
    w = [Wi[i] for i, Wi in enumerate(W)]

    del W[:]
    del W

    Z = la.multiply_matrix_matrix(LT_inv, Y)

    del Y[:]
    del Y

    return w, Z
Ejemplo n.º 5
0
    def test_multiply_matrix_matrix_02(self):

        for angle_deg in xrange(361):
            angle_rad = math.radians(angle_deg)
            cos = math.cos(angle_rad)
            sin = math.sin(angle_rad)
            A = [[cos, -sin],
                 [sin,  cos]]

            angle2_deg = - angle_deg
            angle2_rad = math.radians(angle2_deg)
            cos2 = math.cos(angle2_rad)
            sin2 = math.sin(angle2_rad)
            B = [[cos2, -sin2],
                 [sin2,  cos2]]

            C = la.multiply_matrix_matrix (A, B)
            expected = [[1.0, 0.0],
                        [0.0, 1.0]]

            self.assertEqual(len(C), len(expected))

            for k in xrange(len(expected)):
                self.assertEqual(len(C[k]), len(expected[k]))
                for j in xrange(len(expected[k])):
                    self.assertAlmostEqual(C[k][j], expected[k][j])
Ejemplo n.º 6
0
    def test_multiply_matrix_matrix_01(self):
        A = [[1.0, 1.0],
             [1.0, 1.0]]
        B = [[1.0, -1.0],
             [-1.0, 1.0]]
        C = la.multiply_matrix_matrix(A, B)
        expected = [[0.0, 0.0],
                    [0.0, 0.0]]

        self.assertSequenceEqual(C, expected)
Ejemplo n.º 7
0
    def test_cholesky_decomposition_01(self):
        L_expected = [[10.0, 0.0, 0.0, 0.0], [4.0, 9.0, 0.0, 0.0],
                      [3.0, 5.0, 8.0, 0.0], [1.0, 2.0, 6.0, 7.0]]

        A = la.multiply_matrix_matrix(L_expected, zip(*L_expected))

        L = ea.cholesky_decomposition(A)

        A_expected = la.multiply_matrix_matrix(L, zip(*L))

        # check size
        self.assertEqual(len(A), len(L))

        # check row
        for i_row in xrange(0, len(A)):
            self.assertEqual(len(A), len(L[i_row]))
            for j_column in xrange(0, len(A)):
                self.assertAlmostEqual(A[i_row][j_column],
                                       A_expected[i_row][j_column])
                self.assertAlmostEqual(L_expected[i_row][j_column],
                                       L[i_row][j_column])
Ejemplo n.º 8
0
    def test_cholesky_decomposition_01(self):
        L_expected = [[10.0, 0.0, 0.0, 0.0],
                      [4.0, 9.0, 0.0, 0.0],
                      [3.0, 5.0, 8.0, 0.0],
                      [1.0, 2.0, 6.0, 7.0]]

        A = la.multiply_matrix_matrix(L_expected, zip(*L_expected))

        L = ea.cholesky_decomposition(A)

        A_expected = la.multiply_matrix_matrix(L, zip(*L))

        # check size
        self.assertEqual(len(A), len(L))

        # check row
        for i_row in xrange(0, len(A)):
            self.assertEqual(len(A), len(L[i_row]))
            for j_column in xrange(0, len(A)):
                self.assertAlmostEqual(A[i_row][j_column], A_expected[i_row][j_column])
                self.assertAlmostEqual(L_expected[i_row][j_column], L[i_row][j_column])
Ejemplo n.º 9
0
    def test_multiply_matrix_matrix_03(self):
        A = [[11, 12, 13],
             [21, 22, 23]]
        B = [[111, 112],
             [121, 122],
             [131, 132]]
        C = la.multiply_matrix_matrix(A, B)

        BT = zip(*B)

        expected = [[la.dot(A[0], BT[0]), la.dot(A[0], BT[1])],
                    [la.dot(A[1], BT[0]), la.dot(A[1], BT[1])]]

        self.assertSequenceEqual(C, expected)
Ejemplo n.º 10
0
    def test_gauss_jordan_01(self):
        A = [[3, 2, 1],
             [2, 3, 2],
             [1, 2, 3]]

        A_inverse = gj.gauss_jordan(A)

        result = la.multiply_matrix_matrix(A, A_inverse)

        self.assertEqual(len(A), len(A_inverse))
        for i in xrange(len(A)):
            self.assertEqual(len(A[i]), len(A_inverse[i]))

            for j in xrange(len(A[i])):
                if i == j:
                    self.assertAlmostEqual(result[i][j], 1.0)
                else:
                    self.assertAlmostEqual(result[i][j], 0.0)
Ejemplo n.º 11
0
    def test_cholesky_decomposition_00(self):
        A = [[16.0, 12.0,  4.0,],
             [12.0, 34.0, 13.0,],
             [ 4.0, 13.0, 41.0,]]

        L_expected = ((4, 0, 0),
                      (3, 5, 0),
                      (1, 2, 6),)

        L = ea.cholesky_decomposition(A)

        A_expected = la.multiply_matrix_matrix(L, zip(*L))

        # check size
        self.assertEqual(len(A), len(L))

        # check row
        for i_row in xrange(0, len(A)):
            self.assertEqual(len(A), len(L[i_row]))
            for j_column in xrange(0, len(A)):
                self.assertAlmostEqual(A[i_row][j_column], A_expected[i_row][j_column])
                self.assertAlmostEqual(L_expected[i_row][j_column], L[i_row][j_column])
Ejemplo n.º 12
0
    def test_cholesky_decomposition_00(self):
        A = [[
            16.0,
            12.0,
            4.0,
        ], [
            12.0,
            34.0,
            13.0,
        ], [
            4.0,
            13.0,
            41.0,
        ]]

        L_expected = (
            (4, 0, 0),
            (3, 5, 0),
            (1, 2, 6),
        )

        L = ea.cholesky_decomposition(A)

        A_expected = la.multiply_matrix_matrix(L, zip(*L))

        # check size
        self.assertEqual(len(A), len(L))

        # check row
        for i_row in xrange(0, len(A)):
            self.assertEqual(len(A), len(L[i_row]))
            for j_column in xrange(0, len(A)):
                self.assertAlmostEqual(A[i_row][j_column],
                                       A_expected[i_row][j_column])
                self.assertAlmostEqual(L_expected[i_row][j_column],
                                       L[i_row][j_column])
Ejemplo n.º 13
0
        # 행 반복문
        for j_row in xrange(0, n_row):
            if j_row != i_pivot:
                ratio = -AI[j_row][i_pivot]
                # 열 반복문
                for k_column in xrange(n_column * 2):
                    AI[j_row][k_column] += ratio * AI[i_pivot][k_column]
    # 이 반복문이 끝나고 나면 주 대각선 이외의 요소는 모두 0

    print "After Gauss Jordan"
    pprint(AI)

    # 오른쪽의 행렬을 떼어냄
    result = []
    for i_row in xrange(n_row):
        result.append(AI[i_row][n_column:])

    return result


if "__main__" == __name__:
    A = [[3, 2, 1], [2, 3, 2], [1, 2, 3]]

    A_inverse = gauss_jordan(A)
    print "A inverse"
    pprint(A_inverse)

    I_expected = la.multiply_matrix_matrix(A, A_inverse)
    print "I expected"
    pprint(I_expected)
Ejemplo n.º 14
0
            if j_row != i_pivot:
                ratio = -AI[j_row][i_pivot]
                # 열 반복문
                for k_column in xrange(n_column * 2):
                    AI[j_row][k_column] += ratio * AI[i_pivot][k_column]
    # 이 반복문이 끝나고 나면 주 대각선 이외의 요소는 모두 0

    print "After Gauss Jordan"
    pprint (AI)

    # 오른쪽의 행렬을 떼어냄
    result = []
    for i_row in xrange(n_row):
        result.append(AI[i_row][n_column:])

    return result


if "__main__" == __name__:
    A = [[3, 2, 1],
         [2, 3, 2],
         [1, 2, 3]]

    A_inverse = gauss_jordan(A)
    print "A inverse"
    pprint(A_inverse)

    I_expected = la.multiply_matrix_matrix(A, A_inverse)
    print "I expected"
    pprint(I_expected)
Ejemplo n.º 15
0
            pprint(X)
        counter += 1

    print("Jacobi method counter = %d" % counter)

    return A0, X


if "__main__" == __name__:
    A = [[2.0, 1.0],
         [1.0, 3.0]]

    lamda1, x1 = power_method(A)
    print "lambda =", lamda1
    print "x =", x1

    lamda, x = jacobi_method(A)
    print "lambda ="
    pprint(lamda, width=30)
    print "x ="
    pprint(x)

    import random
    n = 64
    A = [[random.randint(1, 127)/256.0 for j_column in xrange(n)] for i_row in xrange(n)]
    # symmetric
    A = la.multiply_matrix_matrix(A, zip(*A))

    import cProfile
    cProfile.run('jacobi_method(A)')