Unexpected dtype returned when operating with Float32 / ComplexFloat32 tensors and numpy scalars
Description
Unexpected dtype are returned when performing operations involving Float, ComplexFloat Tensors and numpy.float32, numpy.complex64.
Expected Behavior
cytnx.Type.Float / numpy.float32 -> cytnx.Float
cytnx.Type.ComplexFloat / numpy.float32 -> cytnx.Type.ComplexFloat
cytnx.Type.ComplexFloat / numpy.complex64 -> cytnx.Type.ComplexFloat
Actual Behavior
However, all of the above cases currently return ComplexDouble (Complex Float64).
A notebook demonstrating the issue is attached: Float32_issue.ipynb
Thank you for the report and the helpful notebook. This is an issue on the python side: numpy objects are converted to python-specific types (ComplexDouble, Double, Int, Bool) before they are handed to Cytnx vie pybind. Then, arithmetic operations result in an output type that corresponds to the higher precision, which is then the Python-native type.
I wrote explicit pybindings for numpy scalars in #699. This should solve the issue in most cases (and covers all cases in the example provided).
The following code is copied from the notebook for easier reference.
A = cytnx.ones([2,2,2,2]).astype(cytnx.Type.ComplexFloat)
B = numpy.float32(1)
C = A / B
print(A.dtype_str())
print(B.dtype)
print(C.dtype_str())
output:
Complex Float (Complex Float32)
float32
Complex Double (Complex Float64)
Is operating with numpy scalar a feature we want to support?
All cases mentioned here are fixed by #699 However, there are still some cases that do not work yet and have to be fixed. In particular, an operation between a numpy object and a UniTensor object does not work if the numpy object is on the left. For example
A = cytnx.ones([2,2,2,2]).astype(cytnx.Type.ComplexFloat)
B = numpy.float32(1)
C = A * B # works correctly
D = B * A # does not work correctly