Python 3, an Illustrated Tour Transcripts
Chapter: Classes and inheritance
Lecture: Walk-through: Matrix Multiplication
0:00 In this video, we're going to look at the mul test assignment and there's a function test mul that says
0:07 implement a class vector that accepts a list of numbers implement the matrix multiplication operator
0:13 to return the dot product multiplying each corresponding value, then sum the results. Okay, let's do that.
0:21 Let's make a class, it's going to be called vector and let's implement a constructor here and it's going to take data, vector has input
0:34 and we'll just attach that as a member and let's implement matmul guy. Okay, it's going to take another vector presumably
0:42 so what we can do is we can loop over the pairs of data together how we do that is we can use the zip function. So the zip function takes two sequences
0:53 and it will loop over both of them until one exhausts. So we're going to have self.data and other.data
1:00 we're going to need something probably result is equal to 0. And we're going to zip those together so I can say this and that in zip
1:13 res plus equals this, times that, return res. Let's see if this works.
1:30 Okay, that looks like it did work. We can do a little bit of refactoring if we want to here, we can put this into a list comprehension
1:44 and so we could do something like this we could say well we're going to accumulate this, this is a sum operation,
1:51 so we're going to say sum of the iterable what we're going to sum is what we're accumulating which is this
2:06 and we'll take this for loop and we'll plop it into here we'll take off the colon at the end and we should be able to say res is equal to that.
2:17 Let's run this and see if it works. Okay, it looks like we're good.