{"id":7362,"date":"2021-09-30T16:30:40","date_gmt":"2021-09-30T11:00:40","guid":{"rendered":"https:\/\/python-programs.com\/?p=7362"},"modified":"2021-11-22T18:33:34","modified_gmt":"2021-11-22T13:03:34","slug":"python-program-for-multiplication-of-two-matrices","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-multiplication-of-two-matrices\/","title":{"rendered":"Python Program for Multiplication of Two Matrices | How do you do Matrix Multiplication in Python?"},"content":{"rendered":"
Are you a job seeker and trying to find simple java programs for Interview?<\/a> This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.<\/p>\n Looking across the web for guidance on Multiplication of Two Matrices in Python? Before you begin your learning on Matrix Multiplication of Two Matrices know what is meant by a Matrix and what does Matrix Multiplication in actual means. This tutorial is going to be extremely helpful for you as it assists you completely on How to Multiply Two Matrices in Python using different methods like nested loops, nested list comprehension, etc. Furthermore, we have written simple python programs for multiplying two matrices clearly.<\/p>\n A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.<\/p>\n Example:<\/strong><\/p>\n <\/p>\n Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.<\/p>\n In this order, the dimensions of a matrix indicate the number of rows and columns.<\/p>\n Here as there are 5 rows and 4 columns it is called a 5*4 matrix.<\/p>\n Matrix multiplication is only possible if the second matrix’s column equals the first matrix’s rows. A matrix can be represented in Python as a nested list ( a list inside a list ).<\/p>\n Examples for Matrix Multiplication:<\/strong><\/p>\n Example 1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Given two matrices, the task is to write a program in Python and C++ to multiply the two matrices.<\/p>\n A matrix can be implemented as a nested list in Python (list inside a list).<\/p>\n Each element can be thought of as a row in the matrix.<\/p>\n X[0] can be used to choose the first row. Furthermore, the element in the first row, the first column can be chosen as X[0][0].<\/p>\n The multiplication of two matrices X and Y is defined if the number of columns in X equals the number of rows in Y.<\/p>\n Here we give the matrix input as static.<\/p>\n XY is defined and has the dimension n x l if X is a n x m matrix and Y is a m x l matrix (but YX is not defined). In Python, there are a few different approaches to implement matrix multiplication.<\/p>\n Below is the\u00a0 implementation:<\/strong><\/p>\n <\/p>\n Output:<\/strong><\/p>\n To go through each row and column in our program, we used nested for loops. In the end, we add up the sum of the items.<\/p>\n This technique is simple, but it becomes computationally expensive as the order of the matrix increases.<\/p>\n We recommend NumPy for larger matrix operations because it is several (in the order of 1000) times faster than the above code.<\/p>\n This program produces the same results as the previous one. To understand the preceding code, we must first know the built-in method zip() and how to unpack an argument list with the * operator.<\/p>\n To loop through each element in the matrix, we utilized nested list comprehension. At first glance, the code appears to be complicated and difficult to understand. However, once you’ve learned list comprehensions, you won’t want to go back to nested loops.<\/p>\n Below is the implementation:<\/strong><\/p>\n <\/p>\n Output:<\/strong><\/p>\n We used nesting loops in this program to iterate through and row and column. In order to multiply two matrices, we will do the multiplication of matrices as below<\/p>\n Let us take dynamic input in this case.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n Related Programs<\/strong>:<\/p>\n Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview. Looking across the web for guidance on Multiplication of Two Matrices in Python? Before you begin your …<\/p>\n\n
<\/a>What is a Matrix?<\/h2>\n
<\/a>What is Matrix Multiplication?<\/h3>\n
Matrix 1 = [ 1 -8 7 ]\r\n [ 0 2 5 ]\r\n [ 1 6 2 ]\r\n \r\nMatrix 2 = [ 5 2 -6 ]\r\n [ 1 8 9 ]\r\n [ 3 5 7 ]<\/pre>\n
printing the matrix A :\r\n1 -8 7\r\n0 2 5\r\n1 6 2\r\nprinting the matrix B :\r\n5 2 -6\r\n1 8 9\r\n3 5 7\r\nPrinting the product of matrices : \r\n18 -27 -29\r\n17 41 53\r\n17 60 62<\/pre>\n
Matrix 1 = 1 2 3 \r\n -5 8 7 \r\n 6 4 5 \r\n \r\nMatrix 2 = 2 7 9 \r\n 0 -8 5 \r\n 6 -5 2<\/pre>\n
Enter the number of rows and columns of the first matrix3\r\n3\r\nEnter the number of rows and columns of the second matrix3\r\n3\r\nEnter element A11 = 1\r\nEnter element A12 = 2\r\nEnter element A13 = 3\r\nEnter element A21 = -5\r\nEnter element A22 = 8\r\nEnter element A23 = 7\r\nEnter element A31 = 6\r\nEnter element A32 = 4\r\nEnter element A33 = 5\r\n\r\nEnter elements of 2nd matrix: \r\nEnter element B11 = 2\r\nEnter element B12 = 7\r\nEnter element B13 = 9\r\nEnter element B21 = 0\r\nEnter element B22 = -8\r\nEnter element B23 = 5\r\nEnter element B31 = 6\r\nEnter element B32 = -5\r\nEnter element B33 = 2\r\n\r\nprinting the matrix A\r\n1 2 3 \r\n-5 8 7 \r\n6 4 5\r\n\r\nprinting the matrix B\r\n2 7 9 \r\n0 -8 5 \r\n6 -5 2 \r\nPrint the product of two matrices A and B\r\n20 -24 25 \r\n32 -134 9 \r\n42 -15 84<\/pre>\n
<\/a>Simple Python Program for Matrix Multiplication<\/h2>\n
\n
<\/a>Method #1: Using Nested Loops in Python<\/h3>\n
# given matrix A\r\nA = [[1, - 8, 7],\r\n [0, 2, 5],\r\n [1, 6, 2]]\r\n# given matrix B\r\nB = [[5, 2, - 6],\r\n [1, 8, 9],\r\n [3, 5, 7]]\r\n# Initialize the product of matrices elements to 0\r\nmatrixProduct = [[0, 0, 0],\r\n [0, 0, 0],\r\n [0, 0, 0]]\r\n# Traverse the rows of A\r\nfor i in range(len(A)):\r\n # Traverse the columns of B\r\n for j in range(len(B[0])):\r\n # Traverse the rows of B\r\n for k in range(len(B)):\r\n matrixProduct[i][j] += A[i][k] * B[k][j]\r\n# printing the matrix A\r\nprint(\"printing the matrix A :\")\r\nfor rows in A:\r\n print(*rows)\r\n# printing the matrix B\r\nprint(\"printing the matrix B :\")\r\nfor rows in B:\r\n print(*rows)\r\n# printing the product of matrices\r\nprint(\"Printing the product of matrices : \")\r\nfor rows in matrixProduct:\r\n print(*rows)\r\n<\/pre>\n
printing the matrix A :\r\n1 -8 7\r\n0 2 5\r\n1 6 2\r\nprinting the matrix B :\r\n5 2 -6\r\n1 8 9\r\n3 5 7\r\nPrinting the product of matrices : \r\n18 -27 -29\r\n17 41 53\r\n17 60 62<\/pre>\n
Explanation:<\/h3>\n
<\/a>Method #2: Matrix Multiplication List Comprehension Python<\/h3>\n
# given matrix A\r\nA = [[1, - 8, 7],\r\n [0, 2, 5],\r\n [1, 6, 2]]\r\n# given matrix B\r\nB = [[5, 2, - 6],\r\n [1, 8, 9],\r\n [3, 5, 7]]\r\n# using list comprehension to do product of matrices\r\nmatrixProduct = [[sum(a*b for a, b in zip(row, col))\r\n for col in zip(*B)] for row in A]\r\n\r\n# printing the matrix A\r\nprint(\"printing the matrix A :\")\r\nfor rows in A:\r\n print(*rows)\r\n# printing the matrix B\r\nprint(\"printing the matrix B :\")\r\nfor rows in B:\r\n print(*rows)\r\n# printing the product of matrices\r\nprint(\"Printing the product of matrices : \")\r\nfor rows in matrixProduct:\r\n print(*rows)\r\n<\/pre>\n
printing the matrix A :\r\n1 -8 7\r\n0 2 5\r\n1 6 2\r\nprinting the matrix B :\r\n5 2 -6\r\n1 8 9\r\n3 5 7\r\nPrinting the product of matrices : \r\n18 -27 -29\r\n17 41 53\r\n17 60 62<\/pre>\n
<\/a>Method #3: Using Nested Loops in C++<\/h3>\n
#include <iostream>\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n int A[100][100], B[100][100], product[100][100], row1,\r\n col1, row2, col2, i, j, k;\r\n\r\n cout << \"Enter the number of rows and columns of the \"\r\n \"first matrix\";\r\n cin >> row1 >> col1;\r\n cout << \"Enter the number of rows and columns of the \"\r\n \"second matrix\";\r\n cin >> row2 >> col2;\r\n\r\n \/\/ Initializing matrix A with the user defined values\r\n for (i = 0; i < row1; ++i)\r\n for (j = 0; j < col1; ++j) {\r\n cout << \"Enter element A\" << i + 1 << j + 1\r\n << \" = \";\r\n cin >> A[i][j];\r\n }\r\n \/\/ Initializing matrix B with the user defined values\r\n cout << endl\r\n << \"Enter elements of 2nd matrix: \" << endl;\r\n for (i = 0; i < row2; ++i)\r\n for (j = 0; j < col2; ++j) {\r\n cout << \"Enter element B\" << i + 1 << j + 1\r\n << \" = \";\r\n cin >> B[i][j];\r\n }\r\n\r\n \/\/ Initializing the resultant product matrix with 0 to\r\n \/\/ all elements\r\n for (i = 0; i < row1; ++i)\r\n for (j = 0; j < col2; ++j) {\r\n product[i][j] = 0;\r\n }\r\n\r\n \/\/ Calculating the product of two matrices A and B\r\n for (i = 0; i < row1; ++i)\r\n for (j = 0; j < col2; ++j)\r\n for (k = 0; k < col1; ++k) {\r\n product[i][j] += A[i][k] * B[k][j];\r\n }\r\n \/\/ printing matrix A\r\n cout << endl << \" printing the matrix A\" << endl;\r\n for (i = 0; i < row1; ++i) {\r\n for (j = 0; j < col1; ++j) {\r\n cout << A[i][j] << \" \";\r\n }\r\n cout << endl;\r\n }\r\n \/\/ printing matrix B\r\n cout << endl << \" printing the matrix B\" << endl;\r\n for (i = 0; i < row2; ++i) {\r\n for (j = 0; j < col2; ++j) {\r\n cout << B[i][j] << \" \";\r\n }\r\n cout << endl;\r\n }\r\n\r\n \/\/ Print the product of two matrices A and B\r\n cout << \"Print the product of two matrices A and B\"\r\n << endl;\r\n for (i = 0; i < row1; ++i) {\r\n for (j = 0; j < col2; ++j) {\r\n cout << product[i][j] << \" \";\r\n }\r\n cout << endl;\r\n }\r\n\r\n return 0;\r\n}<\/pre>\n
Enter the number of rows and columns of the first matrix3\r\n3\r\nEnter the number of rows and columns of the second matrix3\r\n3\r\nEnter element A11 = 1\r\nEnter element A12 = 2\r\nEnter element A13 = 3\r\nEnter element A21 = -5\r\nEnter element A22 = 8\r\nEnter element A23 = 7\r\nEnter element A31 = 6\r\nEnter element A32 = 4\r\nEnter element A33 = 5\r\n\r\nEnter elements of 2nd matrix: \r\nEnter element B11 = 2\r\nEnter element B12 = 7\r\nEnter element B13 = 9\r\nEnter element B21 = 0\r\nEnter element B22 = -8\r\nEnter element B23 = 5\r\nEnter element B31 = 6\r\nEnter element B32 = -5\r\nEnter element B33 = 2\r\n\r\nprinting the matrix A\r\n1 2 3 \r\n-5 8 7 \r\n6 4 5\r\n\r\nprinting the matrix B\r\n2 7 9 \r\n0 -8 5 \r\n6 -5 2 \r\nPrint the product of two matrices A and B\r\n20 -24 25 \r\n32 -134 9 \r\n42 -15 84<\/pre>\n
<\/a>How to Multiply Two Matrices in Python using Numpy?<\/h3>\n
import numpy as np\r\n\r\nC = np.array([[3, 6, 7], [5, -3, 0]])\r\nD = np.array([[1, 1], [2, 1], [3, -3]])\r\nE = C.dot(D)\r\nprint(E)\r\n\r\n''' \r\n<\/pre>\n
[[ 36 -12]\r\n[ -1 2]]<\/pre>\n
\n