diff --git a/.travis.yml b/.travis.yml index 9e09dda..f1c2423 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: python -python: "2.7" +python: "3.6" install : - pip install pylama diff --git a/main.py b/main.py index 160a281..14155b4 100644 --- a/main.py +++ b/main.py @@ -10,11 +10,12 @@ import sys import os -from PyQt4.QtGui import QWidget, QGridLayout, QVBoxLayout, QHBoxLayout, QTextEdit, QSplitter, QLabel, QFrame, QApplication, QAbstractButton, QPainter -from PyQt4.QtCore import Qt -from PyQt4 import QtGui -from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas -from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar +from PyQt5.QtGui import QPainter +from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QVBoxLayout, QHBoxLayout, QTextEdit, QSplitter, QFrame, QAbstractButton +from PyQt5.QtCore import Qt +from PyQt5 import QtGui, QtWidgets +from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas +from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar from matplotlib.figure import Figure import webbrowser @@ -32,35 +33,21 @@ from visma.transform.factorization import factorize -class Window(QtGui.QMainWindow): +class Window(QtWidgets.QMainWindow): def __init__(self): super(Window, self).__init__() font = QtGui.QFont() font.setPointSize(12) self.setFont(font) - self.setStyleSheet(""" - QPushButton { - background-color: white; - border: 3px solid white; - border-radius: 10px; - color: black - } - QPushButton:pressed { - background-color: black; - border: 3px solid black; - border-radius: 10px; - color: white - } - """) def initUI(self): - exitAction = QtGui.QAction('Exit', self) + exitAction = QtWidgets.QAction('Exit', self) exitAction.setShortcut('Ctrl+Q') exitAction.setStatusTip('Exit application') exitAction.triggered.connect(self.close) - wikiAction = QtGui.QAction('Wiki', self) + wikiAction = QtWidgets.QAction('Wiki', self) wikiAction.setStatusTip('Open Github wiki') # TODO: Pop a mini browser for docs and wiki wikiAction.triggered.connect(lambda: webbrowser.open('https://github.com/aerospaceresearch/visma/wiki')) @@ -144,7 +131,7 @@ def initUI(self): plotFig = QWidget() plotFig.setLayout(self.plotFigure()) - plotFig.setStatusTip("Graph plot") + plotFig.setStatusTip("Visualize graph") stepsFig = QWidget() stepsFig.setLayout(self.stepsFigure()) @@ -181,25 +168,25 @@ def initUI(self): def textChangeTrigger(self): pass - # print self.textedit.toPlainText() + # print(self.textedit.toPlainText()) def equationsLayout(self): - self.myQListWidget = QtGui.QListWidget(self) + self.myQListWidget = QtWidgets.QListWidget(self) for index, name in self.equations: myQCustomQWidget = QCustomQWidget() myQCustomQWidget.setTextUp(index) myQCustomQWidget.setTextDown(name) - myQListWidgetItem = QtGui.QListWidgetItem(self.myQListWidget) + myQListWidgetItem = QtWidgets.QListWidgetItem(self.myQListWidget) myQListWidgetItem.setSizeHint(myQCustomQWidget.sizeHint()) self.myQListWidget.addItem(myQListWidgetItem) self.myQListWidget.setItemWidget( myQListWidgetItem, myQCustomQWidget) self.myQListWidget.resize(400, 300) - self.equationListVbox.addWidget(QLabel("

equation history

")) + # self.equationListVbox.addWidget(QLabel("

equation history

")) self.equationListVbox.addWidget(self.myQListWidget) self.myQListWidget.itemClicked.connect(self.Clicked) # FIXME: Clear button. Clear rightaway. - self.clearButton = QtGui.QPushButton('clear') + self.clearButton = QtWidgets.QPushButton('clear equations') self.clearButton.clicked.connect(self.clearHistory) self.equationListVbox.addWidget(self.clearButton) return self.equationListVbox @@ -218,10 +205,10 @@ class NavigationCustomToolbar(NavigationToolbar): toolitems = [t for t in NavigationToolbar.toolitems if t[0] in ('Home', 'Pan', 'Zoom', 'Save')] self.toolbar = NavigationCustomToolbar(self.canvas, self) - self.button = QtGui.QPushButton('plot') + self.button = QtWidgets.QPushButton('plot graph') self.button.clicked.connect(self.plot) - layout = QtGui.QVBoxLayout() - layout.addWidget(QLabel("

