2019年1月27日 星期日

例題6-8 高斯消去法 Python program to find determinant of Matrix

例題6-8 Python program to find determinant of Matrix


Now consider an example of 4×4 matrix :
C program to find determinant of Matrix | C code champ
Notice the + – + – pattern (+a… -b… +c… -d…). This is important to remember.
The pattern continues for 5×5 matrices and higher.
Now let us see how to write a  MxM . The program will accept the order of matrix i.e. M as input from user and then ask user to enter the values of matrix elements. Then it will calculate the determinant of the matrix and returns the output.
#Python program to find determinant of Matrix
def determinant( a , n):
    b=  [ [0.0 for i in range(n+1)]  for j in range(n+1) ]       #[n][n], 
    c = [0.0 for i in range(n+1)];   
    if(n==2):
        d=0
        d=(a[1][1]*a[2][2])-(a[1][2]*a[2][1]);
        return(d);
    else:
        for j in range(1 , n+1):
            r=1
            s=1
            for p in range (1, n+1):
                for q in range (1, n+1):
                    if ((p!=1) & (q!=j)):
                        b[r][s]=a[p][q]
                        s=s+1
                        if(s>n-1):
                            r=r+1
                            s=1

            pr=1                
            for t in range (1, j+2):
                pr=(-1)*pr;
                c[j]=pr*determinant(b,n-1);

    d=0
    for j in range (1, n+1):
        d=d+(a[1][j]*c[j]);
    return(d);

#++++++++++++++++++++++++++++++++++++++++++
n=3
a=  [ [0.0 for i in range(n+1)]  for j in range(n+1) ]       #[n][n], 
print("------------------------------------------------------------------------")
print("---------Python program to find determinant of Matrix ------------")
print("------------------------------------------------------------------------")

print("\n\nEnter order of matrix : ",n)
print("\nEnter the elements of matrix\n");
print("\n\n---------- Matrix A is --------------\n");   

a=[[ 0.0 ,0.0 ,  0.0 , 0.0 , 0.0],
      [ 0.0 ,2.0 ,  1.0 , -3.0 , 0.0],
      [ 0.0 , -1.0 , 3.0 , 2.0,  0.0],
      [ 0.0 ,3.0 ,  1.0 , -3.0 , 0.0]]
for i in range(1 ,n+1):
    for j in range(1,n+1):
        print( "a[",i,"][",j,"]=",round(a[i][j],4),'\t',end='')
    print("")

print("\n Determinant of Matrix A is %d .",determinant(a,n));
輸出畫面
= RESTART: C:/Users/alex/AppData/Local/Programs/Python/Python37-32/EX6-8.py =
------------------------------------------------------------------------
---------Python program to find determinant of Matrix ------------
------------------------------------------------------------------------
Enter order of matrix :  3
Enter the elements of matrix
---------- Matrix A is --------------
a[ 1 ][ 1 ]= 2.0 a[ 1 ][ 2 ]= 1.0 a[ 1 ][ 3 ]= -3.0
a[ 2 ][ 1 ]= -1.0 a[ 2 ][ 2 ]= 3.0 a[ 2 ][ 3 ]= 2.0
a[ 3 ][ 1 ]= 3.0 a[ 3 ][ 2 ]= 1.0 a[ 3 ][ 3 ]= -3.0
 Determinant of Matrix A is %d . 11.0

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...