How should I get the corresponding real numpy dtype for a complex dtype?

Viewed 167

Is there a more elegant way to get the corresponding real numpy dtype for a possibly complex dtype than the following?

import numpy as np

def dtype_to_real(rvs_dtype: np.dtype[Any]) -> np.dtype[Any]:
    return np.empty(0, dtype=rvs_dtype).real.dtype
1 Answers

As long as the dtypes are not base classes, you should be able to call them with a numeric argument, like 0.

import numpy as np

def dtype_to_real(rvs_dtype: np.dtype[Any]) -> np.dtype[Any]:
    return rvs_dtype.type(0).real.dtype
Related