-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatrix.py
More file actions
145 lines (125 loc) · 3.26 KB
/
matrix.py
File metadata and controls
145 lines (125 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# print an system of equations given by
# matrix A and vector b
def print_eq(A,b):
row_n = 0
for row_A, row_b in zip(A,b):
print("|", end="")
for element in row_A:
print("{0:^5}".format(element), end="")
if row_n == len(A) - 1:
print("| .x= |", end="")
else:
print("| |", end="")
row_n += 1
print("{0:^5}".format(row_b), end="")
print("|")
# print a vector
def print_vector(x):
for element in x:
print("|", end="")
print(" {0:^12.8f} ".format(element), end="")
print("|")
# print a matrix
def print_matrix(X):
for row in X:
print("|", end="")
for element in row:
print(" {0:^12.8f} ".format(element), end="")
print("|")
# calculate determinant of 2x2 matrix
# For a matrix [[a, b], [c, d]]
# the determinant is ad - bc
def det22(mat):
return mat[0][0]*mat[1][1] - mat[0][1]*mat[1][0]
# calculate determinant of 3x3 matrix
# 0 1 2
# 0 | a b c |
# 1 | d e f |
# 2 | g h i |
#
# |A| = a * det( |e f| ) - b * det( |d f| ) + c * det( |d e| )
# |h i| |g i| |g h|
def det33(mat):
submat1=[[mat[1][1], mat[1][2]],
[mat[2][1], mat[2][2]]]
submat2=[[mat[1][0], mat[1][2]],
[mat[2][0], mat[2][2]]]
submat3=[[mat[1][0], mat[1][1]],
[mat[2][0], mat[2][1]]]
det=mat[0][0]*det22(submat1)
det-=mat[0][1]*det22(submat2)
det+=mat[0][2]*det22(submat3)
return(det)
# matrix is a list of lists, to make a copy, deepcopy is needed
# numworks does not have the deepcopy function, this function performs
# deepcopy of a matrix
def deepcopy(mat):
new_mat = []
for row in mat:
new_mat.append(row.copy())
return new_mat
# solve system of 2 equations with 2 variables
# using Cramer's rule
def solve_system_2(A, b):
det_A = det22(A)
if det_A==0:
print("System has no solutions")
return
else:
solution=[]
for col in range(2):
mat1=deepcopy(A)
for row in range(2):
mat1[row][col]=b[row]
solution.append(det22(mat1) / det_A)
return solution
# solve system of 3 equations with 3 variables
# using Cramer's rule
def solve_system_3(A, b):
det_A = det33(A)
if det_A==0:
print("System has no solutions")
return
else:
solution=[]
for col in range(3):
mat1=deepcopy(A)
for row in range(3):
mat1[row][col]=b[row]
solution.append(det33(mat1) / det_A)
return solution
# check a solution x of system
# A.x = b
# by calculating A.x, should be equal to b
def check_solution(A, x):
N=len(A)
b_check=[0]*N
for row in range(N):
for col in range(N):
b_check[row]+=A[row][col]*x[col]
return b_check
# a test with 2 equations, 2 variables
A=[[6, -5],
[-7, 2]]
b=[2, -3]
print("System of 2 equations\nwith 2 variables")
print("A.x = b\n")
print_eq(A,b)
x=solve_system_2(A, b)
print("\nsolution found:\n x = ")
print_vector(x)
print("\nChecking x by\ncalculating vector b:")
print_vector(check_solution(A, x))
# a test with 3 equations, 3 variables
A=[[5,-6,7],
[-3,5,4],
[4,-8,2]]
b=[4,2,-3]
print("\nSystem of 3 equations\nwith 3 variables")
print("A.x = b\n")
print_eq(A,b)
x=solve_system_3(A, b)
print("\nsolution found:\n x = ")
print_vector(x)
print("\nChecking x by\ncalculating vector b:")
print_vector(check_solution(A, x))