Skip to content

Commit

Permalink
Merge 34b48dd into b759524
Browse files Browse the repository at this point in the history
  • Loading branch information
8hantanu committed Jul 21, 2018
2 parents b759524 + 34b48dd commit eb757ed
Show file tree
Hide file tree
Showing 20 changed files with 161 additions and 148 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,5 +1,5 @@
language: python
python: "2.7"
python: "3.6"

install :
- pip install pylama
Expand Down
147 changes: 67 additions & 80 deletions main.py

Large diffs are not rendered by default.

69 changes: 50 additions & 19 deletions tests/test_matrix.py
@@ -1,5 +1,5 @@
from visma.matrix.checks import isMatrix, dimCheck, multiplyCheck
from visma.matrix.operations import simplifyMatrix, addMatrix, multiplyMatrix
from visma.matrix.operations import simplifyMatrix, addMatrix
from tests.tester import getTokens

####################
Expand All @@ -9,7 +9,8 @@

def test_strMatrix():

mat = getTokens("[1+x, 2; 3, 4]")
mat = getTokens("[1+x, 2; \
3 , 4]")
assert mat.__str__() == "[{1.0}+{x},{2.0};{3.0},{4.0}]"


Expand All @@ -20,11 +21,14 @@ def test_strMatrix():

def test_isMatrix():

mat = getTokens("[1, 2, 3; x, z, 3]")
mat = getTokens("[1, 2, 3; \
x, z, 3]")
assert isMatrix(mat)

mat = getTokens("[1, 2; 1, 3; 1]")
assert mat == [] # not a matrix
mat = getTokens("[1, 2; \
1, 3; \
1]")
assert mat == [] # not a matrix; returns empty matrix


def test_dimCheck():
Expand All @@ -35,21 +39,26 @@ def test_dimCheck():
2, 3, y]")
assert not dimCheck(matA, matB)

matA = getTokens("[2, x; 3, y]")
matB = getTokens("[1, 2; 2, 3]")
matA = getTokens("[2, x; \
3, y]")
matB = getTokens("[1, 2; \
2, 3]")
assert dimCheck(matA, matB)


def test_mulDimCheck():
def test_multiplyCheck():

matA = getTokens("[1, 2; x, 2; 3, y]")
matB = getTokens("[2, x; 3, y]")
assert mulDimCheck(matA, matB)
matA = getTokens("[1, 2; \
x, 2; \
3, y]")
matB = getTokens("[2, x; \
3, y]")
assert multiplyCheck(matA, matB)

matA = getTokens("[2, x, 1; \
3, y, z]")
matB = getTokens("[1, 2; 2, 3]")
assert not mulDimCheck(matA, matB)
assert not multiplyCheck(matA, matB)


#####################
Expand All @@ -71,14 +80,36 @@ def test_simplifyMatrix():

def test_addMatrix():

matA = getTokens("[x+y]")
matB = getTokens("[x]")
matA = getTokens("[2x + y, 2x]")
matB = getTokens("[-x, -x]")
matSum = addMatrix(matA, matB)
# assert matSum.__str__() == "[2{x}+{y}]" # BUG: Strange simplification for SUM[0][0]
# assert matSum.__str__() == "[2{x}+{y}]" # BUG: Strange simplification for matSum

matA = getTokens("[ x, x^2; \
3 + x^2, xy ]")
matB = getTokens("[ y + 1, x^2; \
2 - x^2, xy - 1 ]")
matA = getTokens("[ x , x^2; \
3 + x^2, xy ]")
matB = getTokens("[ y + 1 , x^2; \
2 - x^2, xy - 1 ]")
matSum = addMatrix(matA, matB)
assert matSum.__str__() == "[{x}+{y}+{1.0},2{x}^{2.0};{5.0},2{x}{y}-{1.0}]"


