简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM