[Numpy-discussion] Proposal: Chaining np.dot with mdot helper function

josef.pktd at gmail.com josef.pktd at gmail.com
Mon Feb 17 16:57:13 EST 2014

On Mon, Feb 17, 2014 at 4:39 PM, Stefan Otte <stefan.otte at gmail.com> wrote:
> Hey guys,
> I wrote myself a little helper function `mdot` which chains np.dot for
> multiple arrays. So I can write
>     mdot(A, B, C, D, E)
> instead of these
>     A.dot(B).dot(C).dot(D).dot(E)
>     np.dot(np.dot(np.dot(np.dot(A, B), C), D), E)
> I know you can use `numpy.matrix` to get nicer formulas. However, most
> numpy/scipy function return arrays instead of numpy.matrix. Therefore,
> sometimes you actually use array multiplication when you think you use
> matrix multiplication. `mdot` is a simple way to avoid using
> numpy.matrix but to improve the readability.
> What do you think? Is this useful and worthy to integrate in numpy?
> I already created an issuer for this:
> https://github.com/numpy/numpy/issues/4311
> jaimefrio also suggested to do some reordering of the arrays to
> minimize computation:
> https://github.com/numpy/numpy/issues/4311#issuecomment-35295857

statsmodels has a convenience chaindot, but most of the time I don't
like it's usage, because of the missing brackets.

say, you have a (10000, 10) array and you use an intermediate (10000,
10000) array instead of (10,10) array

IIRC, for reordering I looked at this

(don't make it too easy for people to shoot themselves in ...)

> Best,
>  Stefan
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list