forked from ThomIves/SystemOfEquations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevelopmentNotebook.py
More file actions
142 lines (89 loc) · 2.77 KB
/
DevelopmentNotebook.py
File metadata and controls
142 lines (89 loc) · 2.77 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
#!/usr/bin/env python
# coding: utf-8
# In[26]:
def print_matrix(Title, M):
print(Title)
for row in M:
print([round(x,3)+0 for x in row])
def print_matrices(Action, Title1, M1, Title2, M2):
print(Action)
print(Title1, '\t'*int(len(M1)/2)+"\t"*len(M1), Title2)
for i in range(len(M1)):
row1 = ['{0:+7.3f}'.format(x) for x in M1[i]]
row2 = ['{0:+7.3f}'.format(x) for x in M2[i]]
print(row1,'\t', row2)
def zeros_matrix(rows, cols):
A = []
for i in range(rows):
A.append([])
for j in range(cols):
A[-1].append(0.0)
return A
def copy_matrix(M):
rows = len(M)
cols = len(M[0])
MC = zeros_matrix(rows, cols)
for i in range(rows):
for j in range(cols):
MC[i][j] = M[i][j]
return MC
def matrix_multiply(A,B):
rowsA = len(A)
colsA = len(A[0])
rowsB = len(B)
colsB = len(B[0])
if colsA != rowsB:
print('Number of A columns must equal number of B rows.')
sys.exit()
C = zeros_matrix(rowsA, colsB)
for i in range(rowsA):
for j in range(colsB):
total = 0
for ii in range(colsA):
total += A[i][ii] * B[ii][j]
C[i][j] = total
return C
# In[27]:
A = [[5.,3.,1.],[3.,9.,4.],[1.,3.,5.]]
X = [[1.],[1.],[1.]]
B = matrix_multiply(A,X)
print_matrix('B Matrix', B)
# In[28]:
A = [[5.,3.,1.],[3.,9.,4.],[1.,3.,5.]]
B = [[9.],[16.],[9.]]
print('A and I are our starting matrices.')
Action = ''
print_matrices(Action, 'A Matrix', A, 'B Matrix', B)
# In[29]:
AM = copy_matrix(A)
BM = copy_matrix(B)
n = len(AM)
exString = """
Since the matrices won't be the original A and I as we start row operations,
the matrices will be called: AM for "A Morphing", and IM for "I Morphing"
"""
print_matrices(exString, 'AM Matrix', AM, 'BM Matrix', BM)
print()
# In[34]:
# Run this cell then the next for fd = 0, 1, and 2 for a 3x3 matrix.
# Then check for identity matrix in last cell.
fd = 2 # fd stands for focus diagonal OR the current diagonal
fdScaler = 1. / AM[fd][fd]
for j in range(n): # using j to indicate cycling thru columns
AM[fd][j] = fdScaler * AM[fd][j]
BM[fd][0] = fdScaler * BM[fd][0]
print()
print_matrices('', 'AM Matrix', AM, 'BM Matrix', BM)
# In[35]:
n = len(A)
indices = list(range(n))
for i in indices[0:fd] + indices[fd+1:]: # *** skip row with fd in it.
crScaler = AM[i][fd] # cr stands for "current row".
for j in range(n): # cr - crScaler * fdRow, but one element at a time.
AM[i][j] = AM[i][j] - crScaler * AM[fd][j]
BM[i][0] = BM[i][0] - crScaler * BM[fd][0]
print_matrices('', 'AM Matrix', AM, 'BM Matrix', BM)
print()
# In[36]:
print_matrix('Solution for X', matrix_multiply(A,BM))
# In[ ]: