📜  Python程序将两个矩阵相乘

📅  最后修改于: 2022-05-13 01:54:20.596000             🧑  作者: Mango

Python程序将两个矩阵相乘

给定两个矩阵,将它们相乘的任务。矩阵可以是正方形或矩形。

例子:

Input : mat1[][] = {{1, 2}, 
                   {3, 4}}
        mat2[][] = {{1, 1}, 
                    {1, 1}}
Output : {{3, 3}, 
          {7, 7}}
Input : mat1[][] = {{2, 4}, 
                    {3, 4}}
        mat2[][] = {{1, 2}, 
                    {1, 3}}       
Output : {{6, 16}, 
          {7, 18}}

平方矩阵的乘法:
下面的程序将两个大小为 4*4 的方阵相乘,我们可以将 N 更改为不同的维度。

Python3
# 4x4 matrix multiplication using Python3
# Function definition
def matrix_multiplication(M, N):
    # List to store matrix multiplication result
    R = [[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]]
 
    for i in range(0, 4):
        for j in range(0, 4):
            for k in range(0, 4):
                R[i][j] += M[i][k] * N[k][j]
 
    for i in range(0, 4):
        for j in range(0, 4):
            # if we use print(), by default cursor moves to next line each time,
            # Now we can explicitly define ending character or sequence passing
            # second parameter as end =""
            # syntax: print(, end ="")
            # Here space (" ") is used to print a gap after printing
            # each element of R
            print(R[i][j], end =" ")
        print("
", end ="")
 
# First matrix. M is a list
M = [[1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4]]
 
# Second matrix. N is a list
N = [[1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4]]
     
# Call matrix_multiplication function
matrix_multiplication(M, N)
 
# This code is contributed by Santanu


Python3
# Python3 program to multiply two
# rectangular matrices
 
# Multiplies two matrices mat1[][]
# and mat2[][] and prints result.
# (m1) x (m2) and (n1) x (n2) are
# dimensions of given matrices.
def multiply(m1, m2, mat1,
             n1, n2, mat2):
 
    res = [[0 for x in range(n2)]
              for y in range (m1)]
     
    for i in range(m1):
        for j in range(n2):
            res[i][j] = 0
            for x in range(m2):           
                res[i][j] += (mat1[ i][x] *
                              mat2[ x][j])
            
    for i in range(m1):
        for j in range(n2):     
            print (res[i][j],
                   end = " ")       
        print ()
 
# Driver code
if __name__ == "__main__":
   
    mat1 = [[2, 4], [3, 4]]
    mat2 = [[1, 2], [1, 3]]
    m1, m2, n1, n2 = 2, 2, 2, 2
   
    # Function call
    multiply(m1, m2, mat1,
             n1, n2, mat2)
     
# This code is contributed by Chitranayal


输出
Result matrix is 
10 10 10 10 
20 20 20 20 
30 30 30 30 
40 40 40 40

时间复杂度: O(n 3 )。可以使用 Strassen 的矩阵乘法进行优化

辅助空间: O(n 2 )

矩形矩阵的乘法:
我们在 C 中使用指针来乘以矩阵。请参阅以下帖子作为代码的先决条件。
如何在C中将二维数组作为参数传递?

Python3

# Python3 program to multiply two
# rectangular matrices
 
# Multiplies two matrices mat1[][]
# and mat2[][] and prints result.
# (m1) x (m2) and (n1) x (n2) are
# dimensions of given matrices.
def multiply(m1, m2, mat1,
             n1, n2, mat2):
 
    res = [[0 for x in range(n2)]
              for y in range (m1)]
     
    for i in range(m1):
        for j in range(n2):
            res[i][j] = 0
            for x in range(m2):           
                res[i][j] += (mat1[ i][x] *
                              mat2[ x][j])
            
    for i in range(m1):
        for j in range(n2):     
            print (res[i][j],
                   end = " ")       
        print ()
 
# Driver code
if __name__ == "__main__":
   
    mat1 = [[2, 4], [3, 4]]
    mat2 = [[1, 2], [1, 3]]
    m1, m2, n1, n2 = 2, 2, 2, 2
   
    # Function call
    multiply(m1, m2, mat1,
             n1, n2, mat2)
     
# This code is contributed by Chitranayal


输出
6 16 
7 18

时间复杂度: O(n 3 )。可以使用 Strassen 的矩阵乘法进行优化

辅助空间: O(m1 * n2)

有关更多详细信息,请参阅有关将两个矩阵相乘的完整文章!