Matrix Multiplication
matrixmultiplication.xyz
matrixmultiplication.xyz
[1]: https://www.youtube.com/playlist?list=PLZHQObOWTQDPD3MizzM2x...
Once I had finished that, Cullen's "Matrices and linear transformations"[2] was really helpful too. But I wouldn't do Cullen if you're still, as I was, floundering with the concepts of why you're doing this in the first place. It's great once you have those concepts down.
[1]: https://www.amazon.com/All-Mathematics-You-Missed-Graduate/d...
Now let's see what happens after two steps. If the system started out in state i, what's the probability that after two steps it will end up in state k? Well, it's the sum over all possible paths. In other words, the sum of probabilities of i->j->k for all possible j. In other words, the sum of p_{ij} times p_{jk} for j from 1 to N. But that's exactly the definition of multiplying a matrix by itself.
Now it should be easy to understand that whenever you have matrices that represent transformations of some object, composing transformations will correspond to multiplying matrices.
http://math.stackexchange.com/questions/31725/intuition-behi...
Relying to much on explicit coordinates many times obfuscates what's going on.
The point then is that the taps (again, representing the matrix values) determine how much of each item in the input vector, that should be mixed into each item in the output vector.
This analogy has the limitation that the taps are allowed to enhance the flow, not just limit it, like physical taps would. That is, outputting more than 100% of the input :P Also, while this way of illustrating it may make some sense for matrix * vector multiplication, matrix * matrix would probably become a prohibitively cluttered image.
Look at the animation in the article, after the second matrix has been rotated and put on top of the first one. Then flip its top up so the two matrices are orthogonal. Finally, rotate the whole thing 90 degrees to the left (rotating along the axis that goes from the top of the page to the bottom of the page). Now you can see that the result will be a 2x3 matrix. And the values in each spot will be the sum of the products beneath it.
He has a whole series on linear algebra, including matrix mulitplication:
https://www.youtube.com/playlist?list=PLZHQObOWTQDPD3MizzM2x...
I actually disagree with Axler on his avoidance of the determinant, though. I wish instead of avoiding it he'd spent more time developing it conceptually, as it's actually a fascinating construction. But to this day I have yet to find a gentler and better introduction to serious linear algebra than his book.
He recommends it as a second course, but I read it during my first course in the subject and considered it my "secret weapon". I truly believe that book is what allowed me to get a perfect score in the class -- I had a conceptual understanding that was just not possible to glean from the official course textbook.
Axler's book covers more ground (most notably, Halmos presents the polar decomposition but not the singular-value decompostion) and uses more modern terminology and notation. But Halmos's book has the merits of being half as long and a third as expensive, as well as having been written specifically to prepare the reader as directly as possible for Halmos's short introduction to Hilbert spaces [1].
I highly recommend one or the other of these books for readers who want to understand linear algebra as mathematicians do.
0. https://www.amazon.com/Finite-Dimensional-Vector-Spaces-Paul...
1. https://www.amazon.com/Introduction-Hilbert-Theory-Spectral-...
I could never learn mathematics that way, and goodness knows, school systems in both Europe and America have tried to teach it to me that way. What I understand of mathematics today has mostly been achieved through autodidaction.
Some of us cannot, repeat can not go from the abstract to the empirical; I'm one of those people, who have to go from the empirical to the abstract instead. I need to see the mechanics of it, because I learn by observing the pattern. If I cannot make out the pattern, I cannot comprehend the abstraction.
Take UNIX manual pages for example: the first thing I go to after reading the SYNOPSIS is the EXAMPLES section (and since most GNU/Linux manual pages have no EXAMPLES sections, it's a product which is useless to me, unlike UNIX). The more examples the EXAMPLES section entails, the faster I'm able to grasp the concept. Same with http://matrixmultiplication.xyz/, if I could have learned matrix multiplication that way, visually, instead of having to do it in my head, it would have been far easier. The other way around, it was a slow, painful torture, and to this present day, I cannot multiply the matrices in my head, or without these visualisations to remind me of the rules: they're too complicated for me to keep in my head.
The point I'm trying to make is that not everyone learns the same way, and our brains do not process the information in the same way, even if the final result, understanding of the concept, is identical. A lot of abstract concepts which are difficult to reliably repeat, or whose outcome is either non-deterministic or unclear, we do not even understand the same way, hence opinions often differ by a small or large margin. Don't assume that the learning technique which works for you would work for someone else. That's a major failure of most pedagogical approaches, with the exception of perhaps Montessori and Fröbel. Unlike Montessori and Fröbel, most pedagogical approaches force a uniform way of learning, except that a human brain does not learn in a uniform manner, differing from individual to individual instead. Teaching should be a highly individualized approach.
This is actually pretty instructive. For instance, rotations in three-dimensional space do not commute in general; hence neither does matrix multiplication.
It's strange. Somehow I have now problems whatsoever regarding the use of 4-component vectors. As long as the z component stays one, I'm all set. But lately I'm trying to understand the construction of projection matrices and how to apply the knowledge of homogeneous coordinates, for example shadow mapping.
Parallel lines suddenly intersect? 3D space is just a projective plane? Now everything is all weird...
- Flipping something over like that looks cool, but confuses the brain. At least it confused mine.
- Inconsistently calculating 1 or 2 elements at a time is confusing. Having 2 results calculated at a time simply put requires the recipient to untangle what is going on. This only gets worse when the recipient tries to do it themselves for examples with more columns in the right matrix.
Disclaimer: I am biased as I already know how to do matrix operations, and I visualize it differently. When multiplying by hand I translate the right matrix up, like in the animation, but do not flip it over. Then I calculate each element one at a time by taking the sum of products of the row and column that intersects over that element. Like so: http://i.imgur.com/D572zOI.png
As you can see I'm not much of a graphics artist.
Edit: actually, I think this would be really good if you "filter down" one column at a time, instead of moving the whole matrix down, so that it only calculates one element at a time, one column at a time. Means the animation will contain more steps, but I think that is actually to its benefit. Does this make sense?
I think it would be helpful to have a shadow of the right matrix after it's moved to the top. This way it would be easier to see where particular numbers are coming from in the original input.
Disclaimer: I'm similarly biased and actually the reason I came up with this shadow thing is because I also imagine the right matrix moving like here but somehow this never causes it to disappear from its original place on paper, go figure, so this animation looked really alien to me.
If all this still seems counter-intuitive, then realize this is essentially why math is so important to our logic. It helps us to reason about things that are not obvious, yet logically must fall through from the basic tautologies we "chose" (like 0 != 1, addition on integer numbers, etc.). Or, more aptly in this case, it helps retain an analogous symbolic representation (the overall logic of the equation at the high level) despite a change in the underlying nuts and bolts of computing solutions to it (matrix multiplication vs. our regular "1D" multiplication).
Your example is of a second-degree equation, which can't be solved by linear algebra. Linear algebra was invented to solve systems of linear equations, and matrices first arose as a convenient notation for expressing those. Matrix multiplication came later and is now understood to correspond to the composition of linear morphisms on vector spaces.
If you try to extend the (very few) methods we have for obtaining exact solutions to polynomial equations in one variable to higher dimension, you (a) fail and (b) invent algebraic geometry.
Usually, when I do matrix multiplications, the rows on the left side transition to the columns on the right side. In the example, the 1 goes to 2, 2 to 6, 1 to 1.
The animation completely confused me, because it looks like being reversed, but the result is the same.
Maybe, make this a visualization option?
Not wrong, but discordant with my expectations.
Multiplication
We can see matrix by matrix multiplication from 5 different positions:
row by column multiplication
column at a time
row at a time
as sum of outer products
block multiplication
All of them are equivalent and lead to the same result
http://mlwiki.org/index.php/Matrix-Matrix_MultiplicationDoes your mental model fit into one of these, or is it also reversed in the order it is applied? It would indeed be interesting to allow visualizations of all the approaches.
[0] https://upload.wikimedia.org/wikipedia/commons/e/eb/Matrix_m...
Not nice.
If your kid proudly brings home a drawing, you are most probably going to greet him with warm encouragements no matter the quality of the art. The kid is in a process of developing his creative sense, learning, and forming his/her sense of reality. Comparing the drawing to what mature artists produce would certainly have a negative effect on the kid. The point is to provide a feedback relevant to the recipient of the critic.
The same goes here with this animation. I respect your work but obviously disagree with the usefulness of it. You made an animation to explain the matrix product, which is a really good idea. Alas, I think the realization of it gives a confused picture of what it is. I doubt that a student with no prior understanding of matrix products will get much enlightment. Too many operations are going on at the same time. This means that it misses its intended audience: students in need of building an intuitive understanding of the matrix product. That's what all those javascript (among others) animations (ex: http://jackschaedler.github.io/circles-sines-signals/dft_int...) are all about: make it simpler to grasp a concept. Unfortunately, though beautiful, the animation is not clear. At best it provides a visual mnemonic for multiplying matrices which maybe useful for some but not for the younger version of me in need of understanding :)
I realize that instead of providing such a harsh critic, I would have been better inspired to suggest improvements. Mea culpa :)
What they are missing from your explanation is "why". Your animation shows how to multiply two matrices, and anyone confused about the mechanics of doing that might find it helpful.
But the actual mechanics are not of much use if you don't know why they are this way. Everything in mathematics is made up by humans, so why would someone at some point write down this particular set of rules to apply to rectangular arrays of numbers?
To that end, I reaffirm the parent's opinion that your visualization only helps to confuse people, giving them a false sense of understanding. There is already too much emphasis in education on "how", and too little on "why"; the comments from the teachers you mentioned are downright scary to me.
Perhaps you will take this comment as yet another critique that is too harsh - but it must be done, so that others don't mistake your animation for an explanation of concepts behind the operation. When one understands the concepts, visual mnemonics - like the one you created - are not needed.
To start a productive discussion, however, (which, hopefully, might lead to you using your skills to come up with a version 2 of your work), I am very curious to hear your answers to these questions - which people confused about matrices might have:
1. What information is actually stored in a matrix? Why write down a bunch of numbers in a rectangle instead of, say, a list or a triangle (like Pascal's triangle)?
2. Why would one want to "multiply" two matrices? That is, why would one want to combine two matrices, and what information would the "product" matrix store?
3. Why do the rules for "multiplication" actually yield the result desired in (2)? Wouldn't some simpler rule do the same? For instance, if we have two 2x2 matrices, why not just multiply the numbers there elementwise (top left by top left, top right by top right, etc.)?
My claim is that anyone who can answer these questions wouldn't need your visualization - and, conversely, your visualization doesn't help answer these questions at all.
I'd be glad to be proven wrong, so I'm looking forward to your explanations of the three questions above.
On a side note, it's disgusting (I find that an appropriate word) how hackernews threads are so harsh and negative. You can do a fun exploration: search for historical threads announcing tools and services that were revolutionary (Angular.js or React.js, to name a few), and you'll find a plethora of negative comments. If hackernews discussions are good judges of quality, absolutely everything in this world sucks.
Again, it is very unfortunate, but on the average, high school teachers wouldn't be.
For starters, linear algebra (where multiplication belongs) simply isn't taught at high-school level. It is not in the standard math curriculum in any state. Bits and pieces of linear algebra, like teaching matrix multiplication alone, simply don't belong in the high school curriculum.
For that matter, too many high school instructors either don't have deep enough knowledge of linear algebra themselves, or if they do, they don't have enough experience or expertise in teaching this subject - simply because it is not a part of the curriculum.
To compound the problem, too often we don't teach math educators well enough. The math requirements for math teachers are often significantly lower than for math majors, and many don't get to see too many facets of mathematics.
To answer your question: matrix multiplication belongs in a university-level linear algebra course. Our system is so perverse that the version taught to non-math majors is missing most of the explanations, and even the math majors don't really learn the linear algebra until their second (or even third) year.
So here's my opinion:
The instructors qualified to judge linear algebra instructional materials would be people involved in teaching linear algebra courses at a college level - the professors, TA's, and people running help sessions in universities.
The students would be qualified to judge in retrospect, once they have themselves learned the material beyond the definition.
>On a side note, it's disgusting [...] how hackernews threads are so harsh and negative. [...] If hackernews discussions are good judges of quality [...] this world sucks.
Don't take HN threads as judges of quality - you have seen yourself that they are not. Yes, they are endless sources of critique, and some of the critique is valid and useful. Things people here talk about may succeed in spite of the flaws, but it doesn't mean they are flawless.
And it's great! This thread alone can give you many ideas on where to go from here if you like making educational props like this one - and how to improve what you have done. Treat this thread as a feature request, as a todo-list, as a bug report. You have just got yourself a small army of through beta-testers, who were kind enough to submit detailed reports (with links and examples!). Leverage that.
If you need praise: you are a talented programmer, who can create great visualizations that can help many people get a deeper understanding of things that you have mastered. And the critique is here because of this potential - because you really, really, really can do better.
>I totally acknowledge that it is missing the "why", I knew that from the beginning. The widget I built simply answers the "how".
"How" what?
How humans multiply matrices? Certainly not, I don't cut out one matrix and put it on top of the other. And, to be frank, if I need to multiply two matrices, I either use a computer algebra system (or code it up).
How computers multiply matrices? Software like MATLAB or BLAS would use more complicated algorithms which perform faster (Karatsuba, Strassen, whatever).
In the end, your tool is just a visual mnemonic for a definition. It's a SOHCAHTOA for matrices. And really do think we don't need more of that in education.
> Your ordinary calculator never answers the why question.
That's why there should be way more caution in using calculators in classrooms than is currently exercised. The current way of things is horrendous, but doesn't have to be. I have designed Calculus labs with MATLAB in which, I hope, the computer is not doing anything that the students could do by hand (given a lot of time). Done this way, calculators and computers become tools which aid understanding.
Anyway, regardless of what the motivation of others here was, here is where I'm coming from:
I've taught linear algebra help sessions where by the middle of the semester the students could multiply, row-reduce, invert matrices without having a clue what they are doing. They could put a bunch of vectors in a matrix and do - what to them was - some number magic to check for linear dependence.
I would draw them two arrows on the board pointing in different directions, and ask them a simple question - are these vectors linearly independent or not? They would be thoroughly lost.
That's the state of linear algebra education in the US. We are teaching people mechanical operations that computers do better.
There are better approaches! Linear Algebra Done Right (S. Axler), for example, is one of the few books that justifies the definition of matrix multiplication. Practical Linear Algebra does everything visually, and is suitable for beginners. But the first text is only taught to math majors, and the second one is not widely used.
In any case, I am highly opposed to teaching the mechanics an operation, because with a good understanding, people will come up with the mechanics themselves.
You are an example of this. Having an understanding of the operation allowed you to come up with a visualization. But the visualization, in my opinion, doesn't set one on a path to develop a similar understanding. And this is where your work can be improved.
For instance, take this: https://betterexplained.com/articles/matrix-multiplication/ https://betterexplained.com/articles/linear-algebra-guide/
Is this a perfect explanation? Maybe not, and it's not animated. But the author tries to pass on the way to get a better understanding, a new angle on how to understand the operations being done.
Would doing things like that with an animation be challenging? Certainly. But don't blame the audience for having a high expectation of what you can do.
I think this is a big reason why american students rate themselves as being very confident in their understanding, but performing rather poorly compared to other nations. I'll have to look for the citation to back that up.
Just my 2cents.
edit: http://www.newgeography.com/content/003840-high-confidence-n...
there were other more recent surveys on this topic, this is all i can find right now
I've taught people at college level, and have seen way too many cases of people knowing how to perform the steps to, say, solve an equation, find a derivative or take an integral without understanding what the operations actually mean, or why bother with all this stuff in the first place.
It's not the students' fault; the concerns I am trying to voice have been articulated by many over the recent years, and are best summed up here: https://www.maa.org/external_archive/devlin/LockhartsLament....
100% agree. It reminds me of the "yes, and" rather than "yes, but" thing from improv and stuff like that.
If what you've done helps a single person (and it did me), then it is valid. It might not be perfect, and it might not be right for everyone, but it was useful and worth putting the effort into (and this was. Thank you for it).
I think your core fallacy is that all people need to be taught in the same way, and that there is one correct way to teach people. The videos you link to may work for some people, but not for others. This explanation may work for some people, but not others. It turns out not everything works for all people.
This belief is why so many people are complaining about the site. Put simply I (and I assume others) do not believe that you understand matrix multiplication as a result of this site.
We could be wrong. Please explain matrix multiplication.
I don't think that makes the site worthless or worthy of the some of the scorn I'm seeing in here.
I strongly agree with the complaint that "this completely misses the point" is super negative and dismissive and should be condemned.
If we-the-community want people to keep making things for fun or to try to help people, we have to criticize them graciously instead of saying that they are so wrong as to be useless. Not because it is moral or proper to do so, but because in practice dismissive and scornful negative feedback makes creators less likely to create new things or to try to improve old ones, and that's bad.
Still, you're a smart guy - I'm trying to see why it's worth it. Do you really feel model-view-intent shines here?
(Nice visualization by the way)
I'm sure it could have been built with less code. In general, the more shortcuts you take and the less abstraction you use, the less amount of code you will have. But also, it's likely that you would mix concerns more easily. So in general, when separating concerns, you end up getting more LOCs. That's what I've perceived in my experience, not a fact that I can prove.
So I do feel like model-view-intent shines here. Model only has state concerns. I was careful in moving away DOM-related code from the model. So theoretically you could reuse this model for React Native or any other UI target. View only displays state in a visual representation, and intent only captures user events and represents them in a form suitable for the model.
About some TypeScript code, I think you referred to https://github.com/staltz/matrixmultiplication.xyz/blob/mast.... These could have been avoided. The only purpose they serve is to make inference work out for lines like these: https://github.com/staltz/matrixmultiplication.xyz/blob/mast... TypeScript knows that `action` is of type StartMultiplyAction. If we had only done `.filter(ac => ac.type === 'START_MULTIPLY')`, TypeScript couldn't infer anymore. So I understand those isFoo functions are very boilerplatey, but they serve the purpose to provide accurate typings. It's optional and depending how you write TypeScript code, you can use more type casting and more `any`, or you can go down the route of providing accurate types everywhere, which is what I did. This is not at all related to Cycle.js nor MVI.
Feel free to ask more architecture-related questions. I like these. What else feels like "too much code"?
To get the element of the result matrix at position (n,m), compute the inner product < r_n | c_m >, where r_n is the nth row vector of the left matrix and c_m is the mth column vector of the right matrix. Once you try it, you'll see that it's also much easier to visualize than this strange, unintuitive approach.
With great power comes great responsibility. That domain name belongs to that person. I hope they do society a favor and put more content regarding MM that that, because they chose one of the least intuitive ways of thinking about it. Put more ways up, or ditch the domain.
If the goal is to get people to understand what matrices are, the best way is to teach them about operators, transformations, vector spaces and linear algebra in general, because this is really the only way to fully understand what's going on without relying on some heuristic.
If the goal is to get people to remember how to do matrix multiplication, at least put up more ways of doing it.
5 ways to multiply A B = C
1) Each element of C is a dot-product of corresponding rows of A with columns of B
2) Each column of C is a combination of the columns A. Each column of B has the coefficients.
3) Each row of C is a combination of the rows of B. Each row of A has the coefficients.
4) Accumulate outer-product. Accumulate sums of of outer-products of each col of A with each row of B.
5) Block multiplication
They all produce the same answer, but different ones provide different insight for particular situations.
See Video Lecture #3 of Strang's Linear Algebra course for more info: https://ocw.mit.edu/courses/mathematics/18-06-linear-algebra...
Some concepts in math are so much easier to grasp with an interactive visual. I once made an entire game around matrix multiplication -- such that players didn't even know they learned how to do this.
And sometimes, visualizations can help cause discoveries. A lot of progress in theoretical physics came about because physicists 'guessed' what nature's equations should look like...and then got shocked when they discovered they were right.
Take James Clerk Maxwell, for instance:
"He had achieved his greatest success by relying on just one empirical fact known to every school boy. Yet now he tried to do without any empirical facts, by pure thinking. He believed in the power of reason to guess the laws according to which God has built the world."
Why is matrix multiplication not pointwise, was something I had a hard time understanding.
Better explained has a good article on this, that really made it click for me
I found the whole staircase thing quite confusing.
I've always felt that these explicit calculations don't really get to the point. You can memorize them and still not really understand what's going on.
The thing is, most linear algebra classes that are taught to other sections (biology, CS, …) start directly with matrices and are mostly computational.
Actually, proving them without picking a basis and then showing that what you get is invariant under base change is quite nontrivial and involves throwing a lot of heavy machinery around; see for example Coffman's non-coordinate proof of the statement about the trace in http://users.ipfw.edu/CoffmanA/pdf/book.pdf
For the determinant, if you want to do everything without picking a basis, you're basically proving that the exterior power operation defines a functor on the category of finite-dimensional vector spaces, which isn't that bad to do, but you somehow have to explain what all those words mean along the way.
Also, this short tutorial on linear algebra covers the row + column pictures on the first page: https://minireference.com/static/tutorials/linear_algebra_in... (disclaimer: I'm the author)
1. Scale in X and Y of -1
2. Z rotation of 180
I feel like I understand Matrices _less_ now than when I started reading around here.
If you want to actually learn something, then get something like a book or series of posts and work through examples and explore the ideas yourself. But having more than one intuition is useful and complaining about the fact that there is more than one way to understand something is lazy.
Now I do! THANK YOU! :)
- First, show how to multiply a row vector by a column vector.
- Then, show how to "simultaneously" multiply several row vectors by a single column vector by placing the row vectors in a matrix.
- Now, similarly, do the same with the column vector. And voila, matrix by matrix multiplication.
I worked it out here for 2d transformations http://webglfundamentals.org/webgl/lessons/webgl-2d-matrices... and then expanded it to 3d, perspective, and cameras.
1. A "Previous" button would help since otherwise you have to go loop through the whole thing to see what actually happened (e.g. how exactly the first step lifts and rotates the matrix, what's getting added to what).
2. The final animation step is a transition from the patently false equation "[matrix1]=[result_matrix]" to the finally correct equation "[matrix1] x [matrix2] = [result_matrix]". This could be avoided if instead of transforming "[matrix1] x [matrix2]" into "[matrix1 plus overlay]=[result_matrix to be filled]" and then doing the 'waterfall', the equation "[matrix1] x [matrix2] = [result_matrix to be filled]" was maintained throughout, perhaps with the moving overlay slightly grayer after lifting [matrix2].
https://staff.fnwi.uva.nl/r.vandenboomgaard/IPCV/_images/Mat...
But that's just me.
There's two ways of seeing matrix multiplication (from a matrix POV, rather than as linear transformations).
But it's a cool piece of art, I'm guessing that was the intention.