AA10
AA10

Reputation: 207

Numpy vectorizing function

I have this function fs(i,j,dif). It is easy to vectorize this function by doing vfunc = np.vectorize(fs) The thing is, I want calculate the output of this function for

i=0, j=1,2,3,4,5, ...ysize-1

i=1, J=1,2,3,4,5, ...ysize-1

....
i=xsize-1,  j=1,2,3,4,5 ... ysize-1

For one value of i, there is no problem with vfunc(0, np.arange(ysize), 0) (dif=0)

But I can't find out how to do it for all values of i.

The only way I manage to do it was

vfunc([[0],[1],[2],...[xsize-1]], np.arange(ysize), 0)

which is not feasible for a large xsize. Is there a way to do it?

Upvotes: 0

Views: 51

Answers (1)

Jann
Jann

Reputation: 645

I understand your question as follows. You want to know how to express the list [[0],[1],[2],...[xsize-1]] in terms of xsize? List comprehension does the job for you. [[0],[1],[2],...[xsize-1]]=[[i] for i in range(xsize)] The vectorize function can then be called as follows (for an example function fs)

import numpy as np

xsize=10
ysize=15

def fs(i,j,dif):
    return i+j

np.vectorize(fs)([[i] for i in range(xsize)],np.arange(ysize),0)

Upvotes: 1

Related Questions