# HG changeset patch
# User Rob Beezer <beezer@ups.edu>
# Date 1296704419 28800
# Node ID 5d6bd108216664f20cb07fdd2bcfe3216230654b
# Parent 4a44492ec4cb5686487b559553b980c904bc98c1
10714: columns of sparse matrices with no columns
diff r 4a44492ec4cb r 5d6bd1082166 sage/matrix/matrix1.pyx
a

b


726  726  
727  727  
728  728  def sparse_columns(self, copy=True): 
729   """ 
730   Return list of the sparse columns of self. 
 729  r""" 
 730  Return a list of the columns of ``self`` as sparse vectors (or free module elements). 
731  731  
732  732  INPUT: 
733  733  
… 
… 

743  743  [(0, 3), (1, 4), (2, 5)] 
744  744  sage: v[1].is_sparse() 
745  745  True 
 746  
 747  TESTS: 
 748  
 749  Columns of sparse matrices having no columns were fixed on Trac #10714. :: 
 750  
 751  sage: m = matrix(10, 0, sparse=True) 
 752  sage: m.ncols() 
 753  0 
 754  sage: m.columns() 
 755  [] 
746  756  """ 
747  757  x = self.fetch('sparse_columns') 
748  758  if not x is None: 
749  759  if copy: return list(x) 
750  760  return x 
751  761  
752   F = sage.modules.free_module.FreeModule(self._base_ring, self._nrows, sparse=True) 
 762  cdef Py_ssize_t i, j 
 763  C = [] 
 764  if self._ncols > 0: 
 765  F = sage.modules.free_module.FreeModule(self._base_ring, self._nrows, sparse=True) 
753  766  
754   C = [] 
755   k = 0 
756   entries = {} 
757   cdef Py_ssize_t i, j 
 767  k = 0 
 768  entries = {} 
 769  for i, j in self.nonzero_positions(copy=False, column_order=True): 
 770  if j > k: 
 771  # new column  emit vector 
 772  while len(C) < k: 
 773  C.append(F(0)) 
 774  C.append(F(entries, coerce=False, copy=False, check=False)) 
 775  entries = {} 
 776  k = j 
 777  entries[i] = self.get_unsafe(i, j) 
758  778  
759   for i, j in self.nonzero_positions(copy=False, column_order=True): 
760   if j > k: 
761   # new column  emit vector 
762   while len(C) < k: 
763   C.append(F(0)) 
764   C.append(F(entries, coerce=False, copy=False, check=False)) 
765   entries = {} 
766   k = j 
767   entries[i] = self.get_unsafe(i, j) 
 779  # finish up 
 780  while len(C) < k: 
 781  C.append(F(0)) 
 782  C.append(F(entries, coerce=False, copy=False, check=False)) 
 783  while len(C) < self._ncols: 
 784  C.append(F(0)) 
768  785  
769   # finish up 
770   while len(C) < k: 
771   C.append(F(0)) 
772   C.append(F(entries, coerce=False, copy=False, check=False)) 
773   while len(C) < self._ncols: 
774   C.append(F(0)) 
775   
776   # cache result 
 786  # cache and return result 
777  787  self.cache('sparse_columns', C) 
778  788  if copy: 
779  789  return list(C) 
… 
… 

782  792  
783  793  def sparse_rows(self, copy=True): 
784  794  r""" 
785   Return list of the rows of ``self`` as vectors (or free module elements). 
 795  Return a list of the rows of ``self`` as sparse vectors (or free module elements). 
786  796  
787  797  INPUT: 
788  798  
… 
… 

804  814  sage: m[0,0] = 10 
805  815  sage: m.sparse_rows() 
806  816  [(10, 1, 2), (3, 4, 5), (6, 7, 8)] 
807   
808   TESTS: 
809   
 817  
 818  TESTS: 
 819  
810  820  Rows of sparse matrices having no rows were fixed on Trac #10714. :: 
811   
 821  
812  822  sage: m = matrix(0, 10, sparse=True) 
813  823  sage: m.nrows() 
814  824  0 
… 
… 

819  829  if not x is None: 
820  830  if copy: return list(x) 
821  831  return x 
822   
 832  
823  833  cdef Py_ssize_t i, j 
824  834  R = [] 
825  835  if self._nrows > 0: 