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)
| 2762 | |
| 2763 | |
| 2764 | def _multi_dot_three(A, B, C, out=None): |
| 2765 | """ |
| 2766 | Find the best order for three arrays and do the multiplication. |
| 2767 | |
| 2768 | For three arguments `_multi_dot_three` is approximately 15 times faster |
| 2769 | than `_multi_dot_matrix_chain_order` |
| 2770 | |
| 2771 | """ |
| 2772 | a0, a1b0 = A.shape |
| 2773 | b1c0, c1 = C.shape |
| 2774 | # cost1 = cost((AB)C) = a0*a1b0*b1c0 + a0*b1c0*c1 |
| 2775 | cost1 = a0 * b1c0 * (a1b0 + c1) |
| 2776 | # cost2 = cost(A(BC)) = a1b0*b1c0*c1 + a0*a1b0*c1 |
| 2777 | cost2 = a1b0 * c1 * (a0 + b1c0) |
| 2778 | |
| 2779 | if cost1 < cost2: |
| 2780 | return dot(dot(A, B), C, out=out) |
| 2781 | else: |
| 2782 | return dot(A, dot(B, C), out=out) |
| 2783 | |
| 2784 | |
| 2785 | def _multi_dot_matrix_chain_order(arrays, return_costs=False): |