Ask Your Question

Integer vs ZZ

asked 2013-04-02 03:46:47 -0500

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?!


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 flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-04-02 05:42:38 -0500

fidbc gravatar image

updated 2013-04-02 05:44:13 -0500

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.

edit flag offensive delete link more

answered 2013-04-03 01:25:22 -0500

Volker Braun gravatar image

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: type(i.parent())
sage: ZZ is IntegerRing() is i.parent()
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


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

Seen: 162 times

Last updated: Apr 03 '13