Ticket #1323: trac_1323-subspaces.patch
File trac_1323-subspaces.patch, 5.6 KB (added by , 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 41 41 42 42 import types 43 43 from sage.matrix.matrix_space import MatrixSpace 44 from sage.modules.free_module import VectorSpace 44 45 from sage.rings.integer_ring import ZZ 45 46 from sage.rings.arith import binomial, integer_floor 46 47 from sage.rings.finite_field import FiniteField … … 48 49 49 50 ### utility functions ------------------------------------------------------- 50 51 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 = 158 sage: from sage.combinat.designs.block_design import subspaces_of_vs59 sage: subspaces_of_vs(F,n,k)60 [Vector space of degree 2 and dimension 1 over Finite Field of size 261 Basis matrix:62 [0 1],63 Vector space of degree 2 and dimension 1 over Finite Field of size 264 Basis matrix:65 [1 0],66 Vector space of degree 2 and dimension 1 over Finite Field of size 267 Basis matrix:68 [1 1]]69 70 """71 from sage.interfaces.gap import gap, GapElement72 from sage.interfaces.gap import gfq_gap_to_sage73 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 L87 88 52 def tdesign_params(t, v, k, L): 89 53 """ 90 54 Return the design's parameters: (t, v, b, r , k, L). … … 124 88 125 89 """ 126 90 q = F.order() 127 from sage.combinat.designs.block_design import subspaces_of_vs128 91 from sage.interfaces.gap import gap, GapElement 129 92 from sage.sets.set import Set 130 93 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)) 133 97 blcks = [] 134 98 for p in points: 135 99 b = [] -
sage/modules/free_module.py
diff -r ba420fedb04b -r 282b101645a8 sage/modules/free_module.py
a b 2720 2720 """ 2721 2721 return self.submodule(gens, check=check, already_echelonized=already_echelonized) 2722 2722 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 2723 2787 def subspace_with_basis(self, gens, check=True, already_echelonized=False): 2724 2788 """ 2725 2789 Same as \code{self.submodule_with_basis(...)}.