Skip to content

Commit

Permalink
Fixed issue with small matrices being parsed incorrectly.
Browse files Browse the repository at this point in the history
Fixed issue with doing operations on raw matrices.
  • Loading branch information
nrubin29 committed Nov 6, 2017
1 parent 9e8226c commit 7c743b7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
3 changes: 1 addition & 2 deletions ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def _fixed(self, node):
del node.matched[i]

# This flattens rules with a single matched rule.
# TODO: Flattening messes up smaller matrices ([1], [1,2], [1|2], etc.).
if len(node.matched) is 1 and isinstance(node.matched[0], RuleMatch):
if len(node.matched) is 1 and isinstance(node.matched[0], RuleMatch) and node.name not in ('mbd', 'mrw'): # The last condition fixes small matrices like [1], [1,2], and [1|2].
return self._fixed(node.matched[0])

# This makes left-associative operations left-associative.
Expand Down
1 change: 1 addition & 0 deletions calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def evaluate(self, eqtn: str, verbose=True) -> Value:
return res

elif isinstance(res, dict):
print(ast)
self.vrs.update(res)

def _tokenize(self, eqtn: str) -> List[Token]:
Expand Down
27 changes: 18 additions & 9 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,19 @@ def _str(self, node, depth=0) -> str:

class ImmutableIndexedDict:
def __init__(self, data):
self._keys = tuple(item[0] for item in data)
self._key_indices = {key: self._keys.index(key) for key in self._keys} # Caching indices cuts down on runtime.
self._len = len(self._keys)
self._data = dict(data)
self._keys = tuple(item[0].lstrip('^') for item in data)
self._data = {key.lstrip('^'): values for key, values in data}

# Caching indices cuts down on runtime.
idx = 0
self._key_indices = {}

for key in tuple(item[0] for item in data):
if not key.startswith('^'):
self._key_indices[key] = idx
idx += 1

self._len = len(self._key_indices)

def __getitem__(self, key):
return self._data[key]
Expand All @@ -86,18 +95,18 @@ def key_at(self, i):


rules_map = ImmutableIndexedDict((
('asn', ('IDT EQL mat',)),
('mat', ('LBR mbd RBR',)),
('mbd', ('mrw PPE mbd', 'mrw')),
('mrw', ('add CMA mrw', 'add')),
('asn', ('IDT EQL add',)),
('add', ('mul ADD add',)),
('mui', ('pow mul',)),
('mul', ('pow MUL mul',)),
('pow', ('opr POW pow',)),
('opr', ('OPR LPA mat RPA',)),
('opr', ('OPR LPA add RPA',)),
('neg', ('ADD num', 'ADD opr')),
('var', ('IDT',)),
('num', ('NUM', 'LPA add RPA')),
('mat', ('LBR mbd RBR',)),
('^mbd', ('mrw PPE mbd', 'mrw')),
('^mrw', ('add CMA mrw', 'add')),
))


Expand Down
2 changes: 1 addition & 1 deletion vartypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def __str__(self):
def mul(self, other):
if isinstance(other, NumberValue):
# Number * Matrix
return MatrixValue([[cell * other.value for cell in row] for row in other.value])
return MatrixValue([[cell * other.value for cell in row] for row in self.value])

elif isinstance(other, MatrixValue):
# Matrix * Matrix
Expand Down

0 comments on commit 7c743b7

Please sign in to comment.