[PYTHON MATRIX-SIG] Odd behavior of "Numeric.fromfunction"

Charles G Waldman cgw@pgt.com
Wed, 13 Nov 1996 17:03:33 -0500



Can anyone explain to me whether the following is a feature or a bug?



Python 1.4 (Oct 31 1996)  [GCC 2.7.2]
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import ni,Numeric
>>> def g(a):
...     return a
... 
>>> x = Numeric.fromfunction(g,[10])
>>> x
0 1 2 3 4 5 6 7 8 9
>>> len(x)
10
>>>
>>>
>>> def g(a):
...     return 5
... 
>>> x = Numeric.fromfunction(g,[10])
>>> x
5
>>> len(x)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
TypeError: len() of unsized object
>>>
>>>
>>> def g(a):
...     return a*0 + 5
... 
>>> x = Numeric.fromfunction(g,[10])
>>> x
5 5 5 5 5 5 5 5 5 5
>>> len(x)
10
>>> 

This has me quite puzzled.  If the initializer function "g" returns a constant,
I get a scalar rather than an array, even though I have asked for a shape
of [10].  If the function "g" uses its argument (here in a trivial way, 
multiplied by zero!) then I get the behavior I expect.

I first noticed this trying to use "fromfunction" with a random-number 
generator, to create arrays of random numbers.  My code currently works,
but it has the strange "multiply by zero" stuck in there, so it looks
pretty weird.  I'd like to have a better understanding of what's going
on.  Thanks in advance for any comments!


=============================
| Charles G. Waldman        |      
| Princeton Gamma-Tech, Inc.|
| cgw@pgt.com               |
=============================





=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================