5.2.12. numpy.linalg.multi_dot¶
-
numpy.linalg.multi_dot(arrays)[source]¶ 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 [R288] [R289]. Depending on the shapes of the matrices, this can speed up the multiplication a lot.
If the first argument is 1-D it is treated as a row vector. If the last argument is 1-D it is treated as a 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
If the first argument is 1-D it is treated as row vector. If the last argument is 1-D it is treated as column vector. The other arguments must be 2-D.
Returns: output : ndarray
Returns the dot product of the supplied arrays.
See also
dot- dot multiplication with two arguments.
References
[R288] (1, 2) Cormen, “Introduction to Algorithms”, Chapter 15.2, p. 370-378 [R289] (1, 2) http://en.wikipedia.org/wiki/Matrix_chain_multiplication Examples
multi_dot allows you to write:
>>> from numpy.linalg import multi_dot >>> # Prepare some data >>> A = np.random.random(10000, 100) >>> B = np.random.random(100, 1000) >>> C = np.random.random(1000, 5) >>> D = np.random.random(5, 333) >>> # the actual dot multiplication >>> multi_dot([A, B, C, D])
instead of:
>>> np.dot(np.dot(np.dot(A, B), C), D) >>> # or >>> A.dot(B).dot(C).dot(D)