[Numpy-discussion] getting the equivalent complex dtype from a real or int array
David Goldsmith
d.l.goldsmith at gmail.com
Tue Oct 29 22:19:34 EDT 2013
We really ought to have a special page for all of Robert's little gems!
DG
On Tue, Oct 29, 2013 at 10:00 AM, <numpy-discussion-request at scipy.org>wrote:
>
> -----------------------------Message: 5
> Date: Tue, 29 Oct 2013 17:02:33 +0000
> From: Robert Kern <robert.kern at gmail.com>
> Subject: Re: [Numpy-discussion] getting the equivalent complex dtype
> from a real or int array
> To: Discussion of Numerical Python <numpy-discussion at scipy.org>
> Message-ID:
> <
> CAF6FJiuYnDbE1Uo9J6OnL1pq+oVZX-ecqKz0Qe9MigyQT69V_g at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> On Tue, Oct 29, 2013 at 4:47 PM, Henry Gomersall <heng at cantab.net> wrote:
> >
> > Is there a way to extract the size of array that would be created by
> > doing 1j*array?
> >
> > The problem I'm having is in creating an empty array to fill with
> > complex values without knowing a priori what the input data type is.
> >
> > For example, I have a real or int array `a`.
> >
> > I want to create an array `b` which can hold values from 1j*a in such a
> > way that I don't need to compute those explicitly (because I only need
> > parts of the array say), without upcasting (or indeed downcasting) the
> > result.
> >
> > So if `a` was dtype 'float32`, `b` would be of dtype `complex64`. If `a`
> > was `int64`, `b` would be of dtype `complex128` etc.
>
> 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)
>
> --
> Robert Kern
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20131029/ff847bca/attachment.html>
More information about the NumPy-Discussion
mailing list