YouTip LogoYouTip

Python Matrix Multiplication

# Perform matrix multiplication result_matrix = multiply_matrices(matrix1, matrix2) # Print the result print("Matrix multiplication result:") for row in result_matrix: print(row) **Output:** Matrix multiplication result: [58, 64] [139, 154] ## Matrix Multiplication Using List Comprehension List comprehension provides a more concise way to implement matrix multiplication in Python. ```python # Define two matrices matrix1 = [[1, 2, 3], [4, 5, 6]] matrix2 = [[7, 8], [9, 10], [11, 12]] # Function to multiply two matrices using list comprehension def multiply_matrices_comprehension(mat1, mat2): # Get the dimensions of the matrices rows_mat1 = len(mat1) cols_mat1 = len(mat1) rows_mat2 = len(mat2) cols_mat2 = len(mat2) # Check if matrix multiplication is possible if cols_mat1 != rows_mat2: raise ValueError("Matrix multiplication is not possible: number of columns in first matrix must equal number of rows in second matrix") # Perform matrix multiplication using list comprehension result = [[sum(mat1 * mat2 for k in range(cols_mat1)) for j in range(cols_mat2)] for i in range(rows_mat1)] return result # Perform matrix multiplication result_matrix = multiply_matrices_comprehension(matrix1, matrix2) # Print the result print("Matrix multiplication result (using list comprehension):") for row in result_matrix: print(row) **Output:** Matrix multiplication result (using list comprehension): [58, 64] [139, 154] ## Matrix Multiplication Using NumPy For large-scale matrix operations, using the NumPy library is highly recommended as it provides optimized implementations that are significantly faster than pure Python approaches. ```python import numpy as np # Define two matrices using NumPy arrays matrix1 = np.array([[1, 2, 3], [4, 5, 6]]) matrix2 = np.array([[7, 8], [9, 10], [11, 12]]) # Perform matrix multiplication using NumPy result_matrix = np.dot(matrix1, matrix2) # Print the result print("Matrix multiplication result (using NumPy):") print(result_matrix) **Output:** Matrix multiplication result (using NumPy): [ ] ## Key Points to Remember 1. **Matrix Dimension Compatibility**: For matrix multiplication to be possible, the number of columns in the first matrix must equal the number of rows in the second matrix. If matrix A is of size mΓ—n and matrix B is of size nΓ—p, the result will be a matrix of size mΓ—p. 2. **Time Complexity**: The naive implementation using nested loops has a time complexity of O(mΓ—nΓ—p), which can be slow for large matrices. NumPy uses optimized algorithms and hardware acceleration to perform matrix operations much faster. 3. **Memory Efficiency**: When working with large matrices, consider using NumPy arrays which are more memory-efficient than Python lists. 4. **Element-wise Operations**: Be careful not to confuse matrix multiplication with element-wise multiplication. Element-wise multiplication uses the `*` operator and requires matrices of the same dimensions. ## Practical Applications Matrix multiplication is used in various applications including: - Machine learning and deep learning (neural networks) - Computer graphics (transformations, rotations) - Scientific computing - Data analysis and statistics - Physics simulations The choice of method depends on the specific requirements of your application. For learning purposes and small matrices, the nested loop or list comprehension approaches are suitable. For production code and large-scale computations, NumPy is the recommended choice. (#) **Related articles:** - (#) - (#) - (#) - (#) - (#) - (#) - (#) - (#) **Note:** This article is edited and organized by . Reprint indicates the source, and the original link will be retained for copyright issues. [Previous: Python3 First Steps in Programming](#) [Next: Python3 Comprehensions](#) (#) **Advertisement***Navigation** * (javascript:void(0)) * (javascript:void(0)) * (javascript:void(0)) **Bookmark** (javascript:void(0)) **Share** * (javascript:void(0)) * (javascript:void(0)) * (javascript:void(0)) * [More...](javascript:void(0)) **QR Code*Scan with WeChat to follow *| (#) | (#) | (#) | (#)
← Python Bubble Sort2Python Lambda Sort β†’