Skip to content

Commit

Permalink
matrix integrated with CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
notimeforcaution committed Aug 3, 2019
1 parent 7fd3541 commit d1d2471
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 39 deletions.
136 changes: 107 additions & 29 deletions visma/gui/cli.py
@@ -1,9 +1,10 @@
import copy
from visma.calculus.differentiation import differentiate
from visma.calculus.integration import integrate
from visma.discreteMaths.combinatorics import factorial, combination, permutation
from visma.io.checks import checkTypes
from visma.io.tokenize import tokenizer, getLHSandRHS
from visma.io.parser import resultStringCLI
from visma.io.parser import resultStringCLI, resultMatrix_CLI
from visma.simplify.simplify import simplify, simplifyEquation
from visma.simplify.addsub import addition, additionEquation, subtraction, subtractionEquation
from visma.simplify.muldiv import multiplication, multiplicationEquation, division, divisionEquation
Expand All @@ -12,6 +13,7 @@
from visma.solvers.simulEqn import simulSolver
from visma.transform.factorization import factorize
from visma.matrix.structure import Matrix, SquareMat
from visma.matrix.operations import simplifyMatrix, addMatrix, subMatrix, multiplyMatrix


def commandExec(command):
Expand Down Expand Up @@ -65,32 +67,24 @@ def commandExec(command):
lTokens, rTokens, _, _, equationTokens, comments = simplifyEquation(lTokens, rTokens)
elif operation == 'addition':
if solutionType == 'expression':
tokens, _, _, equationTokens, comments = addition(
tokens, True)
tokens, _, _, equationTokens, comments = addition(tokens, True)
else:
lTokens, rTokens, _, _, equationTokens, comments = additionEquation(
lTokens, rTokens, True)
lTokens, rTokens, _, _, equationTokens, comments = additionEquation(lTokens, rTokens, True)
elif operation == 'subtraction':
if solutionType == 'expression':
tokens, _, _, equationTokens, comments = subtraction(
tokens, True)
tokens, _, _, equationTokens, comments = subtraction(tokens, True)
else:
lTokens, rTokens, _, _, equationTokens, comments = subtractionEquation(
lTokens, rTokens, True)
lTokens, rTokens, _, _, equationTokens, comments = subtractionEquation(lTokens, rTokens, True)
elif operation == 'multiplication':
if solutionType == 'expression':
tokens, _, _, equationTokens, comments = multiplication(
tokens, True)
tokens, _, _, equationTokens, comments = multiplication(tokens, True)
else:
lTokens, rTokens, _, _, equationTokens, comments = multiplicationEquation(
lTokens, rTokens, True)
lTokens, rTokens, _, _, equationTokens, comments = multiplicationEquation(lTokens, rTokens, True)
elif operation == 'division':
if solutionType == 'expression':
tokens, _, _, equationTokens, comments = division(
tokens, True)
tokens, _, _, equationTokens, comments = division(tokens, True)
else:
lTokens, rTokens, _, _, equationTokens, comments = divisionEquation(
lTokens, rTokens, True)
lTokens, rTokens, _, _, equationTokens, comments = divisionEquation(lTokens, rTokens, True)
elif operation == 'factorize':
tokens, _, _, equationTokens, comments = factorize(tokens)
elif operation == 'find-roots':
Expand Down Expand Up @@ -125,16 +119,100 @@ def commandExec(command):
print(final_string)
else:
operation = operation[4:]
inputEquation = "[1 2 3; 12 12 33; 12 311 11]"
inputEquation = inputEquation[1:][:-1]
inputEquation = inputEquation.split('; ')
matrixOperand = []
for row in inputEquation:
row1 = row.split(' ')
for i, _ in enumerate(row1):
row1[i] = tokenizer(row1[i])
matrixOperand.append(row1)
dualOperand = False
nonMatrixResult = False
scalarOperations = False
if ', ' in inputEquation:
dualOperand = True
[inputEquation1, inputEquation2] = inputEquation.split(', ')
if '[' in inputEquation1:
inputEquation1 = inputEquation1[1:][:-1]
inputEquation1 = inputEquation1.split('; ')
matrixOperand1 = []
for row in inputEquation1:
row1 = row.split(' ')
for i, _ in enumerate(row1):
row1[i] = tokenizer(row1[i])
matrixOperand1.append(row1)
Matrix1 = Matrix()
Matrix1.value = matrixOperand1
inputEquation2 = inputEquation2[1:][:-1]
inputEquation2 = inputEquation2.split('; ')
matrixOperand2 = []
for row in inputEquation2:
row1 = row.split(' ')
for i, _ in enumerate(row1):
row1[i] = tokenizer(row1[i])
matrixOperand2.append(row1)
Matrix2 = Matrix()
Matrix2.value = matrixOperand2
Matrix1_copy = copy.deepcopy(Matrix1)
Matrix2_copy = copy.deepcopy(Matrix2)
else:
scalarOperations = True
scalar = inputEquation1
scalarTokens = scalar
# scalarTokens = tokenizer(scalar)
inputEquation2 = inputEquation2[1:][:-1]
inputEquation2 = inputEquation2.split('; ')
matrixOperand2 = []
for row in inputEquation2:
row1 = row.split(' ')
for i, _ in enumerate(row1):
row1[i] = tokenizer(row1[i])
matrixOperand2.append(row1)
Matrix2 = Matrix()
Matrix2.value = matrixOperand2
scalarTokens_copy = copy.deepcopy(scalarTokens)
Matrix2_copy = copy.deepcopy(Matrix2)

