Matrix Operations in Python

in

In fact, it can actually be pretty fun once you get the hang of it!

To set the stage: what exactly is a matrix? Well, let me put it to you like this imagine you have a bunch of numbers arranged in rows and columns. That’s right, we’re talking about spreadsheets here! But instead of boring old data entry, we can use these matrices for some serious math magic.

So how do we actually perform matrix operations in Python? Well, let me show you an example:

# Importing the NumPy library to handle large arrays and matrices
import numpy as np 

# Creating a 3x2 matrix with values [1, 2], [3, 4], and [5, 6] in each row
matrix_a = np.array([[1, 2], [3, 4], [5, 6]]) 

# Printing the resulting matrix
print(matrix_a)

This code will output:

# This code creates a 3x2 matrix using nested lists
matrix = [[1, 2], [3, 4], [5, 6]]

# This code iterates through each row in the matrix
for row in matrix:
    # This code iterates through each element in the row
    for element in row:
        # This code prints each element in the row
        print(element, end=" ")
    # This code creates a new line after each row is printed
    print()

Now that we have our first matrix, let’s say we want to multiply it by another matrix. Here’s how you do it in Python using NumPy:

# creating a second 2x1 matrix with values [7] and [8] in each row
matrix_b = np.array([[7], [8]]) # creating a NumPy array with 2 rows and 1 column, containing the values 7 and 8
print(np.matmul(matrix_a, matrix_b)) # multiplying the two matrices using NumPy's matmul function and printing the result

# Note: The original script did not define the matrix_a variable, so it would result in an error. Also, the purpose of this segment is to create a second matrix to multiply with the first one.

# creating a 2x2 matrix with values [1, 2] and [3, 4] in each row
matrix_a = np.array([[1, 2], [3, 4]]) # creating a NumPy array with 2 rows and 2 columns, containing the values 1, 2, 3, and 4

# Note: This segment creates the first matrix that we want to multiply with the second one.

# multiplying the two matrices using NumPy's matmul function and printing the result
print(np.matmul(matrix_a, matrix_b))

# Note: This segment performs the matrix multiplication and prints the result.

This code will output:

# This code will output a 3x1 numpy array with values 58, 139, and 220.

# Import the numpy library
import numpy as np

# Create a 3x1 numpy array with values 58, 139, and 220
arr = np.array([[58], [139], [220]])

# Print the array
print(arr)

# Output:
# [[ 58]
#  [139]
#  [220]]

Pretty cool, right? But what if we want to add or subtract these matrices instead of multiplying them? No problem! Here’s how you do it in Python using NumPy:

# Importing the NumPy library
import numpy as np

# Defining two matrices, matrix_a and matrix_b
matrix_a = np.array([[1, 2], [3, 4]]) # creating a 2x2 matrix with values 1, 2, 3, 4
matrix_b = np.array([[5, 6], [7, 8]]) # creating another 2x2 matrix with values 5, 6, 7, 8

# Adding the two matrices together and printing the result
print(np.add(matrix_a, matrix_b)) # using NumPy's add function to perform addition

# Subtracting the second matrix from the first and printing the result
print(np.subtract(matrix_a, matrix_b)) # using NumPy's subtract function to perform subtraction

# Output:
# [[ 6  8]
#  [10 12]]
# [[-4 -4]
#  [-4 -4]]

# The above code imports the NumPy library and defines two matrices, matrix_a and matrix_b, with values 1, 2, 3, 4 and 5, 6, 7, 8 respectively.
# The first print statement uses the NumPy add function to add the two matrices together and prints the result.
# The second print statement uses the NumPy subtract function to subtract the second matrix from the first and prints the result.

This code will output:

# This code will output a 3x2 matrix with values ranging from 3 to 10, and a second 3x2 matrix with negative values ranging from -6 to -19.

# The first matrix is created using the numpy array function, which takes in a list of lists as its argument. 
# The inner lists represent the rows of the matrix, while the outer list represents the entire matrix. 
# The values in the matrix are separated by commas and enclosed in square brackets.

# The second matrix is created using the numpy negative function, which takes in a matrix as its argument and returns a matrix with all its values negated.

import numpy as np # Importing the numpy library to use its functions.

matrix1 = np.array([[8, 10], [3, 6], [5, 4]]) # Creating the first matrix with values ranging from 3 to 10.

matrix2 = np.negative(matrix1) # Creating the second matrix by negating the values in the first matrix.

print(matrix1) # Printing the first matrix.

print(matrix2) # Printing the second matrix.

Matrix operations in Python made easy (or at least a little less boring). So next time your math teacher assigns some tedious matrix multiplication problem, just remember that with NumPy and Python, it can be fun too.

SICORPS