Applied Math for Creative Coders
  1. Math Models for Creative Coders
  2. Maths Basics
  3. Matrix Algebra Whirlwind Tour
  • Math Models for Creative Coders
    • Maths Basics
      • Vectors
      • Matrix Algebra Whirlwind Tour
      • Things at Right Angles
      • content/courses/MathModelsDesign/Modules/05-Maths/70-MultiDimensionGeometry/index.qmd
    • Tech
      • Tools and Installation
      • Adding Libraries to p5.js
      • Using Constructor Objects in p5.js
      • The Open Sound Protocol
    • Geometry
      • Circles
      • Complex Numbers
      • Fractals
      • Affine Transformation Fractals
      • L-Systems
      • Kolams and Lusona
    • Media
      • Fourier Series
      • Additive Sound Synthesis
      • Making Noise Predictably
      • The Karplus-Strong Guitar Algorithm
    • AI
      • Working with Neural Nets
      • The Perceptron
      • The Multilayer Perceptron
      • MLPs and Backpropagation
      • Gradient Descent
    • Projects
      • Projects

On this page

  • Introduction
  • What is a Matrix?
  • Transpose
  • Addition and Subtraction
    • Addition
    • Subtraction
  • Multiplication
  • References
  1. Math Models for Creative Coders
  2. Maths Basics
  3. Matrix Algebra Whirlwind Tour

Matrix Algebra Whirlwind Tour

Introduction

In this hopefully short and crisp tour, we will look at elementary operations on matrices: transpose, addition/subtraction and multiplication, and finally the inverse.

What is a Matrix?

An rectangular array of numbers, arranged with rows and columns. A few examples are:

Equation 1 is a 3 x 3 matrix (3 rows and 3 columns).

\[ \begin{bmatrix} 2,3,3\\ 3,1,2\\ 1,2,4\\ \end{bmatrix} \tag{1}\]

Equation 2 is a 3 x 4 matrix (3 rows and 4 columns).

\[ \begin{bmatrix} 2,3,3,4\\ 3,1,2,3\\ 1,2,4,1\\ \end{bmatrix} \tag{2}\]

Equation 3 is a 1 x 4 matrix (1 rows and 4 columns).

\[ \begin{bmatrix} 2 ~ 3 ~ 4 ~ 2\\ \end{bmatrix} \tag{3}\]

Equation 2 is a 3 x 1 matrix (3 rows and 1 columns).

\[ \begin{bmatrix} 2\\ 3\\ 1\\ \end{bmatrix} \tag{4}\]

Transpose

A matrix transpose is accomplished by writing rows as columns and vice versa. Hence an \(r~ x ~ c\) matrix becomes transposed into a \(c~ x ~ r\) matrix, as shown below:

c: ⎡a  c  1⎤
   ⎢       ⎥
   ⎣b  d  2⎦
c: ⎡a  b⎤
   ⎢    ⎥
   ⎢c  d⎥
   ⎢    ⎥
   ⎣1  2⎦

Addition and Subtraction

Matrices can be added or subtracted when they are of the same dimensions, rows and columns. Matrix elements are added or subtracted in element-wise fashion.

Addition

c: ⎡a  0⎤
   ⎢    ⎥
   ⎣b  1⎦
c: ⎡a  c⎤
   ⎢    ⎥
   ⎣b  1⎦
c: ⎡2⋅a  c⎤
   ⎢      ⎥
   ⎣2⋅b  2⎦

Subtraction

c: ⎡a  0⎤
   ⎢    ⎥
   ⎣b  1⎦
c: ⎡a  c⎤
   ⎢    ⎥
   ⎣b  1⎦
c: ⎡0  -c⎤
   ⎢     ⎥
   ⎣0  0 ⎦

Multiplication

Matrix multiplication can be visualized in many ways!! According to Gilbert Strang’s famous book on Linear Algebra, there are no less than 4 very useful ways. Let us understand perhaps the easiest one, Linear combination of Columns.

Suppose there are three students who bought two kinds of food and drink:

  • Student A: 2 samosas and 1 chai
  • Student B: 3 samosas and 0 chai
  • Student C: 0 samosa and 2 chai-s (Just before Arvind’s R class.)

Samosas cost 20 and chai costs 10. How much do they pay?

\[ \left[\begin{matrix}\color{red}{2} & \color{blue}{1}\\\\\color{red}{3} & \color{blue}{0}\\\\\color{red}{0} & \color{blue}{2}\end{matrix}\right] \]

\[ \left[\begin{matrix}\color{red}{20}\\\\\color{blue}{10}\end{matrix}\right] \]

\[ \left[\begin{matrix}50\\\\60\\\\20\end{matrix}\right] \]

\[Orders\]

\[Prices\]

\[Bills\]

We see that the first number \(20\) in B multiplies the entire first column in A, and the second number \(10\) multiplies the entire second column in A. These two multiplied columns are added to get the matrix C.

Suppose they had two options for shops? And the prices were different?

  • Samosas cost 20 and chai costs 10 at Shop#1. (as before)
  • Samosas cost 15 and chai costs 15 at Shop#2.

\[ \left[\begin{matrix}\color{red}{2} & \color{blue}{1}\\\\\color{red}{3} & \color{blue}{0}\\\\\color{red}{0} & \color{blue}{2}\end{matrix}\right] \]

\[ \left[\begin{matrix}20 & 15\\\\10 & 15\end{matrix}\right] \]

\[ \left[\begin{matrix}50 & 45\\\\60 & 45\\\\20 & 30\end{matrix}\right] \]

\[Orders\]

\[Menus\]

\[Amounts\]

Shop#2 is cheaper. Drag peasant#3 and go there, peasants#1 and #2.

How did this multiplication happen? Same story as with one shop/prices, repeated to handle the second shop. Matrix B2 now has two columns for prices, for Shop#1 and Shop#2. Multiplication takes each column in B2 and does the same weighted-column-addition with A2. Hence two columns of answers in C2.

ImportantMatrix Multiplication in 4 ways

As shown in Gilbert Strang’s book, there are 4 ways of thinking about matrix multiplication:

  • Our method of weighting columns in A using columns B and adding
  • Weighting rows in B using rows in A and adding
  • Multiplying individual values from rows in A and columns in B and adding up, for each location in C. ( This is the common textbook method)
  • Multiplying rows in A and columns in B. ( A vectorized version of the previous method).

For now, this one method should suffice.

References

  1. Gilbert Strang. Linear Algebra and its Applications. Thomson/Brooks-Cole.
  2. Glenn Henshaw.(Sep 28, 2019) Three Ways to Understand Matrix Multiplication. https://ghenshaw-work.medium.com/3-ways-to-understand-matrix-multiplication-fe8a007d7b26
Back to top
Vectors
Things at Right Angles

License: CC BY-SA 2.0

Website made with ❤️ and Quarto, by Arvind V.

Hosted by Netlify .