else:
inputEquation = inputEquation[1:][:-1]
inputEquation = inputEquation.split('; ')

matrixOperand = []
for row in inputEquation:
row1 = row.split(' ')
for i, _ in enumerate(row1):
row1[i] = tokenizer(row1[i])
matrixOperand.append(row1)

Matrix0 = Matrix()
Matrix0.value = matrixOperand
Matrix0_copy = copy.deepcopy(Matrix0)
if operation == 'simplify':
operandMatrix = Matrix(value=matrixOperand)
operandMatrix = SquareMat(value=matrixOperand)
print(operandMatrix.determinant)
MatrixResult = simplifyMatrix(Matrix0)
elif operation == 'add':
MatrixResult = addMatrix(Matrix1, Matrix2)
elif operation == 'sub':
MatrixResult = subMatrix(Matrix1, Matrix2)
elif operation == 'mult':
MatrixResult = multiplyMatrix(Matrix1, Matrix2)
elif operation == 'determinant':
nonMatrixResult = True
sqMatrix = SquareMat()
sqMatrix.value = Matrix0.value
result = sqMatrix.determinant()
elif operation == 'trace':
nonMatrixResult = True
sqMatrix = SquareMat()
sqMatrix.value = Matrix0.value
result = sqMatrix.traceMat()
elif operation == 'inverse':
sqMatrix = SquareMat()
sqMatrix.value = Matrix0.value
MatrixResult = SquareMat()
MatrixResult = sqMatrix.inverse()

