Python Program to Multiply Matrices Without Using In-Built Function

Introduction: Matrix multiplication is a fundamental operation in linear algebra, and it has numerous applications in various domains, including data science, machine learning, computer graphics, and more. In this blog post, we will explore how to perform matrix multiplication in Python using the popular NumPy library. NumPy is a powerful library for numerical computing that provides efficient and optimized functions for matrix operations, making it an ideal tool for matrix multiplication.

Importing NumPy and Generating Random Matrices: To get started, we begin by importing the NumPy library using the alias "np", which is a common convention. We then define the dimensions of two random matrices, "a" and "b", using variables "b1", "c1", "b2", and "c2". We generate these matrices using NumPy's "randint" function, which generates random integers between a specified range.

import numpy as np

b1 = 3
c1 = 3
a = np.random.randint(0, 10, (b1, c1))

b2 = 3
c2 = 2
b = np.random.randint(0, 10, (b2, c2))

print("Matrix A:")
print(a)
print("Matrix B:")
print(b)

Performing Matrix Multiplication Manually: Next, we check if the dimensions of matrices "a" and "b" are compatible for matrix multiplication. For matrix multiplication to be possible, the number of columns in matrix "a" must be equal to the number of rows in matrix "b". If the condition is satisfied, we initialize an empty array "arr" of zeros with dimensions (b1, c2) to store the result of the matrix multiplication.

We then use nested loops to iterate through the rows of matrix "a" and the columns of matrix "b" to perform the dot product of corresponding rows and columns. The dot product is calculated by multiplying the corresponding elements of the row in matrix "a" with the column in matrix "b" and summing the results. The sum is then stored in the corresponding entry of "arr" after each dot product calculation.

if(c1 == b2):
    arr = np.zeros((b1, c2))
    add = 0
    for i in range(0, b1):
        for j in range(0, c2):
            for k in range(0, b2):
                add = add + (a[i][k] * b[k][j])
            arr[i][j] = add
            add = 0
    print("Manually Computed Matrix Multiplication:")
    print(arr)
else:
    print("Matrix multiplication not possible")

Using NumPy's Matrix Multiplication Function: NumPy provides a built-in function "dot" or "@" operator for matrix multiplication, which is more efficient and concise. We can simply use "np.dot(a, b)" or "a @ b" to perform the same matrix multiplication operation.

# Using NumPy's built-in function for matrix multiplication
arr_np = np.dot(a, b)
# or arr_np = a @ b

print("NumPy Computed Matrix Multiplication:")
print(arr_np)

Conclusion: Matrix multiplication is a fundamental operation in linear algebra and is used in various applications in data science, machine learning, and other fields. In this blog post, we explored how to perform matrix multiplication in Python using the NumPy library. We discussed how to manually compute matrix multiplication using nested loops and also how to use NumPy's built-in function for more efficient and concise matrix multiplication. NumPy provides powerful functions for numerical computing,

Post a Comment

0 Comments