LINEAR ALGEBRA

image

This appendix serves as a cheat sheet for linear algebra. The subject is presented as a set of tools, their properties, and what you can use them for. If you’re interested in the theory behind all this, you can pick up any introductory linear algebra textbook.

The focus here is exclusively on 2D and 3D algebra, as that’s what’s required in this book.

Points

A point represents a position within a coordinate system.

We represent a point as a sequence of numbers between parentheses—for example, (4, 3). We refer to points using capital letters, such as P or Q.

Each of the numbers in the point’s sequence is called a coordinate. The number of coordinates is the point’s dimension. A point with two coordinates is called two-dimensional, or 2D.

The order of the numbers is important; (4, 3) is not the same as (3, 4). By convention, the coordinates are called x and y in 2D, and x, y, and z in 3D; so the point (4, 3) has an x coordinate of 4 and a y coordinate of 3. Figure A-1 shows P, a 2D point with coordinates (4, 3).

image

Figure A-1: The 2D point P has coordinates (4, 3).

We can also refer to specific coordinates of a point using a subscript, like Px or Qy. So the point P can also be written as (Px, Py, Pz) when convenient.

Vectors

A vector represents the difference between two points. Intuitively, imagine a vector as an arrow that connects a point to another point; alternatively, think of it as the instructions to get from one point to another.

Representing Vectors

We represent a vector as a set of numbers between parentheses, and refer to them using a capital letter. This is the same representation we use for points, so we add a small arrow on top to remember they’re vectors and not points. For example, (2, 1) is a vector, which we might decide to call Images. Figure A-2 shows two equal vectors, Images and Images.

image

Figure A-2: The vectors Images and Images are equal. Vectors don’t have a position.

Despite sharing their representation with points, vectors don’t represent or have a position; they are, after all, the difference between two positions. When you have a diagram like Figure A-2, you have to draw vectors somewhere; but the vectors Images and Images are equal, because they represent the same displacement.

In addition, the point (2, 1) and the vector (2, 1) are unrelated. Sure, the vector (2, 1) goes from (0, 0) to (2, 1), but it’s equally true that it goes from, say, (5, 5) to (7, 6).

Vectors are characterized by their direction (the angle in which they point) and their magnitude (how long they are).

The direction can be further decomposed into orientation (the slope of the line they’re on) and sense (which of the possible two ways along that line they point). For example, a vector pointing right and a vector pointing left both have the same horizontal orientation, but they have the opposite sense. However, we don’t make this distinction anywhere in this book.

Vector Magnitude

You can compute the magnitude of a vector from its coordinates. The magnitude is also called the length or norm of the vector. It’s denoted by putting the vector between vertical pipes, as in Images, and it’s computed as follows:

image

A vector with a magnitude equal to 1.0 is called a unit vector.

Point and Vector Operations

Now that we’ve defined points and vectors, let’s explore what we can do with them.

Subtracting Points

A vector is the difference between two points. In other words, you can subtract two points and get a vector:

image

In this case, you can think of Images as “going” from Q to P, as in Figure A-3.

image

Figure A-3: The vector Images is the difference between P and Q.

Algebraically, you subtract each of the coordinates separately:

(Vx, Vy, Vz) = (Px, Py, Pz) (Qx, Qy, Qz) = (Px Qx, Py Qy, Pz Qz)

Adding a Point and a Vector

We can rewrite the equation above coordinate by coordinate:

Vx = Px Qx

Vy = Py Qy

Vz = Pz Qz

These are just numbers, so all the usual rules apply. This means you can do this:

Qx + Vx = Px

Qy + Vy = Py

Qz + Vz = Pz

And grouping the coordinates again,

image

In other words, you can add a vector to a point and get a new point. This makes intuitive and geometric sense; given a starting position (a point) and a displacement (a vector), you end up in a new position (another point). Figure A-4 presents an example.

image

Figure A-4: Adding Images to Q gives us P.

Adding Vectors

You can add two vectors. Geometrically, imagine putting one vector “after” another, as in Figure A-5.

image

Figure A-5: Adding two vectors. Addition is commutative. Remember, vectors don’t have a position.

As you can see, vector addition is commutative—that is, the order of the operands doesn’t matter. In the diagram, we can see that Images.

Algebraically, you add the coordinates individually:

image

Multiplying a Vector by a Number

You can multiply a vector by a number. This is called the scalar product. This makes the vector shorter or longer, as you can see in Figure A-6.

image

Figure A-6: Multiplying a vector by a number

If the number is negative, the vector will point the other way; this means it changes its sense and therefore its direction. But multiplying a vector by a number never changes its orientation—that is, it will remain along the same line.

Algebraically, you multiply the coordinates individually:

image

