Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ufunc signature for true_divide and sqrt seem a little odd #40

Open
tdimitri opened this issue Oct 23, 2020 · 1 comment
Open

ufunc signature for true_divide and sqrt seem a little odd #40

tdimitri opened this issue Oct 23, 2020 · 1 comment

Comments

@tdimitri
Copy link
Collaborator

I tried to make a signature of two inputs:int32, int32 returning output; float64 and this failed.
I notice some inputs like int16 return float32.

On sqrt, some inputs, like int8, return float16.
I thought it was common practice for these operations to return float64 unless the input is a float16, float32 or float128.

Second issue: I believe these are examples of internal upcast loops. That is, no need to convert int32 to float64 (in a first pass creating a temp array), then divide. Rather the loop can upcast ints to floats and then divide.

@mattip
Copy link
Collaborator

mattip commented Nov 3, 2020

I tried to make a signature of two inputs:int32, int32 returning output; float64 and this failed

I am pretty sure you cannot do this via PyUFunc_ReplaceLoopBySignature . The only way (unfortunately) to add a signature is at UFunc creation time. The way NumPy does this is that a generator function spits out __umath_generated.c, which has static arrays of functions and signatures, here is the part for arccosh:

static PyUFuncGenericFunction arccosh_functions[] = { \
   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};

static void * arccosh_data[] = {(void *)NULL, (void *)NULL, \
    (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL, \
    (void *)NULL, (void *)"arccosh"};

static char arccosh_signatures[] = {NPY_HALF, NPY_HALF, 
    NPY_FLOAT, NPY_FLOAT, NPY_DOUBLE, NPY_DOUBLE, 
    NPY_LONGDOUBLE, NPY_LONGDOUBLE, NPY_CFLOAT, NPY_CFLOAT,
    NPY_CDOUBLE, NPY_CDOUBLE, NPY_CLONGDOUBLE, NPY_CLONGDOUBLE,
    NPY_OBJECT, NPY_OBJECT};

and here is the part of the generator code](https://github.com/numpy/numpy/blob/v1.19.4/numpy/core/code_generators/generate_umath.py#L618) that creates that

'arccosh':
    Ufunc(1, 1, None,
          docstrings.get('numpy.core.umath.arccosh'),
          None,
          TD(inexact, f='acosh', astype={'e':'f'}),
          TD(P, f='arccosh'),
          ),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants