How to test if something is integer ?

1,624 views
Skip to first unread message

Laurent

unread,
Mar 15, 2012, 7:30:56 AM3/15/12
to sage-support
Hi all

Why does 'sage.rings.integer.Integer' not have "is_integer" method ??

sage: A=4
sage: A.is
A.is_idempotent A.is_nilpotent A.is_perfect_power A.is_prime
A.is_square A.is_zero
A.is_integral A.is_norm A.is_power
A.is_prime_power A.is_squarefree A.isqrt
A.is_irreducible A.is_one A.is_power_of
A.is_pseudoprime A.is_unit

while :

sage: A.is_
A.is_constant A.is_negative A.is_one
A.is_real A.is_square A.is_unit
A.is_idempotent A.is_nilpotent A.is_polynomial
A.is_relational A.is_symbol A.is_zero
****A.is_integer**** A.is_numeric A.is_positive
A.is_series A.is_terminating_series

In order to test of something is integer, is it safe to use isinstance ?
isinstance(A,sage.rings.integer.Integer)


I've missed something ?

Thanks
Laurent

Simon King

unread,
Mar 15, 2012, 8:23:29 AM3/15/12
to sage-support
Hi Laurent,

On 15 Mrz., 12:30, Laurent <moky.m...@gmail.com> wrote:
> In order to test of something is integer, is it safe to use isinstance ?
> isinstance(A,sage.rings.integer.Integer)

There is a (deprecated) function is_Integer, that does exactly the
"isinstance" test. Whether it is safe or not depends on you
application.

Namely, when you say "test if something is integer", the test *very*
much depends on what you want to do with it. Here are four different
settings that would require different tests for integrality.

1.
If you want to use the underlying C data type of Sage integers, then
of course you need an "isinstance(...)" test.

2.
If you want to use certain methods that are provided by Sage integers,
but not by Sage rationals, then you might consider "duck typing" the
integer: You test whether the object has the methods that you need,
and then you use them, regardless whether the object really is a Sage
Integer or not. For example:
sage: hasattr(SR(5),'xgcd')
False
sage: hasattr(5,'xgcd')
True
sage: hasattr(QQ['x'](5),'xgcd')
True
sage: hasattr(5/1,'xgcd')
False
So, if you really just want to use the xgcd method, then you could
pretend that QQ['x'](5) (that's to say: The number 5 interpreted as a
polynomial of degree zero with rational coefficients) is an integer,
but the rational number 5/1 is not an integer.

3.
If, in your application, it is enough to know whether the given object
x is equal to an integer, then you could do "x in ZZ". Note that this
property has nothing to do with the type!
sage: 5 in ZZ
True
sage: type(5)
<type 'sage.rings.integer.Integer'>
sage: 5/1 in ZZ
True
sage: type(5/1)
<type 'sage.rings.rational.Rational'>
sage: QQ['x'](5) in ZZ
True
sage: type(QQ['x'](5))
<type
'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>
sage: QQ['x','y'](5) in ZZ
True
sage: type(QQ['x','y'](5))
<type
'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'>
sage: SR(5) in ZZ
True
sage: type(SR(5))
<type 'sage.symbolic.expression.Expression'>
sage: int(5) in ZZ
True
sage: type(int(5))
<type 'int'>
Hence, from that point of view, QQ['x'](5) and 5/1 are integers. But
perhaps you don't like
sage: GF(7)(5) in ZZ
True
If you don't like finite field elements being equal to integers, you
have to think of yet another test.

4.
Or perhaps you are number theorist and consider elements of number
fields? Then, "being an integer" means "being root of a monic
polynomial". That property is tested by the "is_integral" method, that
is available for a couple of classes:
sage: def test_integral(x):
....: return hasattr(x,'is_integral') and x.is_integral()
....:
sage: test_integral(5)
True
sage: test_integral(5/1)
True
sage: test_integral(int(5))
# Python ints don't have the is_integral method
False
sage: test_integral(NumberField(x^2-2,'y')(5))
True
sage: test_integral(NumberField(x^2-2,'y')('y')/2)
False

