Live data from Hacker News

Matrix Multiplication

matrixmultiplication.xyz

71–80 of 135 posts

Re: Matrix Multiplication

#71
Very nice, but the mental model I prefer is to think of a Matrix-Vector multiplication as the weighted sum of Matrix columns, and a Matrix-Matrix multiplication as just several Matrix-Vector multiplications (results being the columns of a new matrix).

Re: Matrix Multiplication

#72

I think a cool way to visualize matrix multiplication would be to put three matrices MxN, NxK, MxK on the sides of a three-dimensional box MxNxK. Each cell of the matrix MxK would cast a "shadow" onto some row in the MxN matrix and some column in the NxK matrix, whose dot product would give the value of that cell.

Would you mind elaborating or providing a link for the uninitiated? I use matrix multiplication in almost everything I do and that sounds like a neat way to visualize what is happening.

Re: Matrix Multiplication

#73
post #56

Question for staltz - are you happy with the cycle code at https://github.com/staltz/matrixmultiplication.xyz/ ? It feels like a lot of work for this sort of task which I feel could have been achieved by using an existing (mutable) matrix lib and drawing ad-hoc. It feels like an awful lot of code written - especially with all the `.something === "SOME_STRING_CONSTANT"` (couldn't those be typed directly in TS btw?). S…

Yes, I'm very happy with that code, apart from one thing: how animations are done sometimes with tweens in JS, and sometimes with CSS.

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"?

Re: Matrix Multiplication

#74
This is cool, but I have a few criticisms:

- 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?

Re: Matrix Multiplication

#76
post #64

This is a cool example of what Bret Victor calls an "Explorable Explanation" [0]. That said, I feel that it's more important to understand how and why matrix multiplication corresponds to a composition of linear transformations than learning the actual mechanics of doing the computation. You can get good at matrix multiplication without knowing what is going on. I view that as a less valuable activity than learning a…

This is why I'm a big fan of Axler's Linear Algebra Done Right . The book's emphasis is on the concepts behind the calculations rather than the calculations. 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 introd…

Axler's book is very good. An alternative to consider is Paul Halmos's much older Finite-Dimensional Vector Spaces [0]. Both books take the same basic approach, and the proofs of the major theorems are substantially the same. (Halmos's book is well known and well liked and was probably Axler's starting point.)

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-...

Re: Matrix Multiplication

#77
post #64

This is a cool example of what Bret Victor calls an "Explorable Explanation" [0]. That said, I feel that it's more important to understand how and why matrix multiplication corresponds to a composition of linear transformations than learning the actual mechanics of doing the computation. You can get good at matrix multiplication without knowing what is going on. I view that as a less valuable activity than learning a…

This is why I'm a big fan of Axler's Linear Algebra Done Right . The book's emphasis is on the concepts behind the calculations rather than the calculations. 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 introd…

I can see why he discourages it. It prevents understanding why a matrix is singular. I often find students who use it as the main justification for singularity without further understanding singularity -- namely that it occurs when a linear map sends things to a lower dimensional space than it accepts.

Re: Matrix Multiplication

#79

This is cool, but I have a few criticisms: - 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 rig…

> Flipping something over like that looks cool, but confuses the brain. At least it confused mine.

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.

Re: Matrix Multiplication

#80
Not sure if anyone here has mentioned this yet, but it's useful to think of matrix multiplication as the most natural extension of one-dimensional algebra into multiple dimensions. First, remember that people first wrestled with and found solutions to "1-D" equations like "ax^2 + bx + c = 0". When others came along asking about how to extend these results into multiple dimensions so that previous results might still be useful, the most natural answer was found to be how we now define matrix multiplication. To see further evidence of this, take your favorite linear algebraic equation using matrices and then look at the edge case where you have only 1x1 matrices in your equation. Usually you'll end up with something that looks analogous to a result in basic algebra in real number (or complex) variables, not matrices.

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).

Post reply on HN