 In this video, I want to show you how to do matrix arithmetic. Before we get there though, let's create some mathematical symbols. And if there's one thing in Python here that you have to become familiar with and understand the difference, it's the difference between a computer variable and a mathematical variable. Now if we scroll up, we see that we have created computer variables. Where we created this matrix and we put it inside of a space in our memory and we allocated a name to that space and it was called A. And that space and memory held an object which was a Sympy matrix. So that variable is very different from a mathematical variable. And we are much more comfortable with mathematical variables. So let me show you how to create some of them. As we would do a normal algebra or calculus, let's create two variables and we'll call them X and Y. So I'm going to say X comma Y and I'm going to say equals SYM because we are still dealing with Sympy here. And I'm going to use the symbols function. So symbols, as always, it is followed by parentheses because that is how I'm going to pass my arguments. And the arguments that I want to pass are going to be strings. Now strings are words or letters. So I'm going to put them inside of quotation marks. You can use single quotes or double quotes. And I'm going to just say X and a space and Y. And I'm going to hold down shift and hit enter or shift and return. And X and Y, they are now mathematical symbols. I cannot now use them as a computer variable name to create the space and memory to store an object in. They are now proper mathematical symbols. So let's create X squared. And the square symbol is two stars and two. So shift eight on my keyboard. So two of them. In many other computer languages, you might see something like the carrot sign that is shift and six on my keyboard. But in Python, it's two stars. Shift and enter. And we see beautiful representation of X squared. I have to create these. I have to represent them as mathematical symbols first, mathematical variables using the symbols function. So let's create a few. I'm going to create a11. And just stick with me here. a11, a12. And I'm going to create a21 and a22. And we're going to do b11 and b12 and b21 and b22. All of those, we're going to make symbols. Sympi symbols. So sym.symbles, parentheses, quotation marks. And I've got to do them all again. a11, a12. Note that I'm not putting in commas here. a21, a22, b11, b12, b21, and b22. There we go. I've got some symbols now. Let's create two new matrices. I'm going to call the first one a. That's going to be sym.matrix. Let's select matrix from our list there. I hit the tab key and saw all the mat's that I typed. And it would try and suggest what I was looking for. So I'm going to do a11 and a12 in my first row. And then I'm going to create a21 and a22 in my second. And let's create another matrix. We're going to call it matrix B. It's going to be sym.matrix. And let's see, sym.matrix. That's what I want. And for B, let's do B. Now, you might have picked up that I made a mistake in the first row in as much as I made two rows inside of square brackets. But the whole thing must be in square brackets. What you could do is just go to the front, hold down shift, and move all the way to the back so that that whole area there is highlighted and hit the opening bracket sign. And you would see what happens to open square bracket. You would see that this notebook actually then put the closing one in for me and they all there. So if you do forget to do something like that, it's very easy to fix. So let's do B11 and B12 and comma. And my second set of square brackets B21 and B22. There we go. Let's have a look at what A looks like. And that's really beautiful. The subscript notation, you might have seen it. This is what we would call a square matrix. It's two by two. And that means it's two rows and two columns. I can actually call that using the shape method. So A already exists now. That is a computer variable. It holds an object, which is a matrix. I can call a method on that. And I do that by putting in this dot notation. So A dot what I'm looking for. So A dot shape. And I get this tuple back. A tuple doesn't refer to only two. You can have more than one values. It's a list of things and it's immutable. So we can't change it once we have a tuple. That's not what we're referring to yet. Don't worry about that. I'm just showing you here 2 comma 2. Two rows and two columns is the shape of that matrix. And that's why we use the subscript notation. The one one refers to row one column one. And the second one A12 is row one column two. And then below that right here is row two column one and row two column two. Let's have a look at B. And again we see four elements in B. So if I call the method shape. I notice that's also a two by two matrix. And we have B row one column one, B row one column two. And then in the second row B row two column one, B row two column two. So each of these matrices, all the elements at least have indexes or addresses. And we saw in the previous lecture that we can index them. So let's do a bit of arithmetic now that we have this because we can see what is going on. So let me say three times matrix A. And that is scalar multiplication. I'm taking a scalar in this instance three and I'm multiplying by A. And what you see there, every element is multiplied by three. So that is scalar multiplication. Let's just do A times three. So we see that scalar multiplication, a matrix and a scalar multiply does commute. So no problems there. Let's have a look at what A plus B would be. A plus B. And we see it's element wise addition. A11 and B11. That was no one column one, both of those elements. So putting it this way is very easy to see what is happening. So that would be matrix addition, addition of two matrices. What we do have to note though is those two matrices have the same shape, the same dimension, two rows and two columns. If you want to add two matrices, they have to be in that sort of shape. They both have to have the same number of rows and columns. So let's do matrix multiplication. And I'm just going to simply say A times B. As simple as that. And you see what happens here to get it. It is still A. It is still A2 by 2 matrix. Let's do this. Let's say A times B. So it's parentheses. That's going to happen first. And then call the shape on that multiplication, matrix multiplication. See the solution is still a 2 by 2 matrix. But this is how you get it. It's A row 1 column 1, B row 1 column 1 plus A row 1 column 2, B row 2 column 1 and so on. So you can easily see how matrix multiplication is constructed. Remember if you want to multiply matrices, the column number in the first matrix must equal the row number in the second matrix. And the resultant matrix will have a shape of values, the row value of the first one and the column value of the second one. Because please note if I do B times A, if I do B times A, I'm not going to get the same value. Let's look at the first element. A11, B11, A11, B1 is there. But look, then it's A12, B21 versus A21, B12. So these two are not the same. Matrix multiplication. We do not have the commutative property there. It does not commute. A times B is not necessarily B times A. It's only under very specific circumstances that those are the same. So in short, that is matrix arithmetic. A scalar times a matrix and a matrix times a matrix. Play around with these, create some matrices and see how easy it is to do their multiplication. Perhaps if you have some problems that you are dealing with, punch them into Python and see if the solution that you get when you do it on pen and paper is the same as what you get here.