Introduction to Matrix Algebra with Python

Discovering basic matrix varieties and operations with NumPy

George Marklow
8 min readDec 5, 2024

Introduction to Linear Algebra

Linear algebra explores linear equations, vector spaces, matrices, and linear transformations.

This branch of mathematics has wide applications in science, engineering, data analysis, and business, as well as other areas of mathematics. It is foundational to various fields, including machine learning, medical imaging, financial portfolio optimization, and cryptography.

This article introduces linear algebra using Python and the scientific computing package NumPy.

Installing Python and NumPy

You can install Python and NumPy on your machine or use one of the many convenient online compiler sites.

To install it on your local machine, visit the Downloads section of the Python website https://www.python.org/downloads/. Once installed, you can follow the instructions to install NumPy https://numpy.org/install/

Once both are installed, bring up a command console and type each line listed below followed by ENTER each time to prove both Python and NumPy are successfully installed:

python
import numpy as np
print(np.array([1]))

You should see the output, which proves both installations were successful:

[1]

Create a Matrix

A matrix is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. It is generally represented as:

where

  • A is the name of the matrix
  • m is the row index
  • n is the column index

Here’s how to create and print a 2 x 2 matrix in NumPy, specifying specific matrix elements:

import numpy as np

matrix = np.array([[1, 2],[3, 4]])
print(matrix)

Output:

[[1 2]
[3 4]]

Scalars

Scalars are a special subclass of matrices with one row and one column:

import numpy as np

matrix = np.array([1])
print(matrix)

Output:

[1]

Scalar quantities are physical quantities fully described by their magnitude, without any associated direction, unlike vector quantities.

  • Magnitude Only: Scalars are defined by a numerical value and are typically a unit, lacking any directional component.
  • Scalars remain constant regardless of the reference frame or direction.

Some examples of scalar quantities include:

  • Mass: e.g., 10 kg
  • Time: e.g., 60 s
  • Temperature: e.g., 25 degrees Celcius

Vectors

A vector is a special matrix case with either one column or row.

Take the following matrix A:

The column vectors are the columns of A:

and the row vectors, the rows of the A, are:

Here’s how to create a row vector with NumPy:

import numpy as np

row_vector = np.array([[1, 2, 3]])
print(row_vector)

Output:

[[1 2 3]]

To create a column vector, you need to call the reshape function to transform the row vector into a column vector:

import numpy as np

column_vector = np.array([1, 2, 3]).reshape(-1, 1)
print(column_vector)

Output:

[[1]
[2]
[3]]

A vector quantity has both magnitude and direction, while scalar quantities are described only by magnitude. For example:

  • Velocity: The speed and direction of an object’s motion.
  • Acceleration: The rate of change of velocity in a direction.
  • Force: A push or pull on an object, defined by magnitude and direction.

Dimension of a matrix

The dimension of a matrix is how many rows and columns it has. For a matrix A with m rows and n columns, its dimension is:

In Python, we can extract (or unpack) a tuple back into constituent variables. The matrix shape function returns us a tuple of row and column dimensions that we can unpack as follows:

import numpy as np

matrix = np.array([[1, 2],[3, 4],[5, 6]])
rows, cols = matrix.shape
print(f"Matrix dimensions: {rows} rows, {cols} columns")

Output:

Matrix dimensions: 3 rows, 2 columns

Accessing Matrix Entries

Vectors

Here’s how to access the 3rd element of a vector. Remember that using zero-based indexing means the 3rd element is at position 2:

import numpy as np

arr = np.array([1,2,3])
print(arr[2])

Output:

3

Matrices

To print the element at row 2, column 2 (using zero indexing):

import numpy as np

matrix = np.array([[1, 2],[3, 4]])
print(matrix[1, 1])

Output:

4

We can also extract a matrix's row and column vectors, as described previously in this article.

In NumPy, we can slice the matrix into its column and row vectors by index as follows:

import numpy as np

matrix = np.array([[1, 2],[3, 4]])

# Rows
print(matrix[0, :])
print(matrix[1, :])

# Columns
print(matrix[:, 0])
print(matrix[:, 1])

Output:

[1 2]
[3 4]

[1 3]
[2 4]

Matrix Equality

As it sounds, two matrices are equal if all their elements are equal.

We can create three matrices in NumPy, where matrices one and two are equal but matrix three is different due to a different value in row two column two. We can check the equality using the array_equal function:

import numpy as np