def test_multiplyMatrix():
"""
# FIXME: Fixing addition fixes multiplication
matA = getTokens("[1, 0; 0, 1]")
matB = getTokens("[2; 3]")
matPro = multiplyMatrix(matA, matB)
# assert matPro.__str__() == ""
matA = getTokens("[1, 2; x, 2; 3, y]")
matB = getTokens("[2, x; 3, y]")
matPro = multiplyMatrix(matA, matB)
# assert matPro.__str__() == ""
matA = getTokens("[2, x, 1; \
3, y, z]")
matB = getTokens("[1, 2; 2, 3; 5, 6]")
matPro = multiplyMatrix(matA, matB)
# assert matPro.__str__() == ""
"""
pass
4 changes: 2 additions & 2 deletions tests/test_simplify.py
Expand Up @@ -19,7 +19,7 @@ def test_simplify():

assert quickTest("3/2 + 4/2 - 2/4", simplify) == "3.0"
assert quickTest("x/5 + x/4 - 2/y", simplify) == "0.45x-2.0y^(-1)"
assert quickTest("x/y + x/x + x/x^2 + x^2/x + x/y^2 + x^2/y + x + 1", simplify) == "xy^(-1)+x^(-1.0)+xy^(-2.0)+x^(2.0)y^(-1)+2x+2.0"
assert quickTest("x/y + x/x + x/x^2 + x^2/x + x/y^2 + x^2/y + x + 1", simplify) == "xy^(-1)+x^(-1.0)+xy^(-2.0)+x^(2.0)y^(-1)+2.0x+2.0"

assert quickTest("1 + 2 = 3", simplifyEquation) == "=0" # FIXME: Vanishing zero
assert quickTest("1 + 2 = 4", simplifyEquation) == "-1.0=0" # FIXME: Exclude these cases, raise math error
Expand Down Expand Up @@ -67,6 +67,6 @@ def test_muldiv():

assert quickTest("x^3 * x^2", multiplication) == "x^(3.0)x^(2.0)" # FIXME: Further simplification required

assert quickTest("x^2 / x^2", division) == "1"
assert quickTest("x^2 / x^2", division) == "1.0"
assert quickTest("x^4 / x^2", division) == "x^(2.0)"
assert quickTest("x^2 / x^4", division) == "x^(-2.0)"
6 changes: 3 additions & 3 deletions tests/test_solvers.py
Expand Up @@ -24,8 +24,8 @@ def test_quadraticRoots():
assert quickTest("3x^2 + 7x + 1 = 0", quadraticRoots) == "(x+2.18)*(x+0.15)=0"
assert quickTest("3x^2 - 7x + 1 = 0", quadraticRoots) == "(x-0.15)*(x-2.18)=0"

assert quickTest("x^2 + x + 1 = 0", quadraticRoots) == "(x+0.5+0.87*sqrt[2](-1.0))*(x+0.5-0.87*sqrt[2](-1.0))=0"
assert quickTest("x^2 - x + 1 = 0", quadraticRoots) == "(x-0.5+0.87*sqrt[2](-1.0))*(x-0.5-0.87*sqrt[2](-1.0))=0"
assert quickTest("x^2 + x + 1 = 0", quadraticRoots) == "(x+0.5+0.87*sqrt[2](-1))*(x+0.5-0.87*sqrt[2](-1))=0"
assert quickTest("x^2 - x + 1 = 0", quadraticRoots) == "(x-0.5+0.87*sqrt[2](-1))*(x-0.5-0.87*sqrt[2](-1))=0"


#################
Expand All @@ -46,4 +46,4 @@ def test_solveFor():
assert quickTest("w + x^2 + yz^3 = 1", solveFor, 'w') == "w=(-x^(2.0)-yz^(3.0)+1.0)"
assert quickTest("w + x^2 + yz^3 = 1", solveFor, 'x') == "x=(-w-yz^(3.0)+1.0)^(0.5)"
assert quickTest("w + x^2 + yz^3 = 1", solveFor, 'y') == "y=((-w-x^(2.0)+1.0)/z^(3.0))"
assert quickTest("w + x^2 + yz^3 = 1", solveFor, 'z') == "z=((-w-x^(2.0)+1.0)/y)^(0.333333333333)"
assert quickTest("w + x^2 + yz^3 = 1", solveFor, 'z') == "z=((-w-x^(2.0)+1.0)/y)^(0.3333333333333333)"
2 changes: 1 addition & 1 deletion tests/test_transform.py
Expand Up @@ -32,7 +32,7 @@ def test_substitute():
init_tok = getTokens("2x")
subs_tok = getTokens("4yz^2")
tok_list = getTokens("3 + 2x + zx^4 + 3xyz")
assert tokensToString(substitute(init_tok, subs_tok, tok_list)) == "3.0 + 4.0yz^(2.0) + 16.0z^(9.0)y^(4.0) + 6.0y^(2)z^(3.0)"
assert tokensToString(substitute(init_tok, subs_tok, tok_list)) == "3.0 + 4.0yz^(2.0) + 16.0z^(9.0)y^(4.0) + 6.0y^(2.0)z^(3.0)"

