ENH: add `mdot`: fast dot with multiple arguments.

This is not ready to be merged!

`mdot` makes it easy to chain `dot`. Think of `mdot` as this:

    def mdot(*args): return reduce(np.dot, args)

However, `mdot` uses the fact that matrix multiplication is associative and
finds the optimal order of multiplications with dynamic programing before
doing the mutiplication. This can speed up the multiplication quite a bit.

The algorithm follows:
Cormen, "Introduction to Algorithms", Chapter 15.2, p. 370-378

Note: currently `mdot` only works with arrays which have `ndim == 2` to avoid
ambiguities of `dot`. Also see the discussion in #4311.

TODO:
- [x] Decide if `ndim != 2` should be ok
- [x] Add unittests
- [x] decide on name. possible names are:
  - `mdot`: think "multi dot"
  - `multidot`: as above :)
  - `multi_dot`: as above :)
  - `chain_dot`
  - `fdot`: think "fast dot" (should the parameter optimize be optional then? I don't think so.)
  - `ddot`: think "multiple d for multiple dot"
  - `cdot`: think "chain dot"
  - `odot`: think "optimal do

If there are two different possible optimization criteria - which I doubt,
but I haven't thought it through - then we could support both and have a
flag to choose between them. But I don't see any purpose in having a flag
to disable optimization entirely. Users can always write reduce(...)
themselves if they want that order specifically.
On 13 Sep 2014 12:37, "argriffing" <notifications@github.com> wrote:

> In numpy/linalg/linalg.py:
>
> > +    row and column vector. The other arguments must be 2-D.
> > +
> > +    Think of `multi_dot` as::
> > +
> > +        def multi_dot(arrays): return functools.reduce(np.dot, arrays)
> > +
> > +
> > +    Parameters
> > +    ----------
> > +    arrays : sequence of array_like
> > +        First and last argument can be 1-D and are treated respectively as
> > +        row and column vector, the other arguments must be 2-D.
> > +    optimize : bool, optional
> > +        If `True` (default) a optimal parenthesization is chosen to
> > +        speed up the multiplication, otherwise
> > +        `functools.reduce(np.dot, arrays)` is used.
>
> Reordering would reduce the number of operations but does it always reduce
> memory usage? Maybe it does, but if there could be a time/memory tradeoff
> then this could explain keeping the flag.
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/numpy/numpy/pull/4977/files#r17512991>.
>