is_Integer() function semantics

282 views
Skip to first unread message

nirmal

unread,
Apr 22, 2009, 12:48:36 AM4/22/09
to sage-devel, harald....@gmail.com
In module sage.rings.integer

is_Integer(3/2+1/2)

returns

False The expected output should be True as 3/2+1/2 = 2.

I was planning to use this function to check if the result of division
is a whole number.

Harald responded with a suggestion to use Integer() (thread below)

Integer(p/q) works and returns True is the rational p/q reduces to an
integer otherwise it exits with error.
I wanted the Integer(p/q) to return False is p/q ids not reducible to
an integer.



Hello, I think the function does what it should. It tests if the
*type* is integer, in your case it is a rational number. You can see
this with

type(1/2 + 1/2)

To coerce a rational number ot integers, use "Integer()"

e.g.

Integer(1/2) -> no coercion error
Integer(1/2 + 1/2) -> type(1) -> integer

Harald

----Nirmal writes

Thanks for your response.

I was looking for a function that returns True is the rational operand
reduces to an integer and returns False otherwise. Your suggestion of
using Integer() has the same issue. When the rational is not reducible
to an integer it returns error. For example,

Integer(1/2) returns

Traceback (click to the left for traceback)
...
TypeError: no conversion of this rational to integer

And breaks out of my computation loop.







Craig Citro

unread,
Apr 22, 2009, 1:54:35 AM4/22/09
to sage-...@googlegroups.com
> In module sage.rings.integer
>
> is_Integer(3/2+1/2)
>
> returns
>
> False   The expected output should be True as 3/2+1/2 = 2.
>
> I was planning to use this function to check if the result of division
> is a whole number.
>

You could also use the is_integral method of rational numbers:

sage: n = 3/2 + 1/2
sage: n.is_integral()
True

(This function also exists on Integers, so you could even use it in
situations where you weren't sure if you had an honest Integer or an
integer masquerading as a Rational.)

-cc

Robert Bradshaw

unread,
Apr 22, 2009, 3:05:36 AM4/22/09
to sage-...@googlegroups.com

Another option is

sage: 3/2 + 1/2 in ZZ
True
sage: 3/2 + 1/3 in ZZ
False

- Robert


John Cremona

unread,
Apr 22, 2009, 5:00:34 AM4/22/09
to sage-...@googlegroups.com
This is precisely why we deprecated all the is_*() functions for end-user use:

----------------------------------------------------------------------
| Sage Version 3.4.1.rc4, Release Date: 2009-04-19 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------

sage: is_Integer(3/2+1/2)
/home/masgaj/.sage/temp/host_56_150/29373/_home_masgaj__sage_init_sage_0.py:1:
DeprecationWarning:
Using is_Integer from the top level is deprecated since it was
designed to be used by developers rather than end users.
It most likely does not do what you would expect it to do. If you
really need to use it, import it from the module that it is defined
in.
# -*- coding: utf-8 -*-
False

sage: (3/2+1/2).is_integral()
True

The is_*() functions just test the type of an abject (in programming terms):

sage: type(3/2+1/2)
<type 'sage.rings.rational.Rational'>

and the result of 3/2+1/2 is of type Rational. Mathematically, of
course, the same number can be an integer and a rational (and a real
and a complex and ...).

John

2009/4/22 Robert Bradshaw <robe...@math.washington.edu>:

nirmal

unread,
Apr 24, 2009, 1:59:38 PM4/24/09
to sage-devel
Thanks for all the helpful suggestions. I did not realize that
is_Integer() was deprecated.
> 2009/4/22 Robert Bradshaw <rober...@math.washington.edu>:

William Stein

unread,
Apr 24, 2009, 7:35:20 PM4/24/09
to sage-...@googlegroups.com
On Fri, Apr 24, 2009 at 10:59 AM, nirmal <nrsa...@gmail.com> wrote:
>
> Thanks for all the helpful suggestions. I did not realize that
> is_Integer() was deprecated.

How could you not notice? If I do is_Integer I get a big DeprecationWarning:

sage: is_Integer(3)
/Users/wstein/.sage/temp/D_69_91_158_76.dhcp4.washington.edu/15220/_Users_wstein__sage_init_sage_0.py:1:
DeprecationWarning:
Using is_Integer from the top level is deprecated since it was
designed to be used by developers rather than end users.
It most likely does not do what you would expect it to do. If you
really need to use it, import it from the module that it is defined
in.
# -*- coding: utf-8 -*-
True
sage:

Does your Sage not do that?

William
--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

Nick Alexander

unread,
Apr 24, 2009, 11:24:02 PM4/24/09
to sage-...@googlegroups.com
> Another option is
>
> sage: 3/2 + 1/2 in ZZ
> True
> sage: 3/2 + 1/3 in ZZ
> False

I just ran into the "True in ZZ" returns True thing again. How do I
check to see if I passed an option or "True"?

Nick

Robert Bradshaw

unread,
Apr 24, 2009, 11:42:16 PM4/24/09
to sage-...@googlegroups.com

You can do "x is True"

- Robert

William Stein

unread,
Apr 25, 2009, 2:23:36 AM4/25/09
to sage-...@googlegroups.com

Also you could do

isinstance(x, bool) and x

but I like Robert's solution better.

William

nirmal

unread,
Apr 25, 2009, 10:16:12 AM4/25/09
to sage-devel
> How could you not notice?   If I do is_Integer I get a big DeprecationWarning:
> Does your Sage not do that?

The reason I did not notice it is that is_Integer() was in the body of
a main loop and no warnings were printed.

For example,

[n for n in range(0,10) if is_Integer(n+1)]

returns

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

However

[n for n in range(0,10) if is_Integer(n)]

returns null list

[ ]

In the first case the answer was correct and in the second case the
answer was wrong. In both cases, please note no warning was printed.
My sage version is 3.4 and using it on Mac OS X.

I am using Robert's solution. It seems to work well.

Thanks,
Nirmal

nirmal

unread,
Apr 25, 2009, 10:19:12 AM4/25/09
to sage-devel
> How could you not notice?   If I do is_Integer I get a big DeprecationWarning:
> Does your Sage not do that?

Nick Alexander

unread,
Apr 25, 2009, 1:17:58 PM4/25/09
to sage-...@googlegroups.com
> [n for n in range(0,10) if is_Integer(n+1)]

snip

> [n for n in range(0,10) if is_Integer(n)]

You are doing this from the command line, yes? The first is getting
preparsed, so that 1 is not a python int, it is a sage Integer:

sage: preparse('[n for n in range(0,10) if is_Integer(n+1)]')
'[n for n in range(Integer(0),Integer(10)) if is_Integer(n+Integer(1))]'
sage: preparse('[n for n in range(0,10) if is_Integer(n)]')
'[n for n in range(Integer(0),Integer(10)) if is_Integer(n)]'

As for the warnings not being printed, in the loop you are maybe not
considered to be in Python global scope? I cannot say.

Nick

nirmal

unread,
Apr 25, 2009, 1:32:55 PM4/25/09
to sage-devel

> snip
>
> > [n for n in range(0,10) if is_Integer(n)]
>
> You are doing this from the command line, yes?  

I am doing this in the notebook()

-Nirmal
Reply all
Reply to author
Forward
0 new messages