You can also divide a vector by a number. Just like with numbers, dividing by k is equivalent to multiplying by Images. As usual, division by zero doesn’t work.

One of the applications of vector multiplication and division is to normalize a vector—that is, to turn it into a unit vector. This changes the magnitude of the vector to 1.0, but doesn’t change its other properties. To do this, we just need to divide the vector by its length:

image

Multiplying Vectors

You can multiply a vector by another vector. Interestingly, there are many ways in which you can define an operation like this. We’re going to focus on two kinds of multiplication that are useful to us: the dot product and the cross product.

Dot Product

The dot product between two vectors (also called the inner product) gives you a number. It’s expressed using the dot operator, as in Images. It’s also written between angle braces, as in Images.

Algebraically, you multiply the coordinates individually and add them:

image

Geometrically, the dot product of Images and Images is related to their lengths and to the angle α between them. The exact formula neatly ties together linear algebra and trigonometry:

image

Either of these formulas help us see that the dot product is commutative (that is, Images) and that it’s distributive with respect to a scalar product (that is, Images).

An interesting consequence of the second formula is that if Images and Images are perpendicular, then cos(α) = 0 and therefore Images is also zero. If Images and Images are unit vectors, then Images is always between 1.0 and 1.0, with 1.0 meaning they’re equal and 1.0 meaning they’re opposite.

The second formula also suggests the dot product can be used to calculate the angle between two vectors:

image

Note that the dot product of a vector with itself, Images, reduces to the square of its length:

image

This suggests another way to compute the length of a vector, as the square root of its dot product with itself:

image
Cross Product

The cross product between two vectors gives you another vector. It’s expressed using the cross operator, as in Images

The cross product of two vectors is a vector perpendicular to both of them. In this book we only use the cross product on 3D vectors, shown in Figure A-7.

image

Figure A-7: The cross product of two vectors is a vector perpendicular to both of them.

The computation is a bit more involved than the dot product. If Images = Images × Images, then

Rx = VyWz VzWy

Ry = VxWz VzWx

Rz = VxWy VyWx

The cross product is not commutative. Specifically, Images.

We use the cross product to compute the normal vector of a surface—that is, a unit vector perpendicular to the surface. To do this, we take two vectors on the surface, calculate their cross product, and normalize the result.

Matrices

A matrix is a rectangular array of numbers. For the purposes of this book, matrices represent transformations that can be applied to points or vectors, and we refer to them with a capital letter, such as M. This is the same way we refer to points, but it will be clear by the context whether we’re talking about a matrix or a point.

A matrix is characterized by its size in terms of columns and rows. For example, this is a 4 × 3 matrix:

image

Matrix Operations

Let’s see what we can do with matrices and vectors.

Adding Matrices

You can add two matrices, as long as they have the same size. The addition is done element by element:

image

Multiplying a Matrix by a Number

You can multiply a matrix by a number. You just multiply every element of the matrix by the number:

image

Multiplying Matrices

You can multiply two matrices together, as long as their sizes are compatible: the number of columns in the first matrix must be the same as the number of rows in the second matrix. For example, you can multiply a 2 × 3 matrix by a 3 × 4 matrix, but not the other way around! Unlike numbers, the order of the multiplication matters, even if you’re multiplying together two square matrices that could be multiplied in either order.

The result of multiplying two matrices together is another matrix, with the same number of rows as the left-hand side matrix, and the same number of columns as the right-hand side matrix. Continuing with our example above, the result of multiplying a 2 × 3 matrix by a 3 × 4 matrix is a 2 × 4 matrix.

Let’s see how to multiply two matrices, A and B:

image

To make things more clear, let’s group the values in A and B into vectors: let’s write A as a column of row (horizontal) vectors and B as a row of column (vertical) vectors. For example, the first row of A is the vector (a, b, c) and the second column of B is the vector (h, l, p):

image
image

Let’s give names to these vectors:

image

We know that A is 2 × 3, and B is 3 × 4, so we know the result will be a 2 × 4 matrix:

image

Now we can use a simple formulation for the elements of the resulting matrix: the value of the element in row r and column c of the result—that is, crc—is the dot product of the corresponding row vector in A and column vector in B, that is, Images and Images:

image

For example Images, which expands to ah + bl + cp.

Multiplying a Matrix and a Vector

You can think of an n-dimensional vector as either an n × 1 vertical matrix or as a 1 × n horizontal matrix, and multiply the same way you would multiply any two compatible matrices. For example, here’s how to multiply a 2 × 3 matrix and a 3D vector:

image

Since the result of multiplying a matrix and a vector (or a vector and a matrix) is also a vector and, in our case, matrices represent transformations, we can say that the matrix transforms the vector.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.114.94