Python Basic
Python Interview Programs
- Python Interview Programs
- Python Abundant Number
- Python Amicable Number
- Python Armstrong Number
- Python Average of N Numbers
- Python Automorphic Number
- Python Biggest of three numbers
- Python Binary to Decimal
- Python Common Divisors
- Python Composite Number
- Python Condense a Number
- Python Cube Number
- Python Decimal to Binary
- Python Decimal to Octal
- Python Disarium Number
- Python Even Number
- Python Evil Number
- Python Factorial of a Number
- Python Fibonacci Series
- Python GCD
- Python Happy Number
- Python Harshad Number
- Python LCM
- Python Leap Year
- Python Magic Number
- Python Matrix Addition
- Python Matrix Division
- Python Matrix Multiplication
- Python Matrix Subtraction
- Python Matrix Transpose
- Python Maximum Value of an Array
- Python Minimum Value of an Array
- Python Multiplication Table
- Python Natural Number
- Python Number Combination
- Python Odd Number
- Python Palindrome Number
- Python Pascalβs Triangle
- Python Perfect Number
- Python Perfect Square
- Python Power of 2
- Python Power of 3
- Python Pronic Number
- Python Prime Factor
- Python Prime Number
- Python Smith Number
- Python Strong Number
- Python Sum of Array
- Python Sum of Digits
- Python Swap Two Numbers
- Python Triangular Number
Python Program to Perform Matrix Multiplication
Photo Credit to CodeToFun
π Introduction
Matrix multiplication is a fundamental operation in linear algebra and computer science. It involves multiplying two matrices to produce a third matrix.
In this tutorial, we will explore a python program that performs matrix multiplication.
We'll break down the logic step by step and provide a sample implementation.
π Example
Let's dive into the python code that performs matrix multiplication.
# Function to perform matrix multiplication
def multiply_matrices(first_matrix, second_matrix):
result_matrix = [[0 for _ in range(len(second_matrix[0]))] for _ in range(len(first_matrix))]
# Perform matrix multiplication
for i in range(len(first_matrix)):
for j in range(len(second_matrix[0])):
for k in range(len(second_matrix)):
result_matrix[i][j] += first_matrix[i][k] * second_matrix[k][j]
return result_matrix
# Function to display a matrix
def display_matrix(matrix):
for row in matrix:
print('\t'.join(map(str, row)))
# Sample input matrices (3x3)
first_matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
second_matrix = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]
# Call the function to perform matrix multiplication
result_matrix = multiply_matrices(first_matrix, second_matrix)
# Display the matrices
print("First Matrix:")
display_matrix(first_matrix)
print("\nSecond Matrix:")
display_matrix(second_matrix)
print("\nResult Matrix:")
display_matrix(result_matrix)
π» Testing the Program
Feel free to replace the sample matrices with your own 3x3 matrices in the main function to test the program with different input.
First Matrix: 1 2 3 4 5 6 7 8 9 Second Matrix: 9 8 7 6 5 4 3 2 1 Result Matrix: 30 24 18 84 69 54 138 114 90
Run the program to see the result of the matrix multiplication.
π§ How the Program Works
- The program defines a function multiply_matrices that takes two matrices (first_matrix and second_matrix) and computes their product, storing the result in the result_matrix.
- It initializes the result_matrix with zeros and uses three nested loops to perform the matrix multiplication.
- The display_matrix function is used to display the contents of a matrix.
- Sample input matrices (first_matrix and second_matrix) are provided, and the program calls the multiplication function.
- The program then displays the original matrices and the result matrix.
π§ Understanding the Concept of Matrix Multiplication
Matrix multiplication is defined as follows: given two matrices A (of dimensions m x n) and B (of dimensions n x p), the product matrix C (of dimensions m x p) is obtained by multiplying each element of a row in matrix A by the corresponding element of a column in matrix B and summing up the results.
π Conclusion
Matrix multiplication is a powerful operation used in various fields, including graphics, physics simulations, and machine learning.
Understanding the logic behind matrix multiplication and implementing it in python provides a foundational understanding of linear algebra in computer science.
Feel free to experiment with different matrix sizes and values to further explore the capabilities of this program. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Python Program to Perform Matrix Multiplication), please comment here. I will help you immediately.