cyclops-community / ctf

Cyclops Tensor Framework: parallel arithmetic on multidimensional arrays
Other
194 stars 53 forks source link

python: scalar multiplication doesn't preserve type #114

Open jcmgray opened 3 years ago

jcmgray commented 3 years ago

Currently, multiplying an array by a non-ctf scalar, doesn't preserve e.g. single precision:

import ctf
import numpy as np

x = ctf.random.random((2,)).astype(np.float32)
print(x.dtype)
# numpy.float32

test_cs = [
    2, 
    2., 
    2j,
]

# left multiply
for c in test_cs:
    print(c, (c * x).dtype)
# 2 <class 'numpy.float64'>
# 2.0 <class 'numpy.float64'>
# 2j <class 'numpy.complex128'>

# right multiply
for c in test_cs:
    print(c, (x * c).dtype)
# 2 <class 'numpy.float64'>
# 2.0 <class 'numpy.float64'>
# 2j <class 'numpy.complex128'>

I guess one expected behaviour, i.e. matching numpy, is to produce [float32, float32, complex64]. The current behaviour might be intentional but thought I'd note this here in case.