[Numpy-discussion] element wise help

josef.pktd at gmail.com josef.pktd at gmail.com
Thu May 7 12:56:04 EDT 2009


On Thu, May 7, 2009 at 12:39 PM, Chris Colbert <sccolbert at gmail.com> wrote:
> suppose i have two arrays:  n and t, both are 1-D arrays.
>
> for each value in t, I need to use it to perform an element wise scalar
> operation on every value in n and then sum the results into a single scalar
> to be stored in the output array.
>
> Is there any way to do this without the for loop like below:
>
> for val in t_array:
>
>           out = (n / val).sum()  # not the actual function being done, but
> you get the idea
>


broad casting should work, e.g.

(n[:,np.newaxis] / val[np.newaxis,:]).sum()

but it constructs the full product array, which is memory intensive
for a reduce operation, if the 1d arrays are large.

another candidate for a cython loop if the arrays are large?

Josef



More information about the NumPy-Discussion mailing list