pyccel icon indicating copy to clipboard operation
pyccel copied to clipboard

np.array chooses wrong dtype

Open EmilyBourne opened this issue 2 years ago • 3 comments

Describe the bug np.array chooses wrong dtype from its arguments

To Reproduce Provide code to reproduce the behavior:

@types('int8', 'int8')
def return_array(a, b):
    from numpy import array
    x = array([a,b])
    return x[0]

The return type is int64, but it should be int8

Workaround

@types('int8', 'int8')
def return_array(a, b):
    from numpy import array
    x = array([a,b], dtype=type(a))
    return x[0]

EmilyBourne avatar Jun 29 '22 15:06 EmilyBourne

Beware when debugging. int8 has precision=1, int64 has precision=8

EmilyBourne avatar Jun 29 '22 15:06 EmilyBourne

Sorry if I interpreted it wrong I am a beginner in python. ( I don't know why @types is giving errors( i am attaching screenshots))

The code below is giving correct output

import numpy as np
x=np.int8(3)
y=np.int8(4)
def return_array(a, b):
    from numpy import array
    x = array([a,b], dtype=type(a))
    return x[0]
print(type(return_array(x,y)))

Output is : <class 'numpy.int8'>

Code output ss:- image

Error ss : - image

itstechaj avatar Jul 12 '22 21:07 itstechaj

You have an error because you haven't imported the types decorator:

from pyccel.decorators import types

Pyccel doesn't care about this as we haven't found a simple way to follow python conventions and still be able to use epyccel on a function

EmilyBourne avatar Jul 13 '22 05:07 EmilyBourne