Hi Laurent,
On 15 Mrz., 12:30, Laurent <[email protected]> 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
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org