Methods in GF

56 views
Skip to first unread message

Oleksandr Kazymyrov

unread,
May 20, 2012, 8:15:20 PM5/20/12
to sage-s...@googlegroups.com
Dear all,

1. Why important next functions?
k.a_times_b_minus_c
k.a_times_b_plus_c
k.c_minus_a_times_b
sage: k.some_elements ?  
...
       Returns a collection of elements of this finite field for use in unit testing.

Why this function are defined as public?

2. Also a few misunderstanding functions
  • sage: k.cardinality ? 
    Type: builtin_function_or_method
    Base Class: <type 'builtin_function_or_method'>
    String Form: <built-in method cardinality of FiniteField_givaro_with_category object at 0xbb0eaac>
    Namespace: Interactive
    Definition: k.cardinality(self)
    Docstring:
           Return the order of this finite field (same as self.order()).

  • sage: k.cayley_graph() 
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)

    /home/hamsin/<ipython console> in <module>()

    /home/hamsin/bin/sage/local/lib/python2.7/site-packages/sage/categories/semigroups.pyc in cayley_graph(self, side, simple, elements, generators, connecting_set)
        284                 generators = connecting_set
        285             if generators is None:
    --> 286                 generators = self.semigroup_generators()
        287             if isinstance(generators, (list, tuple)):
        288                 generators = dict((self(g), self(g)) for g in generators)

    /home/hamsin/bin/sage/local/lib/python2.7/site-packages/sage/structure/parent.so in sage.structure.parent.Parent.__getattr__ (sage/structure/parent.c:6805)()

    /home/hamsin/bin/sage/local/lib/python2.7/site-packages/sage/structure/parent.so in sage.structure.parent.getattr_from_other_class (sage/structure/parent.c:3248)()

    AttributeError: 'FiniteField_givaro_with_category' object has no attribute 'semigroup_generators'

  • sage: k.has_base()
    True
    sage: k.has_base ? 
    Type: builtin_function_or_method
    Base Class: <type 'builtin_function_or_method'>
    String Form: <built-in method has_base of FiniteField_givaro_with_category object at 0xbb0eaac>
    Namespace: Interactive
    Definition: k.has_base(self, category=None)
    ??????

  • sage: k.ngens ?
    Type: builtin_function_or_method
    Base Class: <type 'builtin_function_or_method'>
    String Form: <built-in method ngens of FiniteField_givaro_with_category object at 0xbb0eaac>
    Namespace: Interactive
    Definition: k.ngens(self)
    Docstring:
           The number of generators of the finite field.  Always 1.

  • sage: k.normalize_names ? 
    Type: builtin_function_or_method
    Base Class: <type 'builtin_function_or_method'>
    String Form: <built-in method normalize_names of FiniteField_givaro_with_category object at 0xbb0eaac>
    Namespace: Interactive
    Definition: k.normalize_names(self, ngens, names=None)

    sage: k.normalize_names()
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)

    /home/hamsin/<ipython console> in <module>()

    /home/hamsin/bin/sage/local/lib/python2.7/site-packages/sage/structure/category_object.so in sage.structure.category_object.CategoryObject.normalize_names (sage/structure/category_object.c:3939)()

    TypeError: normalize_names() takes at least 1 positional argument (0 given)
    sage: k.normalize_names(1)
    ??????

  • sage: k.on
    k.one          k.one_element  
    sage: k.one ? 
    Type: builtin_function_or_method
    Base Class: <type 'builtin_function_or_method'>
    String Form: <built-in method one_element of FiniteField_givaro_with_category object at 0xbb0eaac>
    Namespace: Interactive
    Definition: k.one(self)
    Docstring:
           Return the one element of this ring (cached), if it exists.
        
           EXAMPLES:
        
              sage: ZZ.one_element()
              1
              sage: QQ.one_element()
              1
              sage: QQ['x'].one_element()
              1
        
           The result is cached:
        
              sage: ZZ.one_element() is ZZ.one_element()
              True

  • sage: k.zero ? 
    Type: builtin_function_or_method
    Base Class: <type 'builtin_function_or_method'>
    String Form: <built-in method zero_element of FiniteField_givaro_with_category object at 0xbb0eaac>
    Namespace: Interactive
    Definition: k.zero(self)
    Docstring:
           Return the zero element of this ring (cached).
        
           EXAMPLES:
        
              sage: ZZ.zero_element()
              0
              sage: QQ.zero_element()
              0
              sage: QQ['x'].zero_element()
              0
        
           The result is cached:
        
              sage: ZZ.zero_element() is ZZ.zero_element()
              True


