In [1]:
import sys
import re
import numpy as np
#import parser as ps
In [2]:
%ls -l
print()
%ls -l input/
In [3]:
# if len(sys.argv) > 1:
# #print sys.argv[1]
# f = open('input/'+sys.argv[1], 'r')
# else:
# f = open('input.txt', 'r')
f = open('input.txt', 'r')
In [4]:
# pattern = re.compile(r'(?P<a>(-?\d+/))x(?P<b>([+-]\d+/))y(?P<c>([+-]\d+/))(\=)(?P<r>[0-9]+)')
pattern = re.compile(r'(?P<a>(-?\d+))x(?P<b>([+-]\d+))y(?P<c>([+-]\d+))(\=)(?P<r>[0-9]+)')
In [5]:
_ = """
matrixA = np.array([])
matrixB = np.array([])
for line in f:
#line = f.readline()
#newLine = ps.expr(line).compile()
#match = pattern.match(line) #or findall
match = re.search(pattern, line)#.groups()
print match, type(match)
print pattern, type(pattern)
if match:
a = int(match.group('a'))
b = int(match.group('b'))
c = int(match.group('c'))
newRow = [a, b, c]
matrixA = np.concatenate(matrixA, newRow)
r = int(match.group('r'))
newRow = [r]
matrixB = np.concatenate(matrixB, newRow)
else:
raise Exception('Pattern not matched')
"""
In [6]:
matrixA = np.array([[2, 3, 6], [0, 4, 2], [1, 3, 5]])
matrixB = np.array([[2], [3], [1]])
print('matrixA:\n %s\n' % matrixA)
print('matrixB:\n %s' % matrixB)
In [7]:
%%time
detA = np.linalg.det(matrixA)
if detA == 0:
print('Determinant is 0. Program will stop')
quit()
matrixAinv = np.linalg.inv(matrixA)
In [8]:
print('---', 'Inverse matrix is: ')
print(matrixAinv)
checkInv = np.allclose(np.dot(matrixA, matrixAinv), np.eye(3))
print("\nIs matrixAinv ok ?", checkInv)
#x = np.linalg.solve(a, b, c)
#Check that the solution is correct:
#np.allclose(np.dot(a, x), b)