Ask Your Question

Integer vs ZZ

What's the difference between sage class ZZ (rings/integer_ring.pyx) and Integer (rings/integer.pyx)?

Is one preferred over the other in some cases? Do they provide different functionality?

Maybe instances of Integer are elements of ZZ?!

Example:

a = ZZ(10)
b = Integer(3)
c = a + b
print a, b, c
print "Type of elements:", type(a), "vs", type(b), "vs", type(c)
print "Type of classes: ", ZZ, "vs", Integer

edit retag close merge delete

2 answers

Sort by ยป oldest newest most voted

Sage uses the parent/element pattern for most mathematical objects, see the documentation on coercion for details. For integers the parent is IntegerRing (with ZZ as shortcut) and the element are Integer.

sage: i = 1
sage: type(i)
sage.rings.integer.Integer
sage: type(i.parent())
sage.rings.integer_ring.IntegerRing_class
sage: ZZ is IntegerRing() is i.parent()
True

more

Yes, Integer is the type of elements in ZZ. ZZ refers to the ring of integers, whereas Integer refers to the type of an element of the ring ZZ. When you execute, say ZZ(int(10)), int(10) is coerced to an Integer. In your example above you can verify this.

sage: print "Type of elements:", type(a), "vs", type(b), "vs", type(c)
Type of elements: <type 'sage.rings.integer.Integer'> vs <type 'sage.rings.integer.Integer'> vs <type 'sage.rings.integer.Integer'>


The elements a, b, and c are of the same type.

more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Stats

Asked: 2013-04-02 03:46:47 -0600

Seen: 1,287 times

Last updated: Apr 03 '13