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

7asd23hasd
  • 293
  • 1
  • 5

1 Answers1

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)
Community
  • 1
  • 1
7asd23hasd
  • 293
  • 1
  • 5
  • 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. – hpaulj Apr 21 '16 at 12:24