Unlearn rotation matrices as rotations
kodkodgames.gitlab.io
kodkodgames.gitlab.io
This makes it clear to me that many people have been very poorly served by their linear algebra courses if that is not obvious.
Years later when I got into graphics programming I found myself dealing with linear algebra concepts but this time it made much more sense to me. Being able to apply the ideas to real problems with visual solutions made all the difference.
Ohhhhh, now I get determinants. I just thought they were some easy way to classify a matrix, not that they had any special meaning. Not that they were also all the eigenvalues multiplied too.
Determinants are kind of abstract and even inelegant. This is because it's defined in terms of coordinates rather than abstractly. A coordinate free definition exists, but it's very complicated.
Ideally they would not be taught at all, but it's useful for doing exam problems. Cramer's rule in particular is quite useful when solving simultaneous equations in engineering under exam conditions, for example. Even in computer/engineering applications, row reduction/diagonal normal form type are used instead.
Determinants are a way to get a scalar number out of a matrix. You have linear forms, bilinear forms, and multilinear forms, the latter of which determinants fall under.
There are certain properties you would like the determinant to satisfy, such as det(AB) = det(A) det(B), so that the determinant can give you an idea of whether a matrix is invertible. Eventually you work out that a determinant _has to be_ a multilinear form with certain properties (alternating, skew symmetric).
Again this is all very complex and messy. Initially 'linear algebra' was just the study of determinants, because people thought of math in terms of solving equations. Linear algebra, as it's known today, came later.
Bilinear forms turn up all the time, so ideally you would first learn bilinear forms, then learn the properties a bilinear form can have (e.g. alternating, skew symmetry, etc), and then learn determinants in that context.
E.g. the determinants of the rotation matrices in the article are all 1, because they don't scale or reflect anything, just rotate.
I think the important detail is if you're changing from right-handed bases to left-handed or vice versa during the transformation.
I fail to see the point you tried to make. Both transformations preserve volume. Why did you expected volume to change if you rotate something?
I don't know where you thought I expected volume to change if I rotate something.
No they really aren't. Determinants represent the change in volume in a transformation.
> Ideally [determinants] would not be taught at all, but it's useful for doing exam problems.
This is outright ignorant and wrong. Determinants are fundamental to a myriad of central concepts in calculus and also in engineering fields. For instance, volume integrals or any integral over a parametrization rely on determinants.
In the specific examples you give, 2-forms could be introduced instead to yield the final formula for the Jacobian. I don't think this is an overkill, because it's possible to use 2-forms without precisely proving the theorems etc about them. This is more convenient for math/physics people, and engineers care more about the final formula than about how exactly it was derived.
As another comment here pointed to, the idea that linear algebra is better off without determinants was popularized by Axler - I'm not making this opinion up out of nowhere.
https://www.amazon.com/Vector-Calculus-Linear-Algebra-Differ...
math majors would probably supplement this with additional material
It's a respectable idea that determinants should not be as important as they are on a pedagogical point of view imo. But they can be extremely useful.
From my point of view, matrices are a bad abstraction in general. For example if we have a linear or a bilinear form, both can be written as matrices of the same size, we can no more differentiate the two objects.
The trouble comes when we want to apply some transformation over those matrices.. as they do not use the same transformations laws.. Namely A^-1.L.A and A^T.B.A
This is a very usual error in computer graphics when we want to make some transformation on a 4x4 matrix and we do not know which kind of object it is.
What I meant is that on a pure mathematical point of view they are seen as inelegant as they require a basis to be defined. I don't have a problem with that when you are doing maths on manifolds etc..
From a practical perspective, a lot of engineering problems go from 1 to 4 dimensions and are basis dependents. And the determinant becomes a useful tool.
I agree with you, that in practical paper computations (e.g. on exams) that determinants are an indespensible practical tool.
On the other hand though, wouldn't you lose the generality? Part of my undergraduate geometry and linear algebra course was on fields, groups and rings and that was built in order to show that once you can define a certain set of operations on a mathematical structure you can do "math" on such structures "as if" they were regular numbers (limitations apply).
Think of matrix fields (not sure if it would be the correct translation, in Italian they were called "campi matriciali").
That was kinda mind blowing to me but at the same time it made perfect sense because of the generic definitions underlying.
Not sure how you could go ahead and explain that with regular 3d-world physics.
For instance, I have a broad math background, nothing too deep, but enough to have an intuition on how complex things worked. One of the defining projects of my career to date was an embedded signal processing project given to me because "it seems like you know math" -- and I figured it out on the job by intuiting my way through. I figured out Fourier analysis and windows and FIR filters by intuition, and was able to use the pieces I had available as API to do something truly impactful.
Most people won't need most things from school, but if you're the one who knows that one thing that's needed that one time, it can be disproportionately rewarding.
Why does it matter that it's unit vectors in a new coordinate system? I don't really care about the new coordinate system, I care about rotating something to this position in my old coordinate system. Saying "oh yeah it's <1,1,1> in this coordinate system" doesn't really help because I still need to translate <1,1,1> in the old coordinate system to <1,1,1> in the new coordinate system... and now I've got the same problem again.
I mean, I get that a rotation matrix, or any matrix for that matter, represents unit vectors in a new coordinate system. If I got nothing else from that course I at least learned that a matrix represents like 50 different concepts like systems of equations, coordinate spaces, etc. A big part of my course was applying rules for one representation of a problem to another equivalent representation to a problem. What I never understood is why it matters how you're representing the problem. Identifying that a rotation matrix is just unit vectors in a new coordinate space doesn't help me solve my problem of wanting to rotate something, yet it felt like 75% of my course was about converting problems from one representation to another.
The question of "how to rotate something" rests on operational semantics. The question of "how is this related to that" rests on denotational semantics, and is much more general. The beauty of linear algebra is how closely the two are related: every linear transformation (the relationship) gives a matrix given a choice of basis, and matrices have a direct algorithm for transforming coordinate vectors.
In 3d graphics (or physics or what have you) you're constantly moving between coordinate systems/frames of reference because certain things are very easy to represent in a local coordinate system but are difficult to work with in a global one
SNES programming also directly introduces people into the notion of 2d vectors advancing over an original axis, which ends up in a rotation in mode 7 stuff.
That's exactly what all square non-singular matrices with unit column spaces are. In fact, all square non-singular matrices are coordinate transformations. That's pretty much the central point of linear algebra.
Framing rotation matrices as changes to the coordinate system doesn't add much info like framing polynomials as continuous functions. Technically it's true, but it doesn't add much to clarify.
More generally, _every_ matrix describes how a change of coordinate system should happen. For any m x n (m rows, n columns) matrix, each of the the n column vectors represent how the current coordinate system (which may or may not be unit vectors) should be represented in the new coordinate system (for a left multiplied matrix).
Whenever a matrix's determinant is zero, it means that you squashed some dimensions. As you can imagine, when m is not equal to n, there will always be dimension squashing. Even when they are equal, that can happen. If you take a 3 x 3 matrix and it transforms all the 3-D vectors into only planes (which are 2-D objects), the determinant will be zero. This would be stated as having a rank of 2. More simply, you'd say the "volume" of the transform to be 0 (because planes have zero volume).
EDIT: Make sure to read important clarifications by JadeNB below.
Although these two sentences are correct individually, it may be worth it to emphasise that they should not be read together: the determinant is defined only for square matrices. (One can do something like computing the determinant of det(A^{adjoint}A) if you want a general numerical invariant that behaves as you like—and then we're getting in the neighbourhood of the SVD).
> If you take a 3 x 3 matrix and it transforms all the 3-D vectors into only planes (which are 2-D objects), the determinant will be zero.
Although your meaning seems clear, I think you may have misspoken slightly. Of course 3D vectors are transformed by a 3 x 3 matrix into vectors, not planes. To me, what I think you mean would read better as "If you take a 3 x 3 matrix and there is a plane such that all 3D vectors are transformed into that plane, then …", or, perhaps even better, "… such that the transforms of all 3D vectors lie in that plane …".
It's a shame that this isn't made more clear in most tutorials and classes. The idea of axis application order really does make things more confusing than is needed.
For me, linear algebra didn't click until I read "Numerical Linear Algebra" by Trefethen & Bau. The first 4-5 sections have great explanations of matrix vector operations.
Other than that, the most popular linear algebra book that actually gets things right is "linear algebra done right" by Sheldon Axler.
Edit: ah, forgive me, didn't notice you said "first time". In that case the Axler book is a safe bet, just be diligent doing the exercises.
All you need to do is to pick a basis for your vector space, and you can start to represent linear transformations as matrices w.r.t. that basis. Even cooler is when you let go of specific basis choices, and start talking about properties of linear transformations that do not depend on a choice of basis. Things like a determinant, eigenvalues, etc.
There is also an augmented 4x4 form if translation is needed as well:
| Q ... Δx | | vx |
| . Δy | | vy |
| . Δz | dot | vz | = new vector
| 0 0 0 1 | | 1 |
where "Q" is the 3 × 3 transformation matrix (e.g., rotation matrix) that is the subject of the OP, and [vx, vy, vz, 1] is the augmented form of a vector "v" = [vx, vy, vz] that is being transformed.The augmented form is especially useful for transforming voxel indices in an 3D image array to spatial coordinates, such as for MRI or CT image data [2]. It is helpful if the index coordinate system has its origin at zero (zero-indexed arrays), like a normal coordinate system.
Finally, if we have many [x, y, z, 1] data points that each have an old and a new position, we can compute the overall best-fit transformation from the old to new positions with the least-squares solution to
A x = b
where "A" is the (unknown) augmented matrix representing the transformation, "x" is a 4 × n array storing the points' old augmented-form positions, and "b" is a 4 × n array storing the points' new augmented-form positions. If we allow the points to have arbitrary dimension, without any particular spatial interpretation, we get least squares curve fitting and the foundation of machine learning.[1] http://homepages.engineering.auckland.ac.nz/~pkel015/SolidMe...
[2] https://nifti.nimh.nih.gov/nifti-1/documentation/nifti1field...
From a mathematical standpoint, this is now a projective vector space.
Some references:
https://en.wikipedia.org/wiki/Transformation_matrix
https://www.quora.com/Why-does-OpenGL-use-4D-matrices-for-ev...
Meaning we have to be very careful about treating our projective transformations as linear maps, as they are, in fact, nothing like that.
Of course people doing graphics often pick a section of the projective space ("last component is 1"), then map that onto R^3 and off they go, sort of ignoring the "planes at infinity" and praying for the best. This is messy but I do not see a better way.
In your second link, I like the trick of storing positions as [x, y, z, 1] and directions as [vx, vy, vz, 0].
Regarding, "Hey, Markus! How come this matrix is 4x4?", I'm not sure if Markus was referring to an affine transformation, part of a perspective projection, a matrix representation of a quaternion, or something else. The mere fact a matrix is 4x4 (or some other size) unfortunately doesn't say much about what it's meant to do. Which probably contributes to Markus getting so many questions.
One old timer laughed in the background and told us how he discovered this on the Amiga a few years before.
Would highly recommend. I feel like Grant's videos gave me a better understanding of Linear Algebra than the course I took in college.
To expand a bit more, I usually don't enjoy resources that emphasize how "practical" they are, because I almost never "learn" anything from them. They teach procedures, not concepts.
This course, and fast.ai's other courses, are different in that they still approach the subject matter in ways that feel tangible and "real world," but they are doing so in a way that reveals and helps you learn the underlying concepts—it's just done in a top-down manner.
YMMV of course, this has just been my experience.
For subjects I've already learned however they can be useful for gaining new visual perspectives.
On the topic of learning rotation matrices / linear algebra in video games, I'd strongly recommend the Handmade Hero youtube channel. Casey explains these subjects at length in multiple videos, using plain chalkboard-style drawings I personally find far less distracting.
I remember this poor teaching fellow where I went to Lehigh University where I studied Mechanical Engineering. He was foreign and had the honor of teaching us Linear Algebra. He must have noticed that none of us had even the slightest damn clue what he was talking about. So, the class before every exam, he would literally just give go over the exam problems nearly verbatim and solve them. I came out of that class with a good grade and not the slightest fucking understanding of Linear Algebra beyond the very superficial algebraic laws of multiplying tensor objects (i.e., scalars (0-D tensor), vectors (1-D tensor), and matrices).
I'd like to thank Grant Sanderson (3Blue1Brown) and Mike Cohen (Udemy Linear Algebra course using Matlab and Python) for teaching me linear algebra. And don't forget Gilbert Strang, of course!! (find his course on MIT OCW) The visualizations of the tensors transforming provided by the first two are what really made Linear Algebra start to click for me. I want to go back to Gilbert Strang's course now that I have a better geometric understanding of what is happening and appreciate all his wisdom on the subject.
Also, learning it as part of a numerical computing stack (like python) where you can experiment and visualize is huge for building intuition.
Lots of how, not enough why
Ket's are vectors and written like |u>. Bra's are objects that compute dot products with vectors. They are written like <u|. So <u| |v> = u · v.
However if you have |u> <v| then nothing happens. This is a new object. It will first take dot product with v and then multiply with |u> vector.
Letting e1, e2.. denote the coordinate basis vectors, we can write a matrix A with coefficients a_ij as the sum Σa_ij |ej> <ei|.
This can be rearranged as
Σ_i |Σ_j a_ij ej> <ei|
Written like this, it is clear that the column is where the unit vector ends up.
>> cuz the composition of linear maps are defined like that.
It took a long time to understand what they meant. Still I see how it is taught to work out mechanically. Brave friends of mine went to the Wikipedia page to come back with more confusion. Wikipedia is a great reference when you know the subject well enough.
https://en.wikipedia.org/wiki/Wikipedia:WikiProject_Mathemat...
Some math pages on Wikipedia are great. Some are... horrid. A while back (2017?) I couldn't remember the terms in the Taylor expansion of sqrt(x), so I went to Wikipedia. The article was one of the most jumbled mishmashes I'd ever tried to read. But, even though I'm qualified to fix it, I didn't dare wade into Wikipedia politics. The rewrite would have taken a while (it's a long article), but I can't even imagine how long shoving it through would have taken!
It's meant for a second course in linear algebra and the focus is on abstract vector spaces and linear transformations (rather than a table of numbers perspective). It also doesn't use determinants until the last chapter.
The last edition also contains chapters on stuff like dual spaces and quotient spaces, which are important yet very often missing in other textbooks.
That being said, his reliance on C/R in about half the book could be a problem for people with CS background. Like, what if I want to compute determinants of linear maps which are over Z_2? There are also no chapters on number wrangling (like the gaussian elimination and friends), which is a big plus in my view but possibly a problem for others.
We need all three because none of them can be made from the other two, but any other rotation can be made from a combination of these three.
That is exactly what these three matrices do. R_x depends on a single parameter and tells you how much you rotate around the x-axis. R_y tells you how much you rotate around the y-axis and R_z for the z-axis. Any rotation can be written as a product of these three (which then is a single 3x3 matrix depending on three parameters).
1. > Right handed, z forward through the nose and x through the left ear.
Interesting, in aviation engineering the convention is different: the body frame has x forward (positive rotation around x is right roll), y to the right (... pitch up), z down (... right yaw).
2. The best reference (reference, not explanation/textbook) for this whole spiel (rotation matrices, Euler angles, quaternions) I've seen is a paper by Diebel, Representing attitude: Euler angles, unit quaternions, and rotation vectors
https://www.astro.rug.nl/software/kapteyn-beta/_downloads/at...
(Or maybe I like it because "Diebels Alt" is my favourite local beer... https://en.wikipedia.org/wiki/Altbier)
Mathworld is much much better in general.
(Somebody's probably going to reply "Wikipedia is an encyclopaedia not a tutorial".)
Wikipdia math pages (and for some sciences too) are correct in the details and yet provide no insight.
It's almost backwards what you want from an encyclopedia, which should be an overview with references to more details.
One of the problems, I think, is that there are competing "views" of linear algebra. Mathematics students are taught a different class than everyone else where vector spaces and linear functions are brought to the fore. Everyone else seems to get a more computation-focused course. So there's a tension there where if you write a math-style article about matrices where this fact about the columns would probably be the definition of a matrix, everyone else might not even recognize what's going on.
There's also the issue that the current article is so sprawling that refactoring it to subject it to a unifying plan would require a large rewrite, and that's hard in an environment like Wikipedia.
It's not the same as a textbook, but generally, after reading the Wikipedia page, I know enough new terminology to intelligently Google for more complete resources like lecture notes, papers, etc..
tutorials are the devil. they do not teach so much as demonstrate a lot of the time.
its a sad place to be
https://github.com/enkimute/ganja.js
and my own geometric algebra also:
z = direction
x = up.cross(z).normalized()
y = z.cross(x)
M = [x, y, z]Edit: I think it has been answered by others - up is in world-space, and the camera is not necessarily aligned horizontally.
y = up.reject(direction).normalized()
where a.reject(b) = a - b.project(a)
[1] https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml...When I was learning 3D graphics, I found this site an excellent resource. Here is the page with a more in-depth explanation of the 'look at' function:
In the code, direction is assumed to be a unit vector, and that's your new z. x = up.cross(direction) is perpendicular to both up and direction, but may have arbitrary length since up and direction aren't generally perpendicular to each other; so x must also be normalized to length of one. y = z.cross(x) is perpendicular to both z and x, and doesn't need normalizing since they were already perpendicular (though extra normalizing doesn't hurt and sometimes--probably not here for most purposes--helps to clean up rounding error).
It's almost never a good idea to use Euler angles unless you're modeling a system where they have physical meaning (e.g., robot arm, gimbal, etc.). The math is almost always simpler when you work in terms of the rotation matrix directly (or quaternions, or axis-angle rotation, or pretty much anything else).
1. Or mirroring, which is the same as a rotation but with any two of the basis vectors swapped or any one negated.