numpy 1.22.4 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
multi_dot(arrays, *, out=None)

multi_dot chains numpy.dot and uses optimal parenthesization of the matrices . 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)

Notes

The cost for a matrix multiplication can be calculated with the following function:

def cost(A, B):
    return A.shape[0] * A.shape[1] * B.shape[1]

Assume we have three matrices $A_{10x100}, B_{100x5}, C_{5x50}$ .

The costs for the two different parenthesizations are as follows:

cost((AB)C) = 10*100*5 + 10*5*50   = 5000 + 2500   = 7500
cost(A(BC)) = 10*100*50 + 100*5*50 = 50000 + 25000 = 75000

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.

out : ndarray, optional

Output argument. This must have the exact kind that would be returned if it was not used. In particular, it must have the right type, must be C-contiguous, and its dtype must be the dtype that would be returned for :None:None:`dot(a, b)`. This is a performance feature. Therefore, if these conditions are not met, an exception is raised, instead of attempting to be flexible.

versionadded

Returns

output : ndarray

Returns the dot product of the supplied arrays.

Compute the dot product of two or more arrays in a single function call, while automatically selecting the fastest evaluation order.

See Also

numpy.dot

dot multiplication with two arguments.

Examples

>>> 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])
>>> _ = np.dot(np.dot(np.dot(A, B), C), D)
... # or
... _ = A.dot(B).dot(C).dot(D)
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

numpy.core._multiarray_umath.dot numpy.einsum_path numpy.dot numpy.core._multiarray_umath.c_einsum numpy.linalg.multi_dot numpy.einsum

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /numpy/linalg/linalg.py#2607
type: <class 'function'>
Commit: