Mathematics with Python: Basics of Matrices

This mini-course provides basics of what is a matrix, its types and how to create and manipulate matrices with the numpy python package.
Did you know?

The term matrix was introduced by the 19th-century English mathematician James Sylvester, but it was his friend the mathematician Arthur Cayley who developed the algebraic aspect of matrices in two papers in the 1850s.

Today in this post we will learn more math less programming. Why? because here we will learn some basic mathematics you must need in the field of Computer Science.

So let's do some maths work...

Mathematics with Numpy: What is a Matrix?

In this post, you will get some challenging problems to solve with every point you learn. Try to solve them. The problems are easy but the real challenge is to solve them using python code. You can share your solution code in the comments.

Table of Contents

Introduction

Before you start reading this post make sure you have basic knowledge of mathematical operations, python programming, and other things that will be explained here.

But before starting, install the Numpy python package. Installation instructions can be found on numpy.org/install.

Numpy is the fundamental python package widely used for scientific computing.

After installing you are ready to start learning about matrices.

What is a Matrix

In mathematics, a matrix (plural matrices) is a rectangular array or table of numbers, symbols, or expressions, arranged in rows and columns. The numbers, symbols, or expressions are called the elements or entries of the matrix.

 For example, this is a matrix

$${\left[\begin{array}{ccc}8& 1& 6\\ 3& 5& 7\\ 4& 9& 2\end{array}\right]}_{3\times 3}$$

The 3x3 at the bottom right-hand corner denotes the size of the given matrix in "row x column" form called order of matrix.

To represent a matrix of any order (size) with python we can use the following code.

import numpy as np

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

print()
# OR

that = np.matrix('1 2 3; 3 3 4')
print(that)

In both ways, you can represent a matrix in python with the help of the numpy python package. There are many ways to achieve the equivalent and you are just a search away from it, everything is available on the Internet for free.

Notation

Every element of a matrix can be defined in the format \({a}_{ij}\). Where \(i\) represents the row and \(j\) represents the column to which the element belongs.

$$A=\left[\begin{array}{cc}{a}_{11}& {a}_{12}\\ {a}_{21}& {a}_{22}\end{array}\right]$$

Generally, matrices are denoted with a capital letter. Like the one above which is denoted by capital A. \({a}_{11}\), \({a}_{12}\), \({a}_{21}\) and \({a}_{22}\) are the respective elements in the matrix.

Challengers
1. If a matrix has 8 elements, what are the possible orders it can have?
2. Construct a \(3\times 3\) matrix whose elements are given by \({a}_{ij}=\frac{1}{2}\left|i-3j\right|\). ( |.| represents the modulus function).

Types of Matrices

Matrices are of the following types,

  • Column matrix
  • Row matrix
  • Square matrix
  • Diagonal matrix
  • Scalar matrix
  • Identity matrix
  • Zero matrix

You can learn more about these types on Wikipedia.

Operations on Matrices

In this section, we will see certain operations on matrices, namely, the addition of matrices, multiplication of a matrix by a scalar, and difference of matrices.

Addition of matrices

if \(\mathrm{A}=\left[\begin{array}{ccc}{\mathrm{a}}_{11}& {\mathrm{a}}_{12}& {\mathrm{a}}_{13}\\ {\mathrm{a}}_{21}& {\mathrm{a}}_{22}& {\mathrm{a}}_{23}\end{array}\right]\phantom{\rule{0ex}{0ex}}\) is a \(2\times 3\) matrix and \(\mathrm{B}=\left[\begin{array}{ccc}{\mathrm{b}}_{11}& {\mathrm{b}}_{12}& {\mathrm{b}}_{13}\\ {\mathrm{b}}_{21}& {\mathrm{b}}_{22}& {\mathrm{b}}_{23}\end{array}\right]\phantom{\rule{0ex}{0ex}}\) is another matrix of same order. Then, we define $$\mathrm{A}+\mathrm{B}=\left[\begin{array}{ccc}{\mathrm{a}}_{11}+{\mathrm{b}}_{11}& {\mathrm{a}}_{12}+{\mathrm{b}}_{12}& {\mathrm{a}}_{13}+{\mathrm{b}}_{13}\\ {\mathrm{a}}_{21}+{\mathrm{b}}_{21}& {\mathrm{a}}_{22}+{\mathrm{b}}_{22}& {\mathrm{a}}_{23}+{\mathrm{b}}_{23}\end{array}\right]\phantom{\rule{0ex}{0ex}}$$.

# Python code to demonstrate matrix operation add()
  
# importing numpy
import numpy
  
# initializing matrices
x = numpy.array([[4, 3], [4, 3]])
y = numpy.array([[2, 3], [3, 13]])
  
# using add() to add matrices
print ("The element wise addition of matrix is : ")
print (numpy.add(x,y)))

