Introduction

A matrix is a two-dimensional representation of data. Representing data in the form of a matrix can be helpful for visualization. In python, will use lists to create a matrix. Before we move forward, it will be better if one reads our post on vectors first. The post contains information on typing, type alias, and assertion features that we will use in this post. We will also be using a matrix to represent vectors. Arguably, this is the second part of our post on vectors.

Creating matrix in python

Creating a matrix in python is a simple process. We will use the type alias feature to equate a list of lists as a matrix. Each list is written in a new line. The length of the list will become the number of columns. The number of lists will equal the total number of rows.

# importing List from typing module
from typing import List # List has capital L

# Creating type alias for a matrix
Matrix = List[List[float]]

# Creating the 3x3 matrix
A = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]

Finding the dimension of a matrix

As already mentioned above, the number of rows of a matrix will be equal to the length of the matrix. The number of columns will be equal to the length of each list. We can use it to create a function for finding the dimensions of a matrix.

from typing import Tuple


# defining function for finding dimension of the matrix
def dimension(M: Matrix) -> Tuple[int, int]:

     num_rows = len(M)  # number of rows is equal to length of list
     num_columns = len(M[0]) if A else 0  # number of columns is equal to lenght of each list
     return num_rows, num_columns


# testing our function
assert dimension([[1, 2, 3], [4, 5, 6]]) == (2, 3

Vectors in matrix

Matrix is made of rows and columns. We can use these rows and columns to represent vectors. One can use the following functions to return a row or column from a matrix.

# creating type alias for Vector
Vector = List[float]


# defining function for getting rows
def get_row(M: Matrix, i: int) -> Vector:

    return M[i]  # Returns ith row


# defining function for getting column
def get_column(M: Matrix, j: int) -> Vector:

    return [M_i[j] for M_i in M]  # Returns Jth column

One can read more about vectors here.

Creating a function for generating a matrix

# importing Callable
from typing import Callable


# defining function
def matrixer(num_rows: int, num_cols: int, entry_fn: Callable[[int, int], float]) -> Matrix:
    return [[entry_fn(i, j) for j in range(num_cols)] for i in range(num_rows)]

Here we created a function that will define the dimension of the matrix we are about to create. We also have defined an entry_fn using Callable checker. Callable checks whether input is a callable function. List comprehension used to generate the matrix. We can use the above function to create an identity matrix as follows.

def identity_matrix(n: int) -> Matrix:

    return matrixer(n, n, lambda i, j: 1 if i == j else 0)


assert identity_matrix(5) == [[1, 0, 0, 0, 0],
                               [0, 1, 0, 0, 0],
                               [0, 0, 1, 0, 0],
                               [0, 0, 0, 1, 0],
                               [0, 0, 0, 0, 1]]

The first two argument defines the number of rows and columns. We use lambda keyword to define a one-line function. The Callable checker will check if it is in fact a callable function and returns True.

When i==j, the corresponding element will be in the diagonal. In an identity matrix, elements in the diagonal should be 1 while the rest should be zero.

Conclusion

In this brief tutorial, we learned to create matrices in python. We also learned to represent vectors inside the matrices. We also created a function to generate a matrix of a given dimension by defining a rule. The function was used to generate an identity matrix.

Written by Aravind Sanjeev, an India-based blogger and web developer. Read all his posts. You can also find him on twitter.