Definition of the field:
sage: R.<x>=ZZ[]
sage: k=GF(2^8,name='a',modulus=x^8+x^4+x^3+x+1)

Martin Albrecht

unread,
May 29, 2012, 4:54:09 AM5/29/12
to sage-s...@googlegroups.com
Hi,

On Monday 21 May 2012, Oleksandr Kazymyrov wrote:
> Dear all,
>
> 1. Why important next functions?
> k.a_times_b_minus_c
> k.a_times_b_plus_c
> k.c_minus_a_times_b

These shouldn't exist I'd say. The reason they do exist is that when I wrapped
Givaro it provided these functions so I figured I might as well provide them.
So what we should do is deprecate these functions and replace them by
functions starting with "_" indicating, that they are kinda internal. Can you
open a ticket?

> sage: k.some_elements ?
> ...
> Returns a collection of elements of this finite field *for use
> in unit testing.*

The function is indeed used in unitests as confirmed by
search_src("some_elements"). Perhaps it should start with an underscore?

> Why this function are defined as public?

> 2. Also a few misunderstanding functions
>
> - sage: *k.cardinality* ?
> Type: builtin_function_or_method
> Base Class: <type 'builtin_function_or_method'>
> String Form: <built-in method cardinality of
> FiniteField_givaro_with_category object at 0xbb0eaac>
> Namespace: Interactive
> Definition: k.cardinality(self)
> Docstring:
> Return the order of this finite field (*same as self.order()*).

Why is this confusing?

> - sage: *k.cayley_graph() *
>
> --------------------------------------------------------------------------
> - AttributeError Traceback (most recent call
> last)
>
> /home/hamsin/<ipython console> in <module>()
>
>
> /home/hamsin/bin/sage/local/lib/python2.7/site-packages/sage/categories/se
> migroups.pyc in cayley_graph(self, side, simple, elements, generators,
> connecting_set) 284 generators = connecting_set
> 285 if generators is None:
> --> 286 generators = self.semigroup_generators()
> 287 if isinstance(generators, (list, tuple)):
> 288 generators = dict((self(g), self(g)) for g in
> generators)
>
>
> /home/hamsin/bin/sage/local/lib/python2.7/site-packages/sage/structure/par
> ent.so in sage.structure.parent.Parent.__getattr__
> (sage/structure/parent.c:6805)()
>
>
> /home/hamsin/bin/sage/local/lib/python2.7/site-packages/sage/structure/par
> ent.so in sage.structure.parent.getattr_from_other_class
> (sage/structure/parent.c:3248)()
>
> AttributeError: 'FiniteField_givaro_with_category' object has no
> attribute 'semigroup_generators'

This is a bug, i.e., K inherits the generic cayley_graph() but does not
provide all the machinery needed to make it work.

> - sage: *k.has_base()*
> *True*
> sage: *k.has_base* ?
> Type: builtin_function_or_method
> Base Class: <type 'builtin_function_or_method'>
> String Form: <built-in method has_base of
> FiniteField_givaro_with_category object at 0xbb0eaac>
> Namespace: Interactive
> Definition: k.has_base(self, category=None)
> *??????*

The base of GF(p^k) is GF(p), the lack of documentation is a bug :)

