• No results found

In this section we give a short introduction to linear algebra for non-mathematicians. A familiarity with some of the concepts involved will be assumed when discussing various parts of subsequent chapters. In particular for this book, linear algebra is helpful in the understanding and manipulation of three-dimensional coordinates. Here, rather than discussing how separate positions on each spatial axis (x, y and z) are used, we can describe a 3D point in its entirety as a single vector that groups the axis positions. Also, changes of position (transformations) may be described with matrices, which specify how vector locations are relocated. An example of an easily visualised transformation is rotation; a shape, defined by a collection of coordinate positions, is moved to a new set of coordinates to change the orientation of the shape.

The way that data is represented in linear algebra is in terms of vectors, which represent positions in space. Although the ‘space’ we mention in this book is usually the three- dimensional in-out, up-down and left-right kind we all recognise it can also be used for more abstract spaces where the ‘axes’ are merely independent qualities. An example of this might be colour space where you can define a colour (or rather a colour vector) by its red, green and blue components. In the language of Python a vector is represented by an ordered collection of numbers, an array, where each number specifies the location of the point along each axis (dimension). You can think of these just as a list of floating point

numbers; this will have a known length and will not contain any other kind of Python object. To take a biological example, an atom in a molecule has a location (relative to the other atoms), which consists of three coordinates (x, y and z) and it is normal to place these values in an array; the three coordinates correspond to what is normally described as a point in space. If you were studying the dynamics of a molecule then in addition there would be a fourth coordinate, time, and you would be dealing with space-time, although normally you would only consider one time value after the other, rather than all in one go. Figure 9.2. Matrix transformations of spatial coordinates, their inverses and combinations. With reference to a triangle of points, various simple linear transformations of coordinates are illustrated. Each transformation may be specified as a matrix and applied to coordinates, specified as vectors, using matrix multiplication. For linear transformations the application of two subsequent transformations can be represented by a single (combined) transformation. All of these simple transformations have inverse transformations which will restore the coordinates of the points to their original values. The ‘linear’ part of linear algebra refers to the fact that the fundamental characteristic of a coordinate transformation does not change with scale (its magnitude). For example, if you break down a transformation into two smaller parts, then it does not matter if you do the full transformation or instead do the two smaller parts one after the other. If you rotate a shape by 30° around some axis and then again by a further 45° around the same axis, you get the same result as if you had instead just rotated the shape in one step by 75° (30+45).

The ‘algebra’ part of linear algebra refers to the fact that different types of transformation may be melded together to define a single transformation. For example, if you rotate a shape about one axis by some angle, and then around another axis by a different angle, then there is a single equivalent rotation that could do the whole job, potentially about a third axis. The converse of combining transformations is also true because a single transformation can be described by combinations of other transformations. This point leads to the idea that transformations may be reversed; a forward and corresponding reverse transformation results in no change. For example, a rotation about a given axis of a given angle has an opposite rotation: around the same axis but in the opposite direction, so that if you combine both rotations it is equivalent to not doing any rotation at all.

If you imagine a vector to represent a point in space, it is convenient to think of what are called unit vectors, vectors of length 1, as merely representing directions. The way linear transformations are normally described is in terms of what happens to the unit vectors that point directly along the coordinate axes (in a positive sense). For example, for 3D space we would consider the transformation of the three vectors (1,0,0), (0,1,0) and (0,0,1), which respectively represent the unit vectors along the positive x, y and z axes. Because the transformation is linear, once you know how it acts on these unit vectors, you know how it acts on any vector.

Suppose for a given transformation operation (1,0,0) gets mapped (transformed) to (a,b,c), and (0,0,1) gets mapped to (d,e,f), and (0,0,1) gets mapped to (g,h,i). Then an arbitrary input vector (x,y,z) gets mapped to the final vector (x′,y′,z′) via:

This looks pretty horrible, but fortunately computers usually do the calculations. A way that you might think about what is happening above is that the x component of the input vector is multiplied by elements a, b and c (which define the transformation), but each product contributes to a different axis of the output vector; xa is added to the new x′, xb is added to the new y′, and xc the new z′. Similarly, the y and z components have their own multiplicative elements to define contributions to the new vector. Thus, overall the elements of the transformation (a to i) say how to combine (multiply and add) the input coordinates to make the output.