Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is_real method should exist for elements of AA, ZZ, QQ,..., and be fixed where it is already defined #16436

Open
seblabbe opened this issue Jun 3, 2014 · 8 comments

Comments

@seblabbe
Copy link
Contributor

seblabbe commented Jun 3, 2014

The function is_real is useful but it is not practical because it it not implemented for every types of numbers in Sage. And I do not want to do type checking before being able to use it in my code.

sage: CC(3).is_real()
True
sage: RR(3).is_real()
True
sage: SR(3).is_real()
True
sage: ZZ(3).is_real()
Traceback (most recent call last):
...
AttributeError: 'sage.rings.integer.Integer' object has no attribute 'is_real'
sage: QQ(3).is_real()
Traceback (most recent call last):
...
AttributeError: 'sage.rings.rational.Rational' object has no attribute 'is_real'
sage: AA(3).is_real()
Traceback (most recent call last)
...
AttributeError: 'AlgebraicReal' object has no attribute 'is_real'
sage: AlgebraicNumber(3).is_real()
Traceback (most recent call last)
...
AttributeError: 'AlgebraicNumber' object has no attribute 'is_real'

sage: RDF(3).is_real()
AttributeError: 'sage.rings.real_double.RealDoubleElement' object has no attribute 'is_real'

sage: QQbar(3).is_real()
AttributeError: 'AlgebraicNumber' object has no attribute 'is_real'

I am sure I forget some other rings...

We need more of this kind of consistency in Sage. is_imaginary is another example.

The .is_real() method does not handle NaN and +/-Infinity correctly, for example:

sage: CC(NaN).is_real()
True

Component: algebra

Issue created by migration from https://trac.sagemath.org/ticket/16436

@seblabbe seblabbe added this to the sage-6.3 milestone Jun 3, 2014
@sagetrac-tmonteil
Copy link
Mannequin

sagetrac-tmonteil mannequin commented Jun 4, 2014

comment:1

Hi Sebastien. Note that you can also use coercion with the poorest real representation:

sage: AA(3) in RealField(2)
True
sage: CC(3) in RealField(2)
True
sage: RR(3) in RealField(2)
True
sage: SR(3) in RealField(2)
True
sage: ZZ(3) in RealField(2)
True
sage: QQ(3) in RealField(2)
True
sage: AA(3) in RealField(2)
True
sage: QQbar(3) in RealField(2)
True

though you need to be careful with NaN and +/-Infinity that also belong to RealField(2). By the way:

sage: SR(Infinity).is_real()
True

@seblabbe

This comment has been minimized.

@seblabbe
Copy link
Contributor Author

seblabbe commented Jun 4, 2014

comment:3

Replying to @sagetrac-tmonteil:

Hi Sebastien. Note that you can also use coercion with the poorest real representation:

I have been using monkey tricks like that for too long now like .n().imag() == 0. I want a clean solution. It should be normal to ask if some eigenvalue of some matrix is a real number.

sage: M = identity_matrix(RR,4)
sage: [a.is_real() for a in M.eigenvalues()]
[True, True, True, True]
sage: M = identity_matrix(ZZ,4)
sage: [a.is_real() for a in M.eigenvalues()]
Traceback (most recent call last):
...
AttributeError: 'sage.rings.rational.Rational' object has no attribute 'is_real'
sage: M = random_matrix(ZZ,4)
sage: [a.is_real() for a in M.eigenvalues()]
Traceback (most recent call last):
...
AttributeError: 'AlgebraicNumber' object has no attribute 'is_real'

@seblabbe

This comment has been minimized.

@sagetrac-tmonteil
Copy link
Mannequin

sagetrac-tmonteil mannequin commented Jun 4, 2014

comment:5

If, as i proposed on sage-devel we could have an overlay (say, RR) for real fields, the syntax CC(3) in RR would be very readable. The only problem with CC(3) in RealField(2) is about NaN and +/-Infinity, but you should notice that the same problem exists with the current .is_real() methods, so the solution is not clean either.

@sagetrac-tmonteil

This comment has been minimized.

@sagetrac-tmonteil sagetrac-tmonteil mannequin changed the title is_real method should exist for elements of AA, ZZ, QQ is_real method should exist for elements of AA, ZZ, QQ,..., and be fixed where it is already defined Jun 4, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.3, sage-6.4 Aug 10, 2014
@rwst
Copy link

rwst commented Mar 21, 2015

comment:8

Replying to @sagetrac-tmonteil:

Hi Sebastien. Note that you can also use coercion with the poorest real representation:

sage: AA(3) in RealField(2)
True

See #12967 and #17984 for why x in RR will determine elementship in RR not in the mathematical R. For this is_real is meant.

@kedlaya
Copy link
Contributor

kedlaya commented Apr 9, 2016

comment:9

One clean way to implement is_real might be to compare x with its conjugate, which is implemented quite often:

sage: def is_real(x):
....:     return(x == x.conjugate())
....:
sage: is_real(ZZ(3))
True
sage: is_real(QQ(3))
True
sage: is_real(AA(3))
True
sage: is_real(RR(3))
True
sage: is_real(CC(3))
True

One could handle is_imaginary similarly.

@mkoeppe mkoeppe removed this from the sage-6.4 milestone Dec 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants