Vectors and Matrices
Scalars We often employ a single number to represent quantities that we use in our daily lives such as weight, height etc. The magnitude of this number depends on our age and whether we use metric or imperial units. Such quantities are called scalars. In computer graphics scalar quantities include colour, height, width, depth, brightness, number of frames, etc.
Vectors There are some things that require more than one number to represent them: wind, force, velocity and sound. These cannot be represented accurately by a single number. For example, wind has a magnitude and a direction. The force we use to lift an object also has a value and a direction. The velocity of a moving object is measured in terms of its speed (Km per hour) and a direction such as northwest. Sound, too, has intensity and a direction. These quantities are called vectors.
Vectors and Scalars A scalar is a single number e.g. 7 A vector is a group of e.g. [4,5,3] Scalar and Vector addition 2 + 5 = 7 and [2 3 5] + [2 7 2] = [4 10 7] Scalar product and scalar product of two vectors 7 8 = 56 and [1 2 3] [2 4 6] = [1 2+2 4+3 6] = 28
Introduction to Vectors All points and vectors we use are defined relative to some co-ordinate system Each system has an origin ϑ and some axis emanating from ϑ a) shows a 2D system whilst b) shows a right handed system and c) a left handed system In a right handed system, if you rotate your right hand around the Z axis by sweeping from the positive x-axis around to the positive y-axis your thumb points along the positive z axis. Right handed systems are used for setting up model views Left handed are used for setting up cameras
Simple Vectors Vector arithmetic provides a unified way to express geometric ideas algebraically In graphics we use 2,3 and 4 dimensional vectors however most operations are applicable to all kinds of vectors Viewed geometrically, vectors are objects having length and direction They represent various physical entities such as force, displacement, and velocity They are often drawn as arrows of a certain length pointing in a certain direction. A good analogy is to think of a vector as a displacement from one point to another
More Vectors Fig a) shows in a 2D co-ordinate system two points P=(1,3) and Q=(4,1) The displacement from P to Q is a vector v having components (3,-2), calculated by subtracting the co-ordinates of the points individually. Because a vector is a displacement, it has a size and a direction, but no inherent location. Fig b) shows the corresponding situation in 3 dimensions : v is the vector from point P to point Q.
Vectors The difference between two points is a vector v=q-p Turning this around, we also say that a point Q is formed by displacing point P by vector v; we say the v offsets P to form Q Algebraically, Q is then the sum: Q=P+v also the sum of a point and a vector is a point P+v=Q Vectors can be represented as a list of components, i.e. an n-dimensional vector is given by an n-tuple w =[w 1 w 2... w n ] For now we will be using 2D and 3D vectors such as and however later we will represent these as a column matrix as shown below r = [3.4 7.78] t =[ 33 142.7 89.1 ] [ ] 33 r = 3.4 and t = 142.7 7.78 89.1
Operations With Vectors Vectors permit two fundamental operations; Addition Multiplication with Scalars The following example assumes a and b are two vectors, and s is a scalar If a= [ 2 5 6 ] and b = [ 2 7 1 ] we can form two vectors :- a + b = [ 0 12 7 ] and 6a = [ 12 30 36 ]
Vector Addition b a+b a+b a a b A) shows both vectors starting at the same point, and forming two sides of a parallelogram. The sum of the vectors is then a diagonal of this parallelogram. B) shows the vector b starting at the head of a and draw the sum as emanating from the tail of a to the head of b
Vector Scaling a The above figure shows the effect of multiplying (scaling) a vector a by a scalar s=2.5 -a 2.5a This now makes the vector a 2.5 times as long. When s is negative the direction of sa is opposite of a This is shown above with s=-1
Vector Subtraction a a a-c a a-c c -c c Subtraction follows easily once adding and scaling have been established For example a-c is simply a+(-c) The above diagram shows this geometrically For the vectors a= [ 1 1 ] and c = [ 2 1 ] The value of a-c= [ 1 2 ] as a+(-c)= [ 1 1 ] + [ 2 1 ] = [ 1 2 ]
Linear Combinations of Vectors To form a linear combination of two vectors v and w (having the same dimensions) we scale each of them by same scalars, say a and b and add the weighted versions to form the new vector av+bw DEFINITION : A linear combination of the m vectors v 1, v 2,..., v m is a vector of the form w=a 1 v 1 + a 2 v 2 +... + a m v m where a 1,a 2...a m are scalars For example the linear combination 2[3 4-1]+6[-1 0 2] forms the vector [0 8 10] Two special types types of linear combinations, ``affine'' and ``convex'' combinations, are particularly important in graphics.
Affine Geometry In geometry, affine geometry is geometry not involving any notions of origin, length or angle, but with the notion of subtraction of points giving a vector. It occupies a place intermediate between Euclidean geometry and projective geometry.
Affine Combinations of Vectors A linear combination of vectors is an affine combination if the coefficients a 1,a 2...a m add up to unity. Thus the linear combination in combination in the previous equation is affine if a 1 + a 2 +... + a m =1 For example 3a+2b-4c is an affine combination of a,b,c but 3a+b-4c is not The combination of two vectors a and b are often forced to sum to unity by writing one vector as some scalar t and the other as (1-t), as in (1-t)a+(t)b
Convex Combinations of Vectors A convex combination arises as a further restriction on an affine combination. Not only must the coefficients of the linear combinations sum to unity, but each coefficient must also be non-negative, thus the linear combination of the previous equation is convex if a 1 + a 2 +... + a m =1 and a i 0, for i =1,...m
Convex Combinations of Vectors As a consequence, all ai must lie between 0 and 1 Accordingly 0.3a+0.7b is a convex combination but 1.8a-0.8b is not The set of coefficients a 1,a 2...a m is sometimes said to form a partition of unity, suggesting that a unit amount of material is partitioned into pieces.
Magnitude of a Vector If a vector w is represented by the n-tuple [ w 1 w 2... w n ] how may it s magnitude (equivalently, its length and size) be computed? We denote the magnitude by w and define it as the distance from the tail to the head of the vector. Using the Pythagorean theorem we obtain w = w 2 1 + w2 2 +... + w2 n For example the magnitude of w = [ 4 2 ] is 4 2 + 2 2 = 20 A vector of zero length is denoted as 0 Note that if w is the vector from point A to point B, then w will be the distance from A to B
Unit Vectors It is often useful to scale a vector so that the result has unity length. This type of scaling is called normalizing a vector, and the result is know as a unit vector For example the normalized version of a vector a is denoted as â And is formed by scaling a with the value 1/ a or â = a a We will use normalized vectors for many calculations in Graphics, such as rotations, normals to a surface and some lighting calculations.
The Dot Product The dot product of two vectors is simple to define and compute ˆ For a two dimensional vector a 1 a 2 ˆ and b1 b 2, it is simply the scalar whose value is a 1 b 1 + a 2 b 2 Thus to calculate the dot product we multiply the corresponding components of the two vectors and add the the results ˆ ˆ For example for two vectors 3 4 and 1 6 the dot product = 27 ˆ ˆ And 2 3 and 9 6 =0 The generalized version of the dot product is shown below The dot product d of two n dimensional vectors v = ˆ v 1 v 2... v n and w = ˆ w1 w 2... w n is denoted as v w and has the value n d = v w = v i w i i=1
Properties of the Dot Product The dot product exhibits four major properties as follows 1. Symmetry: a b = b a 2. Linearity: (a + c) b = a b + c b 3. Homogeneity: (sa) b = s(a b) 4. b 2 = b b The dot product is commutative that means the order in which the vectors are combined does not matter. The final expression asserts that taking the dot product of a vector with itself yields the square of the length of the vector. This is usually expressed in the following form b = b b
The angle between two vectors The most important application of the dot product is in finding the angle between two vectors or between intersecting lines. The figure shows the vectors b and c which lie at angles φ b and φ c relative to the x axis. From basic trigonometry we get b =( b cosφ b, b sinφ b ) and c =( c cosφ c, c sinφ c ) Thus the dot product of b and c is b c = b c cosφ c cosφ b + b c sinφ b sinφ c = b c cos(φ c φ b ), so for any two vectors b and c, b c = b c cos(θ), where θ is the angle from b to c. Hence, b c varies as the cosine of the angle from b to c.
Normalised Vector angles To obtain a more compact form the normalized vectors are usually used Therefore both sides are divided by b c and the unit vector notation is used so ˆb = b b to obtain cos(θ) =ˆb ĉ So the cosine of the angle between two vectors b and c is the dot product of the normalized vectors.
Example find the angle between two vectors b = ˆ 3 4 and c = ˆ 5 2 b = 3 2 +4 2 = 5 and c = 5 2 +2 2 =5.3851 so that ˆb = ˆ 3 5 4 5 and ĉ = ˆ 5 5.3851 2 5.3851 This gives us ˆb = ˆ 0.6 0.8 and ĉ = ˆ 0.9285 0.3714 The dot product ˆb ĉ =0.5571 + 0.296 = 0.8542 = cos(θ) hence θ =31.326 o from the inverse cosine this can then be expanded to work for 3 or 4 dimensions
The sign of b c and perpendicularity cos(θ) is positive if θ is less than 90 o, zero if θ equals 90 o, and negative if θ exceeds 90 o. Because the dot product of two vectors is proportional to the cosine of the angle between them, we can observe immediately that two vectors (of any non zero length) are less than 90 o apart if b c > 0; exactly 90 o apart if b c = 0; more than 90 o apart if b c < 0;
The standard unit Vector The case in which the vectors are 90 o apart, or perpendicular is of special importance Definition : Vectors b and c are perpendicular if b c =0 Other names for perpendicular are orthogonal and normal and they are used interchangeably. The most familiar examples of orthogonal vectors are those aimed along the axes of 2D and 3D coordinate systems as shown
The standard unit Vector The vectors i, j and k are called standard unit vectors and are defined as follows i = ˆ 1 0 0 j = ˆ 0 1 0 and k = ˆ 0 0 1
Cartesian Vectors Let us define three Cartesian unit vectors i, j, k that are aligned with the x, y, z axes: 2 1 3 2 0 3 2 0 3 i = 4 0 5 j = 4 1 5 k = 4 0 5 0 0 1 Any vector aligned with the x-, y- or z-axes can be defined by a scalar multiple of the unit vectors i, j, k. A vector 10 units long aligned with the x-axis is 10i. A vector 20 units long aligned with the z-axis is 20k. By employing the rules of vector addition and subtraction, we can define a vector r by adding three Cartesian vectors as follows: r =ai + bj + ck
Cartesian Vectors r =ai + bj + ck This is equivalent to writing a r = b c Therefore the magnitude of r is r = a 2 + b 2 + c 2 A pair of Cartesian vectors such r and s can be combined as follows r = ai + bj + ck s = di + ej + fk r±s =[a ± d]i +[b ± e]j +[c ± f]k
Cartesian Vectors Example r =2i +3j +4k and s =5i +6j +7k r + s =7i +9j + 11k r + s = 7 2 +9 2 + 11 2 = 251 = 15.84
The cross product of two vectors The cross product (also called the vector product) of two vectors is another vector. It has many useful properties but the most useful is the fact that it is perpendicular to both of the given vectors Given the 3D vectors a = ˆ a x a y a z and b = ˆ bx b y b z their cross product is denoted as a b It is defined in terms of the standard unit vectors i, j and k as a b =[a y b z a z b y ]i +[a z b x a x b z ]j +[a x b y a y b x ]k This form is usually replace using the determinant as follows a b = i j k a x a y a z b x b y b z
Geometric interpretation of the Cross Product By definition the cross product a b of two vectors is another vector and has the following properties a b is perpendicular (orthogonal) to both a and b The length of a b equals the area of the parallelogram determined by a and b this area is equal to a b = a b sin(θ) where θ is the angle between a and b measured from a to b or b to a, whichever produces an angle less than 180 o
Matrices In mathematics, a matrix (plural matrices) is a rectangular table of numbers or, more generally, of elements of a ring-like algebraic structure. The entries in the matrix are real or complex numbers or functions that result in real or complex numbers Image from http://en.wikipedia.org/wiki/image:matrix.png
Definitions and Notations The horizontal lines in a matrix are called rows and the vertical lines are called columns. A matrix with m rows and n columns is called an m-by-n matrix (or mn matrix) and m and n are called its dimensions. The entry of a matrix A that lies in the i-th row and the j-th column is called the i, j entry or (i, j)-th entry of A. This is written as A i,j or A[i, j]. We often write A := [a i,j ] m n to define an m n matrix A with each entry in the matrix A[i, j] called a ij for all 1 i m and 1 j n.
Example is a 4 4 matrix, the element A[2, 3] or a 2,3 is 7
Example is a 4 4 matrix, the element A[2, 3] or a 2,3 is 7
General Matrix of the Form A = m n The general form of a matrix is represented as a 11 a 12 a 1n a 21 a 22 a 2n............ a m1 a m2 a mn or as A =[a ij ] m n or simply as A =[a ij ] if the order is known
Operations with Matrices A matrix has m rows and n columns and is said to be of order m n Thus the matrix M = 1 2 3 4 5 6 Has three rows and two columns and is of order 3 2 not to be confused with [ 1 3 5 ] N = 2 4 6 These matrices are related and N is the transpose of M or N = M T
Multiplication of a Matrix by a Number Multiplying a matrix by a scalar number is simply multiplying every element by the scalar 2 6 10 4 6 = 2 6 2 10 2 4 2 6 = 12 20 8 12 3 5 2 3 2 5 6 10 More generally, for any α R, a 11 a 12 a 1n αa = α a 21 a 22... a24............ = αa 11 αa 12 αa 1n αa 21 αa 22 αa 24............ a m1 a m2 a mn αa m1 αa m2 αa mn
More Matrix Definitions A matrix of any order who s entries are all zero is called a zero matrix and this is represented by O The matrix ( A) is defined to be the matrix ( 1)A Note that if α = 0 then for any matrix A or αa is a zero matrix
Addition of Matrices If two matrices A and B are of the same order m n, then A + B is the matrix defined by A + B = 2 6 4 a 11 a 12 a 1n a 21 a 22 a 2n........ 3 7 5 + 2 6 4 b 11 b 12 b 1n b 21 b 22 b 2n........ 3 7 5 a m1 a m2 a mn b m1 b m2. b mn = 2 6 4 a 11 + b 11 a 12 + b 12 a 1n + b 1n a 21 + b 21 a 22 + b 22 a 2n + b 2n........ 3 7 5 a m1 + b m1 a m2 + b m2 a mn + b mn
Addition of Matrices For example let A B and C be matrices as shown below A =» 1 2 3 4 5 6 B =» 2 1 3 4 2 1 C =» 2 7 5 2 Then A + B =» 1 2 3 4 5 6 +» 2 1 3 4 2 1 =» 3 1 6 8 7 5 Neither A + C nor B + C can be found since C has a different order from that of A and B Also note that A + B = B + A
Subtraction of Matrices To subtract elements of matrices A and B C = A( )B elements of matrix B are subtracted from their corresponding elements in matrix A and stored as elements of matrix C All the matrices must have the same number of elements. In the above example the sign is enclosed in parentheses to indicate subtraction of matrix elements Although for most operation - is sufficient unless we are looking at more advanced matrix theory (which we will not need)
Matrix subtraction The subtraction of matrix elements is defined for two matrices of the same dimension as follows 2 3 2 3 2 3 1 3 0 0 1 0 3 0 4 1 0 5 ( ) 4 7 5 5 = 4 1 7 0 5 5 1 2 2 1 1 2 2 1 = 6 5 1 3 1 1
Matrix Multiplication The most common application of matrices in CG is matrix multiplication. It is defined between two matrices only if the number of columns of the first matrix is the same as the number of rows of the second matrix. If A is an m-by-n matrix and B is an n-by-p matrix, then their product A B is an m-by-p matrix given by n (A B) ij = a ir b rj = a i1 b 1j + a i2 b 2j + + a in b nj r=1
Matrix Multiplication The figure shows how to calculate the (AB) 12 element of A B if A is a 2 4 matrix, and B is a 4 3 matrix. Elements from each matrix are paired off in the direction of the arrows; each pair is multiplied and the products are added. The location of the resulting number in AB corresponds to the row and column that were considered. 4X (A B) 12 = a 1r b r2 = a 11 b 12 + a 12 b 22 + a 13 b 32 + a 14 b 42 r=1
Matrix Multiplication If A is an m n matrix and B is an n q matrix then the product matrix AB is defined by AB = 2 6 4 a 11 a 12 a 1n a 21 a 22 a 2n........ 3 7 5 2 6 4 b 11 b 12 b 1n b 21 b 22 b 2n........ 3 7 5 = a m1 a m2 a mn b m1 b m2 b mn 2 3 c 11 c 12 c 1n c 21 c 22 c 2n 6 4.. 7...... 5 c m1 c m2 c mn where c ij =[a i1 a i2...a in ] 2 6 4 b 1j b 2j. b nj 3 7 5 = a i1b 1j + a i2 b 2j +...+ a in b nj for i =1,..., m, j =1,..., q1
Matrix Multiplication For example 2 4 6 10 4 6 3 5 3 5» 30 20 = 2 4 6 30 + 10 20 4 30 + 6 20 3 30 + 5 20 3 5 = 2 4 380 240 190 3 5 A is an m n matrix and B is a p q matrix, then the product AB can be formed if and only if n = p and if it can be formed, it will be an m q matrix.
Matrix Multiplication Properties All three notions of matrix multiplication are associative A(BC) =(AB)C and distributive: A(B + C) =AB + AC and (A + B)C = AC + BC and compatible with scalar multiplication: c(ab) =(aa)b = A(cB)
The Identity Matrix The n nidentity matrix I n is defined to be the n nmatrix in which every element on the leading diagonal is a 1, and every other element is a zero. Thus, I 1 = [1],I 2 = [ 1 0 0 1 ],I 3 = 1 0 0 0 0 1 0 0 0 0 1 0,... 0 0 0 1 An important property of the identity matrix is the anything multiplied by the identity matrix remains the same For example [ 2 2 1 1 ] 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 [ = 2 2 1 1 ]
Summary of Properties of Matrices The following properties are true whenever the operations of addition or multiplication are valid. If A, B and C are matrices and λ is a real number A + B = B + A A +(B + C) =(A + B)+C λ(a + B) =λa + λb AB BA (in general) A(BC) =(AB)C A(λB) = (λa)b = λ(ab) A(B + C) =AB + AC
Co-ordinate systems and co-ordinate frames When representing Vectors and Points we use the same notation for example v = (3, 2, 7) is a vector whereas P = (5, 3, 1) This makes it seem as if vectors and Points are the same thing However Points have a location, but no size and direction whereas vectors have size and direction but no location What this means is that v has the components (3,2,7) in the underlying coordinate systems and Similarly P has the coordinates (5,3,1) in the underlying coordinate system.
Co-ordinate systems and co-ordinate frames Normally this confusing between the object and it s representation present no problem. A problem arises when there is more than one coordinate system and when one transforms points or vectors from one system into another. We usually think of a coordinate system as three axis emanating from an origin as shown
Co-ordinate Frames In the previous diagram, the coordinate system is located somewhere in the world and its axis are best described by three vectors that point in mutually perpendicular directions In particular it is important to explicit the location of the coordinate system so we extend the notion of a 3D coordinate system to that of a 3D coordinate frame. A coordinate frame consists of a specific point ϑ called the origin and three mutually perpendicular unit vectors a, b and c Now to represent a vector v, we find the three numbers (v 1,v 2,v 3 ) such that v = v 1 a+v 2 b+v 3 c and say that v has the representation (v 1,v 2,v 3 ) in this system.
Co-ordinate Frames On the other hand to represent a point P, we view its location as an offset from the origin by a certain amount we represent the vector P ϑ by finding three numbers (p 1,p 2,p 3 ) such that P ϑ = p 1 a + p 2 b + p 3 c and then, equivalently, write P itself as P = ϑ + p 1 a + p 2 b + p 3 c The representation of P is not just a triple, but a triple along with an origin. P is at a location that is offset from the origin by p 1 a + p 2 b + p 3 c
The homogeneous representation of a Point and a Vector It is useful to represent both points and vectors using the same set of basic underlying objects [a, b, c, ϑ] From the previous equations we see that the vector v = v 1 a + v 2 b + v 3 c needs the four coefficients [v 1,v 2,v 3, 0] Whereas the point P = p 1 a + p 2 b + p 3 c + ϑ needs the four coefficients [p 1,p 2,p 3, 1] The fourth component designates whether the object does or does not include ϑ.
The homogeneous representation of a Point and a Vector We can formally write any v and P using matrix notation multiplication as v 1 p 1 v =[a, b, c, ϑ] v 2 and P =[a, b, c, ϑ] p 2 v 3 0 p 3 1 Here the row matrix captures the nature of the coordinate frame, and the column vector captures the representation of the specific object of interest. Thus, vectors and points have different representations : vectors have 0 as a forth component whereas points have 1 as a fourth component.
Converting between co-ordinate systems To go from ordinary to homogeneous coordinates, if the object is a point, append a 1 if the object is a vector, append a 0 To go from homogeneous coordinates to ordinary coordinates, if the object is a vector, its final coordinate is 0. So delete the 0 if the object is a point, its final coordinate is a 1. So delete the 1
Linear Combinations of Vectors Some things work out quite nicely in homogeneous coordinates when we combine vectors coordinate-wise. The difference of two points [x, y, z, 1] and [u, v, w, 1] is [x u, y v, z w, 0] which results in a vector The sum of a point [x, y, z, 1] and a vector [d, e, f, 0] is [x + d, y + e, z + f,1] another point Two vectors can be added : [d, e, f, 0]+[m, n, r, 0] = [d + m, e + n, f + r, 0], which produces another vector It is meaningful to scale a vector : 3 [d, e, f, 0] = [3d, 3e, 3f,0]
References Basic Algebra and Geometry. Ann Hirst and David Singerman. Prentice Hall 2001 Computer Graphics With OpenGL, F.S. Hill jr, Prentice Hall "Essential Mathematics for Computer Graphics fast" John VinceSpringer- Verlag London "Geometry for Computer Graphics: Formulae, Examples and Proofs" John Vince Springer-Verlag London 2004 "Engineering Mathematics", K. A. Stroud, Macmillan 3rd Edition 1987 http://en.wikipedia.org/wiki/affine_geometry