<div dir="ltr"><div>We really ought to have a special page for all of Robert's little gems!</div><div><br></div><div>DG</div><div><br></div>On Tue, Oct 29, 2013 at 10:00 AM,  <span dir="ltr"><<a href="mailto:numpy-discussion-request@scipy.org" target="_blank">numpy-discussion-request@scipy.org</a>></span> wrote:<br>
<div class="gmail_extra">
<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-----------------------------Message: 5<br>
Date: Tue, 29 Oct 2013 17:02:33 +0000<br>
From: Robert Kern <<a href="mailto:robert.kern@gmail.com" target="_blank">robert.kern@gmail.com</a>><br>
Subject: Re: [Numpy-discussion] getting the equivalent complex dtype<br>
        from a real or int array<br>
To: Discussion of Numerical Python <<a href="mailto:numpy-discussion@scipy.org" target="_blank">numpy-discussion@scipy.org</a>><br>
Message-ID:<br>
        <<a href="mailto:CAF6FJiuYnDbE1Uo9J6OnL1pq%2BoVZX-ecqKz0Qe9MigyQT69V_g@mail.gmail.com" target="_blank">CAF6FJiuYnDbE1Uo9J6OnL1pq+oVZX-ecqKz0Qe9MigyQT69V_g@mail.gmail.com</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
On Tue, Oct 29, 2013 at 4:47 PM, Henry Gomersall <<a href="mailto:heng@cantab.net" target="_blank">heng@cantab.net</a>> wrote:<br>
><br>
> Is there a way to extract the size of array that would be created by<br>
> doing 1j*array?<br>
><br>
> The problem I'm having is in creating an empty array to fill with<br>
> complex values without knowing a priori what the input data type is.<br>
><br>
> For example, I have a real or int array `a`.<br>
><br>
> I want to create an array `b` which can hold values from 1j*a in such a<br>
> way that I don't need to compute those explicitly (because I only need<br>
> parts of the array say), without upcasting (or indeed downcasting) the<br>
> result.<br>
><br>
> So if `a` was dtype 'float32`, `b` would be of dtype `complex64`. If `a`<br>
> was `int64`, `b` would be of dtype `complex128` etc.<br>
<br>
Quick and dirty:<br>
<br>
# Get a tiny array from `a` to test the dtype of its output when multiplied<br>
# by a complex float. It must be an array rather than a scalar since the<br>
# casting rules are different for array*scalar and scalar*scalar.<br>
dt = (a.flat[:2] * 1j).dtype<br>
b = np.empty(shape, dtype=dt)<br>
<br>
--<br>
Robert Kern<br></blockquote></div>
</div></div>