From c5401e51286b17ef136451337eb29f4a205d8318 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Mon, 4 Jun 2018 22:17:09 -0700 Subject: [PATCH] misc/numworks: add echelon script for numworks. --- misc/numworks/README.md | 29 +++++++++++++ misc/numworks/echelon.py | 89 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 misc/numworks/README.md create mode 100644 misc/numworks/echelon.py diff --git a/misc/numworks/README.md b/misc/numworks/README.md new file mode 100644 index 0000000..3991b03 --- /dev/null +++ b/misc/numworks/README.md @@ -0,0 +1,29 @@ +numworks +======== + +Various programs for my [numworks](https://numworks.com/) calculator. + + +Echelon +------- + +This is a tool to help explore transformations to convert a matrix to +row echelon form. + +```python +>>> from echelon import * +>>> m = EMatrix([[1, 1, 1], [3, 2, 1], [2, 1, 2]], [15, 28, 23]) +>>> print(m) + 1 1 1 -> 15 + 3 2 1 -> 28 + 2 1 2 -> 23 +>>> m.add(-3, 0, 1) +>>> m.add(-2, 0, 2) +>>> m.scale(-1, 1) +>>> m.add(1, 1, 2) +>>> m.scale(1/2, 2) +>>> print(m) + 1 1 1 -> 15 + 0 1 2 -> 17 + 0 0 1 -> 5 +``` diff --git a/misc/numworks/echelon.py b/misc/numworks/echelon.py new file mode 100644 index 0000000..ae4934e --- /dev/null +++ b/misc/numworks/echelon.py @@ -0,0 +1,89 @@ +class EMatrix: + + def __init__(self, mat, sol): + self.mat = [] + l = -1 + for i in range(len(mat)): + tmp = [] + if l == -1: + l = len(mat[i]) + else: + assert(len(mat[i]) == l) + for j in range(len(mat[i])): + tmp.append(mat[i][j]) + self.mat.append(tmp) + + assert(len(sol) == l) + self.sol = [] + for i in range(len(sol)): + self.sol.append(sol[i]) + + def copy(self): + mat = [] + sol = [] + for i in range(len(self.mat)): + tmp = [] + for j in range(len(self.mat[i])): + tmp.append(self.mat[i][j]) + mat.append(tmp) + + for i in range(len(self.sol)): + sol.append(self.sol[i]) + + return EMatrix(mat, sol) + + def add(self, k, r1, r2): + row1 = [v for v in self.mat[r1]] + row2 = self.mat[r2] + + for i in range(len(row1)): + row1[i] = k * row1[i] + row2[i] += row1[i] + + sol = self.sol[r1] * k + + self.mat[r2] = row2 + self.sol[r2] += sol + + def sub(self, k, r1, r2): + row1 = [v for v in self.mat[r1]] + row2 = self.mat[r2] + + for i in range(len(row1)): + row1[i] = k * row1[i] + row2[i] -= row1[i] + + sol = self.sol[r1] * k + + self.mat[r2] = row2 + self.sol[r2] -= sol + + def scale(self, k, r): + for i in range(len(self.mat[r])): + self.mat[r][i] *= k + + self.sol[r] *= k + + def __repr__(self): + return "{}x{} EMatrix".format(len(self.sol), len(self.mat)) + + def __str__(self): + m = 0 + s = '' + for i in range(len(self.mat)): + for j in range(len(self.mat[i])): + m = max(m, len('{:g}'.format(self.mat[i][j]))) + + for i in range(len(self.sol)): + m = max(m, len('{:g}'.format(self.sol[i]))) + + fmt = '{' + ':>{}g'.format(m) + '}' + + for i in range(len(self.mat)): + for j in range(len(self.mat[i])): + s += fmt.format(self.mat[i][j]) + s += ' ' + + s += ' -> ' + fmt.format(self.sol[i]) + '\n' + + return s