Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The question is not really clear, and the code producing the error is not complete. So i am trying to rather "guess" the question, and comment about the place delivering the error in the above code. Since this comment would not fit in a comment window, it became an answer.

Before we have a function performing the computations, let us better use the "global level", so the sage interpreter may also give us more information on the methods that can be used. (All following lines are inserted into the sage iron pyhton interpreter, obtained by typing sage in a terminal on this slow linux box.) Let us start with:

n = 7
C = codes.QuadraticResidueCode(n, GF(2))       
D = C.dual_code()

(There is a lower case c in codes.) Some comments so far. I tried first n = 2, getting promptly the warning:

sage: n = 2
sage: C = codes.QuadraticResidueCode(n, GF(2))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
::: some more error lines
ValueError: the argument n must be an odd prime

OK, an odd prime...

sage: n = 3
sage: C = codes.QuadraticResidueCode(n, GF(2))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-145-80930e9ced89> in <module>()
----> 1 C = codes.QuadraticResidueCode(n, GF(Integer(2)))
::: some more lines here    
ValueError: the order of the finite field must be a quadratic residue modulo n

OK, so the odd prime is a special one...

sage: [ p for p in primes(3, 50) if jacobi_symbol(2, p) == 1 ]
[7, 17, 23, 31, 41, 47]

Let us pick the seven... (Please give us the prime next time.) Then we can evaluate and ask for...

sage: n = 7
....: C = codes.QuadraticResidueCode(n, GF(2))       
....: D = C.dual_code()
....: 
sage: C
[7, 4] Cyclic Code over GF(2)
sage: D
[7, 3] linear code over GF(2)

Now it is possible to ask for relevant methods in the interpreter. As the code error in the post was showing, there is no method gen_mat of the instance C, we run into an error. But we can do the following. Type C.gen in the interpreter, then hit TAB (maybe twice), and a list of all matching methods is shown:

sage: C.gen
  C.generator_matrix            C.gens                        C.genus                       
  C.generator_matrix_systematic C.gens_dict                                                 
  C.generator_polynomial        C.gens_dict_recursive

So gen_mat is not in the list, but probably the newer sage version has a new name for the "same method". A possible candidate is from this side (where codes are not a strength) would be the method generator_matrix, so that we can go one step further...

sage: G = C.generator_matrix()
....: H = D.generator_matrix()
....: 
sage: G
[1 1 0 1 0 0 0]
[0 1 1 0 1 0 0]
[0 0 1 1 0 1 0]
[0 0 0 1 1 0 1]
sage: H
[1 0 1 1 1 0 0]
[0 1 0 1 1 1 0]
[0 0 1 0 1 1 1]
sage:

The next operation in the list is to take the inverse of

sage: G * G.transpose()
[1 1 1 1]
[1 1 1 1]
[1 1 1 1]
[1 1 1 1]

which does not exist. So i have to stop here. Please give references to the objects to be constructed, or the source for the code, or the expected answer on the mathematical side... this may be helpful when an expert in the domain sees the lines.

A final note: The posted code cannot be compiled, an evident indent error. Probably something like the following is the start:

def gen_matrices(n):
    C = codes.QuadraticResidueCode(n, GF(2))       
    D = C.dual_code()

    G = C.generator_matrix()
    H = D.generator_matrix()

    # J = G.transpose() * (G * G.transpose())^-1
    # K = H.transpose() * (H * H.transpose())^-1
    J, K = None, None

    if ( rank(block_matrix([[G],[H]])) != G.ncols()
         or G * H.transpose() != 0 ):
        raise("Logic error: The code is not LCD")

    return [ G, H, J, K ]

gen_matrices( 7 )

(This must be improved.)