plotter

")) + layout = QtWidgets.QVBoxLayout() + # layout.addWidget(QLabel("

plotter

")) layout.addWidget(self.canvas) layout.addWidget(self.toolbar) layout.addWidget(self.button) @@ -240,12 +227,12 @@ def stepsFigure(self): self.stepsfigure = Figure() self.stepscanvas = FigureCanvas(self.stepsfigure) self.stepsfigure.clear() - self.stepsbutton = QtGui.QPushButton('show steps') + self.stepsbutton = QtWidgets.QPushButton('show steps') self.stepsbutton.clicked.connect(self.showSteps) self.stepsfigure.patch.set_facecolor('white') - stepslayout = QtGui.QVBoxLayout() - stepslayout.addWidget(QLabel("

step-by-step solution

")) + stepslayout = QtWidgets.QVBoxLayout() + # stepslayout.addWidget(QLabel("

step-by-step solution

")) stepslayout.addWidget(self.stepscanvas) stepslayout.addWidget(self.stepsbutton) return stepslayout @@ -267,7 +254,7 @@ def buttonsLayout(self): vbox = QVBoxLayout() interactionModeLayout = QVBoxLayout() - vismaButton = QtGui.QPushButton('visma') + vismaButton = QtWidgets.QPushButton('visma') interactionModeButton = vismaButton interactionModeButton.clicked.connect(self.interactionMode) @@ -309,7 +296,7 @@ def interactionMode(self): textSelected = str(interactionText) self.mode = 'interaction' self.tokens = tokenizer(textSelected) - # DBP: print self.tokens + # DBP: print(self.tokens) self.addEquation() lhs, rhs = getLHSandRHS(self.tokens) self.lTokens = lhs @@ -337,12 +324,12 @@ def interactionMode(self): opButtons.append(operation) if self.buttonSet: - for i in reversed(xrange(self.solutionOptionsBox.count())): + for i in reversed(range(self.solutionOptionsBox.count())): self.solutionOptionsBox.itemAt(i).widget().setParent(None) - for i in xrange(int(len(opButtons) / 2) + 1): - for j in xrange(2): + for i in range(int(len(opButtons) / 2) + 1): + for j in range(2): if len(opButtons) > (i * 2 + j): - self.solutionButtons[(i, j)] = QtGui.QPushButton( + self.solutionButtons[(i, j)] = QtWidgets.QPushButton( opButtons[i * 2 + j]) self.solutionButtons[(i, j)].resize(100, 100) self.solutionButtons[(i, j)].clicked.connect( @@ -352,10 +339,10 @@ def interactionMode(self): else: self.bottomButton.setParent(None) self.solutionWidget = QWidget() - for i in xrange(int(len(opButtons) / 2) + 1): - for j in xrange(2): + for i in range(int(len(opButtons) / 2) + 1): + for j in range(2): if len(opButtons) > (i * 2 + j): - self.solutionButtons[(i, j)] = QtGui.QPushButton( + self.solutionButtons[(i, j)] = QtWidgets.QPushButton( opButtons[i * 2 + j]) self.solutionButtons[(i, j)].resize(100, 100) self.solutionButtons[(i, j)].clicked.connect( @@ -386,12 +373,12 @@ def refreshButtons(self, operations): opButtons.append("division") else: opButtons.append(operation) - for i in reversed(xrange(self.solutionOptionsBox.count())): + for i in reversed(range(self.solutionOptionsBox.count())): self.solutionOptionsBox.itemAt(i).widget().setParent(None) - for i in xrange(int(len(opButtons) / 2) + 1): - for j in xrange(2): + for i in range(int(len(opButtons) / 2) + 1): + for j in range(2): if len(opButtons) > (i * 2 + j): - self.solutionButtons[(i, j)] = QtGui.QPushButton( + self.solutionButtons[(i, j)] = QtWidgets.QPushButton( opButtons[i * 2 + j]) self.solutionButtons[(i, j)].resize(100, 100) self.solutionButtons[(i, j)].clicked.connect( @@ -400,7 +387,7 @@ def refreshButtons(self, operations): self.solutionButtons[(i, j)], i, j) def clearButtons(self): - for i in reversed(xrange(self.solutionOptionsBox.count())): + for i in reversed(range(self.solutionOptionsBox.count())): self.solutionOptionsBox.itemAt(i).widget().setParent(None) def wrtVariableButtons(self, variables, operation): @@ -410,12 +397,12 @@ def wrtVariableButtons(self, variables, operation): for variable in variables: varButtons.append(variable) varButtons.append("Back") - for i in reversed(xrange(self.solutionOptionsBox.count())): + for i in reversed(range(self.solutionOptionsBox.count())): self.solutionOptionsBox.itemAt(i).widget().setParent(None) - for i in xrange(int(len(varButtons) / 2) + 1): - for j in xrange(2): + for i in range(int(len(varButtons) / 2) + 1): + for j in range(2): if len(varButtons) > (i * 2 + j): - self.solutionButtons[(i, j)] = QtGui.QPushButton( + self.solutionButtons[(i, j)] = QtWidgets.QPushButton( varButtons[i * 2 + j]) self.solutionButtons[(i, j)].resize(100, 100) self.solutionButtons[(i, j)].clicked.connect( @@ -427,10 +414,10 @@ def newEquation(self): self.textedit.setText("") def saveEquation(self): - for i in reversed(xrange(self.equationListVbox.count())): + for i in reversed(range(self.equationListVbox.count())): self.equationListVbox.itemAt(i).widget().setParent(None) - eqn = unicode(self.textedit.toPlainText()) + eqn = str(self.textedit.toPlainText()) if len(self.equations) == 1: index, name = self.equations[0] if index == "No equations stored": @@ -443,7 +430,7 @@ def saveEquation(self): self.textedit.setText('') file = open('local/eqn-list.vis', 'r+') - self.myQListWidget = QtGui.QListWidget(self) + self.myQListWidget = QtWidgets.QListWidget(self) i = 0 for index, name in self.equations: if i != 0: @@ -452,7 +439,7 @@ def saveEquation(self): myQCustomQWidget = QCustomQWidget() myQCustomQWidget.setTextUp(index) myQCustomQWidget.setTextDown(name) - myQListWidgetItem = QtGui.QListWidgetItem(self.myQListWidget) + myQListWidgetItem = QtWidgets.QListWidgetItem(self.myQListWidget) myQListWidgetItem.setSizeHint(myQCustomQWidget.sizeHint()) self.myQListWidget.addItem(myQListWidgetItem) self.myQListWidget.setItemWidget( @@ -466,12 +453,12 @@ def saveEquation(self): return self.equationListVbox def addEquation(self): - eqn = unicode(self.textedit.toPlainText()) + eqn = str(self.textedit.toPlainText()) for index, equation in self.equations: if equation == eqn: return self.equationListVbox - for i in reversed(xrange(self.equationListVbox.count())): + for i in reversed(range(self.equationListVbox.count())): self.equationListVbox.itemAt(i).widget().setParent(None) if len(self.equations) == 1: @@ -484,7 +471,7 @@ def addEquation(self): self.equations.append( ("Equation No. " + str(len(self.equations) + 1), eqn)) file = open('local/eqn-list.vis', 'r+') - self.myQListWidget = QtGui.QListWidget(self) + self.myQListWidget = QtWidgets.QListWidget(self) i = 0 for index, name in self.equations: if i != 0: @@ -493,7 +480,7 @@ def addEquation(self): myQCustomQWidget = QCustomQWidget() myQCustomQWidget.setTextUp(index) myQCustomQWidget.setTextDown(name) - myQListWidgetItem = QtGui.QListWidgetItem(self.myQListWidget) + myQListWidgetItem = QtWidgets.QListWidgetItem(self.myQListWidget) myQListWidgetItem.setSizeHint(myQCustomQWidget.sizeHint()) self.myQListWidget.addItem(myQListWidgetItem) self.myQListWidget.setItemWidget( @@ -503,10 +490,10 @@ def addEquation(self): self.myQListWidget.resize(400, 300) self.myQListWidget.itemClicked.connect(self.Clicked) - self.equationListVbox.addWidget(QLabel("

