Matrix Multiplication

Yaser Rahmati | یاسر رحمتی

Matrix multiplication is a fundamental operation in linear algebra. When multiplying two matrices, the number of columns in the first matrix must be equal to the number of rows in the second matrix.

Example

Suppose we have two matrices A and B:

A=[1234],B=[5678]A=\begin{bmatrix} 1 & 2\\ 3 & 4 \end{bmatrix} , B=\begin{bmatrix} 5 & 6\\ 7 & 8 \end{bmatrix}

To multiply these two matrices together, we follow these steps:

Step 1

First, we check the dimensions of the matrices. Matrix A is a 2x2 matrix, and matrix B is also a 2x2 matrix.

The number of columns in A is equal to the number of rows in B, which satisfies the requirement for multiplication.

Step 2

Following this process, the resulting matrix C will have dimensions equal to the number of rows of A and the number of columns of B, which is 2x2 in this case.

A.B=[1234]×[5678]=[(1×5+2×7)(1×6+2×8)(3×5+4×7)(3×6+4×8)]=[19224350] A.B=\begin{bmatrix} 1 & 2\\ 3 & 4 \end{bmatrix} \times \begin{bmatrix} 5 & 6\\ 7 & 8 \end{bmatrix} = \newline \begin{bmatrix} (1\times5+2\times7) & (1\times6+2\times8)\\ (3\times5+4\times7) & (3\times6+4\times8) \end{bmatrix} = \begin{bmatrix} 19 & 22\\ 43 & 50 \end{bmatrix}

Python

In NumPy, we obtan the matrix-matrix product with the @ operator or dot method:

import numpy as np

# Define the matrices as NumPy arrays
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])

# Perform matrix multiplication using the dot function
result = np.dot(matrix1, matrix2)

# Alternatively, you can also use the @ operator for matrix multiplication in Python 3.5 and later 
# result = matrix1 @ matrix2

# Print the resulting matrix
print(result)

My Training Video

Keywords

Web Development (Django, Flask) , Data Science (Pandas, NumPy, Matplotlib) , Machine Learning (Scikit-learn, TensorFlow, PyTorch) , Artificial Intelligence , Automation , GUI Development (Tkinter, PyQt) , Game Development (Pygame) , Scientific Computing , Financial Analysis (Pandas, NumPy) , Network Programming , Image Processing (OpenCV) , Web Scraping (Beautiful Soup, Scrapy) , Internet of Things (IoT) Development , Robotics , Cybersecurity , Mobile App Development (Kivy) , Cloud Computing (Boto3, AWS Lambda) , Big Data Analysis (PySpark) , Geographical Information Systems , Natural Language Processing (NLTK, spaCy) , Data Visualization (Seaborn, Plotly) , Bioinformatics (Biopython) , Multimedia Applications , Education and Teaching

Last updated