# 'CyclicCode_with_category' object has no attribute 'gen_mat'

I am trying to create the generator matrix for Quadratic Residue code and the code I am using is:

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

G = C.gen_mat()
H = D.gen_mat()
J = G.transpose() * (G * G.transpose())^-1
K = H.transpose() * (H * H.transpose())^-1

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(17)


Here I am first generating the QR code, where G is the generator matrix for the code C and H is the generator matrix for the cyclic code D. So after the encoding is done we now do XOR operation of the two codes - z = xG (XOR) yH. Now I want to do decoding and here J and K are the matrices which are used for doing decoding. Here J is given by J = G.Transpose(GG.Transpose)^-1.

Here I am running a code that is being used in a paper. The paper is (https://hal.archives-ouvertes.fr/hal-01240242/document (https://hal.archives-ouvertes.fr/hal-...)).

When I am running the code I am getting the following error:

AttributeError                            Traceback (most recent call last)
<ipython-input-2-68b96fb2983c> in <module>()
----> 1 gen_matrices(Integer(17))

<ipython-input-1-f09fbcf52b0e> in gen_matrices(n)
2   C = codes.QuadraticResidueCode(n, GF(Integer(2)))
3   D = C.dual_code()
----> 4   G = C.gen_mat()
5   H = D.gen_mat()
6   J = G.transpose() * (G * G.transpose())**-Integer(1)

/opt/sagemath-8.1/src/sage/structure/category_object.pyx in sage.structure.categ                                                                    ory_object.CategoryObject.__getattr__ (build/cythonized/sage/structure/category_                                                                    object.c:8014)()
854             AttributeError: 'PrimeNumbers_with_category' object has no a                                                                    ttribute 'sadfasdf'
855         """
--> 856         return self.getattr_from_category(name)
857
858     cdef getattr_from_category(self, name):

/opt/sagemath-8.1/src/sage/structure/category_object.pyx in sage.structure.categ                                                                    ory_object.CategoryObject.getattr_from_category (build/cythonized/sage/structure                                                                    /category_object.c:8177)()
869                 cls = self._category.parent_class
870
--> 871             attr = getattr_from_other_class(self, cls, name)
872             self.__cached_methods[name] = attr
873             return attr

/opt/sagemath-8.1/src/sage/cpython/getattr.pyx in sage.cpython.getattr.getattr_f                                                                    rom_other_class (build/cythonized/sage/cpython/getattr.c:1837)()
247         dummy_error_message.cls = type(self)
248         dummy_error_message.name = name
--> 249         raise dummy_attribute_error
250     cdef PyObject* attr = _PyType_Lookup(<type>cls, name)
251     if attr is NULL:

AttributeError: 'CyclicCode_with_category' object has no attribute 'gen_mat'


Can anyone please suggest what I need to do?

edit retag close merge delete

Please provide a complete reproducible example, that others can copy-paste to study your problem.

( 2018-02-16 18:52:01 -0500 )edit

There are indentation problems in the code for the function definition in the question.

The way to call the function to obtain the quoted error message is also missing.

( 2018-02-16 18:52:46 -0500 )edit

Thanks for your edits to the question. With code to reproduce the error, debugging is a pleasure.

( 2018-02-17 11:58:25 -0500 )edit

Sort by » oldest newest most voted

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.)

more

Thank you sir for helping me out.

( 2018-02-17 21:44:54 -0500 )edit

## Understanding the error and fixing it

To find the error in such a case, try to execute the function step by step.

We start by defining:

sage: n = 17


and then try to execute the code inside the function, line by line. The first two lines:

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


execute without problem. But when we execute the next line:

sage: G = C.gen_mat()


we get the following error:

Traceback (most recent call last)
...
AttributeError: 'CyclicCode_with_category' object has no attribute 'gen_mat'


This is telling us that we can't do .gen_mat to C.

So we try tab-completion to see what methods starting with genare available for the object C

sage: C.gen<TAB>


(here <TAB> means "press the TAB key).

We see that there is a method called generator_matrix.

Oh, okay, so probably a previous version of Sage (used by the authors of the paper you refer to) had a method gen_mat, but it has now been renamed generator_matrix.

So all you have to do is change the two lines

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


to

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


in your function definition, and all works well!

## If you really like gen_mat better than generator_matrix

Alternatively, if you like the name gen_mat a lot, you could define it as follows:

sage: from sage.coding.cyclic_code import CyclicCode
sage: gen_mat = CyclicCode.generator_matrix


and then write

    G = gen_mat(C)
H = gen_mat(D)


but there is probably not much point in doing that.

## Finding out when and why the method changed name

If you are curious when the change was made, you can visit SageMath's git repository on GitHub:

In the search box, type gen_mat, and then click on "Commits". You get this page:

And sure enough, here is a commit "Replaced gen_mat by generator_matrix" from Mar 17, 2015.

If you click on the box with "80770c5", you can see the full commit.

Instead of visiting GitHub, you could also visit Sage's Trac server, which is the main place for the development activity and discussion (discussion also happens on the "sage-devel" mailing list). There you will find not only the commit that made the change, but the surrounding discussion.

Visiting

and typing "gen_mat" in the search box, you get to

you will see that the first result listed is

#17973: enhancement: Better Sage consistency for naming and calling in linear_code (closed: fixed)

with the following excerpt

... Most importantly, the gen_mat method will be renamed generator_matrix and the check_mat method parity_check_matrix. Besides, some getter methods to access the private fields of linear codes exist but are not used internally in the class. To support s ...

and information about the author and the date

By dlucas — 2015-03-17T14:04:54Z

If you click on the ticket number, you get to the ticket's page

which has a summary and a fuller description giving the reason for making the change, and is followed by the whole discussion, with trials and errors, successive improvements, all the iterations until the code is finally good to go, gets positive review, and is finally included in the next version of Sage.

If you search for "17973" among the changelogs at

you will find at

that it was merged in sage-6.6.beta6.

more

Thank you very much sir for your kind reply.

( 2018-02-17 14:57:33 -0500 )edit