pythran icon indicating copy to clipboard operation
pythran copied to clipboard

return a np.float64

Open paugier opened this issue 5 years ago • 2 comments

I'd like to have this supported by Transonic:

import numpy as np
from transonic import boost, Type, Array

A = Array[Type(np.float32, np.float64), "1d"]

@boost
def mysum(arr: A):

    result: A.dtype
    result = arr.dtype.type(0)
    i: np.int32

    for i in range(arr.shape[0]):
        result += arr[i]

    return result

data = np.ones(100, dtype=np.float32)
result = mysum(data)
assert np.allclose(result, 100.)
assert result.dtype == np.float32

data = np.ones(100)
result = mysum(data)
assert np.allclose(result, 100.)
assert isinstance(result, (float, np.float64))
assert result.dtype == np.float64

It looks like Cython because the goal is to produce efficient Cython code...

For Pythran, it gives:

# pythran export mysum(float32[:])
# pythran export mysum(float64[:])

def mysum(arr):
    result = arr.dtype.type(0)
    for i in range(arr.shape[0]):
        result += arr[i]
    return result

and it nearly works! The only problem is with the last line: assert result.dtype == np.float64. For the float64 case, Pythran returns a Python float (not like with Python-Numpy) whereas for the float32 case, Pythran returns a np.float32 (like with Python-Numpy).

So it's only a tiny inconsistency...

paugier avatar Sep 10 '19 09:09 paugier

We have an issue in pythran here: pythran represents both python's float and numpy's float64 as a C's double. So we currently have no way to decide if the double should be turned into a np.float64 or builtins.float :-/

serge-sans-paille avatar Sep 10 '19 11:09 serge-sans-paille

ok, now I understand better, the problem is that pythran doesn't support 0-dimension array, and uses a regular scalar instead...

serge-sans-paille avatar Jun 07 '20 13:06 serge-sans-paille