How to use 'matrix chain multiplication calculator' in Python

Every line of 'matrix chain multiplication calculator' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
12def matrixChainMultiply(seq):
13 '''matrix chain multiply, find the optimalest comb to multiply
14 eg ABCD, (AB)(CD), A((BC)D)
15 seq: sequence of matrix's scale, eg [A.row,A.col,B.col,C.col,D.col]
16 '''
17 print(seq)
18 n = len(seq)-1
19 mat = [[0]*n for i in range(n)]
20 mark = [[0]*n for i in range(n)]
21 for l in range(1,n):
22 for i in range(n):
23 j = i+l
24 if j>=n: continue
25 mat[i][j] = None
26 for k in range(i,j):
27 tmp = mat[i][k]+mat[k+1][j]+seq[i]*seq[k+1]*seq[j+1]
28 if mat[i][j] is None or mat[i][j]>tmp:
29 mark[i][j] = k
30 mat[i][j]= tmp
31 s= findSolution(mark,0,n-1)
32 print(s)
33 return mat[0][n-1]

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

Related snippets