Skip to content

Commit

Permalink
Added calculation of determinant.
Browse files Browse the repository at this point in the history
  • Loading branch information
nrubin29 committed Sep 13, 2017
1 parent 100cecb commit 0260027
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
(r'\d+(?:\.\d+)?', 'NUM'),
(r'sqrt', 'OPR'),
(r'exp', 'OPR'),
(r'det', 'OPR'),
(r'[a-zA-Z_]+', 'IDT'),
(r'=', 'EQL'),
(r'\+', 'ADD'),
Expand Down Expand Up @@ -40,7 +41,7 @@
('mui', ('pow mul',)),
('mul', ('pow MUL mul', 'pow')),
('pow', ('opr POW pow', 'opr')),
('opr', ('OPR LPA add RPA', 'neg')),
('opr', ('OPR LPA mat RPA', 'neg')),
('neg', ('ADD num', 'ADD opr', 'num')),
('num', ('NUM', 'IDT', 'LPA add RPA')),
))
Expand Down
14 changes: 13 additions & 1 deletion rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def pow(tokens: List[Token]) -> Token:


def opr(tokens: List[Token]) -> Token:
return Token('NUM', {'sqrt': math.sqrt, 'exp': math.exp}[tokens[0].value](tokens[1].value))
return Token('NUM', {'sqrt': math.sqrt, 'exp': math.exp, 'det': det}[tokens[0].value](tokens[1].value))


def neg(tokens: List[Token]) -> Token:
Expand All @@ -43,6 +43,18 @@ def mat(tokens: List[Token]) -> Token:
return tokens[0]


def det(matrix: List[List[float]]) -> float:
if len(matrix) is 2:
return (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0])

cofactors = []

for col in range(len(matrix)):
cofactors.append(det([matrix[row][0:col] + matrix[row][col + 1:] for row in range(1, len(matrix))]) * matrix[0][col] * (1 if col % 2 is 0 else -1))

return sum(cofactors)


calc_map = {
'add': add,
'mul': mul,
Expand Down
17 changes: 8 additions & 9 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,18 @@ def runTest(self):
class MatrixTests(unittest.TestCase):
def runTest(self):
self.assertEqual(evaluate('[1,2]'), [[1.0, 2.0]])
self.assertEqual(evaluate('det([1,2,3|4,5,6|7,8,8])'), 3.0)

self.assertEqual(evaluate('[1,2|4,5]'), [[1.0, 2.0], [4.0, 5.0]])
self.assertEqual(evaluate('[1,0,0|0,1,0|0,0,1]'), [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])
self.assertEqual(evaluate('[1,0,0,0|0,1,0,0|0,0,1,0|0,0,0,1]'), [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]])

for _ in range(10):
mat = []

for i in range(10):
mat.append([random.randint(1, 100) for _ in range(10)])

mat_str = '[' + '|'.join([','.join(map(str, line)) for line in mat]) + ']'
self.assertEqual(evaluate('[1,0,0,0|0,1,0,0|0,0,1,0|0,0,0,1]'), [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]])
self.assertEqual(evaluate('det([1,3,5,7|2,4,6,8|9,7,5,4|8,6,5,9])'), 2.0)

self.assertEqual(evaluate(mat_str), mat)
# for _ in range(10):
# mat = [[random.randint(1, 100) for _ in range(10)] for _ in range(10)]
# mat_str = '[' + '|'.join([','.join(map(str, line)) for line in mat]) + ']'
# self.assertEqual(evaluate(mat_str), mat)


class RandomTests(unittest.TestCase):
Expand Down

0 comments on commit 0260027

Please sign in to comment.