From 669c861ca297b0da385dd6069492a92d0ee4209a Mon Sep 17 00:00:00 2001 From: nrubin29 Date: Sun, 10 Dec 2017 16:35:06 -0500 Subject: [PATCH] Fixed an issue caused by cleaning up the Value superclass. Fixed an issue with the det function not working for 1x1 matrices. --- common.py | 2 ++ vartypes.py | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/common.py b/common.py index cd77460..09b39fb 100644 --- a/common.py +++ b/common.py @@ -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 diff --git a/vartypes.py b/vartypes.py index 87baebc..dd8ad7a 100644 --- a/vartypes.py +++ b/vartypes.py @@ -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): @@ -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 = []