Re: [Numpydiscussion] PEP 31XX: A Type Hierarchy for Numbers (and other algebraic entities)
[sage list added] "Travis E. Oliphant" <oliphant.travis@ieee.org> writes:
The SAGE people may be interested in this, but I doubt there will more than a handful of users of these algebraic base classes.
SAGE has quite a sophisticated type hierarchy, and a sophisticated set of coercion methods. What is done in SAGE should definitely be consulted, since it is probably the most complex set of mathematical types yet written in python. The SAGE tutorial is at http://www.sagemath.org/doc/html/tut/tut.html and Section 2.2 gives a brief introduction to numbers: http://www.sagemath.org/doc/html/tut/node9.html The SAGE reference manual is at http://www.sagemath.org/doc/html/ref/index.html Chapter 20: http://www.sagemath.org/doc/html/ref/node198.html and nearby chapters are quite relevant for this discussion.
For general purpose Python, I would do something like
Complex Rational_Complex Integer_Complex Floating_Complex # uses hardware float Decimal_Complex Real Rational Integer Floating # uses hardware float Decimal
Note also that doubleprecision reals are a subset of the rationals, since each double precision real is exactly representable as a rational number, but many rational numbers are not exactly representable as double precision reals. Not sure if this means that reals should be a subclass of the rationals. I believe that in SAGE these relationships aren't expressed using subclassing. Dan
On Thu, 26 Apr 2007, Dan Christensen wrote:
Note also that doubleprecision reals are a subset of the rationals, since each double precision real is exactly representable as a rational number, but many rational numbers are not exactly representable as double precision reals. Not sure if this means that reals should be a subclass of the rationals.
Not quite all: the space of doubles include a small number of things that aren't representable by a rational (+/ inf, for instance).  jan grant, ISYS, University of Bristol. http://www.bris.ac.uk/ Tel +44 (0)117 3317661 http://ioctl.org/jan/ Spreadsheet through network. Oh yeah.
On 4/27/07, Jan Grant <jan.grant@bristol.ac.uk> wrote:
On Thu, 26 Apr 2007, Dan Christensen wrote:
Note also that doubleprecision reals are a subset of the rationals, since each double precision real is exactly representable as a rational number, but many rational numbers are not exactly representable as double precision reals. Not sure if this means that reals should be a subclass of the rationals.
Not quite all: the space of doubles include a small number of things that aren't representable by a rational (+/ inf, for instance).
This suddenly makes me think of a new idea  perhaps we could changes the type of Inf and NaNs to some *other* numeric type? We could then reserve a place in the numeric hierarchy for its abstract base class. Though I don't know if this extends to complex numbers with one or both parts NaN/Inf or not.  Guido van Rossum (home page: http://www.python.org/~guido/)
On 4/27/07, Guido van Rossum <guido@python.org> wrote:
On 4/27/07, Jan Grant <jan.grant@bristol.ac.uk> wrote:
On Thu, 26 Apr 2007, Dan Christensen wrote:
Note also that doubleprecision reals are a subset of the rationals, since each double precision real is exactly representable as a rational number, but many rational numbers are not exactly representable as double precision reals. Not sure if this means that reals should be a subclass of the rationals.
Not quite all: the space of doubles include a small number of things that aren't representable by a rational (+/ inf, for instance).
This suddenly makes me think of a new idea  perhaps we could changes the type of Inf and NaNs to some *other* numeric type? We could then reserve a place in the numeric hierarchy for its abstract base class. Though I don't know if this extends to complex numbers with one or both parts NaN/Inf or not.
From the Fortress spec: "The trait Q ( QQ ) encompasses all finite rational numbers, the results of dividing any integer by any nonzero integer. The trait Q∗ ( QQ_star ) is Q with two extra elements, + ∞ and −∞ . The trait Q# ( QQ_splat ) is Q∗ with one additional element, the indefinite rational (written 0/0 ), which is used as the result of dividing zero by zero or of adding −∞ to +∞." So separating Inf and NaN into other types has some precedent. I'd also point out that A being a subset of B doesn't make it a subtype also. If operations on A behave differently than the specification of the operations on B, then As aren't substitutable for Bs and A isn't a subtype. Because doubles have finite precision and rationals don't, I don't think doubles are a subtype of the rationals, even if you juggle Nan/Inf to make them a subset. Then again, doubles aren't a group either because of this imprecision, and I'm suggesting claiming they're a subclass of that, so maybe there's room in a practical language to make them a subclass of the rationals too.  Namasté, Jeffrey Yasskin
On Fri, 27 Apr 2007, Jeffrey Yasskin wrote:
Then again, doubles aren't a group either because of this imprecision, and I'm suggesting claiming they're a subclass of that, so maybe there's room in a practical language to make them a subclass of the rationals too.
Would using language from the Scheme report be useful when discussing this? http://wwwswiss.ai.mit.edu/projects/scheme/documentation/scheme_5.html Cheers, Alan Isaac
Guido van Rossum wrote:
On 4/27/07, Jan Grant <jan.grant@bristol.ac.uk> wrote:
On Thu, 26 Apr 2007, Dan Christensen wrote:
Note also that doubleprecision reals are a subset of the rationals, since each double precision real is exactly representable as a rational number, but many rational numbers are not exactly representable as double precision reals. Not sure if this means that reals should be a subclass of the rationals.
Not quite all: the space of doubles include a small number of things that aren't representable by a rational (+/ inf, for instance).
This suddenly makes me think of a new idea  perhaps we could changes the type of Inf and NaNs to some *other* numeric type? We could then reserve a place in the numeric hierarchy for its abstract base class. Though I don't know if this extends to complex numbers with one or both parts NaN/Inf or not.
As far as NaN is concerned, I'd assert that it's no different if we're talking about reals or complex  if it's not a number, it's not a number  IMO a complex number with one part finite and another part NaN makes no sense. And as far as inf is concerned, *if* you're effectively doing your math on the Riemann sphere (or any Riemann surface with countably many cuts, if I remember correctly  someone please correct me if I'm wrong) then there is only one inf  if you need to know the direction from, or, equivalently, the curve along which you arrived at inf, then you need to keep track of that programmatically. (Of course, there might be some instances where you want to know whether an operation made the real part or the imaginary part or both NaN or inf, but then I'd assert that you should be checking for that programmatically also.) DG
Jan Grant wrote:
. . . the space of doubles include a small number of things that aren't representable by a rational (+/ inf, for instance).
+1/0, 1/0  Anton Sherwood, http://www.ogre.nu/
Anton Sherwood wrote:
Jan Grant wrote:
. . . the space of doubles include a small number of things that aren't representable by a rational (+/ inf, for instance).
+1/0, 1/0
Are not unique representations for inf/inf, (if that matters). DG
Jan Grant wrote:
. . . the space of doubles include a small number of things that aren't representable by a rational (+/ inf, for instance).
Anton Sherwood wrote:
+1/0, 1/0
David Goldsmith wrote:
Are not unique representations for inf/inf, (if that matters).
No rational representation is unique!  Anton Sherwood, http://www.ogre.nu/
Anton Sherwood wrote:
Jan Grant wrote:
. . . the space of doubles include a small number of things that aren't representable by a rational (+/ inf, for instance).
Anton Sherwood wrote:
+1/0, 1/0
David Goldsmith wrote:
Are not unique representations for inf/inf, (if that matters).
No rational representation is unique!
Of course  as soon as I hit send I knew someone was going to shoot that back at me! :) But for some reason (I'm speaking rhetorically, I know the reasons) they always drilled home nonzero/zero undef., not = inf. DG
On 4/27/07, Alan Isaac <aisaac@american.edu> wrote:
On Fri, 27 Apr 2007, Jeffrey Yasskin wrote:
Then again, doubles aren't a group either because of this imprecision, and I'm suggesting claiming they're a subclass of that, so maybe there's room in a practical language to make them a subclass of the rationals too.
Would using language from the Scheme report be useful when discussing this? http://wwwswiss.ai.mit.edu/projects/scheme/documentation/scheme_5.html
Very much so. Thanks for sending the link. I'm not going to get a chance to update the document for the next several days, so if you want to put together a patch using such language, I'd be happy to see it go in. Or I'll integrate those ideas once I have a bit more spare time. It looks like the primary ideas there are that it was worthwhile for Scheme to have the full Number:>Complex:>Real:>Rational:>Integer tower, and that it wasn't worthwhile to duplicate it entirely for the exact/inexact distinction. Thanks, Jeffrey
participants (7)

Alan Isaac

Anton Sherwood

Dan Christensen

David Goldsmith

Guido van Rossum

Jan Grant

Jeffrey Yasskin