Matrix chain multiplication: Difference between revisions

Content added Content deleted
mNo edit summary
mNo edit summary
Line 17: Line 17:
Write a function which, given a list of the successive dimension of matrices A1, A2... An, of arbitrary length, returns the optimal way to compute the matrix product, and the total cost. The list does not duplicate shared dimensions: for the previous example of matrices A,B,C, one will only pass the list [5,6,3,1]. Hence, a product of n matrices is represented by a list of n+1 dimensions.
Write a function which, given a list of the successive dimension of matrices A1, A2... An, of arbitrary length, returns the optimal way to compute the matrix product, and the total cost. The list does not duplicate shared dimensions: for the previous example of matrices A,B,C, one will only pass the list [5,6,3,1]. Hence, a product of n matrices is represented by a list of n+1 dimensions.


Try this function on the following two lists
Try this function on the following two lists:


* [1, 5, 25, 30, 100, 70, 2, 1, 100, 250, 1, 1000, 2]
* [1, 5, 25, 30, 100, 70, 2, 1, 100, 250, 1, 1000, 2]
Line 63: Line 63:
[[[0, 1], 2], 3]</lang>
[[[0, 1], 2], 3]</lang>


And here is the optimization step
And here is the optimization step:


<lang python>def optim1(a):
<lang python>def optim1(a):