Skip to content

Commit

Permalink
Fixed an issue caused by cleaning up the Value superclass.
Browse files Browse the repository at this point in the history
Fixed an issue with the det function not working for 1x1 matrices.
  • Loading branch information
nrubin29 committed Dec 10, 2017
1 parent 1d5a725 commit 669c861
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ def key_at(self, i):
'MUL': 'mul',
}

operations = ('pos', 'neg', 'add', 'sub', 'mul', 'div', 'mod', 'pow', 'sqrt', 'exp', 'identity', 'det', 'trans', 'cof', 'adj', 'inv', 'rref', 'trnsform', 'solve', 'ls', 'eval')


class EvaluationException(Exception):
pass
18 changes: 12 additions & 6 deletions vartypes.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import copy
import functools
import math
from abc import ABCMeta
from typing import List

from common import EvaluationException
from common import EvaluationException, operations
from matrix import MatrixTransformer, DynamicVector


def raise_exception(tpe, op):
raise EvaluationException('{} does not have operation {}'.format(tpe, op))


class Value(metaclass=ABCMeta):
__slots__ = ('type', 'value')

def __init__(self):
self.type = self.__class__.__name__

for op in operations:
if not hasattr(self, op):
setattr(self, op, functools.partial(raise_exception, tpe=self.type, op=op))

def __str__(self):
return str(self.value)

def __getattr__(self, item):
raise EvaluationException('{} does not have operation {}'.format(self.type, item))


class VariableValue(Value):
def __init__(self, data):
Expand Down Expand Up @@ -141,8 +147,8 @@ def det(self):

@staticmethod
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])
if len(matrix) is 1:
return matrix[0][0]

cofactors = []

Expand Down

0 comments on commit 669c861

Please sign in to comment.