def testLen(self): for rows in range(1, 5): for cols in range(1, 5): A = matrix((rows, cols)) self.failUnlessEqual(A.getnumberofrows(), rows) self.failUnlessEqual(A.getnumberofcols(), cols) self.failUnlessEqual(str(A), "unnamed_matrix{=(" + ", ".join(["(" + ", ".join(["unnamed_matrix[%i, %i]" % (i, j) for j in range(cols)]) + ")" for i in range(rows)]) + ")}")
def testMatrix(self): solver = Solver() A = matrix([2, 2]) solver.eq(A*vector([1, -1]), vector([2, 0])) solver.eq(A*vector([1, 1]), vector([0, 2])) self.failUnlessAlmostEqual(float(A[0, 0]), 1) self.failUnlessAlmostEqual(float(A[0, 1]), -1) self.failUnlessAlmostEqual(float(A[1, 0]), 1) self.failUnlessAlmostEqual(float(A[1, 1]), 1) B = matrix([2, 2]) solver.eq(A*B, identitymatrix(2)) self.failUnlessAlmostEqual(float(B[0, 0]), 0.5) self.failUnlessAlmostEqual(float(B[0, 1]), 0.5) self.failUnlessAlmostEqual(float(B[1, 0]), -0.5) self.failUnlessAlmostEqual(float(B[1, 1]), 0.5) self.failUnlessAlmostEqual(float((B * vector([2, 0]))[0]), 1) self.failUnlessAlmostEqual(float((B * vector([2, 0]))[1]), -1) self.failUnlessAlmostEqual(float((B * vector([0, 2]))[0]), 1) self.failUnlessAlmostEqual(float((B * vector([0, 2]))[1]), 1)
def testMath(self): self.failUnlessEqual(str(-trafo([2, 3], "A")), str(trafo((-matrix([2, 3], "A_matrix"), -vector(2, "A_vector"))))) self.failUnlessEqual(str(trafo([2, 3], "A") + trafo([2, 3], "B")), str(trafo((matrix([2, 3], "A_matrix") + matrix([2, 3], "B_matrix"), vector(2, "A_vector") + vector(2, "B_vector"))))) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, 1, trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), 1) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, scalar(), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, scalar() + scalar(), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), scalar() + scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(3), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), vector(3)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), matrix([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), trafo([2, 4])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), trafo([3, 3])) self.failUnlessEqual(str(trafo([2, 3], "A") - trafo([2, 3], "B")), str(trafo((matrix([2, 3], "A_matrix") - matrix([2, 3], "B_matrix"), vector(2, "A_vector") - vector(2, "B_vector"))))) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, 1, trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), 1) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, scalar(), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, scalar() + scalar(), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), scalar() + scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(3), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), vector(3)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), matrix([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), trafo([2, 4])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), trafo([3, 3])) self.failUnlessEqual(str(trafo([2, 3], "A") * trafo([3, 4], "B")), str(trafo((matrix([2, 3], "A_matrix") * matrix([3, 4], "B_matrix"), vector(2, "A_vector") + matrix([2, 3], "A_matrix") * vector(3, "B_vector"))))) self.failUnlessEqual(str(2 * trafo([2, 3], "A")), str(trafo((2 * matrix([2, 3], "A_matrix"), 2 * vector(2, "A_vector"))))) self.failUnlessEqual(str(trafo([2, 3], "A") * 2), str(trafo((matrix([2, 3], "A_matrix") * 2, vector(2, "A_vector") * 2)))) self.failUnlessEqual(str(scalar() * trafo([2, 3], "A")), str(trafo((scalar() * matrix([2, 3], "A_matrix"), scalar() * vector(2, "A_vector"))))) self.failUnlessEqual(str(trafo([2, 3], "A") * scalar()), str(trafo((matrix([2, 3], "A_matrix") * scalar(), vector(2, "A_vector") * scalar())))) self.failUnlessEqual(str((scalar() + scalar()) * trafo([2, 3], "A")), str(trafo(((scalar() + scalar()) * matrix([2, 3], "A_matrix"), (scalar() + scalar()) * vector(2, "A_vector"))))) self.failUnlessEqual(str(trafo([2, 3], "A") * (scalar() + scalar())), str(trafo((matrix([2, 3], "A_matrix") * (scalar() + scalar()), vector(2, "A_vector") * (scalar() + scalar()))))) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, vector(2), trafo([2, 3])) self.failUnlessEqual(str(trafo([2, 3], "A") * vector(3, "a")), str(matrix([2, 3], "A_matrix") * vector(3, "a") + vector(2, "A_vector"))) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, trafo([2, 3]), vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, matrix([3, 2]), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, trafo([2, 3]), matrix([3, 2])) self.failUnlessEqual(str(trafo([2, 3], "A") / 2.0), str(trafo((matrix([2, 3], "A_matrix") / 2.0, vector(2, "A_vector") / 2.0)))) self.failUnlessEqual(str(trafo([2, 3], "A") / 2), str(trafo((matrix([2, 3], "A_matrix") / 2, vector(2, "A_vector") / 2)))) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar(), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar() + scalar(), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), trafo([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), scalar() + scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), vector(1)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), matrix([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), trafo([2, 3]))
def testAccess(self): A = matrix([2, 3]) self.failUnlessEqual(str(A), "unnamed_matrix{=((unnamed_matrix[0, 0], unnamed_matrix[0, 1], unnamed_matrix[0, 2]), (unnamed_matrix[1, 0], unnamed_matrix[1, 1], unnamed_matrix[1, 2]))}") self.failUnlessEqual(A[0, 0].is_set(), 0) self.failUnlessEqual(A[1, 0].is_set(), 0) self.failUnlessRaises(IndexError, operator.__getitem__, A, (2, 0)) self.failUnlessEqual(A[0, 1].is_set(), 0) self.failUnlessEqual(A[1, 1].is_set(), 0) self.failUnlessEqual(A[0, 2].is_set(), 0) self.failUnlessEqual(A[1, 2].is_set(), 0) self.failUnlessRaises(IndexError, operator.__getitem__, A, (0, 3)) A[0, 0].set(1) self.failUnlessEqual(A[0, 0].is_set(), 1) self.failUnlessEqual(A[0, 1].is_set(), 0) self.failUnlessEqual(A[1, 0].is_set(), 0) self.failUnlessAlmostEqual(A[0, 0].get(), 1.0) self.failUnlessAlmostEqual(float(A[0, 0]), 1.0)
def testAccess(self): m = matrix([2, 3]) v = vector(2) t = trafo((m, v)) self.failUnlessEqual(t.getmatrix(), m) self.failUnlessEqual(t.getvector(), v)
def testInit(self): self.failUnlessEqual(str(trafo((2, 3))), "unnamed_trafo{=(matrix: unnamed_trafo_matrix{=((unnamed_trafo_matrix[0, 0], unnamed_trafo_matrix[0, 1], unnamed_trafo_matrix[0, 2]), (unnamed_trafo_matrix[1, 0], unnamed_trafo_matrix[1, 1], unnamed_trafo_matrix[1, 2]))}, vector: unnamed_trafo_vector{=(unnamed_trafo_vector[0], unnamed_trafo_vector[1])})}") self.failUnlessEqual(str(trafo((2, 3), "A")), "A{=(matrix: A_matrix{=((A_matrix[0, 0], A_matrix[0, 1], A_matrix[0, 2]), (A_matrix[1, 0], A_matrix[1, 1], A_matrix[1, 2]))}, vector: A_vector{=(A_vector[0], A_vector[1])})}") self.failUnlessEqual(str(trafo((matrix([2, 3], "B"), vector(2, "c")), "A")), "A{=(matrix: B{=((B[0, 0], B[0, 1], B[0, 2]), (B[1, 0], B[1, 1], B[1, 2]))}, vector: c{=(c[0], c[1])})}") self.failUnlessRaises((RuntimeError, AttributeError, TypeError), trafo, (matrix([2, 3]), vector(3)))
def testMath(self): self.failUnlessEqual(str(-matrix([2, 3], "A")), "unnamed_matrix{=((unnamed_scalar{=-1.0} * A[0, 0], unnamed_scalar{=-1.0} * A[0, 1], unnamed_scalar{=-1.0} * A[0, 2]), (unnamed_scalar{=-1.0} * A[1, 0], unnamed_scalar{=-1.0} * A[1, 1], unnamed_scalar{=-1.0} * A[1, 2]))}") self.failUnlessEqual(str(matrix([2, 3], "A") + matrix([2, 3], "B")), "unnamed_matrix{=((A[0, 0] + B[0, 0], A[0, 1] + B[0, 1], A[0, 2] + B[0, 2]), (A[1, 0] + B[1, 0], A[1, 1] + B[1, 1], A[1, 2] + B[1, 2]))}") self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), 1) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), scalar() + scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), vector(3)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), matrix([2, 4])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), matrix([3, 3])) self.failUnlessEqual(str(matrix([2, 3], "A") - matrix([2, 3], "B")), "unnamed_matrix{=((unnamed_scalar{=-1.0} * B[0, 0] + A[0, 0], unnamed_scalar{=-1.0} * B[0, 1] + A[0, 1], unnamed_scalar{=-1.0} * B[0, 2] + A[0, 2]), (unnamed_scalar{=-1.0} * B[1, 0] + A[1, 0], unnamed_scalar{=-1.0} * B[1, 1] + A[1, 1], unnamed_scalar{=-1.0} * B[1, 2] + A[1, 2]))}") self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), 1) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), scalar() + scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), vector(2)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), vector(3)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), matrix([2, 4])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), matrix([3, 3])) self.failUnlessEqual(str(2 * matrix([2, 3], "A")), "unnamed_matrix{=((A[0, 0] * unnamed_scalar{=2.0}, A[0, 1] * unnamed_scalar{=2.0}, A[0, 2] * unnamed_scalar{=2.0}), (A[1, 0] * unnamed_scalar{=2.0}, A[1, 1] * unnamed_scalar{=2.0}, A[1, 2] * unnamed_scalar{=2.0}))}") self.failUnlessEqual(str(matrix([2, 3], "A") * 2), "unnamed_matrix{=((A[0, 0] * unnamed_scalar{=2.0}, A[0, 1] * unnamed_scalar{=2.0}, A[0, 2] * unnamed_scalar{=2.0}), (A[1, 0] * unnamed_scalar{=2.0}, A[1, 1] * unnamed_scalar{=2.0}, A[1, 2] * unnamed_scalar{=2.0}))}") self.failUnlessEqual(str(matrix([2, 3], "A") * vector(3, "a")), "unnamed_vector{=(A[0, 0] * a[0] + A[0, 1] * a[1] + A[0, 2] * a[2], A[1, 0] * a[0] + A[1, 1] * a[1] + A[1, 2] * a[2])}") self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, vector(2, "a"), matrix([2, 3], "A")) self.failUnlessEqual(str(matrix([2, 3], "A") * matrix([3, 2], "B")), "unnamed_matrix{=((A[0, 0] * B[0, 0] + A[0, 1] * B[1, 0] + A[0, 2] * B[2, 0], A[0, 0] * B[0, 1] + A[0, 1] * B[1, 1] + A[0, 2] * B[2, 1]), (A[1, 0] * B[0, 0] + A[1, 1] * B[1, 0] + A[1, 2] * B[2, 0], A[1, 0] * B[0, 1] + A[1, 1] * B[1, 1] + A[1, 2] * B[2, 1]))}") self.failUnlessEqual(str(matrix([2, 3], "A") / 2.0), "unnamed_matrix{=((unnamed_scalar{=0.5} * A[0, 0], unnamed_scalar{=0.5} * A[0, 1], unnamed_scalar{=0.5} * A[0, 2]), (unnamed_scalar{=0.5} * A[1, 0], unnamed_scalar{=0.5} * A[1, 1], unnamed_scalar{=0.5} * A[1, 2]))}") self.failUnlessEqual(str(matrix([2, 3], "A") / 2), "unnamed_matrix{=((unnamed_scalar{=0.0} * A[0, 0], unnamed_scalar{=0.0} * A[0, 1], unnamed_scalar{=0.0} * A[0, 2]), (unnamed_scalar{=0.0} * A[1, 0], unnamed_scalar{=0.0} * A[1, 1], unnamed_scalar{=0.0} * A[1, 2]))}") self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar(), matrix([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar() + scalar(), matrix([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), matrix([2, 3])) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), scalar() + scalar()) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), vector(1)) self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), matrix([2, 3]))
def testInit(self): self.failUnlessEqual(str(matrix((2, 3))), "unnamed_matrix{=((unnamed_matrix[0, 0], unnamed_matrix[0, 1], unnamed_matrix[0, 2]), (unnamed_matrix[1, 0], unnamed_matrix[1, 1], unnamed_matrix[1, 2]))}") self.failUnlessEqual(str(matrix([[1, 2, 3], [4, 5, 6]])), "unnamed_matrix{=((unnamed_matrix[0, 0]{=1.0}, unnamed_matrix[0, 1]{=2.0}, unnamed_matrix[0, 2]{=3.0}), (unnamed_matrix[1, 0]{=4.0}, unnamed_matrix[1, 1]{=5.0}, unnamed_matrix[1, 2]{=6.0}))}") self.failUnlessEqual(str(matrix((2, 3), "a")), "a{=((a[0, 0], a[0, 1], a[0, 2]), (a[1, 0], a[1, 1], a[1, 2]))}") self.failUnlessEqual(str(matrix([[1, 2, 3], [4, 5, 6]], "a")), "a{=((a[0, 0]{=1.0}, a[0, 1]{=2.0}, a[0, 2]{=3.0}), (a[1, 0]{=4.0}, a[1, 1]{=5.0}, a[1, 2]{=6.0}))}") self.failUnlessRaises((RuntimeError, AttributeError, TypeError), matrix, [[1, 2, 3], [4, 5]])