Ticket #1323: trac_1323-subspaces.patch

File trac_1323-subspaces.patch, 5.6 KB (added by rlm, 14 years ago)
  • sage/combinat/designs/block_design.py

    # HG changeset patch
    # User Robert L. Miller <rlm@rlmiller.org>
    # Date 1224250553 -7200
    # Node ID 282b101645a8ae9e398b8e797cd5365aed8040ac
    # Parent  ba420fedb04b39498ada0613da9109a0c3cdd6fa
    Generate subspaces of a vector space of fixed dimension.
    
    diff -r ba420fedb04b -r 282b101645a8 sage/combinat/designs/block_design.py
    a b  
    4141
    4242import types
    4343from sage.matrix.matrix_space import MatrixSpace
     44from sage.modules.free_module import VectorSpace
    4445from sage.rings.integer_ring import ZZ
    4546from sage.rings.arith import binomial, integer_floor
    4647from sage.rings.finite_field import FiniteField
     
    4849
    4950###  utility functions  -------------------------------------------------------
    5051
    51 def subspaces_of_vs(F,n,k):
    52     """
    53     Computes the subsppaces of F^n of dimension k (k<=n),
    54     if F is a finite field.
    55 
    56     EXAMPLES:
    57         sage: F = GF(2); n = 2; k = 1
    58         sage: from sage.combinat.designs.block_design import subspaces_of_vs
    59         sage: subspaces_of_vs(F,n,k)
    60         [Vector space of degree 2 and dimension 1 over Finite Field of size 2
    61         Basis matrix:
    62         [0 1],
    63          Vector space of degree 2 and dimension 1 over Finite Field of size 2
    64         Basis matrix:
    65         [1 0],
    66          Vector space of degree 2 and dimension 1 over Finite Field of size 2
    67         Basis matrix:
    68         [1 1]]
    69 
    70     """
    71     from sage.interfaces.gap import gap, GapElement
    72     from sage.interfaces.gap import gfq_gap_to_sage
    73     q = F.order()
    74     V = gap("GaloisField(%s)^%s"%(q,n))
    75     S = V.Subspaces(k).AsSet()
    76     MS = MatrixSpace(F,k,n)
    77     L = []
    78     for s in S:
    79         B = s.Basis()
    80         rows = []
    81         for b in B:
    82             r = [gfq_gap_to_sage(b[i],F) for i in range(1,n+1)]
    83             rows.append(r)
    84         M = MS(rows)
    85         L.append(M.row_space())
    86     return L
    87    
    8852def tdesign_params(t, v, k, L):
    8953    """
    9054    Return the design's parameters: (t, v, b, r , k, L).
     
    12488
    12589    """
    12690    q = F.order()
    127     from sage.combinat.designs.block_design import subspaces_of_vs
    12891    from sage.interfaces.gap import gap, GapElement
    12992    from sage.sets.set import Set
    13093    if method == None:
    131         points = subspaces_of_vs(F,n+1,1)
    132         flats = subspaces_of_vs(F,n+1,d+1)
     94        V = VectorSpace(F, n+1)
     95        points = list(V.subspaces(1))
     96        flats = list(V.subspaces(d+1))
    13397        blcks = []
    13498        for p in points:
    13599            b = []
  • sage/modules/free_module.py

    diff -r ba420fedb04b -r 282b101645a8 sage/modules/free_module.py
    a b  
    27202720        """
    27212721        return self.submodule(gens, check=check, already_echelonized=already_echelonized)
    27222722   
     2723    def subspaces(self, dim):
     2724        """
     2725        Iterate over all subspaces of dimension dim.
     2726       
     2727        INPUT:
     2728            dim -- int, dimension of subspaces to be generated
     2729       
     2730        EXAMPLE:
     2731            sage: V = VectorSpace(GF(3), 5)
     2732            sage: len(list(V.subspaces(0)))
     2733            1
     2734            sage: len(list(V.subspaces(1)))
     2735            121
     2736            sage: len(list(V.subspaces(2)))
     2737            1210
     2738            sage: len(list(V.subspaces(3)))
     2739            1210
     2740            sage: len(list(V.subspaces(4)))
     2741            121
     2742            sage: len(list(V.subspaces(5)))
     2743            1
     2744       
     2745            sage: V = VectorSpace(GF(3), 5)
     2746            sage: V = V.subspace([V([1,1,0,0,0]),V([0,0,1,1,0])])
     2747            sage: list(V.subspaces(1))
     2748            [Vector space of degree 5 and dimension 1 over Finite Field of size 3
     2749            Basis matrix:
     2750            [1 1 0 0 0],
     2751             Vector space of degree 5 and dimension 1 over Finite Field of size 3
     2752            Basis matrix:
     2753            [1 1 1 1 0],
     2754             Vector space of degree 5 and dimension 1 over Finite Field of size 3
     2755            Basis matrix:
     2756            [1 1 2 2 0],
     2757             Vector space of degree 5 and dimension 1 over Finite Field of size 3
     2758            Basis matrix:
     2759            [0 0 1 1 0]]
     2760
     2761        """
     2762        if not self.base_ring().is_finite():
     2763            raise RuntimeError("Base ring must be finite.")
     2764        # First, we select which columns will be pivots:
     2765        from sage.combinat.subset import Subsets
     2766        BASE = self.basis_matrix()
     2767        for pivots in Subsets(range(self.dimension()), dim):
     2768            MAT = sage.matrix.matrix_space.MatrixSpace(self.base_ring(), dim,
     2769                self.dimension(), sparse = self.is_sparse())()
     2770            free_positions = []
     2771            for i in range(dim):
     2772                MAT[i, pivots[i]] = 1
     2773                for j in range(pivots[i]+1,self.dimension()):
     2774                    if j not in pivots:
     2775                        free_positions.append((i,j))
     2776            # Next, we fill in those entries that are not
     2777            # determined by the echelon form alone:
     2778            num_free_pos = len(free_positions)
     2779            ENTS = VectorSpace(self.base_ring(), num_free_pos)
     2780            for v in ENTS:
     2781                for k in range(num_free_pos):
     2782                    MAT[free_positions[k]] = v[k]
     2783                # Finally, we have to multiply by the basis matrix
     2784                # to take corresponding linear combinations of the basis
     2785                yield self.subspace((MAT*BASE).rows())
     2786   
    27232787    def subspace_with_basis(self, gens, check=True, already_echelonized=False):
    27242788        """
    27252789        Same as \code{self.submodule_with_basis(...)}.