0

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?

3
  • 1
    That last i is np.arange(xsize)[:,None], a (xsjze,1) shape array. Commented Dec 12, 2019 at 23:40
  • Cool, then a reshape(xsize*ysize) does what I intend to obtain. Commented Dec 12, 2019 at 23:43
  • I would strongly encourage you to take a look at numba. Commented Dec 12, 2019 at 23:51

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.