matrix_chain_brute, a C code which finds the cost of the most efficient ordering to use when multiplying a sequence of matrices, using brute force.
We are given a sequence of n matrices of conformable dimensions. Matrix Ai has D(i) rows and D(i+1) columns. The product A1 * A2 * ... * An is to be computed. In terms of scalar multiplications, the cost of computing A(i) * A(i+1) is D(i) * D(i+1) * D(i+2). We may carry out the pairs of multiplication in any order we wish. The total cost of the computation will, in general, vary depending on the order in which we compute the individual matrix multiplications. The goal of the algorithm is simply to determine the cost of the most efficient ordering.
Given n matrices, there are (n-1)! possible orderings for the sequence of matrix multiplications. The brute force approach considers each of these orderings, determines each cost, and reports the minimum.
The information on this web page is distributed under the MIT license.
matrix_chain_brute is available in a C version and a C++ version and a Fortran90 version and a MATLAB version and an Octave version and a Python version.
cpp_combinatorics, a C++ code which considers a variety of problems in combinatorics involving counting, combinations, permutations, and so on.