Matrices, What Are They Good For?
Tags: computerscience, programming, learning, code
Published: 2020-06-24 17:21:41

The Matrix (1999) is a science fiction film that explores the idea of reality as we know it being a simulated experience.

Okay, now that that's out of the way, we can move onto the cream of the milk. What are matrices, why do we use them, and what do we use them for?

Many of our first introductions to matrices in programming have been related to algorithmic exercises, usually to test our comprehension of traversing nested data structures. And it may have stopped there. As such, sometimes when our only interaction to concepts is in an academic context, it can feel futile and pointlessly difficult to understand. So, I wanted to further explore their purpose, especially practically.

Most simply, a matrix is a way to represent data. The data is typically numerical in some way or another - it could be a set of numbers, symbols, or expressions. It's represented as a grid, or a 2D array, like this:

```c
[ [ -1, -1, -1 ],
  [ -1,  9, -1 ],
  [ -1, -1, -1 ] ]
```

So, as you can see here, we have an array consisting of three arrays, each one being a row in the matrix, and each one consisting of three elements. A 3x3 grid, with 9 elements total. For good measure, let's go over some basics. If we wanted to access the **top right** element, we would access the first row (`row = 0`, using zero-indexing) and then the third and last element ( `column = 2`), or `matrix[0][2]` . If we wanted to access the center element - the `9` - we would access the second element in the second row, or `matrix[1][1]`. 

Essentially, a collection of some rows and some columns. What's so special about that? Well, as goes for any data structure, its power comes from how it's leveraged. Barring special types, matrices have fixed dimensions - so, while the data inside it may change, a matrix of size 3x3 like the one above will remain a 3x3 matrix. The containing data is also related in some way. Sure, if you are implementing the matrix yourself, you could remove a row or a column or throw in some random data. But it would kind of make it obsolete, right? Abstract as it may be, matrices are what they are because they are meant to serve a specific purpose - that's kind of the beauty of data structures in general.

One of the benefits of the matrix structure is its ability to apply linear transformations. I'm going to use the definition of a linear transformation found [here](https://brilliant.org/wiki/linear-transformations/):

> "A linear transformation is a function from one vector space 
 to another that respects the underlying (linear) structure of each vector space"

There's a lot of math that surrounds this - specifically linear algebra - and I'll provide a few links for further reading below. That is, however, outside of the scope of this post. To keep it brief, operations that can be simplified to linear mapping are a perfect application for working with matrices. Consider this simple example: 

![matrix multiplication example](https://dev-to-uploads.s3.amazonaws.com/i/b0zmo0udwmfc7vkddbq8.png)

Let's scale that idea up with some complexity and practicality. We have a digital image file, represented as pixels. Those pixels are represented in a grid - a *matrix*. We want all the pixels in the image to change by some amount - maybe we want the image to be brighter. Remember that pixels are often represented as the combination of red, blue, and green channels, each ranging form 0-255 in value. In the digital space, color is additive - so, the higher the value, the more saturated and brighter a color becomes. Brightness filters can be more complex, but for our sake, let's just say we want to increase each `rgb` value by `30`. See where we're going, here? Each value needs the matrix needs to change by a fixed amount - `30`. A *linear* mapping - a case of a matrix addition! 

Matrices have a huge standing in graphics processing for cases like the above. It can be used for other operations, like image rotation, color filters, warping, scaling, convolution* and so on. They are not confined to 2D images - they are made just as useful in 3D graphics rendering as well. Though graphics are a major application of matrices, it doesn't stop there - they are, after all, a data structure, and as such can be useful in general transformation, analysis, and representation of data.  

At the heart of it, the take-away is this: because of their structure, data that is represented in matrices can make otherwise complex operations very efficient. As Larry Hardesty notes in an [MIT News article](https://news.mit.edu/2013/explained-matrices-1206#:~:text=Among%20the%20most%20common%20tools,can%20also%20represent%20mathematical%20equations.&text=The%20point%20(2%2C1),of%20the%20two%20equations%20intersect.) on matrix application: 

> "...that points to one of the reasons that matrices are so common in computer science: They allow computers to, in effect, do a lot of the computational heavy lifting in advance. Creating a matrix that yields useful computational results may be difficult, **but performing matrix multiplication generally isn’t**." 

And that's the whipped cream and sprinkles. We humans do just as much Big Brain work as necessary and pass it onto the computer to do the real Beefy Barbarian work for us.

Of course, it's all about using the right tool for the job. You might not need to work with matrices very often, or you might make heavy use of them. When it's the latter, you'll sure be glad they exist.

Links:

* [Matrix Wiki](https://en.wikipedia.org/wiki/Matrix_(mathematics))
* [Linear Transformations](https://brilliant.org/wiki/linear-transformations/) - same link from my definition
* [Basic Matrix Operations + Resources Working with JS](https://doc.xuwenliang.com/docs/math/3312) -- this is where I got the cover photo from!
* [3Blue1Brown Video Series: Essence of Linear Algebra](https://www.youtube.com/watch?v=fNk_zzaMoSs&list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab) - starts at first video in playlist
* [CodingTrain Video: Matrix Multiplication with 3D Graphics](https://www.youtube.com/watch?v=tzsgS19RRc8) - s/out to Dan Shiffman!
* [MIT Article on Matrix Application]
(https://news.mit.edu/2013/explained-matrices-1206#:~:text=Among%20the%20most%20common%20tools,can%20also%20represent%20mathematical%20equations.&text=The%20point%20(2%2C1),of%20the%20two%20equations%20intersect.)
* [convolution*](https://p5js.org/examples/image-convolution.html) - Shameless self-plug for an image convolution example in p5.js that I ported over that uses a matrix! 

With <3, happy coding!