def index(): if "comments" not in session: session["comments"] = "" if "m" not in session: session["m"] = "3" if "n" not in session: session["n"] = "3" if request.method == "GET": return render_template("index.html", comments=session["comments"].split(";"), matrix=[[rowop.F(1) if (i == j) else rowop.F(0) for i in range(int(session["n"]))] for j in range(int(session["m"]))]) # if request is not GET, we know it's post try: comments = session["comments"].split(";") row1 = int(request.form["row1"]) row2 = int(request.form["row2"]) multiplier = rowop.F(request.form["multiplier"]) operation = request.form["op"] tmpmatrix = create_matrix(request.form) new_mat = rowop.print_row_op(tmpmatrix, row1, row2, rowop.F(multiplier), operation) comments.insert(0, "Here is your latex:") comments.insert(1, str(new_mat[1])) session["comments"] += ";".join(comments) return render_template("index.html", comments=comments, matrix=new_mat[0]) except: comments.append("Exception thrown") comments.append(traceback.format_exc()) return redirect(url_for('index'))
def reset(): session["comments"] = "" #session["matrix"] = "" return render_template("index.html", comments=session["comments"].split(";"), matrix=[[rowop.F(1) if (i == j) else rowop.F(0) for i in range(int(session["n"]))] for j in range(int(session["m"]))])
def create_matrix(form): """Creates a matrix from form input""" # Find the matrix entries keys = sorted(filter(lambda x: x[0] == "a", form.keys())) m_rows = int(keys[-1][1]) n_cols = int(keys[-1][2]) matrix = [[rowop.F(0) for xi in range(n_cols)] for x in range(m_rows)] for key in keys: val = form[key] matrix[int(key[1])-1][int(key[2])-1] = rowop.F(val) return matrix
def resize(): try: session["m"] = request.form["M"] session["n"] = request.form["N"] matrix = [[rowop.F(1) if (i == j) else rowop.F(0) for i in range(int(session["n"]))] for j in range(int(session["m"]))] return render_template("index.html", comments=[], matrix=matrix) except: pass return redirect(url_for('index'))
def test_four(self): string_multiplier = rowop.frac_to_latex(rowop.F(-1, 17)) row1 = 1 row2 = 2 operation = "+" expected = "\\frac{-1}{17}\\mathbf{r}_1 + \\mathbf{r}_2 \\to \\mathbf{r}_2" actual = rowop.create_op_string(string_multiplier, row1, row2, operation) self.failUnless(actual == expected)
def test_eight(self): string_multiplier = rowop.frac_to_latex(rowop.F(-1)) row1 = 1 # row2 is irrelevant here row2 = -1 operation = "scale" expected = "-\\mathbf{r}_1 \\to \\mathbf{r}_1" actual = rowop.create_op_string(string_multiplier, row1, row2, operation) self.failUnless(actual == expected)
def test_three(self): expected = "\\frac{-2}{3}" actual = rowop.frac_to_latex(rowop.F(-2, 3)) self.failUnless(actual == expected)
def test_two(self): expected = "1" actual = rowop.frac_to_latex(rowop.F(2, 2)) self.failUnless(actual == expected)
def test_two(self): a = [[1]] expected = [[rowop.F(1, 7)]] actual = rowop.scale_row(a, 1, rowop.F(1, 7)) self.failUnless(actual == expected)
def test_three(self): a = [[1, -1, rowop.F(-1, 2)], [1, 1, 1]] expected = [[0, -2, rowop.F(-3, 2)], [1, 1, 1]] actual = rowop.subtract_row(a, 2, 1, 1) self.failUnless(actual == expected)
def test_four(self): a = [[rowop.F(1, 2) for xi in range(10)] for x in range(2)] expected = [[1 for xi in range(10)], [rowop.F(1, 2) for x in range(10)]] actual = rowop.add_row(a, 2, 1, 1) self.failUnless(actual == expected)
def test_three(self): a = [[rowop.F(-1) for xi in range(3)] for x in range(3)] expected = [[-2, -2, -2], [-1, -1, -1], [-1, -1, -1]] actual = rowop.add_row(a, 2, 1, 1) self.failUnless(actual == expected)
def test_four(self): expected = "-17" actual = rowop.frac_to_latex(rowop.F(-17, 1)) self.failUnless(actual == expected)