[Numpy-discussion] Array scalar math ideas

Sasha ndarray at mac.com
Fri Mar 17 15:44:03 CST 2006

On 3/17/06, Colin J. Williams <cjw at sympatico.ca> wrote:
> Sasha wrote:
> ...
> >http://aspn.activestate.com/ASPN/Mail/Message/numpy-discussion/3028210
> ...
> Many thanks, this sets things out clearly.  On a first scan, one thing
> caught my eye:
>     I think we should implement Python scalars for the other types and then
>     eliminate rank-0 arrays completely. I could have a student do this in a
>     few weeks if it was agreed on.
> -Travis Oliphant [13-Sep-02]
I expressed my view when I started "A case for rank-0 arrays" thread.
(See link above.)  In summary, dimensionless arrays and scalars are
different beasts and both have a right to live.

> The Wiki sems to start with the design decision to implement zero
> dimension arrays.
> It then goes on to explore the problems created by that design decision.

The wiki has a short section on "The Need for Zero-Rank Arrays." 
Please feel free to take over that page and add you criticism of the
status quo.  I think wiki is a better medium to discuss this issue
than the mailing list.

> For matrices, we accept the rule that a matrix has two dimensions, not more
> not less.  What problems would be created if we had the rule that an array
> has not less than one dimension?
For me the main problem is the dificulty to write generic code both in
C and in Python.
Most issues stem from the fact that scalars are immutable while arrays
(including dimentionless) are not.

> The little script below produces different results, depending on the ordering
> of the operands.  This provides another challenge for the newcomer.

I don't understand how this is related to dimensionless arrays.  Looks
like an array scalar feature:

>>> type(2.0 * int_(1))
<type 'float'>
>>> type(int_(1) * 2.0)
<type 'float64scalar'>

(I believe this can be fixed once scalar math is implemented.)

Dimensionless arrays seem to be more consistent in this particular case:

>>> type(array(1) * 2.0)
<type 'float64scalar'>
>>> type(2.0*array(1))
<type 'float64scalar'>

I believe  type(array(1) * 2.0) should be ndarray, but that's a different story.

>     # arrayStuff.py
>     def inArrayWorld():
>       import numpy as _n
>       a= _n.arange(4)
>       return a[2]
>     def anotherWorld(a):
>       import math
>       w= 2.6 * a
>       x= a * 2
>       y= 2.6 * a
>       z= math.log10(a)
>       return z, type(z), type(y), type(x), type(w)
>     print anotherWorld(inArrayWorld())

More information about the Numpy-discussion mailing list