> *
> - sage: *k.ngens* *?*
> Type: builtin_function_or_method
> Base Class: <type 'builtin_function_or_method'>
> String Form: <built-in method ngens of FiniteField_givaro_with_category
> object at 0xbb0eaac>
> Namespace: Interactive
> Definition: k.ngens(self)
> Docstring:
> The number of generators of the finite field. * Always 1.*

This is a generic function provided for compatibility with other parents in
Sage.

> *
> *
> - sage: *k.normalize_names ?*
> Type: builtin_function_or_method
> Base Class: <type 'builtin_function_or_method'>
> String Form: <built-in method normalize_names of
> FiniteField_givaro_with_category object at 0xbb0eaac>
> Namespace: Interactive
> Definition: k.normalize_names(self, ngens, names=None)
>
> sage: k.normalize_names()

This is a generic method that perhaps could be private, i.e.,
"_normalize_names()" or moved somewhere else?


> - sage: *k.on*
> k.one k.one_element
> sage: k.one ?
> Type: builtin_function_or_method
> Base Class: <type 'builtin_function_or_method'>
> String Form: <built-in method one_element of
> FiniteField_givaro_with_category object at 0xbb0eaac>
> Namespace: Interactive
> Definition: k.one(self)
> Docstring:
> Return the one element of this ring (cached), if it exists.
>
> EXAMPLES:
>
> sage: ZZ.*one_element()*
> 1
> sage: QQ.*one_element()*
> 1
> sage: QQ['x'].*one_element()*
> 1
>
> The result is cached:
>
> sage: ZZ.*one_element() *is *ZZ.one_element()*
> True
>
>
>
> - sage: *k.zero ? *
> Type: builtin_function_or_method
> Base Class: <type 'builtin_function_or_method'>
> String Form: <built-in method zero_element of
> FiniteField_givaro_with_category object at 0xbb0eaac>
> Namespace: Interactive
> Definition: k.zero(self)
> Docstring:
> Return the zero element of this ring (cached).
>
> EXAMPLES:
>
> sage: *ZZ.zero_element()*
> 0
> sage: *QQ.zero_element()*
> 0
> sage: QQ['x'].*zero_element()*
> 0
>
> The result is cached:
>
> sage: ZZ.*zero_element()* is ZZ.*zero_element()*
> True

These are aliases, I don't find this confusing.

Anyway, do you want to provide (documentation) patches for the issues you
discovered?

Cheers,
Martin

--
name: Martin Albrecht
_pgp: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0x8EF0DC99
_otr: 47F43D1A 5D68C36F 468BAEBA 640E8856 D7951CCF
_www: http://martinralbrecht.wordpress.com/
_jab: martinr...@jabber.ccc.de

Simon King

unread,
May 29, 2012, 5:26:46 AM5/29/12
to sage-s...@googlegroups.com
Hi,

On 2012-05-29, Martin Albrecht <martinr...@googlemail.com> wrote:
>> sage: k.some_elements ?
>> ...
>> Returns a collection of elements of this finite field *for use
>> in unit testing.*
>
> The function is indeed used in unitests as confirmed by
> search_src("some_elements"). Perhaps it should start with an underscore?

That is a method required and defined by the category framework, such as
sage: ZZ.some_elements.__module__
'sage.categories.sets_cat'

In some cases, it is overridden by something more useful (by default,
that method just returns a list of length one).

And yes, it *is* used in the testing framework. For example, if
associativity is tested then some_elements provides the elements on
which the test is performed (that's why it is a good idea to not just
inherit some_elements but override it with something more meaningful).
See
sage: ZZ._test_associativity??


>> Why this function are defined as public?

Why not?

>> 2. Also a few misunderstanding functions
>>
>> - sage: *k.cardinality* ?
>> Type: builtin_function_or_method
>> Base Class: <type 'builtin_function_or_method'>
>> String Form: <built-in method cardinality of
>> FiniteField_givaro_with_category object at 0xbb0eaac>
>> Namespace: Interactive
>> Definition: k.cardinality(self)
>> Docstring:
>> Return the order of this finite field (*same as self.order()*).
>
> Why is this confusing?

