Using arithmetic on numpy structured array elements?

93 views Asked by At

Lets say that I have a structured numpy array like this:

x = np.array([(1, 1.5), (2, 5.21)], dtype=[('foo', 'i4'), ('bar', 'f8')])

What I want to do is be able to add and scalar-multiply these structured array elements together much like you would a numpy vector. E.g. something equivalent to 2 * x[0] + x[1] gives me a structured array element that looks like (4, 8.21).

Of course, simply adding the array elements together doesn't work, nor does passing them into a ufunc like np.add(), I assume because its not technically an array, just a scalar that contains multiple values packaged together.

I know that this would be very easy to do algorithmically, but in my case these array elements actually get pretty large, so I would like to do this in as 'numpyic' of a way as possible for any of the optimization it grants me. Is there any numpy functionality that lets me do this?

0

There are 0 answers