## pyx-checkins

 [PyX-checkins] pyx/test/experimental solve.py,1.6,1.7 test_solve.py,1.2,1.3 From: André Wobst - 2004-08-01 20:30:45 ```Update of /cvsroot/pyx/pyx/test/experimental In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23575 Modified Files: solve.py test_solve.py Log Message: matrix class Index: solve.py =================================================================== RCS file: /cvsroot/pyx/pyx/test/experimental/solve.py,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** solve.py 1 Aug 2004 15:12:37 -0000 1.6 --- solve.py 1 Aug 2004 20:30:34 -0000 1.7 *************** *** 25,28 **** --- 25,43 ---- + def sum(list): + # we can assume len(list) != 0 here (and do not start from the scalar 0) + sum = list[0] + for item in list[1:]: + sum += item + return sum + + def product(list): + # we can assume len(list) != 0 here (and do not start from the scalar 1) + product = list[0] + for item in list[1:]: + product *= item + return product + + class scalar: # represents a scalar variable or constant *************** *** 138,145 **** def __float__(self): ! product = float(self._scalars[0]) ! for scalar in self._scalars[1:]: ! product *= float(scalar) ! return product def is_linear(self): --- 153,157 ---- def __float__(self): ! return product([float(scalar) for scalar in self._scalars]) def is_linear(self): *************** *** 197,204 **** def __mul__(self, other): ! sum = self._addends[0]*other ! for addend in self._addends[1:]: ! sum += addend*other ! return sum __rmul__ = __mul__ --- 209,213 ---- def __mul__(self, other): ! return sum([addend*other for addend in self._addends]) __rmul__ = __mul__ *************** *** 208,215 **** def __float__(self): ! sum = float(self._addends[0]) ! for addend in self._addends[1:]: ! sum += float(addend) ! return sum def is_linear(self): --- 217,221 ---- def __float__(self): ! return sum([float(addend) for addend in self._addends]) def is_linear(self): *************** *** 295,307 **** other = other.vector() except (TypeError, AttributeError): ! return vector([item*other for item in self._items]) else: # scalar product if len(self) != len(other): raise RuntimeError("vector length mismatch in scalar product") ! sum = self._items[0]*other._items[0] ! for selfitem, otheritem in zip(self._items[1:], other._items[1:]): ! sum += selfitem*otheritem ! return sum __rmul__ = __mul__ --- 301,316 ---- other = other.vector() except (TypeError, AttributeError): ! try: ! polynom = other.polynom() ! except (TypeError, AttributeError): ! # inverse matrix multiplication ! return other.__rmul__(self) ! else: ! return vector([item*other for item in self._items]) else: # scalar product if len(self) != len(other): raise RuntimeError("vector length mismatch in scalar product") ! return sum([selfitem*otheritem for selfitem, otheritem in zip(self._items, other._items)]) __rmul__ = __mul__ *************** *** 318,321 **** --- 327,442 ---- + class matrix: + # represents a matrix, i.e. a 2d list of terms + + def __init__(self, dimensions_or_values, name="unnamed_matrix"): + try: + name + "" + except TypeError: + raise RuntimeError("a matrix name should be a string (you probably wanted to write matrix([x, y]) instead of matrix(x, y))") + try: + for row in dimensions_or_values: + for col in row: + pass + except: + # dimension + self._numberofrows, self._numberofcols = [int(x) for x in dimensions_or_values] + self._rows = [[scalar(name="%s[%i, %i]" % (name, row, col)) + for col in range(self._numberofcols)] + for row in range(self._numberofrows)] + else: + # values + self._rows = [] + self._numberofcols = None + for row in dimensions_or_values: + _cols = [] + for col in row: + try: + col.polynom() + except (TypeError, AttributeError): + _cols.append(scalar(value=col, name="%s[%i, %i]" % (name, len(self._rows), len(_cols)))) + else: + _cols.append(col) + self._rows.append(_cols) + if self._numberofcols is None: + self._numberofcols = len(_cols) + elif self._numberofcols != len(_cols): + raise RuntimeError("column length mismatch") + self._numberofrows = len(self._rows) + if not self._numberofrows or not self._numberofcols: + raise RuntimeError("empty matrix not allowed") + self.name = name + + def __getitem__(self, (row, col)): + return self._rows[row][col] + + def matrix(self): + return self + + def __neg__(self): + return matrix([[-col for col in row] for row in self._rows]) + + def __add__(self, other): + other = other.matrix() + if self._numberofrows != other._numberofrows or self._numberofcols != other._numberofcols: + raise RuntimeError("matrix geometry mismatch in add") + return matrix([[selfcol + othercol + for selfcol, othercol in zip(selfrow, otherrow)] + for selfrow, otherrow in zip(self._rows, other._rows)]) + + __radd__ = __add__ + + def __sub__(self, other): + return -other + self + + def __rsub__(self, other): + return -self + other + + def __mul__(self, other): + try: + other = other.matrix() + except (TypeError, AttributeError): + try: + other = other.vector() + except (TypeError, AttributeError): + return matrix([[col*other for col in row] for row in self._rows]) + else: + if self._numberofcols != len(other): + raise RuntimeError("size mismatch in matrix vector product") + return vector([sum([col*otheritem + for col, otheritem in zip(row, other)]) + for row in self._rows]) + else: + if self._numberofcols != other._numberofrows: + raise RuntimeError("size mismatch in matrix product") + return matrix([[sum([self._rows[row][i]*other._rows[i][col] + for i in range(self._numberofcols)]) + for col in range(other._numberofcols)] + for row in range(self._numberofrows)]) + + def __rmul__(self, other): + try: + other = other.vector() + except (TypeError, AttributeError): + return matrix([[other*col for col in row] for row in self._rows]) + else: + if self._numberofrows != len(other): + raise RuntimeError("size mismatch in matrix vector product") + return vector([sum([other[i]*self._rows[i][col] + for i in range(self._numberofrows)]) + for col in range(self._numberofcols)]) + + def __div__(self, other): + return matrix([[col/other for col in row] for row in self._rows]) + + def __str__(self): + return "%s{=(%s)}" % (self.name, ", ".join(["(" + ", ".join([str(col) for col in row]) + ")" for row in self._rows])) + + def solve(self, solver): + for row in self._rows: + for col in row: + solver.addequation(col) + + class Solver: # linear equation solver *************** *** 389,402 **** if __name__ == "__main__": ! x = vector(2, "x") ! y = vector(2, "y") ! z = vector(2, "z") - solver.eq(4*x + y, 2*x - y + vector([4, 0])) # => x + y = (2, 0) - solver.eq(x[0] - y[0], z[1]) - solver.eq(x[1] - y[1], z[0]) - solver.eq(vector([5, 0]), z) - print x - print y - print z --- 510,539 ---- if __name__ == "__main__": ! # x = vector(2, "x") ! # y = vector(2, "y") ! # z = vector(2, "z") ! ! # solver.eq(4*x + y, 2*x - y + vector([4, 0])) # => x + y = (2, 0) ! # solver.eq(x[0] - y[0], z[1]) ! # solver.eq(x[1] - y[1], z[0]) ! # solver.eq(vector([5, 0]), z) ! ! # print x ! # print y ! # print z ! ! A = matrix([2, 2]) ! solver.eq(vector([1, 1]), A * vector([0, 1])) ! solver.eq(vector([0, 2]), A * vector([1, 0])) ! ! I = matrix([2, 2]) ! solver.eq(vector([0, 1]), I * vector([0, 1])) ! solver.eq(vector([1, 0]), I * vector([1, 0])) ! ! B = matrix([2, 2]) ! solver.eq(I, A*B) ! ! print A ! print B Index: test_solve.py =================================================================== RCS file: /cvsroot/pyx/pyx/test/experimental/test_solve.py,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** test_solve.py 1 Aug 2004 15:12:37 -0000 1.2 --- test_solve.py 1 Aug 2004 20:30:34 -0000 1.3 *************** *** 1,5 **** import unittest, operator ! from solve import scalar, vector, solver class ScalarTestCase(unittest.TestCase): --- 1,5 ---- import unittest, operator ! from solve import scalar, vector, matrix, solver class ScalarTestCase(unittest.TestCase): *************** *** 149,156 **** def testMath(self): self.failUnlessEqual(str(-vector(2, "a")), "unnamed_vector{=(unnamed_scalar{=-1.0} * a[0], unnamed_scalar{=-1.0} * a[1])}") ! self.failUnlessEqual(str(vector(2, "a") + vector(2, "t")), "unnamed_vector{=(a[0] + t[0], a[1] + t[1])}") self.failUnlessRaises(AttributeError, operator.__add__, vector(2), scalar()) self.failUnlessRaises(RuntimeError, operator.__add__, vector(2), vector(3)) ! self.failUnlessEqual(str(vector(2, "a") - vector(2, "t")), "unnamed_vector{=(unnamed_scalar{=-1.0} * t[0] + a[0], unnamed_scalar{=-1.0} * t[1] + a[1])}") self.failUnlessRaises(RuntimeError, operator.__sub__, vector(2), scalar()) self.failUnlessRaises(RuntimeError, operator.__sub__, vector(2), vector(3)) --- 149,156 ---- def testMath(self): self.failUnlessEqual(str(-vector(2, "a")), "unnamed_vector{=(unnamed_scalar{=-1.0} * a[0], unnamed_scalar{=-1.0} * a[1])}") ! self.failUnlessEqual(str(vector(2, "a") + vector(2, "b")), "unnamed_vector{=(a[0] + b[0], a[1] + b[1])}") self.failUnlessRaises(AttributeError, operator.__add__, vector(2), scalar()) self.failUnlessRaises(RuntimeError, operator.__add__, vector(2), vector(3)) ! self.failUnlessEqual(str(vector(2, "a") - vector(2, "b")), "unnamed_vector{=(unnamed_scalar{=-1.0} * b[0] + a[0], unnamed_scalar{=-1.0} * b[1] + a[1])}") self.failUnlessRaises(RuntimeError, operator.__sub__, vector(2), scalar()) self.failUnlessRaises(RuntimeError, operator.__sub__, vector(2), vector(3)) *************** *** 158,161 **** --- 158,164 ---- self.failUnlessEqual(str(vector(2, "a") * 2), "unnamed_vector{=(a[0] * unnamed_scalar{=2.0}, a[1] * unnamed_scalar{=2.0})}") self.failUnlessEqual(str(scalar(name="s") * vector(2, "a")), "unnamed_vector{=(a[0] * s, a[1] * s)}") + self.failUnlessEqual(str(scalar(name="s") * (vector(2, "a") + vector(2, "b"))), "unnamed_vector{=(a[0] * s + b[0] * s, a[1] * s + b[1] * s)}") + self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) * vector(2, "a")), "unnamed_vector{=(a[0] * s + a[0] * t, a[1] * s + a[1] * t)}") + self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) * (vector(2, "a") + vector(2, "b"))), "unnamed_vector{=(a[0] * s + b[0] * s + a[0] * t + b[0] * t, a[1] * s + b[1] * s + a[1] * t + b[1] * t)}") self.failUnlessEqual(str(vector(2, "a") * scalar(name="s")), "unnamed_vector{=(a[0] * s, a[1] * s)}") self.failUnlessEqual(str(vector(2, "a") * vector(2, "b")), "a[0] * b[0] + a[1] * b[1]") *************** *** 169,172 **** --- 172,190 ---- + class MatrixTestCase(unittest.TestCase): + + 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}))}") + + def testMath(self): + self.failUnlessEqual(str(-matrix([2, 2], "A")), "unnamed_matrix{=((unnamed_scalar{=-1.0} * A[0, 0], unnamed_scalar{=-1.0} * A[0, 1]), (unnamed_scalar{=-1.0} * A[1, 0], unnamed_scalar{=-1.0} * A[1, 1]))}") + 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.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") * 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])}") + + if __name__ == "__main__": unittest.main() ```