equation history

")) + # self.equationListVbox.addWidget(QLabel("

equation history

")) self.equationListVbox.addWidget(self.myQListWidget) self.myQListWidget.itemClicked.connect(self.Clicked) - self.clearButton = QtGui.QPushButton('clear') + self.clearButton = QtWidgets.QPushButton('clear equations') self.clearButton.clicked.connect(self.clearHistory) self.equationListVbox.addWidget(self.clearButton) return self.equationListVbox @@ -515,11 +502,11 @@ def inputsLayout(self, loadList="Greek"): inputLayout = QHBoxLayout(self) blank = QFrame() # TODO: Move input type to config - # comboLabel = QtGui.QLabel() + # comboLabel = QtWidgets.QLabel() # comboLabel.setText("Input Type:") # comboLabel.setFixedSize(100, 30) - # combo = QtGui.QComboBox(self) + # combo = QtWidgets.QComboBox(self) # combo.addItem("Greek") # combo.addItem("LaTeX") # combo.setFixedSize(100, 30) @@ -535,11 +522,11 @@ def inputsLayout(self, loadList="Greek"): inputSplitter = QSplitter(Qt.Vertical) inputWidget = QWidget() self.selectedCombo = str(loadList) - for i in xrange(4): - for j in xrange(10): + for i in range(4): + for j in range(10): if str(loadList) in "Greek": if (i * 10 + j) < len(self.inputGreek): - self.buttons[(i, j)] = QtGui.QPushButton( + self.buttons[(i, j)] = QtWidgets.QPushButton( self.inputGreek[i * 10 + j]) self.buttons[(i, j)].resize(100, 100) self.buttons[(i, j)].clicked.connect( @@ -547,7 +534,7 @@ def inputsLayout(self, loadList="Greek"): self.inputBox.addWidget(self.buttons[(i, j)], i, j) elif str(loadList) in "LaTeX": if (i * 10 + j) < len(self.inputLaTeX): - self.buttons[(i, j)] = QtGui.QPushButton( + self.buttons[(i, j)] = QtWidgets.QPushButton( self.inputLaTeX[i * 10 + j]) self.buttons[(i, j)].resize(100, 100) self.buttons[(i, j)].clicked.connect( @@ -561,14 +548,14 @@ def inputsLayout(self, loadList="Greek"): return inputLayout def onActivated(self, text): - for i in reversed(xrange(self.inputBox.count())): + for i in reversed(range(self.inputBox.count())): self.inputBox.itemAt(i).widget().setParent(None) - for i in xrange(4): - for j in xrange(10): + for i in range(4): + for j in range(10): if str(text) in "Greek": if (i * 10 + j) < len(self.inputGreek): - self.buttons[(i, j)] = QtGui.QPushButton( + self.buttons[(i, j)] = QtWidgets.QPushButton( self.inputGreek[i * 10 + j]) self.buttons[(i, j)].resize(100, 100) self.buttons[(i, j)].clicked.connect( @@ -576,7 +563,7 @@ def onActivated(self, text): self.inputBox.addWidget(self.buttons[(i, j)], i, j) elif str(text) in "LaTeX": if (i * 10 + j) < len(self.inputLaTeX): - self.buttons[(i, j)] = QtGui.QPushButton( + self.buttons[(i, j)] = QtWidgets.QPushButton( self.inputLaTeX[i * 10 + j]) self.buttons[(i, j)].resize(100, 100) self.buttons[(i, j)].clicked.connect( @@ -592,7 +579,7 @@ def calluser(): cursor = self.textedit.textCursor() cursor.deletePreviousChar() else: - self.textedit.insertPlainText(unicode(name)) + self.textedit.insertPlainText(str(name)) return calluser def onSolvePress(self, name): @@ -675,7 +662,7 @@ def calluser(): if varName == 'Back': textSelected = str(self.textedit.toPlainText()) self.tokens = tokenizer(textSelected) - # print self.tokens + # print(self.tokens) lhs, rhs = getLHSandRHS(self.tokens) operations, self.solutionType = checkTypes( lhs, rhs) @@ -704,16 +691,16 @@ def calluser(): return calluser -class QCustomQWidget(QtGui.QWidget): +class QCustomQWidget(QtWidgets.QWidget): def __init__(self, parent=None): super(QCustomQWidget, self).__init__(parent) - self.textQVBoxLayout = QtGui.QVBoxLayout() - self.textUpQLabel = QtGui.QLabel() - self.textDownQLabel = QtGui.QLabel() + self.textQVBoxLayout = QtWidgets.QVBoxLayout() + self.textUpQLabel = QtWidgets.QLabel() + self.textDownQLabel = QtWidgets.QLabel() self.textQVBoxLayout.addWidget(self.textUpQLabel) self.textQVBoxLayout.addWidget(self.textDownQLabel) - self.allQHBoxLayout = QtGui.QHBoxLayout() + self.allQHBoxLayout = QtWidgets.QHBoxLayout() self.allQHBoxLayout.addLayout(self.textQVBoxLayout, 1) self.setLayout(self.allQHBoxLayout) self.textUpQLabel.setStyleSheet(''' diff --git a/tests/test_matrix.py b/tests/test_matrix.py index e0952e9..160f7bf 100644 --- a/tests/test_matrix.py +++ b/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 #################### @@ -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}]" @@ -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(): @@ -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) ##################### @@ -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 diff --git a/tests/test_simplify.py b/tests/test_simplify.py index 4204977..f72cc6f 100644 --- a/tests/test_simplify.py +++ b/tests/test_simplify.py @@ -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 @@ -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)" diff --git a/tests/test_solvers.py b/tests/test_solvers.py index 3036036..4d38563 100644 --- a/tests/test_solvers.py +++ b/tests/test_solvers.py @@ -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" ################# @@ -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)" diff --git a/tests/test_transform.py b/tests/test_transform.py index e9101e4..44df524 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -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") diff --git a/visma/calculus/integration.py b/visma/calculus/integration.py index 125e98e..b1ded87 100644 --- a/visma/calculus/integration.py +++ b/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 diff --git a/visma/functions/structure.py b/visma/functions/structure.py index 734bb08..88fd082 100644 --- a/visma/functions/structure.py +++ b/visma/functions/structure.py @@ -132,6 +132,6 @@ class Equation(Expression): """ def __init__(self): - super(Equation, self).__init__() + super(self).__init__() self.tokens = None self.type = 'Equation' diff --git a/visma/functions/variable.py b/visma/functions/variable.py index 62c27dd..51e4d29 100644 --- a/visma/functions/variable.py +++ b/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 @@ -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 diff --git a/visma/gui/logger.py b/visma/gui/logger.py index 815c404..e69de29 100644 --- a/visma/gui/logger.py +++ b/visma/gui/logger.py @@ -1 +0,0 @@ -# TODO: Logger initialized diff --git a/visma/gui/plotter.py b/visma/gui/plotter.py index 20f2974..8b2f5c8 100644 --- a/visma/gui/plotter.py +++ b/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 @@ -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 diff --git a/visma/io/checks.py b/visma/io/checks.py index b36b61a..919ed6b 100644 --- a/visma/io/checks.py +++ b/visma/io/checks.py @@ -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)) @@ -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 == ']': @@ -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 @@ -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 @@ -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 @@ -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 @@ -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): @@ -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 @@ -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 @@ -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 diff --git a/visma/io/parser.py b/visma/io/parser.py index ea3d73c..fe3b6d0 100644 --- a/visma/io/parser.py +++ b/visma/io/parser.py @@ -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 diff --git a/visma/matrix/structure.py b/visma/matrix/structure.py index 1fd35d0..ac21a00 100644 --- a/visma/matrix/structure.py +++ b/visma/matrix/structure.py @@ -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]): @@ -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]): diff --git a/visma/simplify/addsub.py b/visma/simplify/addsub.py index 1725572..2727d80 100644 --- a/visma/simplify/addsub.py +++ b/visma/simplify/addsub.py @@ -148,7 +148,7 @@ def expressionAddition(variables, tokens): if len(variable.value) > 1: constantAdd = [] constant = [] - for j in xrange(len(variable.value)): + for j in range(len(variable.value)): if variable.after[j] in ['+', '-', ''] and variable.before[j] in ['+']: constantAdd.append(j) elif variable.after[j] in ['+', '-', ''] and variable.before[j] in ['', '-']: @@ -293,7 +293,7 @@ def expressionAddition(variables, tokens): if len(variable.power) > 1: constantAdd = [] constant = [] - for j in xrange(len(variable.power)): + for j in range(len(variable.power)): if variable.after[j] in ['+', '-', ''] and variable.before[j] in ['+']: constantAdd.append(j) elif variable.after[j] in ['+', '-', ''] and variable.before[j] in ['', '-']: @@ -427,12 +427,12 @@ def equationAddition(lVariables, lTokens, rVariables, rTokens): comments = [] for variable in lVariables: if isinstance(variable, Constant): - for j in xrange(len(variable.value)): + for j in range(len(variable.value)): if variable.before[j] in ['-', '+', ''] and variable.after[j] in ['+', '-', '']: for variable2 in rVariables: if isinstance(variable2, Constant): if variable2.power[0] == variable.power[0] and variable2.value[0] == variable.value[0]: - for k in xrange(len(variable2.value)): + for k in range(len(variable2.value)): if (variable2.before[k] == '-' or (variable2.before[k] == '' and variable2.value[k] < 0)) and variable2.after[k] in ['-', '+', '']: comments.append( "Moving " + r"$" + variable2.before[k] + variable2.__str__() + r"$" + " to LHS") @@ -482,12 +482,12 @@ def equationAddition(lVariables, lTokens, rVariables, rTokens): return lVariables, lTokens, lRemoveScopes, lChange, rVariables, rTokens, rRemoveScopes, rChange, comments elif isinstance(variable, Variable): - for j in xrange(len(variable.power)): + for j in range(len(variable.power)): if variable.before[j] in ['-', '+', ''] and variable.after[j] in ['+', '-', '']: for variable2 in rVariables: if isinstance(variable2, Variable): if variable2.power[0] == variable.power[0] and variable2.value[0] == variable.value[0]: - for k in xrange(len(variable2.value)): + for k in range(len(variable2.value)): if variable2.before[k] == '-' and variable2.after[k] in ['-', '+', '']: comments.append("Moving " + r"$" + variable2.before[k] + variable2.__str__() + r"$" + " to LHS") if variable.before[j] == '-': @@ -671,7 +671,7 @@ def expressionSubtraction(variables, tokens): if len(variable.value) > 1: constantAdd = [] constant = [] - for j in xrange(len(variable.value)): + for j in range(len(variable.value)): if variable.after[j] in ['+', '-', ''] and variable.before[j] in ['-']: constantAdd.append(j) elif variable.after[j] in ['+', '-', ''] and variable.before[j] in ['', '+']: @@ -816,7 +816,7 @@ def expressionSubtraction(variables, tokens): if len(variable.power) > 1: constantAdd = [] constant = [] - for j in xrange(len(variable.power)): + for j in range(len(variable.power)): if variable.after[j] in ['+', '-', ''] and variable.before[j] in ['-']: constantAdd.append(j) elif variable.after[j] in ['+', '-', ''] and variable.before[j] in ['', '+']: diff --git a/visma/simplify/muldiv.py b/visma/simplify/muldiv.py index 155e110..fefa251 100644 --- a/visma/simplify/muldiv.py +++ b/visma/simplify/muldiv.py @@ -122,7 +122,7 @@ def multiplyExpressions(expression1, expression2): tokens2 = expression2.tokens coeff = expression1.coefficient * expression2.coefficient for i, token1 in enumerate(tokens1): - # print token1.value + # print(token1.value) op = 1 if i != 0: if isinstance(tokens1[i - 1], Binary): @@ -132,7 +132,7 @@ def multiplyExpressions(expression1, expression2): op *= -1 if isinstance(token1, Variable) or isinstance(token1, Constant): for j, token2 in enumerate(tokens2): - # print token2.value + # print(token2.value) op2 = op if isinstance(token2, Variable) or isinstance(token2, Constant): if j == 0 and i == 0: @@ -151,7 +151,7 @@ def multiplyExpressions(expression1, expression2): binary.value = '+' tokens.append(binary) tokens.append(multiplySelect(token1, token2, coeff)) - # print tokens + # print(tokens) def multiplicationEquation(lToks, rToks, direct=False): diff --git a/visma/solvers/polynomial/roots.py b/visma/solvers/polynomial/roots.py index 0e91b46..3533175 100644 --- a/visma/solvers/polynomial/roots.py +++ b/visma/solvers/polynomial/roots.py @@ -8,7 +8,6 @@ Logic Description: """ -from __future__ import division import math import copy from visma.io.checks import evaluateConstant, availableVariables diff --git a/visma/testbed/id.py b/visma/testbed/id.py index 3020c07..eb8416a 100644 --- a/visma/testbed/id.py +++ b/visma/testbed/id.py @@ -3,11 +3,11 @@ ############ ''' -Following is a list of random IDs (func.tid property of func class) of a functions generated during calculations/simplifications +Following is an example list of IDs (func.tid property of Function class) of the functions generated during calculations/simplifications Only + and * are used as binary operations, the - and / will be taken care by func.coefficient and func.power respectively. ''' -# TODO: Use this Token ID in func.scope +# Using nested expressions instead of ID. Leaving here for future use. random = ['a0', 'b0', 'b0a2', 'b0a1', 'b0a3', 'b0b0', 'a0b0', 'a0a1', 'a0a2', 'a0a2a0', 'a0a3', 'c1', 'c2'] @@ -32,7 +32,7 @@ final.append('(') openbrac += 1 elif(levelold > level): - for i in xrange(0, levelold - level): + for i in range(0, levelold - level): final.append(')') closebrac += 1 if(func[2 * level + 1] <= '1'): @@ -50,7 +50,7 @@ # FIXME: Find a way to handle functions in exponentials Ex: f1^(f2+f3) -for i in xrange(0, openbrac - closebrac): +for i in range(0, openbrac - closebrac): final.append(')') eqstring = "" for func in final: diff --git a/visma/utils/integers.py b/visma/utils/integers.py index d0f0748..5dbb1e4 100644 --- a/visma/utils/integers.py +++ b/visma/utils/integers.py @@ -21,7 +21,7 @@ def factors(number): """Return the factors list for given number. """ factors = [] - for i in xrange(1, abs(int(number)) + 1): + for i in range(1, abs(int(number)) + 1): if number % i == 0: factors.append(i) return factors diff --git a/visma/utils/polynomials.py b/visma/utils/polynomials.py index 34d740d..6d4f826 100644 --- a/visma/utils/polynomials.py +++ b/visma/utils/polynomials.py @@ -3,7 +3,7 @@ def syntheticDivision(coefficients, divisor): coefficients of polynomial """ quotient = [float(coefficients[0])] - for i in xrange(len(coefficients) - 1): + for i in range(len(coefficients) - 1): quotient.append(quotient[-1] * divisor + coefficients[i + 1]) remainder = quotient.pop() return quotient, remainder