[Scipy-svn] r3719 - in trunk/scipy/sparse: . tests
scipy-svn at scipy.org
scipy-svn at scipy.org
Tue Dec 25 18:44:01 EST 2007
Author: wnbell
Date: 2007-12-25 17:43:46 -0600 (Tue, 25 Dec 2007)
New Revision: 3719
Modified:
trunk/scipy/sparse/block.py
trunk/scipy/sparse/bsr.py
trunk/scipy/sparse/compressed.py
trunk/scipy/sparse/coo.py
trunk/scipy/sparse/tests/test_base.py
trunk/scipy/sparse/tests/test_sparse.py
Log:
added bsr_matrix docstring
additional unittests
Modified: trunk/scipy/sparse/block.py
===================================================================
--- trunk/scipy/sparse/block.py 2007-12-25 22:41:38 UTC (rev 3718)
+++ trunk/scipy/sparse/block.py 2007-12-25 23:43:46 UTC (rev 3719)
@@ -45,9 +45,13 @@
raise ValueError, 'shape must be multiple of blocksize'
self.indptr = zeros(self._swap((M/X,N/Y))[0] + 1, dtype=intc )
+
+ elif len(arg1) == 2:
+ # (data,(row,col)) format
+ self._set_self( coo_matrix(arg1).tobsr(blocksize=blocksize) )
elif len(arg1) == 3:
- # data,indices,indptr format
+ # (data,indices,indptr) format
(data, indices, indptr) = arg1
self.indices = array(indices, copy=copy)
self.indptr = array(indptr, copy=copy)
Modified: trunk/scipy/sparse/bsr.py
===================================================================
--- trunk/scipy/sparse/bsr.py 2007-12-25 22:41:38 UTC (rev 3718)
+++ trunk/scipy/sparse/bsr.py 2007-12-25 23:43:46 UTC (rev 3719)
@@ -12,8 +12,78 @@
from sputils import isdense, upcast, isscalarlike
class bsr_matrix(_block_matrix):
- #TODO add docstring
+ """Block Sparse Row matrix
+ This can be instantiated in several ways:
+ - bsr_matrix(D, [blocksize=(R,C)])
+ with a dense matrix or rank-2 ndarray D
+
+ - bsr_matrix(S, [blocksize=(R,C)])
+ with another sparse matrix S (equivalent to S.tocsr())
+
+ - bsr_matrix((M, N), [blocksize=(R,C), dtype])
+ to construct an empty matrix with shape (M, N)
+ dtype is optional, defaulting to dtype='d'.
+
+ - bsr_matrix((data, ij), [blocksize=(R,C), shape=(M, N)])
+ where data, ij satisfy:
+ a[ij[0, k], ij[1, k]] = data[k]
+
+ - bsr_matrix((data, indices, indptr), [shape=(M, N)])
+ is the standard BSR representation where:
+ the block column indices for row i are stored in
+ indices[ indptr[i]: indices[i+1] ]
+ and their corresponding block values are stored in
+ data[ indptr[i]: indptr[i+1] ]
+ If the shape parameter is not supplied, the matrix dimensions
+ are inferred from the index arrays.
+
+
+ *Notes*
+ -------
+ The blocksize (R,C) must evenly divide the shape of
+ the matrix (M,N). That is, R and C must satisfy the
+ relationship M % R = 0 and N % C = 0.
+
+ The Block Compressed Row (BSR) format is very similar to the
+ Compressed Sparse Row (CSR) format. BSR is appropriate for
+ sparse matrices with dense sub matrices like the last example
+ below. Such matrices often arise, for instance, in finite
+ element discretizations.
+
+
+ *Examples*
+ ----------
+
+ >>> from scipy.sparse import *
+ >>> from scipy import *
+ >>> bsr_matrix( (3,4), dtype='i' ).todense()
+ matrix([[0, 0, 0, 0],
+ [0, 0, 0, 0],
+ [0, 0, 0, 0]])
+
+ >>> row = array([0,0,1,2,2,2])
+ >>> col = array([0,2,2,0,1,2])
+ >>> data = kron([1,2,3,4,5,6])
+ >>> bsr_matrix( (data,(row,col)), shape=(3,3) ).todense()
+ matrix([[1, 0, 2],
+ [0, 0, 3],
+ [4, 5, 6]])
+
+ >>> indptr = array([0,2,3,6])
+ >>> indices = array([0,2,2,0,1,2])
+ >>> data = array([1,2,3,4,5,6]).repeat(4).reshape(6,2,2)
+ >>> bsr_matrix( (data,indices,indptr), shape=(6,6) ).todense()
+ matrix([[1, 1, 0, 0, 2, 2],
+ [1, 1, 0, 0, 2, 2],
+ [0, 0, 0, 0, 3, 3],
+ [0, 0, 0, 0, 3, 3],
+ [4, 4, 5, 5, 6, 6],
+ [4, 4, 5, 5, 6, 6]])
+
+ """
+
+
def __mul__(self, other): # self * other
""" Scalar, vector, or matrix multiplication
"""
Modified: trunk/scipy/sparse/compressed.py
===================================================================
--- trunk/scipy/sparse/compressed.py 2007-12-25 22:41:38 UTC (rev 3718)
+++ trunk/scipy/sparse/compressed.py 2007-12-25 23:43:46 UTC (rev 3719)
@@ -456,6 +456,8 @@
The is an *in place* operation
"""
+ self.sort_indices()
+
fn = sparsetools.csr_sum_duplicates
M,N = self._swap(self.shape)
fn( M, N, self.indptr, self.indices, self.data)
Modified: trunk/scipy/sparse/coo.py
===================================================================
--- trunk/scipy/sparse/coo.py 2007-12-25 22:41:38 UTC (rev 3718)
+++ trunk/scipy/sparse/coo.py 2007-12-25 23:43:46 UTC (rev 3719)
@@ -232,7 +232,6 @@
indptr, indices, data)
A = csc_matrix((data, indices, indptr), self.shape)
- A.sort_indices()
A.sum_duplicates()
return A
Modified: trunk/scipy/sparse/tests/test_base.py
===================================================================
--- trunk/scipy/sparse/tests/test_base.py 2007-12-25 22:41:38 UTC (rev 3718)
+++ trunk/scipy/sparse/tests/test_base.py 2007-12-25 23:43:46 UTC (rev 3719)
@@ -27,7 +27,6 @@
restore_path()
-#TODO test spmatrix(DENSE) and spmatrix(SPARSE) for all combos
#TODO test spmatrix( [[1,2],[3,4]] ) format
#TODO check that invalid shape in constructor raises exception
#TODO check that spmatrix( ... , copy=X ) is respected
@@ -310,12 +309,17 @@
for format in ['bsr','coo','csc','csr','dia','dok','lil']:
a = A.asformat(format)
assert_equal(a.format,format)
- assert_array_almost_equal(a.todense(), D)
+ assert_array_equal(a.todense(), D)
b = self.spmatrix(D+3j).asformat(format)
assert_equal(b.format,format)
- assert_array_almost_equal(b.todense(), D+3j)
+ assert_array_equal(b.todense(), D+3j)
+ c = eval(format + '_matrix')(A)
+ assert_equal(c.format,format)
+ assert_array_equal(c.todense(), D)
+
+
def check_todia(self):
#TODO, add and test .todia(maxdiags)
@@ -339,31 +343,9 @@
b = self.dat.transpose()
assert_array_equal(a.todense(), b)
assert_array_equal(a.transpose().todense(), self.dat)
+
+ assert_array_equal( self.spmatrix((3,4)).T.todense(), zeros((4,3)) )
- def check_large(self):
- # Create a 100x100 matrix with 100 non-zero elements
- # and play around with it
- #TODO move this out of Common since it doesn't use spmatrix
- random.seed(0)
- A = dok_matrix((100,100))
- for k in range(100):
- i = random.randrange(100)
- j = random.randrange(100)
- A[i,j] = 1.
- csr = A.tocsr()
- csc = A.tocsc()
- csc2 = csr.tocsc()
- coo = A.tocoo()
- csr2 = coo.tocsr()
- assert_array_equal(A.transpose().todense(), csr.transpose().todense())
- assert_array_equal(csc.todense(), csr.todense())
- assert_array_equal(csr.todense(), csr2.todense())
- assert_array_equal(csr2.todense().transpose(), coo.transpose().todense())
- assert_array_equal(csr2.todense(), csc2.todense())
- csr_plus_csc = csr + csc
- csc_plus_csr = csc + csr
- assert_array_equal(csr_plus_csc.todense(), (2*A).todense())
- assert_array_equal(csr_plus_csc.todense(), csc_plus_csr.todense())
def check_add_dense(self):
""" Check whether adding a dense matrix to a sparse matrix works
@@ -1226,8 +1208,10 @@
def check_constructor1(self):
pass
#TODO add test
+
-class TestBSR(_TestCommon, _TestArithmetic, NumpyTestCase):
+class TestBSR(_TestCommon, _TestArithmetic, _TestInplaceArithmetic,
+ _TestMatvecOutput, NumpyTestCase):
spmatrix = bsr_matrix
def check_constructor1(self):
@@ -1277,16 +1261,7 @@
A = kron( [[1,0,2,0],[0,1,0,0],[0,0,0,0]], [[0,1,2],[3,0,5]] )
assert_equal(bsr_matrix(A,blocksize=(2,3)).todense(),A)
-
-
-
-#class TestBSC(_TestCommon, _TestArithmetic, NumpyTestCase):
-# spmatrix = bsc_matrix
-#
-# def check_constructor1(self):
-# pass
-# #TODO add test
if __name__ == "__main__":
NumpyTest().run()
Modified: trunk/scipy/sparse/tests/test_sparse.py
===================================================================
--- trunk/scipy/sparse/tests/test_sparse.py 2007-12-25 22:41:38 UTC (rev 3718)
+++ trunk/scipy/sparse/tests/test_sparse.py 2007-12-25 23:43:46 UTC (rev 3719)
@@ -252,7 +252,33 @@
print output
-
+class TestLarge(NumpyTestCase):
+ def check_large(self):
+ # Create a 100x100 matrix with 100 non-zero elements
+ # and play around with it
+ #TODO move this out of Common since it doesn't use spmatrix
+ random.seed(0)
+ A = dok_matrix((100,100))
+ for k in range(100):
+ i = random.randrange(100)
+ j = random.randrange(100)
+ A[i,j] = 1.
+ csr = A.tocsr()
+ csc = A.tocsc()
+ csc2 = csr.tocsc()
+ coo = A.tocoo()
+ csr2 = coo.tocsr()
+ assert_array_equal(A.transpose().todense(), csr.transpose().todense())
+ assert_array_equal(csc.todense(), csr.todense())
+ assert_array_equal(csr.todense(), csr2.todense())
+ assert_array_equal(csr2.todense().transpose(), coo.transpose().todense())
+ assert_array_equal(csr2.todense(), csc2.todense())
+ csr_plus_csc = csr + csc
+ csc_plus_csr = csc + csr
+ assert_array_equal(csr_plus_csc.todense(), (2*A).todense())
+ assert_array_equal(csr_plus_csc.todense(), csc_plus_csr.todense())
+
+
if __name__ == "__main__":
NumpyTest().run()
More information about the Scipy-svn
mailing list