MCPcopy Index your code
hub / github.com/numpy/numpy / _multi_dot_three

Function _multi_dot_three

numpy/linalg/_linalg.py:2984–3002  ·  view source on GitHub ↗

Find the best order for three arrays and do the multiplication. For three arguments `_multi_dot_three` is approximately 15 times faster than `_multi_dot_matrix_chain_order`

(A, B, C, out=None)

Source from the content-addressed store, hash-verified

2982
2983
2984def _multi_dot_three(A, B, C, out=None):
2985 """
2986 Find the best order for three arrays and do the multiplication.
2987
2988 For three arguments `_multi_dot_three` is approximately 15 times faster
2989 than `_multi_dot_matrix_chain_order`
2990
2991 """
2992 a0, a1b0 = A.shape
2993 b1c0, c1 = C.shape
2994 # cost1 = cost((AB)C) = a0*a1b0*b1c0 + a0*b1c0*c1
2995 cost1 = a0 * b1c0 * (a1b0 + c1)
2996 # cost2 = cost(A(BC)) = a1b0*b1c0*c1 + a0*a1b0*c1
2997 cost2 = a1b0 * c1 * (a0 + b1c0)
2998
2999 if cost1 < cost2:
3000 return dot(dot(A, B), C, out=out)
3001 else:
3002 return dot(A, dot(B, C), out=out)
3003
3004
3005def _multi_dot_matrix_chain_order(arrays, return_costs=False):

Callers 1

multi_dotFunction · 0.85

Calls 1

dotFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…