You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

动态规划确定矩阵最优乘法顺序后,如何计算最终乘积?

Calculating Matrix Product with Optimal Parenthesization

Great question! Once you’ve nailed down the optimal parenthesization via dynamic programming, computing the actual matrix product is just a matter of following that grouping step by step. Let’s break this down using your example to make it totally concrete.

First, let’s map your dimension array m = [40,20,30,10,30] to the actual matrices:

  • A₁ is a 40×20 matrix (rows = m[0], columns = m[1])
  • A₂ is a 20×30 matrix (rows = m[1], columns = m[2])
  • A₃ is a 30×10 matrix (rows = m[2], columns = m[3])
  • A₄ is a 10×30 matrix (rows = m[3], columns = m[4])

Your optimal order is ((A₁(A₂A₃))A₄)—we’ll compute this from the innermost parentheses outward, just like you’d evaluate arithmetic expressions.

Step 1: Compute the innermost product A₂×A₃

Start with the grouping that’s nested deepest: A₂A₃. Since A₂ is 20×30 and A₃ is 30×10, their product will be a 20×10 matrix (let’s call this intermediate result B₁).

To calculate B₁:

  • For each element B₁[i][j] (where 0 ≤ i < 20, 0 ≤ j < 10), compute the dot product of the i-th row of A₂ and the j-th column of A₃.
  • Mathematically: B₁[i][j] = Σₖ=0 to 29 (A₂[i][k] * A₃[k][j])

Step 2: Compute A₁×B₁ (which is A₁(A₂A₃))

Next, take the result from Step 1 (B₁, 20×10) and multiply it by A₁ (40×20). The result here is a 40×10 matrix (let’s call this B₂).

Calculating B₂ follows the same matrix multiplication rule:

  • For each element B₂[i][j] (0 ≤ i < 40, 0 ≤ j < 10), compute the dot product of the i-th row of A₁ and the j-th column of B₁.
  • B₂[i][j] = Σₖ=0 to 19 (A₁[i][k] * B₁[k][j])

Step 3: Compute B₂×A₄ (the final product)

Finally, multiply B₂ (40×10) by A₄ (10×30) to get your final 40×30 matrix.

For each element in the final matrix C[i][j] (0 ≤ i < 40, 0 ≤ j < 30):

  • C[i][j] = Σₖ=0 to 9 (B₂[i][k] * A₄[k][j])

How to Implement This in Code (Quick Example)

If you have a DP table that tracks the optimal split points (say, s[i][j] tells you where to split matrices Aᵢ to Aⱼ), you can write a recursive function to compute the product:

def matrix_multiply(matrices, s, i, j):
    if i == j:
        return matrices[i]
    # Split into left (A_i to A_s[i][j]) and right (A_s[i][j]+1 to A_j)
    left = matrix_multiply(matrices, s, i, s[i][j])
    right = matrix_multiply(matrices, s, s[i][j]+1, j)
    # Multiply the two intermediate matrices
    return multiply_two_matrices(left, right)

# Helper function to multiply two matrices
def multiply_two_matrices(a, b):
    rows_a, cols_a = len(a), len(a[0])
    rows_b, cols_b = len(b), len(b[0])
    result = [[0 for _ in range(cols_b)] for _ in range(rows_a)]
    for i in range(rows_a):
        for k in range(cols_a):
            if a[i][k] == 0:
                continue  # Skip zero elements for efficiency
            for j in range(cols_b):
                result[i][j] += a[i][k] * b[k][j]
    return result

You’d call this with your list of matrices [A1, A2, A3, A4], your split table s, and i=0, j=3 (assuming 0-indexed).

The key takeaway is that optimal parenthesization just tells you the order to group multiplications—once you have that, you compute each group exactly like you’d multiply any two matrices, then combine the results.

内容的提问来源于stack exchange,提问作者Ramtin Mousavi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 13:57:42