# [Numpy-discussion] simple reduction question

Nathaniel Smith njs at pobox.com
Wed Dec 24 10:34:51 EST 2014

On Wed, Dec 24, 2014 at 3:25 PM, Neal Becker <ndbecker2 at gmail.com> wrote:
> What would be the most efficient way to compute:
>
> c[j] = \sum_i (a[i] * b[i,j])
>
> where a[i] is a 1-d vector, b[i,j] is a 2-d array?

I think this formula is just np.dot(a, b). Did you mean c = \sum_j
\sum_i (a[i] * b[i, j])?

> This seems to be one way:
>
> import numpy as np
> a = np.arange (3)
> b = np.arange (12).reshape (3,4)
> c = np.dot (a, b).sum()
>
> but np.dot returns a vector, which then needs further reduction.  Don't know if
> there's a better way.
>
> --
> -- Those who don't understand recursion are doomed to repeat it
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

--
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org



More information about the NumPy-Discussion mailing list