-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseminar7.py
More file actions
52 lines (42 loc) · 1.17 KB
/
Copy pathseminar7.py
File metadata and controls
52 lines (42 loc) · 1.17 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
import numpy
import re
from collections import Counter
data = numpy.loadtxt('matrix.txt')
print("matrix: \n" + str(data))
print("determinant: " + str(numpy.linalg.det(data)))
print("inverse matrix: \n" + str(numpy.linalg.inv(data)))
data = open('equation.txt', 'r')
re_equation = re.compile(r'(.*)[ ]*=[ ]*(.*)')
b = []
vars_values = []
equations = []
for line in data:
equations.append(line)
equation = re_equation.match(line)
left = equation.group(1).split()
right = int(equation.group(2))
b.append(right)
vars_ = Counter()
number = 1
sign = 1
for word in left:
if word.isnumeric():
number = int(word)
elif word == '-':
sign = -1
elif word == '+':
sign = 1
else:
vars_[word] = sign*number
number = 1
vars_values.append(list(vars_.values()))
vars_names = list(vars_.keys())
a = numpy.array(vars_values)
res = numpy.linalg.solve(a,b)
print("\nequations: ")
for i in range(0, len(res)):
print(equations[i])
print("\nsolution: ")
for i in range(0, len(res)):
print("{}={:.2}\n".format(vars_names[i], res[i]))
data.close()