And then
sage: test_integral(NumberField(x^2-2,'y')('y'))
True
sage: test_integral(sqrt(2))
False
sage: NumberField(x^2-2,'y')('y')^2 == 2
True
(so, the generator of the number field somehow is a square root of 2,
but sqrt(2) is a symbolic expression, not an element of a number
field)

CONCLUSION:

We already have four different meanings of the question "Is x an
integer?". Since different meanings of the question require different
answers, there is certainly not *one* single test for "bein an
integer" in Sage.

Best regards,
Simon

Laurent

unread,
Mar 15, 2012, 8:35:34 AM3/15/12
to sage-s...@googlegroups.com

> 3.
> If, in your application, it is enough to know whether the given object
> x is equal to an integer, then you could do "x in ZZ". Note that this
> property has nothing to do with the type!

>


> CONCLUSION:
>
> We already have four different meanings of the question "Is x an
> integer?". Since different meanings of the question require different
> answers, there is certainly not *one* single test for "bein an
> integer" in Sage.

Well. I didn't think to that. Thanks very much for your answer. I take
the third solution :)
In my case, I just want to make the difference between numbers like pi/2
and 90 in order to guess if the user is thinking about degrees or radian.

Laurent

John Cremona

unread,
Mar 15, 2012, 11:36:43 AM3/15/12
to sage-support
Laurent,

I your question you gave an example where you set A=4, after which A
has the type Integer:

sage: A=4
sage: type(A)
<type 'sage.rings.integer.Integer'>

and it would not make very much sense to provide a method for this
class to test for integrality, since every such element is an Integer
by definition. I suspect that in your intended application, A will be
the result of come computation resulting in a real number, and you
want to test whether it is (at least approximately) integral. Here
are some ideas:

sage: A = 4.001
sage: A in ZZ
False
sage: A == round(A)
False

sage: A=4.0
sage: A in ZZ
True
sage: A == round(A)
True

but you have to be careful:
sage: x = RR(exp(pi*sqrt(163)))
sage: x-round(x)
0.000000000000000
sage: x in ZZ
True
sage: x = RealField(100)(exp(pi*sqrt(163)))
sage: x-round(x)
-2.2737367544323205947875976562e-12
sage: x in ZZ
False

John Cremona

Laurent

unread,
Mar 15, 2012, 11:48:07 AM3/15/12
to sage-s...@googlegroups.com

> and it would not make very much sense to provide a method for this
> class to test for integrality, since every such element is an Integer
> by definition. I suspect that in your intended application, A will be
> the result of come computation resulting in a real number, and you
> want to test whether it is (at least approximately) integral. Here
> are some ideas:

You are guessing almost right.
In my case A is an user-given angle value that will be passed to
trigonometric functions.
I want to guess if the user gave radiant or degree.
My first test if

if "pi" in repr(A)

I cannot believe that someone will provide an angle with "pi" in degree.
In that case, I deal with A as radian.

Then my second test is to see if A is integer. One almost never deal
with integer radian. So if the A is integer, I will deal with it as degree.

I'm expecting some problems with degree values given as
pi/2 * 180/pi
(result of a conversion)


But well, up to now my function works :)

Thanks
Laurent

Robert Bradshaw

unread,
Mar 15, 2012, 1:15:58 PM3/15/12
to sage-s...@googlegroups.com

You'd want to make sure this behavior is well documented, otherwise it
could have unexpected behavior (e.g. what happens if you try to plot
it? Is 10.5 treated as a degree or radian?)

Laurent

unread,
Mar 15, 2012, 1:25:36 PM3/15/12
to sage-s...@googlegroups.com

> You'd want to make sure this behavior is well documented, otherwise it
> could have unexpected behavior (e.g. what happens if you try to plot
> it? Is 10.5 treated as a degree or radian?)

I'll be prudent since I am the user. It is not intended to be plotted.
What I'm doing is a class "Angle" that represent an angle and I'm
creating a method __add__ in such a way that

A=Angle(degree=45)
A+pi/2

will represent the angle 3pi/4

A=Angle(radian=pi)
A-180

will represent the angle 0.

But well, following your advise, I'm keeping an eye on the behavior of
that. I'll be mercyless against my function if I see a border effect.

Laurent

Reply all
Reply to author
Forward
0 new messages