[SciPy-user] difference between x, array(x) and array([x])

Steve Schmerler elcorto at gmx.net
Sun Apr 29 06:14:59 EDT 2007


I saw this sometimes but never really paid attention to it:
While playing with scipy.factorial I found that it returns array(<number>):

In [23]: scipy.factorial(3)
Out[23]: array(6.0)

E.g. 6 is a sacalar, array([6]) an 1D-array of len 1, but array(6)?

In [8]: import numpy as N

In [9]: type(N.array([6]))
Out[9]: <type 'numpy.ndarray'>

In [10]: type(N.array(6))
Out[10]: <type 'numpy.ndarray'>

So, array(6) is an array.

But this looks as if it is a scalar:

In [11]: N.array(6) == N.array([6])
Out[11]: array([ True], dtype=bool)

In [12]: 6 == N.array([6])
Out[12]: array([ True], dtype=bool)

In [13]: 6 == N.array(6)
Out[13]: True

In [14]: N.array([6])[0]
Out[14]: 6

In [15]: N.array(6)[0]
---------------------------------------------------------------------------
exceptions.IndexError                                Traceback (most recent call last)

/home/elcorto/<ipython console>

IndexError: 0-d arrays can't be indexed

OK, array(6) is a 0-d array. What is the advantage of returning an 0-d array object
array(6) instead of just 6?

-- 
cheers,
steve

I love deadlines. I like the whooshing sound they make as they fly by. -- Douglas Adams




More information about the SciPy-User mailing list