def __mul__(self, other): """ Multiply a sparse matrix by another sparse matrix >>> L1 = PysparseMatrix(size = 3) >>> L1.put([3.,10.,numpy.pi,2.5], [0,0,1,2], [2,1,1,0]) >>> L2 = PysparseMatrix(size = 3) >>> L2.put(numpy.ones(3), numpy.arange(3), numpy.arange(3)) >>> L2.put([4.38,12357.2,1.1], [2,1,0], [1,0,2]) >>> tmp = numpy.array(((1.23572000e+05, 2.31400000e+01, 3.00000000e+00), ... (3.88212887e+04, 3.14159265e+00, 0.00000000e+00), ... (2.50000000e+00, 0.00000000e+00, 2.75000000e+00))) >>> numpy.allclose((L1 * L2).getNumpyArray(), tmp) 1 or a sparse matrix by a vector >>> tmp = numpy.array((29., 6.28318531, 2.5)) >>> numpy.allclose(L1 * numpy.array((1,2,3),'d'), tmp) 1 or a vector by a sparse matrix >>> tmp = numpy.array((7.5, 16.28318531, 3.)) >>> numpy.allclose(numpy.array((1,2,3),'d') * L1, tmp) ## The multiplication is broken. Numpy is calling __rmul__ for every element instead of with the whole array. 1 """ M, N = self.getShape() if isinstance(other, PysparseMatrix): if N != other.getShape()[0]: raise TypeError, 'Matrices dimensions do not match for product' p = spmatrix.matrixmultiply(self.matrix, other.getMatrix()) return PysparseMatrix(matrix=p) else: shape = numpy.shape(other) if shape == (): # other is a scalar p = self.matrix.copy() p.scale(other) return PysparseMatrix(matrix=p) elif shape == (N, ): y = numpy.empty(M) self.matrix.matvec(other, y) return y else: raise TypeError, 'Cannot multiply objects'
def __mul__(self, other): """ Multiply a sparse matrix by another sparse matrix >>> L1 = PysparseMatrix(size = 3) >>> L1.put([3.,10.,numpy.pi,2.5], [0,0,1,2], [2,1,1,0]) >>> L2 = PysparseMatrix(size = 3) >>> L2.put(numpy.ones(3), numpy.arange(3), numpy.arange(3)) >>> L2.put([4.38,12357.2,1.1], [2,1,0], [1,0,2]) >>> tmp = numpy.array(((1.23572000e+05, 2.31400000e+01, 3.00000000e+00), ... (3.88212887e+04, 3.14159265e+00, 0.00000000e+00), ... (2.50000000e+00, 0.00000000e+00, 2.75000000e+00))) >>> numpy.allclose((L1 * L2).getNumpyArray(), tmp) 1 or a sparse matrix by a vector >>> tmp = numpy.array((29., 6.28318531, 2.5)) >>> numpy.allclose(L1 * numpy.array((1,2,3),'d'), tmp) 1 or a vector by a sparse matrix >>> tmp = numpy.array((7.5, 16.28318531, 3.)) >>> numpy.allclose(numpy.array((1,2,3),'d') * L1, tmp) ## The multiplication is broken. Numpy is calling __rmul__ for every element instead of with the whole array. 1 """ M, N = self.getShape() if isinstance(other, PysparseMatrix): if N != other.getShape()[0]: raise TypeError, 'Matrices dimensions do not match for product' p = spmatrix.matrixmultiply(self.matrix, other.getMatrix()) return PysparseMatrix(matrix=p) else: shape = numpy.shape(other) if shape == (): # other is a scalar p = self.matrix.copy() p.scale(other) return PysparseMatrix(matrix=p) elif shape == (N,): y = numpy.empty(M) self.matrix.matvec(other, y) return y else: raise TypeError, 'Cannot multiply objects'
def testRandomMat(self): eps = 2.2204460492503131e-16 n = 30 m = 60 k = 30 for i in range(100): A = spmatrix_util.ll_mat_rand(n, k, 0.9) B = spmatrix_util.ll_mat_rand(k, m, 0.4) C = spmatrix.matrixmultiply(A, B) t = numpy.zeros(k, "d") y1 = numpy.zeros(n, "d") y2 = numpy.zeros(n, "d") for s in range(10): x = RandomArray.random((m,)) C.matvec(x, y1) B.matvec(x, t) A.matvec(t, y2) self.failUnless(math.sqrt(numpy.dot(y1 - y2, y1 - y2)) < eps * n * m * k)
def testRandomMat(self): eps = 2.2204460492503131E-16 n = 30 m = 60 k = 30 for i in range(100): A = spmatrix_util.ll_mat_rand(n, k, 0.9) B = spmatrix_util.ll_mat_rand(k, m, 0.4) C = spmatrix.matrixmultiply(A, B) t = numpy.zeros(k, 'd') y1 = numpy.zeros(n, 'd') y2 = numpy.zeros(n, 'd') for s in range(10): x = RandomArray.random((m, )) C.matvec(x, y1) B.matvec(x, t) A.matvec(t, y2) self.failUnless( math.sqrt(numpy.dot(y1 - y2, y1 - y2)) < eps * n * m * k)
T[4:8,1:3] = As[4:8,1:3] printMatrix(T) print 'this should raise execptions...\n' try: T[6:9,6:9] = A[6:9,6:9] except: traceback.print_exc() try: T[5:9, 4:10] = A[5:9, 4:10] except: traceback.print_exc() print 'Matrix multiplications' printMatrix(spmatrix.matrixmultiply(I, A)) printMatrix(spmatrix.matrixmultiply(Is, A)) printMatrix(spmatrix.matrixmultiply(O, O)) printMatrix(spmatrix.matrixmultiply(Os, O)) print 'Dot product' printMatrix(spmatrix.dot(I, A)) print 'Matrix export' A[:4,:4].export_mtx('A.mtx', 3) As[:4,:4].export_mtx('As.mtx', 3) print open('A.mtx').read() print open('As.mtx').read()
import time from pysparse.sparse import spmatrix n = 1000000 # create 2 nxn tridiag matrices A = spmatrix.ll_mat(n, n) B = spmatrix.ll_mat(n, n) for i in xrange(n): A[i,i] = i B[i,i] = i if i > 0: A[i,i-1] = 1 B[i,i-1] = 1 if i < n-1: A[i,i+1] = 1 B[i,i-1] = 1 t1 = time.clock() C = spmatrix.matrixmultiply(A, B) t_mult = time.clock() - t1 print 'time for multiplying %dx%d matrices: %.2f sec' % (n, n, t_mult) print C[:10,:10]
import time from pysparse.sparse import spmatrix n = 1000000 # create 2 nxn tridiag matrices A = spmatrix.ll_mat(n, n) B = spmatrix.ll_mat(n, n) for i in xrange(n): A[i, i] = i B[i, i] = i if i > 0: A[i, i - 1] = 1 B[i, i - 1] = 1 if i < n - 1: A[i, i + 1] = 1 B[i, i - 1] = 1 t1 = time.clock() C = spmatrix.matrixmultiply(A, B) t_mult = time.clock() - t1 print 'time for multiplying %dx%d matrices: %.2f sec' % (n, n, t_mult) print C[:10, :10]