I'm using two numpy arrays of floating point numbers
Rs = np.linspace(*realBounds, realResolution, dtype=fdtype)
Is = np.linspace(*imagBounds, imagResolution, dtype=fdtype)
to make a grid Zs of complex numbers with shape (realResolution, imagResolution). I'd like to only specify the datatype of Zs and use this to determine the float datatype fdtype.
As I understand it from this page, complex datatypes are always represented as two floats with the same datatype, hence why Rs and Is both have dtype=fdtype. If the datatype of Zs is specified as np.csingle then I'd like fdtype to be np.single, if it's specified as np.dtype("complex128") then I'd like fdtype to be np.dtype("float64"), and so on. Is there a nice way to do this?
Edit: I'm equally interested in the inverse function which sends, for example, np.single to np.csingle.
I don't think there is such a function in numpy but you can easily roll your own using
num, which uniquely identifies each of the built-in types:The dict was obtained from
{np.dtype(dt).num: dt for dt in (np.single, np.double, np.longdouble, np.csingle, np.cdouble, np.clongdouble)}which returnson
win-amd64(sysconfig.get_platform()). The names may vary, e.g. onlinux-x86_64it showsnumpy.complex256instead ofnumpy.clongdouble, but thanks tonumyou don't need to pay attention to it.So for instance for all of
np.double,np.float_,np.float64,float,'float64'etc.,get_corresonding_dtypewill returnnumpy.complex128.If you like you can also add
np.halfas23: np.csingleto the dict, as there's no complex half/float16 type in numpy.