Skip to content

Commit

Permalink
Fixed a rule pertaining to implicit multiplication and negation.
Browse files Browse the repository at this point in the history
  • Loading branch information
nrubin29 committed Nov 27, 2017
1 parent ea92d64 commit 7c2a8c9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
5 changes: 3 additions & 2 deletions calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def _match(self, tokens: List[Token], target_rule: str):
# Success!
return RuleMatch(target_rule, matched), remaining_tokens

if rules_map.index(target_rule) + 1 < len(rules_map):
return self._match(tokens, rules_map.key_at(rules_map.index(target_rule) + 1))
idx = rules_map.index(target_rule)
if idx + 1 < len(rules_map):
return self._match(tokens, rules_map.key_at(idx + 1))

return None, None
2 changes: 1 addition & 1 deletion common.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def key_at(self, i):

rules_map = ImmutableIndexedDict((
('asn', ('IDT EQL add',)),
('add', ('mul ADD add',)),
('add', ('mul ADD add', 'mui ADD add',)),
('mui', ('pow mul',)),
('mul', ('pow MUL mul',)),
('pow', ('opr POW pow',)),
Expand Down
6 changes: 4 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MultiplicationTests(unittest.TestCase):
def runTest(self):
self.assertEqual(evaluate('1 * 1'), 1.0)
self.assertEqual(evaluate('2 * 3'), 6.0)
# self.assertEqual(evaluate('-2 * -2'), 4.0)
self.assertEqual(evaluate('-2 * -2'), 4.0)
self.assertEqual(evaluate('(2 * 3) (2 * 3)'), 36.0)


Expand Down Expand Up @@ -116,7 +116,9 @@ def runTest(self):

self.assertTrue(sympy.Matrix(evaluate('identity({})'.format(r_dim), False)).equals(sympy.Identity(r_dim)))

for _ in range(5):
for _ in range(1):
print(_)

mat = [[more_zeroes(random.randint(0, 100)) for _ in range(r_dim)] for _ in range(r_dim)]
mat_str = '[' + '|'.join([','.join(map(str, line)) for line in mat]) + ']'

Expand Down

0 comments on commit 7c2a8c9

Please sign in to comment.