[Numpy-discussion] getting the equivalent complex dtype from a real or int array

Henry Gomersall heng at cantab.net
Tue Oct 29 13:36:11 EDT 2013

On 29/10/13 17:02, Robert Kern wrote:
> Quick and dirty:
> # Get a tiny array from `a` to test the dtype of its output when 
> multiplied
> # by a complex float. It must be an array rather than a scalar since the
> # casting rules are different for array*scalar and scalar*scalar.
> dt = (a.flat[:2] * 1j).dtype
> b = np.empty(shape, dtype=dt)
Yeah, I thought about that, but it seemed a little *too* dirty.

I'm currently doing this, which is fairly elegant, but a bit wordy:

_rc_dtype_pairs = {numpy.dtype('float32'): numpy.dtype('complex64'),
         numpy.dtype('float64'): numpy.dtype('complex128'),
         numpy.dtype('longdouble'): numpy.dtype('clongdouble'),
         numpy.dtype('complex64'): numpy.dtype('float32'),
         numpy.dtype('complex128'): numpy.dtype('float64'),
         numpy.dtype('clongdouble'): numpy.dtype('longdouble')}

_default_dtype = numpy.dtype('float64')

if a.dtype not in _rc_dtype_pairs:



More information about the NumPy-Discussion mailing list