2019年1月27日 星期日

例題6-8 求行列式的結果

例題6-8 求行列式的結果

'''
/*This is a program to display the
upper triangular matrix and lower triangular matrix
for any given matrix. The method used here is
LU decomposition method. This program works for matrices
of order equal to or below 10x10*/
'''
#n=3
n=5
print("\nENTER THE SIZE OF THE MATRIX(size<10)",n);
uppertriangle= [ [0.0 for i in range(n+1)]  for j in range(n+1) ]
lowertriangle= [ [0.0 for i in range(n+1)]  for j in range(n+1) ]
for i in range (0,n):
    lowertriangle[i][i]=1.0
'''
matrix=[[ 4.0 ,  -2.0 ,  1.0 ],
               [ 3.0 ,  0.0 ,  -5.0 ],
               [ 1.0 ,  -3.0 , -4.0]]

matrix=[[2.00, 3.00 , 1.00 , 5.00],
               [6.00 , 13.00 ,5.00 ,19.00],
               [2.00 ,19.00 ,10.00, 23.00],
               [4.00 ,10.00 ,11.00, 31.00]]

'''
matrix=[[-6.00, 0.00 , 1.00 ,3.00 , 2.00],
               [-1.00 , 5.00 , 0.00 ,  1.00 , 7.00],
               [8.00 , 3.00 , 2.00 , 1.00 , 7.00],
               [0.00 , 1.00 , 5.00 ,  -3.00 , 2.00],
               [1.00 ,15.00 , -3.00,  9.00 , 4.00]]

for i in range(0 ,n):
    for j in range(0,n):
        print( "m[",i,"][",j,"]=",round(matrix[i][j],4),'\t',end='')
    print("")

#/*The algorithm used later is from the book Introduction to algorithms
#    by Thomas Cormen.For a better understaning one can
#    read this text under chapter Matrix Operations. */
for ctr1 in range(0 , n):
    uppertriangle[ctr1][ctr1]=matrix[ctr1][ctr1];
    for ctr2 in range(ctr1+1, n):
        uppertriangle[ctr1][ctr2]=matrix[ctr1][ctr2];
        lowertriangle[ctr2][ctr1]=matrix[ctr2][ctr1]/uppertriangle[ctr1][ctr1];

    for row in range (ctr1+1 , n):
        for column in range(ctr1+1 , n):
                    matrix[row][column]=matrix[row][column]-lowertriangle[row][ctr1]*uppertriangle[ctr1][column];

#+++++++++++++++++++++++++++++++++++   
print("\nUPPER TRIANGULAR MATRIX");
for i in range(0 ,n):
    for j in range(0,n):
        print( "u[",i,"][",j,"]=",round(uppertriangle[i][j],4),'\t',end='')
    print("")
#+++++++++++++++++++++++++++++++++++   
print("\nLOWER TRIANGULAR MATRIX");
for i in range(0 ,n):
    for j in range(0,n):
        print( "l[",i,"][",j,"]=",round(lowertriangle[i][j],4),'\t',end='')
    print("")


det=1.0
print("\nDeterminant of Matrix  is")
for i in range(0 ,n):
    for j in range(0,n):
        if (i==j):
            print( "u[",i,"][",j,"]=",round(uppertriangle[i][j],4),'*',end='')
            det=det*uppertriangle[i][j]
       

print("\nDeterminant of Matrix  is",det)



輸出畫面
======= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX6-8-2.py ===========

ENTER THE SIZE OF THE MATRIX(size<10) 5
m[ 0 ][ 0 ]= -6.0 m[ 0 ][ 1 ]= 0.0 m[ 0 ][ 2 ]= 1.0 m[ 0 ][ 3 ]= 3.0 m[ 0 ][ 4 ]= 2.0
m[ 1 ][ 0 ]= -1.0 m[ 1 ][ 1 ]= 5.0 m[ 1 ][ 2 ]= 0.0 m[ 1 ][ 3 ]= 1.0 m[ 1 ][ 4 ]= 7.0
m[ 2 ][ 0 ]= 8.0 m[ 2 ][ 1 ]= 3.0 m[ 2 ][ 2 ]= 2.0 m[ 2 ][ 3 ]= 1.0 m[ 2 ][ 4 ]= 7.0
m[ 3 ][ 0 ]= 0.0 m[ 3 ][ 1 ]= 1.0 m[ 3 ][ 2 ]= 5.0 m[ 3 ][ 3 ]= -3.0 m[ 3 ][ 4 ]= 2.0
m[ 4 ][ 0 ]= 1.0 m[ 4 ][ 1 ]= 15.0 m[ 4 ][ 2 ]= -3.0 m[ 4 ][ 3 ]= 9.0 m[ 4 ][ 4 ]= 4.0

UPPER TRIANGULAR MATRIX
u[ 0 ][ 0 ]= -6.0 u[ 0 ][ 1 ]= 0.0 u[ 0 ][ 2 ]= 1.0 u[ 0 ][ 3 ]= 3.0 u[ 0 ][ 4 ]= 2.0
u[ 1 ][ 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