0

I have the following question concerning universal functions in numpy. How can I define a universal function which returns the same type of numpy array as the numpy build-in functions. The following sample code:

import numpy as np
def mysimplefunc(a):
    return np.sin(a)
mysimpleufunc = np.frompyfunc(mysimplefunc,1,1)
a = np.linspace(0.0,1.0,3)
print(np.sin(a).dtype)
print(mysimpleufunc(a).dtype)

results in the output:

float64
object

Any help is very much appreciated :)

PS.: I am using python 3.4

1 Answer 1

0

I found the solution (see also discussion on stackoverflow): use vectorize instead of frompyfunc

import numpy as np
def mysimplefunc(a):
    return np.sin(a)
mysimpleufunc = np.vectorize(mysimplefunc)
a = np.linspace(0.0,1.0,3)
print(np.sin(a).dtype)
print(mysimpleufunc(a).dtype)
Sign up to request clarification or add additional context in comments.

1 Comment

I knew from other SO discussions that frompyfunc returned object and was faster, but didn't realize it was used by vectorize. vectorize is often misused as a replacement for simple iteration.

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.