Beispiel #1
0
def main():
    a = Dmatrix(1, N, 1, N) # 行列領域の確保
    x = Dvector(1, N)       # ベクトル領域の確保

    # ファイルのオープン
    with open("input_eigen.dat", "r") as fin:
        with open("result_eigen.dat", "w") as fout:
            input_matrix( a, 'A', fin, fout ) # 行列 A の入出力
            input_vector( x, 'x', fin, fout ) # ベクトル x の入出力
            power_method( a, x, fout )        # べき乗法
Beispiel #2
0
def main():
    a = Dmatrix(1, N, 1, N)

    # ファイルのオープン
    with open("input_eigen.dat", "r") as fin:
        with open("result_eigen.dat", "w") as fout:
            input_matrix(a, 'A', fin, fout)  # 行列 A の入出力
            a_hh = householder(a, N)  # ハウスホルダー法

            # 結果の出力
            print("Hessenberg 行列は")
            for i in range(1, N + 1):
                for j in range(1, N + 1):
                    print("{:10.7f}\t".format(a_hh[i][j]), end="")
                print()
Beispiel #3
0
def main():
    global N
    a = Dmatrix(1, N, 1, N) # 行列 a[1...N][1...N]
    b = Dvector(1, N) # b[1...N]

    # ファイルのオープン
    with open("input.dat", "r") as fin:
        with open("output.dat", "w") as fout:
            input_matrix( a, 'A', fin, fout ) # 行列 A の入出力
            input_vector( b, 'b', fin, fout ) # ベクトル b の入出力
            b = simple_gauss( a, b )          # ガウス消去法

            # 結果の出力
            fout.write("Ax=b の解は次の通りです\n")
            for i in range(1, N+1):
                fout.write("{:.6f}\n".format(b[i]))
Beispiel #4
0
def main():
    global N

    a = Dmatrix(1, N, 1, N)
    b = Dvector(1, N)

    # ファイルのオープン
    with open("input.dat", "r") as fin:
        with open("output.dat", "w") as fout:
            input_matrix(a, 'A', fin, fout)  # 行列 A の入出力
            input_vector(b, 'b', fin, fout)  # ベクトル b の入出力
            b = gauss(a, b)  # ガウス消去法

            # 結果の出力
            fout.write("Ax=b の解は次の通りです\n")
            for i in range(1, N + 1):
                fout.write(f"{b[i]}\n")
Beispiel #5
0
def main():
    a = Dmatrix(1, N, 1, N) # 行列 a[1...N][1...N]
    b = Dvector(1, N)       # b[1...N]
    x = Dvector(1, N)       # x[1...N]

    # ファイルのオープン
    with open("input_sp.dat", "r") as fin:
        with open("output_sp.dat", "w") as fout:
            input_matrix( a, 'A', fin, fout ) # 行列 A の入出力
            input_vector( b, 'b', fin, fout ) # ベクトル b の入出力
            input_vector( x, 'x', fin, fout ) # 初期ベクトル x0 の入出力
            x = jacobi_lin( a, b, x )         # ヤコビ法

            # 結果の出力
            fout.write("Ax=b の解は次の通りです\n")
            for i in range(1, N+1):
                fout.write("{:.6f}\n".format(x[i]))
Beispiel #6
0
def main():
    a = Dmatrix(1, N, 1, N)  # 行列 a[1...N][1...N]
    b = Dvector(1, N)  # b[1...N]
    x0 = Dvector(1, N)  # x[1...N]

    # ファイルのオープン
    with open("input_sp.dat", "r") as fin:
        with open("output_sp.dat", "w") as fout:
            input_matrix(a, 'A', fin, fout)  # 行列 A の入出力
            input_vector(b, 'b', fin, fout)  # ベクトル b の入出力
            input_vector(x0, 'x0', fin, fout)  # 初期ベクトル x0 の入出力
            x = gauss_seidel(a, b, x0)  # ガウス・ザイデル法

            # 結果の出力
            fout.write("Ax=b の解は次の通りです\n")
            for i in range(1, N + 1):
                fout.write("{:.6f}\n".format(x[i]))
Beispiel #7
0
def main():
    global N

    a = Dmatrix(1, N, 1, N)  # 行列 a[1...N][1...N]
    b = Dvector(1, N)  # b[1...N]

    # ファイルのオープン
    with open("input_cho.dat", "r") as fin:
        with open("output_cho.dat", "w") as fout:
            input_matrix(a, 'A', fin, fout)  # 行列 A の入出力
            input_vector(b, 'b', fin, fout)  # ベクトル b の入出力
            a_cd = cholesky_decomp(a)  # 修正コレスキー分解
            b_cs = cholesky_solve(a_cd, b)  # 前進代入・後退代入

            # 結果の出力
            fout.write("Ax=bの解は次の通りです\n")
            for i in range(1, N + 1):
                fout.write("{:.6f}\t\n".format(b_cs[i]))
Beispiel #8
0
def main():
    global N

    a = Dmatrix(1, N, 1, N) # 行列 a[1...N][1...N]
    b = Dvector(1,N) # b[1...N]

    # ファイルのオープン
    with open("input_lu.dat", "r") as fin:
        with open("output_lu.dat", "w") as fout:
            input_matrix( a, 'A', fin, fout ) # 行列 A の入力
            input_vector( b, 'B', fin, fout ) # ベクトル b の入出力
            a_lu, p = lu_decomp( a )          # LU分解
            b_lu = lu_solve( a_lu, b, p )     # 前進代入・後退代入

            # 結果の出力
            fout.write("Ax=b の解は次の通りです\n")
            for i in b_lu:
                fout.write(f"{i}\n")
Beispiel #9
0
def main():
    eps = 10.0 ** -8.0

    a = Dmatrix(1, N, 1, N) # 行列領域の確保

    # ファイルのオープン
    with open("input_eigen.dat", "r") as fin:
        with open("result_eigen.dat", "w") as fout:
            input_matrix( a, 'A', fin, fout ) # 行列 A の入出力
            a_hh = householder( a, N )               # ハウスホルダー法
            a_qr = qr( a_hh, eps, N )                   # QR法

            # 結果の出力
            print("QR法の結果は")
            for i in range(1, N+1):
                for j in range(1, N+1):
                    print("{:10.7f}\t".format(a_qr[i][j]), end="")
                print()

            print("固有値は")
            for i in range(1, N+1):
                print("{:10.7f}\t".format(a_qr[i][i]), end="")
            print()
Beispiel #10
0
def main():
    eps = 10.0**-8.0

    a = Dmatrix(1, N, 1, N)  # 行列領域の確保

    with open("input_eigen.dat", "r") as fin:
        with open("result_eigen.dat", "w") as fout:
            input_matrix(a, 'A', fin, fout)  # 行列Aの入出力

            a_hh = householder(a, N)  # ハウスホルダー法

            a_qr = qr(a_hh, eps, N)  # QR 法
            print("固有値は")
            for i in range(1, N + 1):
                print("{:10.7f}".format(a_qr[i][i]), end="\t")
            print()

            a_ii = inverse_iteration(a, a_qr, eps)  # 逆反復法
            print("固有ベクトルは")
            for i in range(1, N + 1):
                print("[", end="")
                for j in range(1, N + 1):
                    print("{:10.7f}".format(a_ii[j][i]), end="\t")
                print("]")