ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> numpy-discussion
numpy-discussion
Re: [Numpy-discussion] rank-0 arrays
by Tim Hochberg other posts by this author
Sep 16 2002 7:35PM messages near this date
RE: [Numpy-discussion] rank-0 arrays | Re: [Numpy-discussion] rank-0 arrays
From: "Konrad Hinsen" <hinsen@[...].fr> 
>  > Travis Oliphant wrote:
>  > Would they be recognised as scalars by Python? In particular, could you
> 
>  There is no "scalar" category in Python. New scalar datatypes would be
>  types with the same behaviour as the existing Python scalar types, but
>  different from the. That means that explicitly type-checking code would
>  not accept them, but everything else would.
> 
>  > use one as an index? Personally, this is what has bit me in the past: I
> 
>  At least up to Python 1.5, no, indices have to be of integer type. I
>  don't know if that condition was extended in later versions.

In python 2.2 (I'm not sure about 2.0,2.1), you can subclass int and the
resultant class can be used as an index. So I think this could be done.

>  > Frankly, I have no idea what the implimentation details would be, but
>  > could we get rid of rank-0 arrays altogether? I have always simply found
> 
>  If we introduce additional scalars, yes.

Given that numarray has changed its coercion rules, is this still necessary?
From http://stsdas.stsci.edu/numarray/Userguide.html:

----------start quote----------
*************
Type Coercion
*************

In expressions involving only arrays, the normal coercion rules
apply (i.e., the same as Numeric). However, the same rules do
not apply to binary operations between arrays and Python scalars
in certain cases. If the kind of number is the same for the array
and scalar (e.g., both are integer types or both are float types),
then the type of the output is determined by the precision of the
array, not the scalar. Some examples will best illustrate:

Scalar type  *  Array type        Numeric result type    numarray result
type
   Int            Int16                Int32                    Int16
   Int            Int8                 Int32                    Int8
   Float          Int8                 Float64                  Float64
   Float          Float32              Float64                  Float32

The change in the rules was made so that it would be easy to preserve the
precision of arrays in expressions involving scalars. Previous solutions
with Numeric were either quite awkward (using a function to create a rank-0
array from a scalar with the desired type) or surprising (the savespace
attribute, that never allowed type coercion). The problem arises because
Python has a limited selection of scalar types. This appears to be the
best solution though it admittedy may surprise some who are used to the
classical type coercion model.

----------end quote----------


>  Whether or not we would want
>  to get rid of them is of course another question.

Agreed. It's possible that rank zero arrays would be useful in certain
unusual situations, although I can't think of any good examples. Regardless
of that choice, if indexing never returns rank-0 arrays most people will
never have to deal with them.

-tim





-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@[...].net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion
Thread:
Perry Greenfield
Travis Oliphant
Pearu Peterson
Konrad Hinsen
Perry Greenfield
Todd Miller
Pearu Peterson
Konrad Hinsen
Travis Oliphant
Konrad Hinsen
Perry Greenfield
Travis Oliphant
Tim Hochberg
Chris Barker
Konrad Hinsen
Perry Greenfield
Travis Oliphant
Pearu Peterson
Tim Hochberg
Perry Greenfield
Konrad Hinsen
Perry Greenfield
eric jones
Konrad Hinsen
eric jones
Konrad Hinsen
Tim Hochberg
Tim Hochberg
Paul F Dubois
Scott Gilbert
Konrad Hinsen

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved