
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Matrix Multiplication
What is Matrix Multiplication?
Matrix multiplication is an operation in linear algebra that involves multiplying two matrices. It is not just a simple element-wise multiplication, but follows specific mathematical rules.
In this operation, the rows of the first matrix are multiplied with the columns of the second matrix, and the results are summed up to form a new matrix.
Matrix multiplication is useful in various fields like physics, computer graphics, and machine learning. It is a key operation in transformations and solving systems of linear equations.
Conditions for Matrix Multiplication
Before performing matrix multiplication, it is important to ensure that the two matrices meet certain conditions −
- Number of columns in the first matrix must be equal to the number of rows in the second matrix.
- Resulting Matrix will have dimensions where the number of rows is taken from the first matrix, and the number of columns is taken from the second matrix.
For example, a matrix of shape (2, 3) can be multiplied by a matrix of shape (3, 4), resulting in a matrix of shape (2, 4).
Matrix Multiplication Formula
The formula for matrix multiplication is as follows −
For matrices A of shape (m, n) and B of shape (n, p), the resulting matrix C will have shape (m, p) and each element cij is calculated by:
cij = (aik * bkj) where i = 1 to m, j = 1 to p, and k = 1 to n
Matrix Multiplication in NumPy
In NumPy, matrix multiplication can be performed using various methods −
- Using the * operator: This operator can be used for element-wise multiplication, but it can also be used for matrix multiplication when applied to two 2D arrays.
- Using @ operator (Python 3.5 and above): The @ operator is designed for matrix multiplication, making the code cleaner and easier to understand.
- Using np.dot() function: This function computes the dot product of two arrays. For 2D arrays, it performs matrix multiplication.
- Using np.matmul() function: Similar to np.dot() function, this function is specifically designed for matrix multiplication.
Example of Matrix Multiplication in NumPy
In this example, we will demonstrate matrix multiplication using all the methods mentioned above −
import numpy as np # Define two matrices matrix_1 = np.array([[1, 2], [3, 4]]) matrix_2 = np.array([[5, 6], [7, 8]]) # Matrix multiplication using * result_1 = matrix_1 * matrix_2 # Matrix multiplication using @ result_2 = matrix_1 @ matrix_2 # Matrix multiplication using np.dot() result_3 = np.dot(matrix_1, matrix_2) # Matrix multiplication using np.matmul() result_4 = np.matmul(matrix_1, matrix_2) print("Matrix Multiplication (*):\n", result_1) print("Matrix Multiplication (@):\n", result_2) print("Matrix Multiplication (np.dot()):\n", result_3) print("Matrix Multiplication (np.matmul()):\n", result_4)
Following is the output obtained −
Matrix Multiplication (*): [[ 5 12] [21 32]] Matrix Multiplication (@): [[19 22] [43 50]] Matrix Multiplication (np.dot()): [[19 22] [43 50]] Matrix Multiplication (np.matmul()): [[19 22] [43 50]]
In the output above, you will see that the result of matrix multiplication using different methods is the same, but the approach varies. Here is what each result means −
- Matrix Multiplication (*): This performs element-wise multiplication, which is not the same as matrix multiplication. It multiplies corresponding elements of the two matrices.
- Matrix Multiplication (@): This performs true matrix multiplication, following the linear algebra rules. It multiplies the rows of the first matrix by the columns of the second matrix.
- Matrix Multiplication (np.dot()): The np.dot() function computes the dot product of the two matrices, which in the case of 2D arrays, equals matrix multiplication.
- Matrix Multiplication (np.matmul()): This function works similarly to np.dot() but is specifically designed for matrix multiplication.
Matrix Multiplication with Larger Matrices
Let us consider a larger example where we have matrices of shapes (2, 3) and (3, 4), and we will multiply them. The resulting matrix will have the shape (2, 4). Here is the code −
import numpy as np matrix_1 = np.array([[1, 2, 3], [4, 5, 6]]) matrix_2 = np.array([[7, 8, 9, 10], [11, 12, 13, 14], [15, 16, 17, 18]]) result = np.matmul(matrix_1, matrix_2) print(result)
Here, the result matrix has the shape (2, 4), which is consistent with the rules of matrix multiplication.
Each element in the resulting matrix is the sum of the products of corresponding elements from the rows of the first matrix and the columns of the second matrix.
[[ 74 80 86 92] [173 188 203 218]]
Real-World Applications
Matrix multiplication is a core operation in various real-world applications −
- Machine Learning: In machine learning, especially in neural networks, matrix multiplication is used to calculate activations in layers of the network. The weights of the network are represented as matrices, and multiplication helps to compute the output.
- Graphics Rendering: In computer graphics, matrix multiplication is used to transform objects in space. The transformation matrix is multiplied with the objects coordinates to perform scaling, rotation, or translation.
- Economics: In economics, matrices are used to model systems of linear equations and solve optimization problems like supply and demand models.