Changeset 7839:637a8d65f88a


Ignore:
Timestamp:
12/14/07 03:11:55 (5 years ago)
Author:
Mike Hansen <mhansen@…>
Branch:
default
Message:

Fixed #1448 -- added better algorithm for when the base ring is infinite
but iterable.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sage/matrix/matrix_space.py

    r7838 r7839  
    8383import sage.modules.free_module_element 
    8484import sage.modules.free_module 
    85  
    8685from sage.structure.sequence import Sequence 
    8786 
     
    527526            [0 0 0] 
    528527 
    529             sage: MS = MatrixSpace(ZZ, 2) 
    530             sage: a = list(MS) 
    531             Traceback (most recent call last): 
    532             ... 
    533             NotImplementedError: object does not support iteration 
     528            sage: MS = MatrixSpace(ZZ, 2, 3) 
     529            sage: i = iter(MS) 
     530            sage: a = [ i.next() for _ in range(6) ] 
     531            sage: a[0] 
     532            [0 0 0] 
     533            [0 0 0] 
     534            sage: a[4] 
     535            [0 0 0] 
     536            [1 0 0] 
    534537 
    535538            sage: MS = MatrixSpace(RR, 2) 
     
    539542            NotImplementedError: object does not support iteration 
    540543        """ 
     544        #Make sure that we can interate over the base ring 
    541545        base_ring = self.base_ring() 
    542         #Make sure that we can interate over the base ring 
    543         i = iter(base_ring) 
    544          
     546        base_iter = iter(base_ring) 
     547        base_elements = [ base_iter.next() ] 
     548 
     549        number_of_entries = (self.__nrows*self.__ncols) 
     550        import sage.combinat.integer_vector 
     551 
    545552        if not base_ring.is_finite(): 
    546             #TODO: Make a smarter implementation for iterating 
    547             #      over base rings with an infinite number of 
    548             #      of elements so that all of the entries of 
    549             #      the matrix change 
    550             raise NotImplementedError, "object does not support iteration" 
    551  
    552         for entries in sage.misc.mrange.cartesian_product_iterator([base_ring]*(self.__nrows*self.__ncols)): 
    553             yield self(entries=list(entries), rows=True) 
     553            #When the base ring is not finite, then we should go 
     554            #through and yield the matrices by "weight", which is 
     555            #the total number of iterations that need to be done 
     556            #on the base ring to reach the matrix. 
     557             
     558            weight = 0 
     559            while True: 
     560                for iv in sage.combinat.integer_vector.IntegerVectors(weight, number_of_entries): 
     561                    yield self(entries=[base_elements[i] for i in iv], rows=True)         
     562                         
     563                weight += 1 
     564                base_elements.append( base_iter.next() ) 
     565        else: 
     566            #When the base ring is finite, then we can generate the 
     567            #matrices in "lexicographic" order. 
     568            for entries in sage.misc.mrange.cartesian_product_iterator([base_ring]*number_of_entries): 
     569                yield self(entries=list(entries), rows=True) 
    554570           
    555571    def _get_matrix_class(self): 
Note: See TracChangeset for help on using the changeset viewer.