init_tok = getTokens("4x^2")
subs_tok = getTokens("9yz")
Expand Down
2 changes: 0 additions & 2 deletions visma/calculus/integration.py
@@ -1,6 +1,4 @@
from __future__ import division
import copy

from visma.functions.structure import Function
from visma.functions.constant import Constant, Zero
from visma.functions.variable import Variable
Expand Down
2 changes: 1 addition & 1 deletion visma/functions/structure.py
Expand Up @@ -132,6 +132,6 @@ class Equation(Expression):
"""

def __init__(self):
super(Equation, self).__init__()
super(self).__init__()
self.tokens = None
self.type = 'Equation'
3 changes: 1 addition & 2 deletions visma/functions/variable.py
@@ -1,4 +1,3 @@
from __future__ import division
from visma.functions.structure import Function, Expression
from visma.functions.exponential import Logarithm
from visma.functions.operator import Divide
Expand Down Expand Up @@ -43,7 +42,7 @@ def inverse(self, rToken, wrtVar):

def differentiate(self):
from visma.functions.constant import Constant
super(Variable, self).differentiate()
super().differentiate()
self.value = 1
self.__class__ = Constant

Expand Down
1 change: 0 additions & 1 deletion visma/gui/logger.py
@@ -1 +0,0 @@
# TODO: Logger initialized
8 changes: 4 additions & 4 deletions visma/gui/plotter.py
@@ -1,5 +1,5 @@
from visma.io.tokenize import getLHSandRHS
import numpy as np
from visma.io.tokenize import getLHSandRHS
from visma.functions.variable import Variable
from visma.functions.constant import Constant
from visma.functions.operator import Binary
Expand All @@ -8,15 +8,15 @@

def plotThis(equationTokens):

# FIXME: Quite basic right now. Need fix for multi-variables
# FIXME: Quite basic right now. Needs fix for multi-variables

LHStok, RHStok = getLHSandRHS(equationTokens)

varDict = {}
delta = 0.1
xrange = np.arange(-20, 20.0, delta)
range = np.arange(-20, 20.0, delta)
yrange = np.arange(-20, 20.0, delta)
varDict['x'], varDict['y'] = np.meshgrid(xrange, yrange)
varDict['x'], varDict['y'] = np.meshgrid(range, yrange)

LHS = 0
coeff = 1
Expand Down
22 changes: 11 additions & 11 deletions visma/io/checks.py
Expand Up @@ -21,14 +21,14 @@ def __init__(self, lTokens, rTokens):
if checkSolveFor(lTokens, rTokens):
self.availableOperations.append('solve')
self.availableOperations.extend(getOperationsEquation(self.lVariables, self.lTokens, self.rVariables, self.rTokens))
# print self.availableOperations
# print(self.availableOperations)


class ExpressionCompatibility(object):
"""docstring for ExpressionCompatibility"""

def __init__(self, tokens):
super(ExpressionCompatibility, self).__init__()
super().__init__()
self.tokens = tokens
self.variables = []
self.variables.extend(getLevelVariables(self.tokens))
Expand Down Expand Up @@ -119,8 +119,8 @@ def checkEquation(terms, symTokens):
elif term == ')':
brackets -= 1
if brackets < 0:
# TODO: logger.log("Too many ')'")
return False
# TODO: logger.log("Too many ')'")
elif term == '[':
sqrBrackets += 1
elif term == ']':
Expand Down Expand Up @@ -466,7 +466,7 @@ def getOperationsEquation(lVariables, lTokens, rVariables, rTokens):
ops = []

if len(variable.value) > 1:
for j in xrange(len(variable.value)):
for j in range(len(variable.value)):
if variable.after[j] in ['+', '-', ''] and variable.before[j] in ['+', '-']:
count += 1
opCount += 1
Expand All @@ -481,7 +481,7 @@ def getOperationsEquation(lVariables, lTokens, rVariables, rTokens):
if (len(variable.value) > 0 and rCount > 0):
for variable2 in rVariables:
if isinstance(variable2, Constant):
for l in xrange(len(variable2.value)):
for l in range(len(variable2.value)):
if variable2.after[l] in ['+', '-', ''] and variable2.before[l] in ['+', '-', ''] and variable2.value[l] != 0:
count += 1
opCount += 1
Expand Down Expand Up @@ -511,7 +511,7 @@ def getOperationsEquation(lVariables, lTokens, rVariables, rTokens):
power = []
opCount = 0
if len(variable.power) > 1:
for j in xrange(len(variable.power)):
for j in range(len(variable.power)):
if variable.after[j] in ['+', '-', ''] and variable.before[j] in ['+', '-']:
count += 1
opCount += 1
Expand All @@ -528,7 +528,7 @@ def getOperationsEquation(lVariables, lTokens, rVariables, rTokens):
for variable2 in rVariables:
if isinstance(variable2, Variable):
if variable2.value == variable.value and variable2.power[0] == variable.power[0]:
for l in xrange(len(variable2.power)):
for l in range(len(variable2.power)):
if variable2.after[l] in ['+', '-', ''] and variable2.before[l] in ['+', '-', '']:
count += 1
opCount += 1
Expand Down Expand Up @@ -558,7 +558,7 @@ def getOperationsEquation(lVariables, lTokens, rVariables, rTokens):
if len(variable.value) > 1:
count = 0
ops = []
for j in xrange(len(variable.value)):
for j in range(len(variable.value)):
if variable.after[j] in ['+', '-', ''] and variable.before[j] in ['+', '-']:
count += 1
if not (variable.before[j] in ops):
Expand All @@ -573,7 +573,7 @@ def getOperationsEquation(lVariables, lTokens, rVariables, rTokens):
ops = []
power = []
opCount = 0
for j in xrange(len(variable.power)):
for j in range(len(variable.power)):
if variable.after[j] in ['+', '-', ''] and variable.before[j] in ['+', '-']:
count += 1
opCount += 1
Expand Down Expand Up @@ -621,7 +621,7 @@ def getOperationsExpression(variables, tokens):
count = 0
opCount = 0
ops = []
for j in xrange(len(variable.value)):
for j in range(len(variable.value)):
if variable.after[j] in ['+', '-', ''] and variable.before[j] in ['+', '-']:
count += 1
opCount += 1
Expand All @@ -640,7 +640,7 @@ def getOperationsExpression(variables, tokens):
ops = []
power = []
opCount = 0
for j in xrange(len(variable.power)):
for j in range(len(variable.power)):
if variable.after[j] in ['+', '-', ''] and variable.before[j] in ['+', '-']:
count += 1
opCount += 1
Expand Down
2 changes: 1 addition & 1 deletion visma/io/parser.py
Expand Up @@ -19,7 +19,7 @@ def resultLatex(operation, equations, comments, wrtVar=None):
finalSteps += "\n"
finalSteps += "OUTPUT: " + r"$" + equationLatex[-1] + r"$" + "\n"*2

for i in xrange(len(equationLatex)):
for i in range(len(equationLatex)):
if comments[i] != []:
finalSteps += str(comments[i][0]) + "\n"
finalSteps += r"$" + equationLatex[i] + r"$" + "\n"*2
Expand Down
4 changes: 2 additions & 2 deletions visma/matrix/structure.py
Expand Up @@ -60,7 +60,7 @@ def dimension(self):
class ValMat(Matrix):

def __init__(self, dim, token):
super(ValMat, self).__init__()
super().__init__()
for i in range(0, dim[0]):
row = []
for j in range(0, dim[1]):
Expand All @@ -80,7 +80,7 @@ def determinant(self):
class IdenMat(SquareMat):

def __init__(self, dim):
super(IdenMat, self).__init__()
super().__init__()
for i in range(0, dim[0]):
row = []
for j in range(0, dim[1]):
Expand Down

0 comments on commit eb757ed

Please sign in to comment.