{'fraction_non_alphanumeric': 0.01925722145804677, 'fraction_numerical': 0.0041265474552957355, 'mean_word_length': 5.275862068965517, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
A heuristic for optimizing the multiplication of matrix chains

A consequence of the associative property of matrix multiplication is that the product of n matrices can be calculated in (n-1)! ways. Different choices for the multiplication sequence can cause the numbers of scalar multiplications required to vary by several orders of magnitude. The optimal ordering with respect to number of scalar multiplications can be found in O(n3) time using dynamic programming. A heuristic is presented which finds an optimal or nearly optimal strategy in O(n2) time in its most straightforward implementation. The problem is important in interpretation or code generation for languages which allow high-level manipulation of matrices.