-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInverseMethod.py
More file actions
196 lines (150 loc) · 5.04 KB
/
InverseMethod.py
File metadata and controls
196 lines (150 loc) · 5.04 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import numpy
import os
import random
import sys
#from sklearn.preprocessing import normalize
# method to randomly generate matrices
def generateMatrix(nr_of_unknowns):
m = numpy.eye(nr_of_unknowns)
for i in range(nr_of_unknowns):
if random.randint(0, 1) == 0: # for diagonal elements
m[i, i] = -1
else:
m[i, i] = 1
for j in range(i + 1, nr_of_unknowns):
m[i, j] = random.randint(-3, 3)
for j in range(nr_of_unknowns - 1, -1, -1):
for i in range(j + i, nr_of_unknowns):
if random.randint(0, 1) == 0:
m[i] = fillValues(m, i, j, random.randint(-3, 1))
else:
m[i] = fillValues(m, i, j, random.randint(1, 4))
swapValues(m, 0, random.randint(1, nr_of_unknowns - 1))
n = numpy.zeros((nr_of_unknowns, 1)) # creating B matrix
for i in range(nr_of_unknowns):
n[i] = random.randint(-5, 5)
return m.astype(int),n.astype(int)
# method to fill values
def fillValues(m, i, j, value):
return m[i] + value * m[j]
# method to randomly swap values
def swapValues(m, i, j):
m[i] = m[i] + m[j]
m[j] = m[i] - m[j]
m[i] = m[i] - m[j]
#find x with inverse matrix method
def inverseMatrixMethod(m,n):
m_inverse = numpy.linalg.inv(m) # Compute the (multiplicative) inverse of a matrix
x = numpy.dot(m_inverse, n)
return x.astype(int)
def normalize_rows(x):
return x / numpy.linalg.norm(x, ord=2, axis=1, keepdims=True)
def createSLE(nr_of_unknowns):
if os.path.exists("GeneratedSleMatrix.tex"):
os.remove("GeneratedSleMatrix.tex")
infile = open("GeneratedSleMatrix.tex", "a")
infile.write('\documentclass{article}\n')
infile.write('\\begin{document}\n')
infile.write('\\begin{enumerate}\n')
infile.write('\item \n')
a, b = generateMatrix(nr_of_unknowns)
a_norm = normalize_rows(a)
b_norm = normalize_rows(b)
x = inverseMatrixMethod(a,b)
eigval, eigvec = numpy.linalg.eig(a)
rank = numpy.linalg.matrix_rank(a)
cond_number = numpy.linalg.cond(a)
trace = numpy.trace(a)
inv_a = numpy.linalg.inv(a)
det_a = numpy.linalg.det(a)
print ("\nUnimodular matrix A:")
print (a)
print ("\nMatrix B:")
print (b)
print ("\nSolution matrix X:")
print (x)
print ("\nvalues:")
print (rank)
print(cond_number)
print(trace)
print(inv_a)
print(det_a)
infile.write('$\\begin{array}{')
for i in range(nr_of_unknowns + 1):
infile.write('r@{\ }c@{\ }')
infile.write('}\n')
print("\nThe system of linear equations is: ")
for i in range(nr_of_unknowns):
text = ""
for j in range(nr_of_unknowns):
if a[i, j] < 0:
if j == 0:
token = '-'
else:
token = ' -& '
if a[i, j] == -1:
k = ""
else:
k = str(abs(a[i, j]))
l = k + 'x_{' + str(j + 1) + '}&'
elif a[i, j] == 0:
if j == 0:
token = ''
else:
token = '&'
l = '&'
else:
if j == 0:
token = ''
else:
token = ' +& '
if a[i, j] == 1:
k = ""
else:
k = str(abs(a[i, j]))
l = k + 'x_{' + str(j + 1) + '}&'
text = text + token + l
if j + 1 == nr_of_unknowns:
text = text + '=&' + str(b[i, 0]) + ' \\\\\n '
#print (text)
infile.write(text)
infile.write('\\end{array}$\n')
infile.write('\end{enumerate}\n')
infile.write('\end{document}\n')
infile.close()
os.system("pdflatex GeneratedSleMatrix.tex")
outfile = open("Solution.txt", "w")
outfile.write("Solution matrix X:\n")
outfile.write(str(x))
outfile.close()
outfile = open("Matrices.txt", "w")
outfile.write("Generated matrices:\n")
outfile.write("\nMatrix A\n" + str(a) + "\n" + "\nMatrix B\n" + str(b))
outfile.close()
outfile = open("Normalized.txt", "w")
outfile.write("Normalized Matrix A:\n")
outfile.write(str(a_norm))
outfile.write("\n\nNormalized Matrix B:\n")
outfile.write(str(b_norm))
outfile.close()
outfile = open("Eigen.txt", "w")
outfile.write("Eigenvalues for A:\n")
outfile.write(str(eigval))
outfile.write("\n\nEigenvectors for A:\n")
outfile.write(str(eigvec))
outfile.close()
outfile = open("Info.txt", "w")
outfile.write("Rank of A: ")
outfile.write(str(rank))
outfile.write("\n\nDeterminant of A: ")
outfile.write(str(det_a))
outfile.write("\n\nTrace of A: ")
outfile.write(str(trace))
outfile.write("\n\nCondition number of A: ")
outfile.write(str(cond_number))
outfile.write("\n\nInverse of A:\n")
outfile.write(str(inv_a))
outfile.close()
#os.system("GeneratedSleMatrix.pdf")
numberOfUnknowns = int(sys.argv[1])
createSLE(numberOfUnknowns)