ashen-aetna

Ashen Aetna

— Rustily stumbling around on an ash-covered volcano

(A tutorial on/in/about/with 3D graphics, Rust, Vulkan, ash)

Vectors, linear maps and matrices

Vectors

The mathematical concept of choice when dealing with points and directions in space is that of a vector.

What is a vector?

The formally correct answer may be “an element of a vector space” and provoke the question “and what’s a vector space?”, but in the end it boils down to “something that you can multiply by numbers and that you can add to other vectors” (footnote: where this multiplication and addition work as one could reasonably expect). Example (“something” admittedly is not very helpful in getting a feeling for them): triplets of numbers, like

or, more precisely, all of the following

Adding two of these, or multiplying with numbers … let’s see. How about

and

That works for me, and since the rules are based on the usual addition and multiplication in , we can be confident that all additional requirements from the footnote that was no footnote are satisfied. (We could also check this, but then I’d have to elaborate on what “work as one could expect” means, and I’d rather avoid talking about the definition of vector spaces in more detail.)

This definition also has the effect that we can split every vector in the following way:

or

if we introduce new names , , .

Okay, so, for example those triplets of numbers are vectors.

Any other meaning? For example something less abstract?

Sure. How about motions in space?

Think of shifting (“translations”, not in the language sense), and let’s say means moving something by one metre to the right, two metres down and three metres forward. Can we multiply a shift by a number? Sure: times 3 means thrice as far. Can we add two shifts? Imagine you carry out two shifting motions after each other. Then you realize you could have achieved the same by just one (different) shift. Well, let’s call that one the sum of the previous two. (Actually, my description of “moving by one metre to the right, two metres down and three metres forward” seems to have this as a builtin feature.) So these shifting motions form another vector space; , , are motions to the right, down or forward, respectively, and we can describe every shifting as sum of multiples of these.

In a similar vein, vectors are often visualized as “arrows” (where two arrows are considered the same if they have the same length and direction, no matter whether they start at the same point), and there are also many physical quantities that are best encoded by vectors (velocities (with their magnitudes and directions), to name but one of the most prominent examples).

What about points? Our goal was to define points. Do points also form a vector space?

Welllllll, it is not so clear what “adding two points” should mean. So: kind-of “no”? (At least as long we stick to addition with obvious meanings.)

But if we designate one special point (the “origin”), we can with reasonable soundness claim that “a point” and “the shift motion needed to get from the origin to this point” (or “the arrow from origin to this point”) are interchangeable. Using the corresponding additions and multiplications by numbers, we can give meaning to “point plus point” — and very soon claim that obviously, you can add two points — no matter how meaningless such an operation is (and no matter that it would make much more sense to speak of addition of point and translation/arrow than of point and point). “Two times some point” is just “the point that is twice as far away from the origin, but in the same direction”. It is usually not seen as “worth it” to maintain a distinction between points and “arrows” (“vectors”). In that sense: The points in our screen are vectors, we write them with their three components.

Linear Maps, Matrices

Now that we have points, let’s see how to transform one point into another. “Transform”? Well, somehow take one point , do something, obtain a (probably different) point . And we heavily restrict the “something” in that description, just so we end up with a well-understood class of transformations that is extremely easy to work with and that comes with helpful formalism: Linear transformations.

What are linear functions? Linearity means they work well together with the two operations we needed to define a vector space: addition of two vectors, and multiplication of number and vector.

“Work well” is supposed to mean: the order (first apply the function, then multiply by number versus first multiply, then apply the function) does not matter:

(for every possible choice of numbers λ and vectors v and w).

Not every function is linear, but this is still a very large and important amount. (And there are techniques to pretend that other functions are “locally” and/or “approximately” linear — “derivative” should be the key word here, which does not concern us for now.)

One example would be “move each point to the middle between itself and the origin” or “rotate the (each) point around the z-axis by a quarter of a circle, counterclockwise if we’re looking toward positive z”. So, scaling, and rotations are linear. These descriptions can become wordy. Fortunately, they are not necessary.

First observation: When we know what a linear transformation does with , and , that’s enough information.

Because we can write every vector as and because is linear, we obtain

Let’s say, , , — or, if you prefer a longer form with less line height:

, , .

Nine numbers to describe the map, or one table of numbers, one “matrix”:

We define the product of the matrix and the vector as follows:

so that and actually coincide.

Trying to put our examples into this framework then looks as follows: “Move each point to the middle between itself and the origin” (a.k.a. make everything smaller): is turned into , so

becomes , hence

and with similarly being mapped to , we end up with

“rotate the (each) point around the z-axis by a quarter of a circle, counterclockwise if we’re looking toward positive z”: Rotating about z means that is fixed (mapped to ), hence we can easily begin filling in the matrix:

and we observe that (initially pointing to the right) after turning becomes (pointing towards the top)

And (originally pointing down)? After rotation it points to the right: , that is

If we want to rotate, for example, , we compute

Bad news: Not all maps are linear, and there are important cases we are missing:

Continue