On Thu, Jul 19, 2012 at 5:52 AM, Cheng Li <scrappedprince...@gmail.com>wrote:

> Hi All,****
>
> ** **
>
> I have spot a strange behavior of numpy.fromfunction(). The sample codes
> are as follows:****
>
> >>>  import numpy as np****
>
> >>>  def myOnes(i,j):****
>
>              return 1.0****
>
> >>>  a = np.fromfunction(myOnes,(2000,2000))****
>
> >>>  a****
>
> 1.0****
>
> ** **
>
> Actually what I expected is that the function will return a 2000*2000 2d
> array with unit value. The returned single float value really confused me.
> Is this a known bug? The numpy version I used is 1.6.1.****
>
> **
>



Your function will be called *once*, with arguments that are *arrays* of
coordinate values.  It must handle these arrays when it computes the values
of the array to be created.

To see what is happening, print the values of i and j from within your
function, e.g.:


In [57]: def ijsum(i, j):
   ....:     print "i =", i
   ....:     print "j =", j
   ....:     return i + j
   ....:

In [58]: fromfunction(ijsum, (3, 4))
i = [[ 0.  0.  0.  0.]
 [ 1.  1.  1.  1.]
 [ 2.  2.  2.  2.]]
j = [[ 0.  1.  2.  3.]
 [ 0.  1.  2.  3.]
 [ 0.  1.  2.  3.]]
Out[58]:
array([[ 0.,  1.,  2.,  3.],
       [ 1.,  2.,  3.,  4.],
       [ 2.,  3.,  4.,  5.]])


Your `myOnes` function will work if you modify it something like this:


In [59]: def myOnes(i, j):
   ....:     return np.ones(i.shape)
   ....:

In [60]: fromfunction(myOnes, (3, 4))
Out[60]:
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])



The bug is in the docstring for fromfunction.  In the description of the
`function` argument, it says "`function` must be capable of operating on
arrays, and should return a scalar value."  But the function should *not*
return a scalar value.  It should return an array of values appropriate for
the given arguments.

Warren




> **
>
> Regards,****
>
> Cheng****
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to