finalCLIstring = ''
if dualOperand:
if not scalarOperations:
finalCLIstring = resultMatrix_CLI(operation=operation, operand1=Matrix1_copy, operand2=Matrix2_copy, result=MatrixResult)
else:
finalCLIstring = resultMatrix_CLI(operation=operation, operand1=scalarTokens_copy, operand2=Matrix2_copy, result=MatrixResult)
else:
if nonMatrixResult:
finalCLIstring = resultMatrix_CLI(operation=operation, operand1=Matrix0_copy, nonMatrixResult=True, result=result)
else:
finalCLIstring = resultMatrix_CLI(operation=operation, operand1=Matrix0_copy, result=MatrixResult)
print(finalCLIstring)
36 changes: 35 additions & 1 deletion visma/io/parser.py
Expand Up @@ -53,7 +53,7 @@ def resultLatex(equationTokens, operation, comments, solutionType, simul=False,
return finalSteps


def resultStringCLI(equationTokens, operation, comments, solutionType, simul=False):
def resultStringCLI(equationTokens, operation, comments, solutionType, simul=False, mat=False):
"""Converts tokens to final string format for displaying in terminal in CLI
Arguments:
Expand Down Expand Up @@ -100,6 +100,40 @@ def resultStringCLI(equationTokens, operation, comments, solutionType, simul=Fal
return finalSteps


def resultMatrix_CLI(operation=None, operand1=None, operand2=None, nonMatrixResult=False, result=None):
if operation == 'sub':
operation = 'Subtraction'
elif operation == 'add':
operation = 'Addition'
elif operation == 'mult':
operation = 'Multiplication'
elif operation == 'determinant':
operation = 'Determinant'
elif operation == 'trace':
operation = 'Trace: sum of diagonal elements'
elif operation == 'simplify':
operation = 'Simplification'
finalSteps = ''
if operand2 is not None:
finalSteps += 'INPUT: ' + 'Two matrices provided as follows:' + 2*'\n'
finalSteps += '1st Matrix Provided: \n'
finalSteps += operand1.convertInCLIString() + '\n'
finalSteps += '2nd Matrix Provided: \n'
finalSteps += operand2.convertInCLIString() + 2*'\n'
else:
finalSteps += 'INPUT: ' + 'Single matrix provided as follows:' + '\n'
finalSteps += '1st Matrix Provided: \n'
finalSteps += operand1.convertInCLIString() + '\n'
finalSteps += 'OPERATION: ' + operation + 2*'\n'
if not nonMatrixResult:
finalSteps += 'RESULT: Result Matrix calculated as: \n'
finalSteps += result.convertInCLIString() + '\n'
else:
finalSteps += 'RESULT: Result calculated as: \n'
finalSteps += tokensToString(result) + '\n'
return finalSteps


def tokensToLatex(eqTokens):
"""Converts tokens to LaTeX string
Expand Down
12 changes: 12 additions & 0 deletions visma/matrix/operations.py
Expand Up @@ -15,6 +15,8 @@ def simplifyMatrix(mat):
Returns:
mat {visma.matrix.structure.Matrix} -- simplified matrix token
"""
mat.dim[0] = len(mat.value)
mat.dim[1] = len(mat.value[0])
for i in range(mat.dim[0]):
for j in range(mat.dim[1]):
mat.value[i][j], _, _, _, _ = simplify(mat.value[i][j])
Expand All @@ -35,6 +37,8 @@ def addMatrix(matA, matB):
Make dimCheck before calling addMatrix
"""
matSum = Matrix()
matA.dim[0] = len(matA.value)
matA.dim[1] = len(matA.value[0])
matSum.empty(matA.dim)
for i in range(matA.dim[0]):
for j in range(matA.dim[1]):
Expand All @@ -59,6 +63,8 @@ def subMatrix(matA, matB):
Make dimCheck before calling subMatrix
"""
matSub = Matrix()
matA.dim[0] = len(matA.value)
matA.dim[1] = len(matA.value[0])
matSub.empty(matA.dim)
for i in range(matA.dim[0]):
for j in range(matA.dim[1]):
Expand All @@ -84,6 +90,10 @@ def multiplyMatrix(matA, matB):
Not commutative
"""
matPro = Matrix()
matA.dim[0] = len(matA.value)
matA.dim[1] = len(matA.value[0])
matB.dim[0] = len(matB.value)
matB.dim[1] = len(matB.value[0])
matPro.empty([matA.dim[0], matB.dim[1]])
for i in range(matA.dim[0]):
for j in range(matB.dim[1]):
Expand Down Expand Up @@ -120,6 +130,8 @@ def scalarAdd(const, mat):
"""
matRes = Matrix()
mat.dim[0] = len(mat.value)
mat.dim[1] = len(mat.value[0])
matRes.empty(mat.dim)
for i in range(mat.dim[0]):
for j in range(mat.dim[1]):
Expand Down
42 changes: 33 additions & 9 deletions visma/matrix/structure.py
Expand Up @@ -22,12 +22,35 @@ class Matrix(object):
and stored in matrix.value.
"""

def __init__(self):
def __init__(self, value=None, coefficient=None, power=None, dim=None, scope=None):
self.scope = None
self.value = []
self.coefficient = 1
self.power = 1
self.dim = [0, 0]
if value is not None:
self.value = value
else:
self.value = []
if coefficient is not None:
self.coefficient = coefficient
else:
self.coefficient = 1
if power is not None:
self.power = power
else:
self.power = 1
if dim is not None:
self.dim = dim
else:
self.dim = [0, 0]

def convertInCLIString(self):
from visma.io.parser import tokensToString
MatrixString = ''
self.dim[0] = len(self.value)
self.dim[1] = len(self.value[0])
for i in range(self.dim[0]):
for j in range(self.dim[1]):
MatrixString += tokensToString(self.value[i][j]) + '\t'
MatrixString += '\n'
return MatrixString

def __add__(self, other):
"""Adds two matrices
Expand Down Expand Up @@ -215,7 +238,6 @@ def determinant(self, mat=None):
list of tokens forming the determinant
"""
from visma.simplify.simplify import simplify
from visma.io.parser import tokensToString

if mat is None:
self.dimension()
Expand Down Expand Up @@ -248,7 +270,6 @@ def determinant(self, mat=None):
ans, _, _, _, _ = simplify(mat[0][0])
if not ans:
ans = Zero()
print(tokensToString(ans))
return ans

def traceMat(self):
Expand All @@ -262,10 +283,12 @@ def traceMat(self):
"""
from visma.simplify.simplify import simplify
trace = []
self.dim[0] = len(self.value)
self.dim[1] = len(self.value[0])
for i in range(self.dim[0]):
trace.extend(self.value[i][i])
trace.append(Binary('+'))
trace.append(Constant(0))
trace.pop()
trace, _, _, _, _ = simplify(trace)
return trace

Expand All @@ -284,7 +307,8 @@ def inverse(self):

if tokensToString(self.determinant()) == "0":
return -1

self.dim[0] = len(self.value)
self.dim[1] = len(self.value[0])
n = self.dim[0]
mat = Matrix()
mat.empty([n, 2*n])
Expand Down

0 comments on commit d1d2471

Please sign in to comment.