Skip to content

Commit

Permalink
Added least squares.
Browse files Browse the repository at this point in the history
Did a bit of cleanup.
  • Loading branch information
nrubin29 committed Dec 4, 2017
1 parent 7c2a8c9 commit a211b94
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def _str(self, node, depth=0) -> str:
(r'rref', 'OPR'),
(r'solve', 'OPR'),
(r'eval', 'OPR'),
(r'ls', 'OPR'),
(r'[a-zA-Z_]+', 'IDT'),
(r'=', 'EQL'),
(r'\+', 'ADD'),
Expand Down
2 changes: 1 addition & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def runTest(self):

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

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

mat = [[more_zeroes(random.randint(0, 100)) for _ in range(r_dim)] for _ in range(r_dim)]
Expand Down
18 changes: 12 additions & 6 deletions vartypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def identity(self):
def solve(self, other):
raise EvaluationException('Operation not defined for ' + self.type)

def eval(self, other):
raise EvaluationException('Operation not defined for ' + self.type)

def ls(self, other):
raise EvaluationException('Operation not defined for ' + self.type)


class VariableValue(Value):
def __init__(self, data):
Expand Down Expand Up @@ -188,16 +194,13 @@ def trans(self):
return MatrixValue(list(map(list, zip(*self.value))))

def cof(self):
# TODO: self code is pretty ugly.

cofactor_matrix = []
mat = self.value

for row in range(len(mat)):
for row in range(len(self.value)):
cofactor_matrix.append([])

for col in range(len(mat[row])):
minor = copy.deepcopy(mat)
for col in range(len(self.value[row])):
minor = copy.deepcopy(self.value)
del minor[row]

for r in minor:
Expand Down Expand Up @@ -234,6 +237,9 @@ def trnsform(self):
def solve(self, other):
return DynamicVectorValue(MatrixTransformer(copy.deepcopy(self.value)).rref(other.value[0])[2])

def ls(self, other):
return (self.trans().mul(self)).inv().mul(self.trans()).mul(other)


class MatrixRowValue(Value):
def __init__(self, data):
Expand Down

0 comments on commit a211b94

Please sign in to comment.