Python 3, an illustrated tour Transcripts
Chapter: Classes and inheritance
Lecture: Walk-through: Matrix Multiplication

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this video, we're going to look at the mul test assignment
0:03 and there's a function test mul that says
0:06 implement a class vector that accepts a list of numbers
0:08 implement the matrix multiplication operator
0:12 to return the dot product multiplying each corresponding value, then sum the results.
0:18 Okay, let's do that.
0:20 Let's make a class, it's going to be called vector
0:23 and let's implement a constructor here
0:30 and it's going to take data, vector has input
0:33 and we'll just attach that as a member and let's implement matmul guy.
0:38 Okay, it's going to take another vector presumably
0:41 so what we can do is we can loop over the pairs of data together
0:46 how we do that is we can use the zip function.
0:49 So the zip function takes two sequences
0:52 and it will loop over both of them until one exhausts.
0:57 So we're going to have self.data and other.data
0:59 we're going to need something probably result is equal to 0.
1:03 And we're going to zip those together so I can say this and that in zip
1:12 res plus equals this, times that, return res.
1:21 Let's see if this works.
1:29 Okay, that looks like it did work.
1:31 We can do a little bit of refactoring if we want to here,
1:38 we can put this into a list comprehension
1:43 and so we could do something like this
1:46 we could say well we're going to accumulate this, this is a sum operation,
1:50 so we're going to say sum of the iterable
1:59 what we're going to sum is what we're accumulating which is this
2:05 and we'll take this for loop and we'll plop it into here
2:09 we'll take off the colon at the end
2:14 and we should be able to say res is equal to that.
2:16 Let's run this and see if it works.
2:22 Okay, it looks like we're good.