matrix1 = np.array([[1, 2],[3, 4]])
matrix2 = np.array([[1, 2],[3, 4]])
matrix3 = np.array([[1, 2],[3, -1]])

print(np.array_equal(matrix1, matrix2))
print(np.array_equal(matrix1, matrix3))

Output:

True
False

Matrix Transpose

The transpose of a matrix is obtained by flipping the matrix over its diagonal and swapping its rows with columns.

Let the original 3 x 2 matrix A be:

The transposed matrix is:

In NumPy, use the T property of the matrix object to obtain the transpose:

import numpy as np

matrix = np.array([[1, 2],[3, 4],[5, 6]])
print(matrix.T)

Output:

[[1 3 5]
[2 4 6]]

Square Matrix

This last section deals with square matrices, which have the same number of rows and columns, as well as important subclasses of square matrices.

Here’s the definition of a 2 x 2 matrix, which contains the same number of rows and columns:

Define a square matrix manually

Take the following square matrix:

We already know how to create this matrix with NumPy:

import numpy as np

square_matrix = np.array([[1, 2],[3, 4]])
print(square_matrix)

Output:

[[1 2]
[3 4]]

Zeros

A zeros matrix is a matrix with all zero elements:

Use the zeros function to create a 3 x 3 matrix with zeros:

import numpy as np

square_matrix = np.zeros((3, 3))
print(square_matrix)

Output:

[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

Ones

Similarly, a ones matrix is a matrix with all ones:

Use the ones function to create a 3 x 3 matrix of ones:

import numpy as np

square_matrix = np.ones((3, 3))
print(square_matrix)

Output:

[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]

Identity

An identity matrix is a square matrix in which all the diagonal elements are 1, and all off-diagonal elements are 0:

For example, a 3 x 3 identity matrix is:

Here’s how to create that matrix in NumPy:

import numpy as np

identity_matrix = np.identity(3)
print(identity_matrix)

Output:

[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

Diagonal

We’ve already spoken about a diagonal matrix above — the identity matrix. A diagonal matrix is a square matrix in which all the off-diagonal elements are zero.

The only potentially non-zero elements are those on the main diagonal:

Take the following 3 x 3 matrix:

Here’s how to create that with NumPy:

import numpy as np

diagonal_matrix = np.diag([1, 2, 3])
print(diagonal_matrix)

Output:

[[1 0 0]
[0 2 0]
[0 0 3]]

Random Matrix

Exactly as it sounds, NumPy can generate a matrix with random values using the rand function.

Here’s a randomly generated 3 x 3 matrix:

import numpy as np

square_matrix = np.random.rand(3, 3)
print(square_matrix)

Output (varies each time):

[[0.3509402  0.50208545 0.25264607]
[0.75654587 0.75966254 0.73497304]
[0.48103758 0.02960833 0.50763037]]

Matrix Addition

We can add two matrices of the same dimensions to create a new matrix by adding the corresponding elements.

Adding matrices A and B:

This creates a new matrix C:

For example:

The addition of A and B is:

Here’s how to perform that operation with NumPy:

import numpy as np

matrix1 = np.array([[1, 2],[3, 4]])
matrix2 = np.array([[5, 6],[7, 8]])
print(matrix1 + matrix2)

Output:

[[ 6  8]
[10 12]]

Dot Product

The dot product of two matrices A and B is as follows for the number of columns of A equals the number of rows of B:

where

This sum can also be written succinctly using sigma notation:

For example,

The dot product of A and B is:

Here’s how to perform that operation with NumPy:

import numpy as np

matrix1 = np.array([[1, 2, 3],[4, 5, 6]])
matrix2 = np.array([[7, 8],[9, 10],[11, 12]])
print(np.dot(matrix1, matrix2))

Output:

[[ 58  64]
[139 154]]

Thanks for reading! Let me know your thoughts in the comments section below, and don’t forget to subscribe. 👍

References

  • Introduction to Applied Linear Algebra by Stephen Boyd and Lieven Vandenberghe, ISBN 978–1–31651–896–0, Cambridge University Press (2019)
  • Lipschutz, S. and Lipson, M. (2009) Linear Algebra (Schaum’s Outlines). 4th Edition, McGraw Hill, New York.
  • NumPy Documentation: https://numpy.org/doc/stable/

--

--

George Marklow
George Marklow

Written by George Marklow

George is a software engineer, author, blogger, and abstract artist who believes in helping others to make us happier and healthier.

No responses yet