Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Multiply to Matrix Using Multi-dimensional Arrays
A matrix is a set of numbers arranged in rows and columns. A matrix with m rows and n columns is called an m × n matrix and m and n are called its dimensions. In Python, matrices can be created using lists or NumPy arrays.
Matrix multiplication can be done in two ways: standard matrix multiplication (dot product) where the number of columns in the first matrix must equal the number of rows in the second matrix, and element-wise multiplication where matrices must have the same dimensions.
Matrix Multiplication Types
For standard matrix multiplication, if we have matrices A (2×3) and B (3×2), the result will be a 2×2 matrix ?
For element-wise multiplication, matrices must have identical dimensions ?
Using Nested For Loops
We can implement standard matrix multiplication using nested loops to iterate through rows and columns ?
# Define matrices using multidimensional arrays
matrix_a = [[1, 2, 3],
[4, 1, 2],
[2, 3, 1]]
matrix_b = [[1, 2, 3, 2],
[2, 3, 6, 3],
[3, 1, 4, 2]]
def display(matrix):
for row in matrix:
print(row)
print()
# Display input matrices
print('The first matrix is:')
display(matrix_a)
print('The second matrix is:')
display(matrix_b)
# Initialize result matrix with zeros
result = [[0 for j in range(len(matrix_b[0]))] for i in range(len(matrix_a))]
# Multiply matrices using nested loops
for i in range(len(matrix_a)):
for j in range(len(matrix_b[0])):
for k in range(len(matrix_b)):
result[i][j] += matrix_a[i][k] * matrix_b[k][j]
print('The multiplication result is:')
display(result)
The first matrix is: [1, 2, 3] [4, 1, 2] [2, 3, 1] The second matrix is: [1, 2, 3, 2] [2, 3, 6, 3] [3, 1, 4, 2] The multiplication result is: [14, 11, 27, 14] [12, 13, 26, 15] [11, 14, 28, 15]
Using NumPy @ Operator
NumPy provides the @ operator for matrix multiplication, which is more efficient and readable ?
import numpy as np
# Define matrices using NumPy arrays
matrix_a = np.array([[1, 2, 5],
[1, 0, 6],
[9, 8, 0]])
matrix_b = np.array([[0, 3, 5],
[4, 6, 9],
[1, 8, 0]])
print('First matrix:')
print(matrix_a)
print('\nSecond matrix:')
print(matrix_b)
# Matrix multiplication using @ operator
result = matrix_a @ matrix_b
print('\nMatrix multiplication result:')
print(result)
First matrix: [[1 2 5] [1 0 6] [9 8 0]] Second matrix: [[0 3 5] [4 6 9] [1 8 0]] Matrix multiplication result: [[ 13 55 23] [ 6 51 5] [ 32 75 117]]
Element-wise Multiplication
For element-wise multiplication, use the * operator with NumPy arrays ?
import numpy as np
# Define matrices with same dimensions
matrix_a = np.array([[1, 2, 5],
[1, 0, 6],
[9, 8, 0]])
matrix_b = np.array([[0, 3, 5],
[4, 6, 9],
[1, 8, 0]])
print('First matrix:')
print(matrix_a)
print('\nSecond matrix:')
print(matrix_b)
# Element-wise multiplication
result = matrix_a * matrix_b
print('\nElement-wise multiplication result:')
print(result)
First matrix: [[1 2 5] [1 0 6] [9 8 0]] Second matrix: [[0 3 5] [4 6 9] [1 8 0]] Element-wise multiplication result: [[ 0 6 25] [ 4 0 54] [ 9 64 0]]
Comparison
| Method | Syntax | Type | Requirements |
|---|---|---|---|
| Nested Loops | Manual implementation | Standard multiplication | Columns of A = Rows of B |
| @ Operator | A @ B |
Standard multiplication | Columns of A = Rows of B |
| * Operator | A * B |
Element-wise | Same dimensions |
Conclusion
Use nested loops for learning matrix multiplication concepts. For practical applications, NumPy's @ operator provides efficient standard matrix multiplication, while * performs element-wise operations.
