#++++++++++++++++++++++++++++++++++
#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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