 matrix multiplication in NumPy. I'm going to do this in two ways. First of all we're going to program it by hand so we understand how functions work and how we can access different elements of a vector and of an array, and then we'll do it the fast way because obviously someone has programmed this for us in the past. I'm going to reproduce my matrix here 4, 2, 7, 3, 1, 4, and now I need to enter my vector x. Now you might want to sort of think about x mentally as being a column of three numbers, but to the computer it really doesn't matter. The point is it's three numbers which have a single index, say x i, where i goes from 0, 1, and 2. So for example, if I can say of course a print a that we know what that does, I can also say print x, I know what that does, that's going to print off 1, 0, 2. So again that doesn't look like a vector in the way we think of it as being a column of three numbers, but the computer what really matters is that this is something with a single index. It's got x0, x1, and x2. Again we're using the square brackets as the way to access the individual elements of this. So the important thing about a is that it is a object where we use two numbers to access it, say a01 or a02. The important thing about x is it's an object that we use a single number to access its elements. Okay, so now I want to make the matrix b, or I should say the output vector b. Well this is sort of interesting because you know I don't want to type in the operation by hand, I want to sort of go through this, go through the actual procedure of building it. So the thing we would like the computer to do is the following. b is going to have two entries, the first one will be 4 times 1 plus 2 times 0 plus 7 times 2, and the second one will be 3 times 1 plus 1 times 4, sorry 1 times 0, plus 4 times 2, 1811 as before. And again it's printing it sort of as a row of numbers, but the point, the thing that matters here is it's two numbers in a list, that's what really matters. Now of course this is a silly way to do this, right? I can compute this by hand, why would I even use a computer other than for bookkeeping purposes? We want the computer to do this for us. So what I'm going to do instead is I'm going to reproduce this using a loop. So for i in range 2, I'm going to say this element, let me call it say bi, or element of b is 0, and for j in range 3, I want to multiply aij by xj, and I want to add that to what I already computed. So let's think about what this is doing, right? I have two rows in my matrix, so I'm going to run i from 0 to 1. For each of those rows, I'm going to start off counting at 0. I'm going to work through the columns of the matrix and down the entries of x, multiplying them together, right? aij xj, that was the key thing inside the sum for our definition of work by hand. Then I'm going to add them up, so I'm going to take the value I already know, and add the new value to it, and then have that be the current value. At the end of that loop, notice this is backed out so that it's no longer inside the loop. I'm going to print off that number, so I'm printing off one number for each row, and so these are the two numbers that we want. Now, how do I turn this into an array? Well, we saw last time I could make a list, let me call it say b list, and this is an empty list, and so instead of just printing that number, I could append it to the list of entries, and now this is technically a list, not an array, so let's make it do an array. So b is a numpy array built out of this list of numbers, so we could actually do this, we could define a matrix multiplication function which takes in a and takes in x and basically runs these two loops and makes a list and converts it to an array, and that would be a perfectly adequate way to multiply a matrix times a vector. Now, of course, someone's already programmed this into Python, into numpy, and very reasonably it goes by the name dot, so let's say a dot x, and let's print that, because after all the matrix product is essentially a dot product with, you know, keeping track of the rows along the way. So the syntax here is a little funny, it's a dot dot x, because the dot operation is being done by the matrix a to the input x, and the output is that. So you could say, for example, more simply, b equals a dot x, print b. So you have many different ways of sort of seeing the same thing, a basic loop, we can write ourselves, or the built-in operation dot.