2019年1月27日 星期日

例題6-7 利用LU分解法 求行列式的值

例題 6-7 利用LU分解法 求行列式的值
 
#++++++++++++++++++++++++++++++++++
#Determinant of nxn matrix using Python
#++++++++++++++++++++++++++++++++++
#n=3
n=4
print("Enter the order of square matrix: ",n)
'''
a =[ [-1.0 ,   1.0 ,   2.0],
         [3.0 ,  -1.0 ,   1.0],
         [-1.0 ,   3.0 ,   4.0]]

a =[ [2.0 ,   1.0 ,   -3.0],
         [-1.0 ,  3.0 ,   2.0],
         [3.0 ,   1.0 ,   -3.0]]
'''
a =[ [ 8.0 ,   1.0 , 3.0 ,   2.0],
         [2.0 ,  9.0 ,  -1.0 ,  -2.0],
         [1.0 ,  3.0 ,  2.0 ,    -1.0],
         [1.0 ,   0.0 , 6.0 ,   4.0]]

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


#/* Conversion of matrix to upper triangular */
for i in range (0 , n):
    for j in range (0 , n):
        if(j>i):
            ratio = a[j][i]/a[i][i]
            for k in range (0 , n):
                a[j][k] -= ratio * a[i][k];

print("\nConversion of matrix to upper triangular: ")
for i in range(0 ,n):
    for j in range(0,n):
        print( "a[",i,"][",j,"]=",round(a[i][j],4),'\t',end='')
    print("")

det = 1   # //storage for determinant
for i in range (0 ,n):
    det *= a[i][i];
print("\nThe determinant of matrix is: {%.2f}\n\n" %(det))
   


======= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX6-7.py =====
Enter the order of square matrix:  3
a[ 0 ][ 0 ]= -1.0 a[ 0 ][ 1 ]= 1.0 a[ 0 ][ 2 ]= 2.0
a[ 1 ][ 0 ]= 3.0 a[ 1 ][ 1 ]= -1.0 a[ 1 ][ 2 ]= 1.0
a[ 2 ][ 0 ]= -1.0 a[ 2 ][ 1 ]= 3.0 a[ 2 ][ 2 ]= 4.0

Conversion of matrix to upper triangular: 
a[ 0 ][ 0 ]= -1.0 a[ 0 ][ 1 ]= 1.0 a[ 0 ][ 2 ]= 2.0
a[ 1 ][ 0 ]= 0.0 a[ 1 ][ 1 ]= 2.0 a[ 1 ][ 2 ]= 7.0
a[ 2 ][ 0 ]= 0.0 a[ 2 ][ 1 ]= 0.0 a[ 2 ][ 2 ]= -5.0

The determinant of matrix is: {10.00}


>>> 
======== RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX6-7.py ============
Enter the order of square matrix:  3
a[ 0 ][ 0 ]= 2.0 a[ 0 ][ 1 ]= 1.0 a[ 0 ][ 2 ]= -3.0
a[ 1 ][ 0 ]= -1.0 a[ 1 ][ 1 ]= 3.0 a[ 1 ][ 2 ]= 2.0
a[ 2 ][ 0 ]= 3.0 a[ 2 ][ 1 ]= 1.0 a[ 2 ][ 2 ]= -3.0

Conversion of matrix to upper triangular: 
a[ 0 ][ 0 ]= 2.0 a[ 0 ][ 1 ]= 1.0 a[ 0 ][ 2 ]= -3.0
a[ 1 ][ 0 ]= 0.0 a[ 1 ][ 1 ]= 3.5 a[ 1 ][ 2 ]= 0.5
a[ 2 ][ 0 ]= 0.0 a[ 2 ][ 1 ]= 0.0 a[ 2 ][ 2 ]= 1.5714

The determinant of matrix is: {11.00}


>>> 
========= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX6-7.py =============
Enter the order of square matrix:  4
a[ 0 ][ 0 ]= 8.0 a[ 0 ][ 1 ]= 1.0 a[ 0 ][ 2 ]= 3.0 a[ 0 ][ 3 ]= 2.0
a[ 1 ][ 0 ]= 2.0 a[ 1 ][ 1 ]= 9.0 a[ 1 ][ 2 ]= -1.0 a[ 1 ][ 3 ]= -2.0
a[ 2 ][ 0 ]= 1.0 a[ 2 ][ 1 ]= 3.0 a[ 2 ][ 2 ]= 2.0 a[ 2 ][ 3 ]= -1.0
a[ 3 ][ 0 ]= 1.0 a[ 3 ][ 1 ]= 0.0 a[ 3 ][ 2 ]= 6.0 a[ 3 ][ 3 ]= 4.0

Conversion of matrix to upper triangular: 
a[ 0 ][ 0 ]= 8.0 a[ 0 ][ 1 ]= 1.0 a[ 0 ][ 2 ]= 3.0 a[ 0 ][ 3 ]= 2.0
a[ 1 ][ 0 ]= 0.0 a[ 1 ][ 1 ]= 8.75 a[ 1 ][ 2 ]= -1.75 a[ 1 ][ 3 ]= -2.5
a[ 2 ][ 0 ]= 0.0 a[ 2 ][ 1 ]= 0.0 a[ 2 ][ 2 ]= 2.2 a[ 2 ][ 3 ]= -0.4286
a[ 3 ][ 0 ]= 0.0 a[ 3 ][ 1 ]= 0.0 a[ 3 ][ 2 ]= 0.0 a[ 3 ][ 3 ]= 4.8052

The determinant of matrix is: {740.00}


>>> 

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...