[Numpy-discussion] fromfunction documentation

Paul F. Dubois paul at pfdubois.com
Mon May 28 15:31:44 EDT 2001


A previous report of this doc error had caused me to fix it in my copy but
it hadn't made it up to the web yet.
The section has been redone to make it clearer, I hope.

I am in the process of doing the update now.

The documentation source is not available to you.

Conventional use of fromfunction is like this:
>>> from Numeric import *
>>> def f (x, y):
	return x**2 + y**2

>>> a=fromfunction(f, (3,2))
>>> a
array([[0, 1],
       [1, 2],
       [4, 5]])

For your use, you need a "helper" function. Suppose g is your function.
>>> def g(x):
	return x[0] + x[1]**2

>>> def helper (x, y):
	return g([x,y])

>>> from Numeric import *
>>> a = fromfunction(helper, (3,3))
>>> a
array([[0, 1, 4],
       [1, 2, 5],
       [2, 3, 6]])

Of course, you can use a lambda to avoid explicity creating the helper.
>>> fromfunction(lambda x,y:g([x,y]), (3,3))
array([[0, 1, 4],
       [1, 2, 5],
       [2, 3, 6]])
>>>
-----Original Message-----
From: numpy-discussion-admin at lists.sourceforge.net
[mailto:numpy-discussion-admin at lists.sourceforge.net]On Behalf Of Kevin
Turner
Sent: Sunday, May 27, 2001 9:28 PM
To: numpy-discussion at lists.sourceforge.net
Subject: [Numpy-discussion] fromfunction documentation


Quoting from numdoc.pdf, dated March 31, 2001, in section
"Creating an array from a function: fromfunction()"

> The implementation of fromfunction consists of:
>
> def fromfunction(function, dimensions):
>     return apply(function, tuple(indices(dimensions)))
>
> which means that the function function is called for each element in
> the sequence indices(dimensions) [...]

This is wholly incorrect.  apply() does not call a function for each
element in the sequence, apply calls a function exactly *once*, using
the elements of the sequence for arguments.

Also, that same section of the documentation refers to "the first
example below [...] works" and "the second array [...] fails", but there
is only one example, which apparently is the failing one (as it's named
"buggy").  Having a *working* example would be nice.  =)

Perhaps this is a formatting glitch in the documentation processor, but
I couldn't find the documentation's source format.


and then...  there's always the possibility that I'm barking up the
wrong tree.  What I'm trying to achieve is this:  I've got a function
which, given a vector describing a point in n-dimensional space, returns
a value for that point.  I want a matrix of the shape I specify, filled
these values, with the array indicies as coordinates for the points.

Is that too much to ask?  ;)

Thanks,

 - Kevin Turner

[using Python version 2.0, PyNum 19.0.0]

--
Kevin Turner <acapnotic at users.sf.net> | OpenPGP encryption welcome here
http://www.purl.org/wiki/python/KevinTurner

_______________________________________________
Numpy-discussion mailing list
Numpy-discussion at lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/numpy-discussion





More information about the NumPy-Discussion mailing list