def _multi_dot_three(A, B, C):
 cost1 = (((A.shape[0] * A.shape[1]) * B.shape[1]) + ((A.shape[0] * B.shape[1]) * C.shape[1]))
 cost2 = (((B.shape[0] * B.shape[1]) * C.shape[1]) + ((A.shape[0] * A.shape[1]) * C.shape[1]))
 if (cost1 < cost2):
  return _dot(_dot(A, B), C)
 else:
  return _dot(A, _dot(B, C))

def _multi_dot_three ( A , B , C ) : cost1 = ( ( ( A . shape [ 0 ] * A . shape [ 1 ] ) * B . shape [ 1 ] ) + ( ( A . shape [ 0 ] * B . shape [ 1 ] ) * C . shape [ 1 ] ) ) cost2 = ( ( ( B . shape [ 0 ] * B . shape [ 1 ] ) * C . shape [ 1 ] ) + ( ( A . shape [ 0 ] * A . shape [ 1 ] ) * C . shape [ 1 ] ) ) if ( cost1 < cost2 ) : return _dot ( _dot ( A , B ) , C ) else : return _dot ( A , _dot ( B , C ) )
find best ordering for three arrays and do the multiplication .