# [Numpy-discussion] fromfunction question

john saponara john at saponara.net
Wed Aug 8 18:13:35 EDT 2007

```Thinking I could use fromfunction to generate the x,y,z coordinates of a
3D surface, I instead got separate arrays of x, y, and z coordinates (as
I should have expected) and needed to use a nested listcomp to produce
the unified array of 3D points:

x,y,z=fromfunction( lambda i,j: (xfun(i,j),yfun(i,j),zfun(i,j)), (1,3),
dtype=int )
result=[[(a,b,c) for  a,b,c in zip(p,q,r)] for p,q,r in zip(x,y,z)]

Is it possible to compute the unified array of 3-tuples in a single
step?  Below is working code.

Thanks.

--- start python session ---
r=array([[0,1],[1,0],[2,1]])
c=array([[0,1],[1,0],[2,1]])
p=array([[-1,0]])
rLen=len(r)
cLen=len(c)

# functions to compute x,y,z coordinates of 3d points (the exact
expressions are not important)
def xfun(i,j):
return r[j,0]

def yfun(i,j):
return r[j,1]*p[i,0]+c[i+1,0]

def zfun(i,j):
return r[j,1]*p[i,1]+c[i+1,1]

# the fromfunction and an extra step to arrange coordinates into 3-tuples
x,y,z=fromfunction( lambda i,j: (xfun(i,j),yfun(i,j),zfun(i,j)), (1,3),
dtype=int )
result=[[(a,b,c) for  a,b,c in zip(p,q,r)] for p,q,r in zip(x,y,z)]

print result   # prints [[(0, 0, 0), (1, 1, 0), (2, 0, 0)]]
--- end python session ---

```