I can't understand the confusion either. Some people say "order" and
some people say "cardinality" when they refer to the number of elements
of a group or field. Personally, I'd tend to say "cardinality" in the
case of a set (but a field *is* as set!), and "order" in the case of a
field or group. But why shouldn't one offer both, if both is used by
people?

>> - sage: *k.ngens* *?*
>> Docstring:
>> The number of generators of the finite field. * Always 1.*
>
> This is a generic function provided for compatibility with other parents in
> Sage.

At that point one should mention "duck typing", I guess. Sometimes one
writes generic code, that is supposed to work with various kinds of
objects, and the type of these objects does not (and should not) matter.
In some cases, all what one wants to know is whether the object
is defined in terms of generators (e.g., a polynomial ring is, but a set
is not), and how many generators there are. ngens() answers both
questions.

>> - sage: *k.normalize_names ?*
>>
>> sage: k.normalize_names()
>
> This is a generic method that perhaps could be private, i.e.,
> "_normalize_names()" or moved somewhere else?

I sometimes wondered why it is a method of CategoryObject, and not a
function defined in, say, sage.structure.parent.

Cheers,
Simon


Oleksandr Kazymyrov

unread,
Jun 3, 2012, 7:00:01 AM6/3/12
to sage-s...@googlegroups.com
Hi,

I agree with Martin, especially about private methods.

> sage: k.some_elements ? 
> ... 

>        Returns a collection of elements of this finite field *for use 
> in unit testing.* 
The function is indeed used in unitests as confirmed by 
search_src("some_elements").  Perhaps it should start with an underscore?
In my opinion it must start with an underscore. The method k.random_element() return a random element and it is enough for users. 

>> 2. Also a few misunderstanding functions 
>> 
>>    - sage: *k.cardinality* ? 

>>    Type: builtin_function_or_method 
>>    Base Class: <type 'builtin_function_or_method'> 
>>    String Form: <built-in method cardinality of 
>>    FiniteField_givaro_with_category object at 0xbb0eaac> 
>>    Namespace: Interactive 
>>    Definition: k.cardinality(self) 
>>    Docstring: 
>>           Return the order of this finite field (*same as self.order()*). 

> Why is this confusing? 
I can't understand the confusion either. Some people say "order" and 
some people say "cardinality" when they refer to the number of elements 
of a group or field. Personally, I'd tend to say "cardinality" in the 
case of a set (but a field *is* as set!), and "order" in the case of a 
field or group. But why shouldn't one offer both, if both is used by 
people? 
In most of books "Finite Fields" you can find definition "order" (imho). But if some use it, then it's no problem.

In general I can't understand why it is important to have two (or more) functions with the same functionality in user space. If some of them are used for compatibility, then maybe they should be private?

>>    - sage: *k.ngens* *?* 
>>    Docstring: 

>>           The number of generators of the finite field. * Always 1.* 

> This is a generic function provided for compatibility with other parents in 
> Sage. 
At that point one should mention "duck typing", I guess. Sometimes one 
writes generic code, that is supposed to work with various kinds of 
objects, and the type of these objects does not (and should not) matter. 
In some cases, all what one wants to know is whether the object 
is defined in terms of generators (e.g., a polynomial ring is, but a set 
is not), and how many generators there are. ngens() answers both 
questions. 
Ok, but the finite field has a set of generators. I'm a bit confused.
sage: R.<x>=ZZ[]
sage
: K=GF(2^8,name='a',modulus=x^8+x^4+x^3+x+1)
sage
: m=[i.multiplicative_order() for i in K if i != 0]
sage
: m.count(255)
128

Anyway, do you want to provide (documentation) patches for the issues you discovered?
My English is not so good for documentation, but I can provide some code (logic and programming) patches after my registration at trac.sagemath.org.
Reply all
Reply to author
Forward
0 new messages