コード例 #1
0
def least_square_first_order(vec_x, vec_y):
    mat_x_t = [vec_x,
               [1] * len(vec_x)]
    mat_x = matrix.transpose_mat(mat_x_t)
    mat_y_measured = matrix.transpose_mat([vec_y])
    mat_w_estimated = mul_left_inverse(mat_x, mat_y_measured)
    return mat_w_estimated
コード例 #2
0
    def test_jacobi_method_03(self):
        b_verbose = False
        if b_verbose:
            print("test_jacobi_method_03")
        self.mat_lambda, self.mat_x = evp.jacobi_method(self.mat_h)
        if b_verbose:
            print("L =")
            matrix.mat_xt = matrix.transpose_mat(self.mat_x)
            print("X =")
            matrix.show_mat(self.mat_x)

        self.mat_xt = matrix.transpose_mat(self.mat_x)
        self.mat_x_labda = matrix.mul_mat(self.mat_x, self.mat_labda)
        self.mat_x_labda_xt = matrix.mul_mat(self.mat_x_labda, self.mat_xt)
        if b_verbose:
            print("X L XT")
            matrix.show_mat(self.mat_x_lambda_xt)

        n = len(self.mat_h)
        for i in range(n):
            for j in range(n):
                st = "X L XT[%d][%d] = %g vs A[%d][%d] = %g" % (
                    i, j, self.mat_x_labda_xt[i][j], i, j, self.mat_h[i][j])
                self.assertAlmostEqual(self.mat_x_lambda_xt[i][j],
                                       self.mat_h[i][j],
                                       msg=st)
コード例 #3
0
def least_square_first_order(vec_x, vec_y):
    """
    x와 y 사이에
    y = ax+b
    와 같은 관계과 있을 것으로 가정하고 a, b 를 찾으려고 함
    :param vec_x: List[float]
    :param vec_y: List[float]
    :return:
    """
    # 예를 들어 data point 의 갯수를 n 이라 할 때
    mat_x_t = [vec_x, [1] * len(vec_x)]  #Q : 이 행렬의 크기는?
    mat_x = matrix.transpose_mat(mat_x_t)  #Q : 이 행렬의 크기는?
    mat_y_measured = matrix.transpose_mat([vec_y])  #Q : 이 행렬의 크기는?
    mat_w_estimated = mul_left_inverse(mat_x, mat_y_measured)  #Q : 이 행렬의 크기는?
    return mat_w_estimated
コード例 #4
0
ファイル: test_evp.py プロジェクト: soake/16eca_sangwonjung
    def test_power_method_01(self):
        b_verbose = False
        if b_verbose:
            print ("Power method 01")
        for n in range(3, 10):
            self.mat_a_half = matrix.get_random_mat(n, n)
            self.mat_a_half_transpose = matrix.transpose_mat(self.mat_a_half)

            self.mat_a = matrix.mul_mat(self.mat_a_half_transpose, self.mat_a_half)
            lam, vec_x0 = evp.power_method(self.mat_a, espilon=1e-12)
            if b_verbose:
                print ('%s %s' % (lam, vec_x0))

            vec_x1 = matrix.mul_mat_vec(self.mat_a, vec_x0)
            vec_x0l = [lam * x0k for x0k in vec_x0]

            self.assertEqual(len(vec_x0), n)

            message = '''
x0 =
%s
mat_a x0 =
%s
''' % (vec_x0, vec_x0l)
            self.assertSequenceAlmostEqual(vec_x0l, vec_x1, msg=me)