In general, if \(\mathrm{A}=\left[\begin{array}{c}{\mathrm{a}}_{\mathrm{ij}}\end{array}\right]\phantom{\rule{0ex}{0ex}}\) and \(\mathrm{B}=\left[\begin{array}{c}{\mathrm{b}}_{\mathrm{ij}}\end{array}\right]\phantom{\rule{0ex}{0ex}}\) are two matrices of the same order, \(m\times n\). Then, the sum of the two matrices A and B is defined as a matrix \(\mathrm{C}={\left[\begin{array}{c}{\mathrm{c}}_{\mathrm{ij}}\end{array}\right]}_{\mathrm{i}\times \mathrm{j}}\phantom{\rule{0ex}{0ex}}\), where \({\mathrm{c}}_{\mathrm{ij}}={\mathrm{a}}_{\mathrm{ij}}+{\mathrm{b}}_{\mathrm{ij}}\phantom{\rule{0ex}{0ex}}\), for all possible values of \(i\) and \(j\)

We emphasize that if A and B are not of the same order, A+B is not defined.

Multiplication of a Matix by Scalar

In general, we may define multiplication of a matrix by a scalar as follows: if \(\mathrm{A}={\left[\begin{array}{c}{\mathrm{a}}_{\mathrm{ij}}\end{array}\right]}_{\mathrm{i}\times \mathrm{j}}\phantom{\rule{0ex}{0ex}}\) is a matrix and \(k\) is a scalar, then \(kA\) is another matrix which is obtained by multiplying each element of A by the scalar \(k\)

In other words, \(\mathrm{kA}=\mathrm{k}{\left[{\mathrm{a}}_{\mathrm{ij}}\right]}_{\mathrm{m}\times \mathrm{n}}={\left[\mathrm{k}\left({\mathrm{a}}_{\mathrm{ij}}\right)\right]}_{\mathrm{m}\times \mathrm{n}}\phantom{\rule{0ex}{0ex}}\), that is, \((\mathrm{i},\mathrm{j}{)}^{\mathrm{th}}\phantom{\rule{0ex}{0ex}}\) element of \(kA\) is \({\mathrm{ka}}_{\mathrm{ij}}\) for all possible values of \(i\) and \(j\).

# Python code to demonstrate matrix operation multiply()
  
# importing numpy
import numpy
  
# initializing matrices
x = numpy.array([[4, 13], [7, 35]])
n = 5
  
# using multiply() to multiply matrix with a scalar
print ("The product of matrix and scalar is : ")
print (numpy.multiply(x,n))

Negative of a matrix The negative of a matrix is denoted by -A. We define -A=(-1)A.

Difference of Matrices

In general, if \(\mathrm{A}=\left[\begin{array}{c}{\mathrm{a}}_{\mathrm{ij}}\end{array}\right]\phantom{\rule{0ex}{0ex}}\) and \(\mathrm{B}=\left[\begin{array}{c}{\mathrm{b}}_{\mathrm{ij}}\end{array}\right]\phantom{\rule{0ex}{0ex}}\) are two matrices of the same order, \(m\times n\). Then, the difference A-B is defined as a matrix \(\mathrm{D}={\left[\begin{array}{c}{\mathrm{d}}_{\mathrm{ij}}\end{array}\right]}_{\mathrm{i}\times \mathrm{j}}\phantom{\rule{0ex}{0ex}}\), where \({\mathrm{d}}_{\mathrm{ij}}={\mathrm{a}}_{\mathrm{ij}}-{\mathrm{b}}_{\mathrm{ij}}\phantom{\rule{0ex}{0ex}}\), for all possible values of \(i\) and \(j\). In other words, D=A-B=A+(-1)B, that is sum of the matrix A and matrix -B.

# Python code to demonstrate matrix operation subtract()
  
# importing numpy
import numpy
  
# initializing matrices
x = numpy.array([[65, 34], [44, 33]])
y = numpy.array([[22, 34], [34, 13]])
  
# using subtract() to subtract matrices
print ("The element wise subtraction of matrix is : ")
print (numpy.subtract(x,y))

Challengers
1. Let \(A=\left[\begin{array}{cc}2& 4\\ 3& 2\end{array}\right]\), \(B=\left[\begin{array}{cc}1& 3\\ -2& 5\end{array}\right]\) and \(C=\left[\begin{array}{cc}-2& 5\\ 3& 4\end{array}\right]\)
Find each of the following: (i) A+B (ii) A-B (iii) 3A-C

2. Simplify
\(\cos\left(\theta \right)\left[\begin{array}{cc}\cos\left(\theta \right)& \sin\left(\theta \right)\\ -\sin\left(\theta \right)& \cos\left(\theta \right)\end{array}\right]+\sin\left(\theta \right)\left[\begin{array}{cc}\sin\left(\theta \right)& -\cos\left(\theta \right)\\ \cos\left(\theta \right)& \sin\left(\theta \right)\end{array}\right]\)

So that is enough for this as basics. Next time we will see some advance operations on matrices like multiplication of matrices, transverse, inverse and conjugate of a matrix. So stay tuned and subscribe to my newsletter.

Till the next post why not solve the challenger problems and share the code in the comments, remember to solve them using code.

Till then sayonara 👋

Hello! Myself Tejas Mahajan. I am an Android developer, Programmer, UI/UX designer, Student and Navodayan.