Ticket #3112: trac-3112-doc.patch

File trac-3112-doc.patch, 15.0 KB (added by rlm, 13 years ago)

That should do it.

  • sage/coding/binary_code.pxd

    # HG changeset patch
    # User Robert L. Miller <rlm@rlmiller.org>
    # Date 1211432503 25200
    # Node ID c6efe7bee26b163c9e60c85cce03eb21dc2041f0
    # Parent  d8cf41c5a6c54e4a954ae8c0f0988e8039407594
    Doctest coverage.
    
    diff -r d8cf41c5a6c5 -r c6efe7bee26b sage/coding/binary_code.pxd
    a b cdef class BinaryCode: 
    2323
    2424    cdef int is_one(self, int, int)
    2525    cdef int is_automorphism(self, int *, int *)
    26     cdef int put_in_std_form(self)
     26    cpdef int put_in_std_form(self)
     27    cdef void _apply_permutation_to_basis(self, object labeling)
     28    cdef void _update_words_from_basis(self)
    2729
    2830cdef WordPermutation *create_word_perm(object)
    2931cdef WordPermutation *create_array_word_perm(int *, int, int)
  • sage/coding/binary_code.pyx

    diff -r d8cf41c5a6c5 -r c6efe7bee26b sage/coding/binary_code.pyx
    a b cdef int *hamming_weights(): 
    7171        ham_wts[i] = ham_wts[i & 255] + ham_wts[(i>>8) & 255]
    7272    return ham_wts
    7373
    74 def test_word_perms(t_limit=5.0, n=32):
     74def test_word_perms(t_limit=5.0):
    7575    """
    7676    Tests the WordPermutation structs for at least t_limit seconds.
     77   
     78    These are structures written in pure C for speed, and are tested from this
     79    function, which performs the following tests:
     80   
     81    1. Tests create_word_perm, which creates a WordPermutation from a Python
     82        list L representing a permutation i --> L[i]. Takes a random word and
     83        permutes it by a random list permutation, and tests that the result
     84        agrees with doing it the slow way.
     85   
     86    1b. Tests create_array_word_perm, which creates a WordPermutation from a
     87        C array. Does the same as above.
     88   
     89    2. Tests create_comp_word_perm, which creates a WordPermutation as a
     90        composition of two WordPermutations. Takes a random word and
     91        two random permutations, and tests that the result of permuting by the
     92        composition is correct.
     93   
     94    3. Tests create_inv_word_perm and create_id_word_perm, which create a
     95        WordPermutation as the inverse and identity permutations, resp.
     96        Takes a random word and a random permutation, and tests that the result
     97        permuting by the permutation and its inverse in either order, and
     98        permuting by the identity both return the original word.
     99   
     100    NOTE:
     101        The functions permute_word_by_wp and dealloc_word_perm are implicitly
     102        involved in each of the above tests.
    77103   
    78104    TESTS:
    79105        sage: from sage.coding.binary_code import test_word_perms
    def test_word_perms(t_limit=5.0, n=32): 
    82108    """
    83109    cdef WordPermutation *g, *h, *i
    84110    cdef codeword cw1, cw2, cw3
    85     cdef int j
     111    cdef int n = sizeof(codeword) << 3
     112    cdef int j, *arr = <int*> sage_malloc(n * sizeof(int))
     113    if arr is NULL:
     114        raise MemoryError("Error allocating memory.")
    86115    from sage.misc.prandom import randint
    87116    from sage.combinat.permutation import Permutations
    88117    S = Permutations(range(n))
    def test_word_perms(t_limit=5.0, n=32): 
    92121        cw1 = 0
    93122        for j from 0 <= j < n:
    94123            cw1 += (<codeword>word[j]) << (<codeword>j)
    95         # test create_word_perm
     124        # 1. test create_word_perm
    96125        gg = S.random_element()
    97126        g = create_word_perm(gg)
    98127        word2 = [0]*n
    def test_word_perms(t_limit=5.0, n=32): 
    105134        if cw3 != cw2:
    106135            print "ERROR1"
    107136        dealloc_word_perm(g)
    108         # test create_comp_word_perm
     137        # 1b. test create_array_word_perm
     138        gg = S.random_element()
     139        for j from 0 <= j < n:
     140            arr[j] = gg[j]
     141        g = create_array_word_perm(arr, 0, n)
     142        word2 = [0]*n
     143        for j from 0 <= j < n:
     144            word2[gg[j]] = word[j]
     145        cw2 = permute_word_by_wp(g, cw1)
     146        cw3 = 0
     147        for j from 0 <= j < n:
     148            cw3 += (<codeword>word2[j]) << (<codeword>j)
     149        if cw3 != cw2:
     150            print "ERROR1b"
     151        dealloc_word_perm(g)
     152        # 2. test create_comp_word_perm
    109153        gg = S.random_element()
    110154        hh = S.random_element()
    111155        g = create_word_perm(gg)
    def test_word_perms(t_limit=5.0, n=32): 
    123167        dealloc_word_perm(g)
    124168        dealloc_word_perm(h)
    125169        dealloc_word_perm(i)
    126         # test create_inv_word_perm
     170        # 3. test create_inv_word_perm and create_id_word_perm
    127171        gg = S.random_element()
    128172        g = create_word_perm(gg)
    129173        h = create_inv_word_perm(g)
     174        i = create_id_word_perm(n)
    130175        cw2 = permute_word_by_wp(g, cw1)
    131176        cw2 = permute_word_by_wp(h, cw2)
    132177        if cw1 != cw2:
    def test_word_perms(t_limit=5.0, n=32): 
    135180        cw2 = permute_word_by_wp(g, cw2)
    136181        if cw1 != cw2:
    137182            print "ERROR3b"
     183        cw2 = permute_word_by_wp(i, cw1)
     184        if cw1 != cw2:
     185            print "ERROR3c"
    138186        dealloc_word_perm(g)
    139187        dealloc_word_perm(h)
    140188
    cdef codeword permute_word_by_wp(WordPer 
    372420        image += images[i][(word >> i*chunk_size) & gate]
    373421    return image
    374422
     423def test_expand_to_ortho_basis(B=None):
     424    """
     425    This function is written in pure C for speed, and is tested from this
     426    function.
     427   
     428    INPUT:
     429    B -- a BinaryCode in standard form
     430
     431    OUTPUT:
     432    An array of codewords which represent the expansion of a basis for $B$ to a
     433    basis for $(B^\prime)^\perp$, where $B^\prime = B$ if the all-ones vector 1
     434    is in $B$, otherwise $B^\prime = \text{span}(B,1)$ (note that this guarantees
     435    that all the vectors in the span of the output have even weight).
     436   
     437    TESTS:
     438        sage: from sage.coding.binary_code import test_expand_to_ortho_basis, BinaryCode
     439        sage: M = Matrix(GF(2), [[1,1,1,1,1,1,0,0,0,0],[0,0,1,1,1,1,1,1,1,1]])
     440        sage: B = BinaryCode(M)
     441        sage: B.put_in_std_form()
     442        0
     443        sage: test_expand_to_ortho_basis(B=B)
     444        INPUT CODE:
     445        Binary [10,2] linear code, generator matrix
     446        [1010001111]
     447        [0101111111]
     448        Expanding to the basis of an orthogonal complement...
     449        Basis:
     450        0010000010
     451        0001000010
     452        0000100001
     453        0000010001
     454        0000001001
     455
     456    """
     457    cdef codeword *output
     458    cdef int k=0, i
     459    cdef BinaryCode C
     460    if not isinstance(B, BinaryCode):
     461        raise TypeError()
     462    C = B
     463    print "INPUT CODE:"
     464    print C
     465    print "Expanding to the basis of an orthogonal complement..."
     466    output = expand_to_ortho_basis(C, C.ncols)
     467    print "Basis:"
     468    while output[k]:
     469        k += 1
     470    for i from 0 <= i < k:
     471        print ''.join(reversed(Integer(output[i]).binary().zfill(C.ncols)))
     472
    375473cdef codeword *expand_to_ortho_basis(BinaryCode B, int n):
    376474    r"""
    377475    INPUT:
    378     B -- a BinaryCode
     476    B -- a BinaryCode in standard form
    379477    n -- the degree
    380478   
    381479    OUTPUT:
    cdef class BinaryCode: 
    854952            i = i << 1
    855953        return 1
    856954
    857     def _put_in_std_form(self):
     955    def apply_permutation(self, labeling):
     956        """
     957        Apply a column permutation to the code.
     958       
     959        INPUT:
     960        labeling -- a list permutation of the columns
     961       
     962        EXAMPLE:
     963            sage: from sage.coding.binary_code import *
     964            sage: B = BinaryCode(ExtendedBinaryGolayCode().gen_mat())
     965            sage: B
     966            Binary [24,12] linear code, generator matrix
     967            [100000000000101011100011]
     968            [010000000000111110010010]
     969            [001000000000110100101011]
     970            [000100000000110001110110]
     971            [000010000000110011011001]
     972            [000001000000011001101101]
     973            [000000100000001100110111]
     974            [000000010000101101111000]
     975            [000000001000010110111100]
     976            [000000000100001011011110]
     977            [000000000010101110001101]
     978            [000000000001010111000111]
     979            sage: B.apply_permutation(range(11,-1,-1) + range(12, 24))
     980            sage: B
     981            Binary [24,12] linear code, generator matrix
     982            [000000000001101011100011]
     983            [000000000010111110010010]
     984            [000000000100110100101011]
     985            [000000001000110001110110]
     986            [000000010000110011011001]
     987            [000000100000011001101101]
     988            [000001000000001100110111]
     989            [000010000000101101111000]
     990            [000100000000010110111100]
     991            [001000000000001011011110]
     992            [010000000000101110001101]
     993            [100000000000010111000111]
     994
     995        """
     996        # Tests for this function implicitly test _apply_permutation_to_basis
     997        # and _update_words_from_basis. These functions should not be used
     998        # individually by the user, so they remain cdef'd.
     999        self._apply_permutation_to_basis(labeling)
     1000        self._update_words_from_basis()
     1001
     1002    cdef void _apply_permutation_to_basis(self, object labeling):
     1003        cdef WordPermutation *wp
     1004        cdef int i
     1005        wp = create_word_perm(labeling)
     1006        for i from 0 <= i < self.nrows:
     1007            self.basis[i] = permute_word_by_wp(wp, self.basis[i])
     1008        dealloc_word_perm(wp)
     1009
     1010    cdef void _update_words_from_basis(self):
     1011        cdef codeword word
     1012        cdef int j, parity, combination
     1013        word = 0
     1014        parity = 0
     1015        combination = 0
     1016        while 1:
     1017            self.words[combination] = word
     1018            parity ^= 1
     1019            j = 0
     1020            if not parity:
     1021                while not combination & (1 << j): j += 1
     1022                j += 1
     1023            if j == self.nrows: break
     1024            else:
     1025                combination ^= (1 << j)
     1026                word ^= self.basis[j]
     1027       
     1028
     1029    cpdef int put_in_std_form(self):
    8581030        """
    8591031        Put the code in binary form, which is defined by an identity matrix on
    8601032        the left, augmented by a matrix of data.
    cdef class BinaryCode: 
    8661038            Binary [6,2] linear code, generator matrix
    8671039            [111100]
    8681040            [001111]
    869             sage: B._put_in_std_form(); B
     1041            sage: B.put_in_std_form(); B
     1042            0
    8701043            Binary [6,2] linear code, generator matrix
    8711044            [101011]
    8721045            [010111]
    8731046
    8741047        """
    875         self.put_in_std_form()
    876    
    877     cdef int put_in_std_form(self):
    878         cdef codeword swap, current = 1, pivots = 0, word
    879         cdef int i, j, k, row = 0, parity, combination
     1048        cdef codeword swap, current = 1, pivots = 0
     1049        cdef int i, j, k, row = 0
    8801050        cdef object perm
    881         cdef WordPermutation *wp
    8821051        while row < self.nrows:
    8831052            i = row
    8841053            while i < self.nrows and not self.basis[i] & current:
    cdef class BinaryCode: 
    9071076            else:
    9081077                perm[i] = k
    9091078                k += 1
    910         wp = create_word_perm(perm)
    911         for i from 0 <= i < self.nrows:
    912             self.basis[i] = permute_word_by_wp(wp, self.basis[i])
    913         dealloc_word_perm(wp)
    914         word = 0
    915         parity = 0
    916         combination = 0
    917         while 1:
    918             self.words[combination] = word
    919             parity ^= 1
    920             j = 0
    921             if not parity:
    922                 while not combination & (1 << j): j += 1
    923                 j += 1
    924             if j == self.nrows: break
    925             else:
    926                 combination ^= (1 << j)
    927                 word ^= self.basis[j]
    928 
     1079        self._apply_permutation_to_basis(perm)
     1080        self._update_words_from_basis()
    9291081
    9301082cdef class OrbitPartition:
    9311083    """
    cdef class BinaryCodeClassifier: 
    35193671        for i from 0 <= i < 2*nrows:
    35203672            self.labeling[i+ncols] = rho.basis_locations[i]
    35213673
     3674    def put_in_canonical_form(self, BinaryCode B):
     3675        """
     3676        Puts the code into canonical form.
     3677       
     3678        Canonical form is obtained by performing row reduction, permuting the
     3679        pivots to the front so that the generator matrix is of the form: the
     3680        identity matrix augmented to the right by arbitrary data.
     3681       
     3682        EXAMPLE:
     3683            sage: from sage.coding.binary_code import *
     3684            sage: BC = BinaryCodeClassifier()
     3685            sage: B = BinaryCode(ExtendedBinaryGolayCode().gen_mat())
     3686            sage: B.apply_permutation(range(24,-1,-1))
     3687            sage: B
     3688            Binary [24,12] linear code, generator matrix
     3689            [011000111010100000000000]
     3690            [001001001111100000000001]
     3691            [011010100101100000000010]
     3692            [001101110001100000000100]
     3693            [010011011001100000001000]
     3694            [010110110011000000010000]
     3695            [011101100110000000100000]
     3696            [000011110110100001000000]
     3697            [000111101101000010000000]
     3698            [001111011010000100000000]
     3699            [010110001110101000000000]
     3700            [011100011101010000000000]
     3701            sage: BC.put_in_canonical_form(B)
     3702            sage: B
     3703            Binary [24,12] linear code, generator matrix
     3704            [100000000000001100111001]
     3705            [010000000000001010001111]
     3706            [001000000000001111010010]
     3707            [000100000000010110101010]
     3708            [000010000000010110010101]
     3709            [000001000000010001101101]
     3710            [000000100000011000110110]
     3711            [000000010000011111001001]
     3712            [000000001000010101110011]
     3713            [000000000100010011011110]
     3714            [000000000010001011110101]
     3715            [000000000001001101101110]
     3716
     3717        """
     3718        aut_gp_gens, labeling, size, base = self._aut_gp_and_can_label(B)
     3719        B._apply_permutation_to_basis(labeling)
     3720        B.put_in_std_form()
     3721
    35223722    def generate_children(self, BinaryCode B, int n, int d=2):
    35233723        """
    35243724        Use canonical augmentation to generate children of the code B.
    cdef class BinaryCodeClassifier: 
    35373737            sage: BC.generate_children(B, 6, 4)
    35383738            [[1 1 1 1 0 0]
    35393739            [0 1 0 1 1 1]]
     3740       
     3741        NOTE:
     3742        The function self_orthogonal_binary_codes makes heavy use of this
     3743        function.
     3744       
     3745        MORE EXAMPLES:
     3746            sage: soc_iter = self_orthogonal_binary_codes(12, 6, 4)
     3747            sage: L = list(soc_iter)
     3748            sage: for n in range(0, 13):
     3749            ...     s = 'n=%2d : '%n
     3750            ...     for k in range(1,7):
     3751            ...         s += '%3d '%len([C for C in L if C.length() == n and C.dimension() == k])
     3752            ...     print s
     3753            n= 0 :   0   0   0   0   0   0
     3754            n= 1 :   0   0   0   0   0   0
     3755            n= 2 :   0   0   0   0   0   0
     3756            n= 3 :   0   0   0   0   0   0
     3757            n= 4 :   1   0   0   0   0   0
     3758            n= 5 :   0   0   0   0   0   0
     3759            n= 6 :   0   1   0   0   0   0
     3760            n= 7 :   0   0   1   0   0   0
     3761            n= 8 :   1   1   1   1   0   0
     3762            n= 9 :   0   0   0   0   0   0
     3763            n=10 :   0   1   1   1   0   0
     3764            n=11 :   0   0   1   1   0   0
     3765            n=12 :   1   2   3   4   2   0
    35403766       
    35413767        """
    35423768        cdef BinaryCode m