コード例 #5
0
    def test_jacobi_method_01(self):
        b_verbose = 0
        if b_verbose:
            print("test_jacobi_method_01")
        self.mat_lambda, self.mat_x = evp.jacobi_method(self.mat_h)
        if b_verbose:
            print("L =")
            matrix.show_mat(self.mat_lambda)
            print("X =")
            matrix.show_mat(self.mat_x)

        self.mat_x_transpose = matrix.transpose_mat(self.mat_x)
        self.mat_xt_h = matrix.mul_mat(self.mat_x_transpose, self.mat_h)
        self.mat_xt_h_x = matrix.mul_mat(self.mat_xt_h, self.mat_x)
        if b_verbose:
            print("XT A X")
            matrix.show_mat(self.mat_xt_h_x)

        n = len(self.mat_h)
        for i in range(n):
            for j in range(n):
                st = "XT A X [%d][%d] = %g" % (i, j, self.mat_xt_h_x[i][j], i,
                                               j, self.mat_lambda[i][j])
                self.assertAlmostEqual(self.mat_xt_h_x[i][j],
                                       self.mat_lambda[i][j],
                                       msg=st)
コード例 #6
0
 def test_transpose(self):
     self.mat_r = m.transpose_mat(self.mat_a)
     self.mat_exp = [
         [self.mat_a[0][0], self.mat_a[1][0]],
         [self.mat_a[0][1], self.mat_a[1][1]],
     ]
     self.assertSequenceEqual(self.mat_r, self.mat_exp)
コード例 #7
0
def main():
    mat_a = [[2.0, 1.0], [1.0, 3.0]]
    lambda1, vec_x1 = power_method(mat_a)
    print("lambda = %s" % lambda1)
    print("x = %s" % vec_x1)
    lambda2, mat_x = jacobi_method(mat_a)
    print("lambda = %s" % lambda2)
    print("x =")

    matrix.show_mat(mat_x)
    mat_a3 = [[8.0, 4.0, 2.0], [4.0, 8.0, 4.0], [2.0, 4.0, 8.0]]
    mat_l, mat_x = jacobi_method(mat_a3, b_verbose=True)
    print("L =")
    matrix.show.mat(mat_l)
    print("X =")
    matrix.show_mat(mat_x)
    mat_x_t = matrix.transpose_mat(mat_x)
    print("XT =")
    matrix.show_mat(mat_x_t)
    print("X XT =")
    matrix.show_mat(matrix.mul_mat(mat_x, mat_x_t))
    print("XT A X = L")
    matrix.show_mat(matrix.mul_mat(matrix.mul_mat(mat_x_t, mat_a3), mat_x))
    print("A = X L XT")
    matrix.show_mat(matrix.mul_mat(matrix.mul_mat(mat_x, mat_l), mat_x_t))
コード例 #8
0
def get_left_inverse(mat_x):
    """
    :param mat_x: List[List[float]]
    :return: List[List[float]]
    """
    # 예를 들어 data point 의 갯수를 n,
    #           매개변수의 갯수를 m 이라 할 때
    mat_x_t = matrix.transpose_mat(mat_x)  #Q : 이 행렬의 크기는?
    mat_xt_x = matrix.mul_mat(mat_x_t, mat_x)  #Q : 이 행렬의 크기는?
    mat_xt_x_inv = gj.gauss_jordan(mat_xt_x)  #Q : 이 행렬의 크기는?
    mat_x_left_inverse = matrix.mul_mat(mat_xt_x_inv, mat_x_t)  #Q : 이 행렬의 크기는?
    return mat_x_left_inverse
コード例 #9
0
def get_left_inverse(mat_x):
    '''
    :param mat_x: List[List[float]]
    :return: List[List[float]]
    '''
    # 예를 들어 deta point의 갯수를 n
    #           매개변수의 갯수를 n이라 할때
    mat_x_t = matrix.transpose_mat(mat_x)
    mat_xt_x = matrix.mul_mat(mat_x_t, mat_x)
    mat_xt_x_inv = gj.gauss_jordan(mat_xt_x)
    mat_x_left_inverse = matrix.mul_mat(mat_xt_x_inv, mat_x_t)
    return mat_x_left_inverse
コード例 #10
0
def get_left_inverse(mat_x):
    mat_x_t = matrix.transpose_mat(mat_x)
    mat_xt_x = matrix.mul_mat(mat_x_t, mat_x)
    mat_xt_x_inv = gj.gauss_jordan(mat_xt_x)
    mat_x_left_inverse = matrix.mul_mat(mat_xt_x_inv, mat_x_t)
    return mat_x_left_inverse