MCPcopy Create free account
hub / github.com/numpy/numpy / multi_dot

Function multi_dot

numpy/linalg/linalg.py:2644–2761  ·  view source on GitHub ↗

Compute the dot product of two or more arrays in a single function call, while automatically selecting the fastest evaluation order. `multi_dot` chains `numpy.dot` and uses optimal parenthesization of the matrices [1]_ [2]_. Depending on the shapes of the matrices, this can spe

(arrays, *, out=None)

Source from the content-addressed store, hash-verified

2642
2643@array_function_dispatch(_multidot_dispatcher)
2644def multi_dot(arrays, *, out=None):
2645 """
2646 Compute the dot product of two or more arrays in a single function call,
2647 while automatically selecting the fastest evaluation order.
2648
2649 `multi_dot` chains `numpy.dot` and uses optimal parenthesization
2650 of the matrices [1]_ [2]_. Depending on the shapes of the matrices,
2651 this can speed up the multiplication a lot.
2652
2653 If the first argument is 1-D it is treated as a row vector.
2654 If the last argument is 1-D it is treated as a column vector.
2655 The other arguments must be 2-D.
2656
2657 Think of `multi_dot` as::
2658
2659 def multi_dot(arrays): return functools.reduce(np.dot, arrays)
2660
2661
2662 Parameters
2663 ----------
2664 arrays : sequence of array_like
2665 If the first argument is 1-D it is treated as row vector.
2666 If the last argument is 1-D it is treated as column vector.
2667 The other arguments must be 2-D.
2668 out : ndarray, optional
2669 Output argument. This must have the exact kind that would be returned
2670 if it was not used. In particular, it must have the right type, must be
2671 C-contiguous, and its dtype must be the dtype that would be returned
2672 for `dot(a, b)`. This is a performance feature. Therefore, if these
2673 conditions are not met, an exception is raised, instead of attempting
2674 to be flexible.
2675
2676 .. versionadded:: 1.19.0
2677
2678 Returns
2679 -------
2680 output : ndarray
2681 Returns the dot product of the supplied arrays.
2682
2683 See Also
2684 --------
2685 numpy.dot : dot multiplication with two arguments.
2686
2687 References
2688 ----------
2689
2690 .. [1] Cormen, "Introduction to Algorithms", Chapter 15.2, p. 370-378
2691 .. [2] https://en.wikipedia.org/wiki/Matrix_chain_multiplication
2692
2693 Examples
2694 --------
2695 `multi_dot` allows you to write::
2696
2697 >>> from numpy.linalg import multi_dot
2698 >>> # Prepare some data
2699 >>> A = np.random.random((10000, 100))
2700 >>> B = np.random.random((100, 1000))
2701 >>> C = np.random.random((1000, 5))

Calls 8

dotFunction · 0.90
asanyarrayFunction · 0.90
atleast_2dFunction · 0.90
_assert_2dFunction · 0.85
_multi_dot_threeFunction · 0.85
_multi_dotFunction · 0.85
ravelMethod · 0.45