Mathematics for Graphics and Vision

Similar documents
ACCESS TO SCIENCE, ENGINEERING AND AGRICULTURE: MATHEMATICS 1 MATH00030 SEMESTER / Functions

Vectors Part 1: Two Dimensions

A primer on matrices

1 Matrices and matrix algebra

For a semi-circle with radius r, its circumfrence is πr, so the radian measure of a semi-circle (a straight line) is

Getting Started with Communications Engineering. Rows first, columns second. Remember that. R then C. 1

Linear Algebra V = T = ( 4 3 ).

CS123 INTRODUCTION TO COMPUTER GRAPHICS. Linear Algebra 1/33

Notation, Matrices, and Matrix Mathematics

Matrices and Vectors

Vectors. A vector is usually denoted in bold, like vector a, or sometimes it is denoted a, or many other deviations exist in various text books.

The Not-Formula Book for C2 Everything you need to know for Core 2 that won t be in the formula book Examination Board: AQA

CS123 INTRODUCTION TO COMPUTER GRAPHICS. Linear Algebra /34

Matrix Algebra: Vectors

Linear Algebra. The analysis of many models in the social sciences reduces to the study of systems of equations.

Math 123, Week 2: Matrix Operations, Inverses

CSCI 239 Discrete Structures of Computer Science Lab 6 Vectors and Matrices

2 Trigonometric functions

A primer on matrices

Trigonometry - Part 1 (12 pages; 4/9/16) fmng.uk

Vectors a vector is a quantity that has both a magnitude (size) and a direction

TRIGONOMETRY REVIEW (PART 1)

Section 6.1 Sinusoidal Graphs

M. Matrices and Linear Algebra

Vectors. Vector Practice Problems: Odd-numbered problems from

Vectors Summary. can slide along the line of action. not restricted, defined by magnitude & direction but can be anywhere.

Crash Course in Trigonometry

Chapter 2. Matrix Arithmetic. Chapter 2

Linear Algebra Tutorial for Math3315/CSE3365 Daniel R. Reynolds

More with Angles Reference Angles

CS 246 Review of Linear Algebra 01/17/19

Maths Pack. Distance Learning Mathematics Support Pack. For the University Certificates in Astronomy and Cosmology

Core Mathematics 2 Trigonometry

STEP Support Programme. STEP 2 Matrices Topic Notes

MAC 1114: Trigonometry Notes

Elementary maths for GMT

Lecture 3 Linear Algebra Background

Matrices. Chapter Definitions and Notations

NOTES ON LINEAR ALGEBRA CLASS HANDOUT

Chapter 8B - Trigonometric Functions (the first part)

LS.1 Review of Linear Algebra

Linear Algebra March 16, 2019

4 The Trigonometric Functions

MATH 320, WEEK 7: Matrices, Matrix Operations

ELEMENTARY LINEAR ALGEBRA


A Geometric Review of Linear Algebra

( and 1 degree (1 ) , there are. radians in a full circle. As the circumference of a circle is. radians. Therefore, 1 radian.

Section 1.6. M N = [a ij b ij ], (1.6.2)

Notes: Vectors and Scalars

BASIC NOTIONS. x + y = 1 3, 3x 5y + z = A + 3B,C + 2D, DC are not defined. A + C =

Notes on vectors and matrices

A matrix over a field F is a rectangular array of elements from F. The symbol

Introduction to Matrix Algebra

Trigonometry Trigonometry comes from the Greek word meaning measurement of triangles Angles are typically labeled with Greek letters

Review of linear algebra

TRIGONOMETRIC RATIOS AND GRAPHS

Matrices Gaussian elimination Determinants. Graphics 2009/2010, period 1. Lecture 4: matrices

Definition 2.3. We define addition and multiplication of matrices as follows.

Linear Algebra II. 2 Matrices. Notes 2 21st October Matrix algebra

LINEAR ALGEBRA - CHAPTER 1: VECTORS

a 11 x 1 + a 12 x a 1n x n = b 1 a 21 x 1 + a 22 x a 2n x n = b 2.

Unit 3 Trigonometry Note Package. Name:

Notes on multivariable calculus

Introduction Assignment

Complex Numbers Year 12 QLD Maths C

This appendix provides a very basic introduction to linear algebra concepts.

Math Bootcamp An p-dimensional vector is p numbers put together. Written as. x 1 x =. x p

Chapter 2. Linear Algebra. rather simple and learning them will eventually allow us to explain the strange results of

Linear Algebra. Min Yan

DuVal High School Summer Review Packet AP Calculus

Functions and their Graphs

Assignment 1 and 2: Complete practice worksheet: Simplifying Radicals and check your answers

(, ) : R n R n R. 1. It is bilinear, meaning it s linear in each argument: that is

Introduction Assignment

xvi xxiii xxvi Construction of the Real Line 2 Is Every Real Number Rational? 3 Problems Algebra of the Real Numbers 7

Matrices BUSINESS MATHEMATICS

Chapter 13: Trigonometry Unit 1

Mathematical Fundamentals

n n matrices The system of m linear equations in n variables x 1, x 2,..., x n can be written as a matrix equation by Ax = b, or in full

Lecture 7. Econ August 18

1.1 Single Variable Calculus versus Multivariable Calculus Rectangular Coordinate Systems... 4

VECTORS. 3-1 What is Physics? 3-2 Vectors and Scalars CHAPTER

Designing Information Devices and Systems I Spring 2019 Lecture Notes Note 2

Chapter 1: Systems of linear equations and matrices. Section 1.1: Introduction to systems of linear equations

Linear Algebra Homework and Study Guide

Chapter 1. Introduction to Vectors. Po-Ning Chen, Professor. Department of Electrical and Computer Engineering. National Chiao Tung University

Kevin James. MTHSC 3110 Section 2.1 Matrix Operations

Analytic Trigonometry. Copyright Cengage Learning. All rights reserved.

Sometimes the domains X and Z will be the same, so this might be written:

Vector calculus background

Basic Linear Algebra in MATLAB

FORCE TABLE INTRODUCTION

1 Matrices and Systems of Linear Equations. a 1n a 2n

Answers in blue. If you have questions or spot an error, let me know. 1. Find all matrices that commute with A =. 4 3

and sinθ = cosb =, and we know a and b are acute angles, find cos( a+ b) Trigonometry Topics Accuplacer Review revised July 2016 sin.

Chapter 4 - MATRIX ALGEBRA. ... a 2j... a 2n. a i1 a i2... a ij... a in

A-Level Notes CORE 1

Basic Concepts in Linear Algebra

Course Notes Math 275 Boise State University. Shari Ultman

Transcription:

Mathematics for Graphics and Vision Steven Mills March 3, 06 Contents Introduction 5 Scalars 6. Visualising Scalars........................ 6. Operations on Scalars...................... 6.3 A Note on Multiplication Symbols............... 7 3 Trigonometric Functions 8 3. Degrees and Radians....................... 9 3. Sine, Cosine, and Tangent.................... 9 3.3 Plots of Trigonometric Functions................ 0 3.4 Inverse Functions......................... 0 3.5 Some Common Angles...................... 4 Vectors 3 4. Visualising Vectors........................ 3 4. Some Special Vectors....................... 3 4.3 Vector Addition and Subtraction................ 4 4.4 Scalar Multiplication....................... 5 4.5 The Dot Product......................... 6 4.6 Vector Lengths and Normalisation............... 6 4.7 Vector Cross Product...................... 7 4.8 Geometric Interpretation of Products of Vector........ 7 5 Matrices 9 5. Matrix Addition and Subtraction................ 9 5. Scalar Multiplication....................... 5.3 Matrix Multiplication......................

5.4 Matrix Transpose......................... 5.5 Matrices as Transforms of Vectors............... 3 5.6 The Identity Matrix....................... 4 5.7 Inverse Matrices......................... 4

Notation Summary General Notation a, b,... Lower case letters in italics are scalars (simple numbers) a, b,... Lower case letters in bold are vectors (-D arrays of numbers) A, B,... Upper case letters are matrices (-D arrays of numbers) sin (θ) Means (sin(θ)), not sin(sin(θ)). Similarly for cos and tan. sin (x) Arcsine, the angle whose sine is x. Similarly for cos and tan. Matrices and Vectors v i m ij v T, M T The ith element of a vector, v The element of a matrix, M, in the ith row and jth column Transpose of a vector or matrix 0 The zero-vector, 0 = [ 0 0... 0 ] T. u v Dot product (a scalar value) of two vectors u v Cross product (a 3-vector) of two 3-vectors v The length of a vector, v = v v ˆv A unit vector in the same direction as v, ˆv = v v. u v The vectors u and v are perpendicular to one another. u v The vectors u and v are parallel to one another. I The identity matrix, with diagonal entries of, and offdiagonal entries of 0. M The inverse of a matrix, M. MM = I = M M. 3

Quick Reference A few results and identities are gathered here for easy reference: Trigonometric functions of some common angles: sin (θ) + cos (θ) =. π θ 0 6 4 3 Degrees 0 30 45 60 90 Opposite 0 3 π π π 3 0 Adjacent Hypotenuse sin θ 0 3 cos θ tan θ 0 The angle between two vectors is cos ( 3 0 3 3 u v u v Two non-zero vectors are perpendicular if and only if u v = 0. Two unit vectors are parallel if and only if û ˆv =. Two non-zero 3-vectors are parallel if and only if u v = 0. u v is perpendicular to u and v unless they are zero or parallel. Matrix multiplication is not commutative, so AB BA in general. Inverses and transposes of products reverse the order of multiplication, so (AB) T = B T A T and (AB) = B A. ). 4

Introduction This document provides mathematics which is be useful for computer graphics and vision students. It is mainly aimed at students taking COSC34 (Computer Graphics) at Otago, but may be of interest to those taking related papers such as COSC360 (Computer Game Design) or COSC450 (Computer Graphics and Vision) as well. We will be dealing with three main types of mathematical object scalars, vectors, and matrices. Scalars are just ordinary numbers like, 3 7, or π. Vectors are one-dimensional arrays of numbers, and matrices are two-dimensional arrays of numbers. It is important to keep the type of values in mind, because it affects what you can do with them and what the result is. For example, while it makes sense to divide two numbers (as long as the second is not zero), division is not defined for vectors or matrices. Vectors will be used to represent geometric objects like points and lines. Matrices will represent transformations of these objects, like scaling, translation, and rotation. Using vector and matrix notation gives us a set of operations (an algebra) to work with. Representing these geometric ideas algebraically is very useful, since it allow us to easily write computer programs that work with them. 5

Scalars We ll start by revising some properties of scalars (ordinary numbers). This may be familiar to you, but it is important to make sure we have a common language and have defined a few important terms.. Visualising Scalars Scalar values can be visualised on the number line, as shown in Figure. -½ - 0 π Figure : The values,, 0,, and π shown on the number line Scalars can be thought of as either a point on the line, or as a distance along the line from zero. If thought of as distances, negative numbers are a step to the left, and positive numbers to the right. Addition can be thought of as combining steps, and multiplication as repeating a step. So 3 is a step of units to the right, followed by a step of 3 units to the left. Instead 3 can be thought of as taking two steps to the right three times (although 6 is not shown in Figure ).. Operations on Scalars The usual operations that are applied to numbers are addition, subtraction, multiplication, and division. For this discussion, it helps to think of subtraction as adding negative numbers, and as division as multiplying by reciprocals, so when we see 3, we think + ( 3), and by we mean. This makes the order of operations easier, because the meaning of +3, is not immediately clear is it (+3) = 4 or ( )+3 =? If we think of this as + ( ) + 3 then the order of operations doesn t matter. Likewise 3 is not clear, but 3 is. This leads us to the first properties of addition and multiplication they are associative and commutative, which means that when you add a list of numbers or multiply a list of numbers the order in which do you things doesn t matter. More formally the commutative rule for addition says that a + b = b + a a, b. () 6

A few words about notation are probably useful here. Firstly, we use lower case italic letters to denote scalars. Secondly, the symbol, which is usually read for all, means that this is true for any values of a and b. The commutative rule for multiplication tells us that a b = b a a, b. () The associative rule tells us that we can apply multiple instances of addition or multiplication in any order. For addition this gives us and for multiplication a + (b + c) = (a + b) + c a, b, c, (3) a (b c) = (a b) c a, b, c. (4) Things get a little more complex when we mix multiplication and addition, but the key rule is to apply multiplication first, unless brackets tell you otherwise. So + 3 means + ( 3), and + 3 means ( ) + 3. Combining operators also gives us a third property distributive. We say that multiplication distributes over addition to mean that a (b + c) = (a b) + (a c) a, b, c. (5) Note that addition does not distribute over multiplication, since + ( 3) ( + ) ( + 3). (6).3 A Note on Multiplication Symbols There are many ways to write multiplication of two values. The most usual is form is probably a b, which we ve used so far, but there are two other common forms used when dealing with symbols (rather than numbers). The first is a dot, a b and the second is just writing two symbols side-by-side, ab. Note that when dealing with actual numbers, is preferred, since can look like., and is a different number all together. This will become important when we come to vectors and matrices where multiplication is a little more complicated. We ll typically just place symbols side by side to indicate multiplication, but use when needed for clarity. Note that this is also why mathematics typically uses single-letter variables, and why precise notation is important. For example, sin should be read as s i n, where as sin is the trigonometric function sine. (Shortening sine to sin doesn t save much space, but mathematicians do this to make the abbreviation the same length as the shortening of cosine to cos and tangent to tan.) 7

3 Trigonometric Functions We will be dealing with angles quite a lot, and this leads to trigonometry. We will need to be familiar with measuring angles in degrees and radians, and with the basic trigonometric functions, sine, cosine, and tangent. Trigonometry is often introduced in terms of right-angled triangles, but it is also helpful to keep in mind the relationship with circles. These relationships are illustrated in Figure. y θ x s Figure : Angles can be thought about as rotation around a circle, or as part of a right-angled triangle. Some points to note from the figure: The angle of interest is θ. We will often use Greek letters for angles, but this is just convention, and not always followed. We have a unit circle (a circle with radius ), and measure the angle anti-clockwise from the positive x-axis (the positive x-axis is pointing to the right in Figure ). The angle then relates to some point (x, y) on the circle. The length of the arc around the circle to (x, y) is s. This point defines a right-angled triangle with sides x, y, and. The length of the longest side (the hypotenuse) of the triangle is. The length of the side of the triangle opposite the angle is y. The length of the shorter side adjacent the angle is x. For angles greater than 90 this still holds, but the x and/or y values may be negative. 8

3. Degrees and Radians Angles are commonly measured in degrees, but mathematically it is easier to work in radians. Radians measure the distance around a circumference of circle of unit radius (r = ) associated with a given angle, labelled s in Figure. Since the total circumference of a unit circle is π, that is equivalent to 360, and the conversion from an angle, s, measured in radians to and from the equivalent value, d, in degrees are s = π 80 d, d = s. (7) 80 π Note that radians are a unitless quantity you do not have degrees radian, and while we may talk about an angle being π radians for clarity, it is equally correct to just say that the angle is π. 3. Sine, Cosine, and Tangent The trigonometric functions we will use are sine, cosine, and tangent. These are denoted sin, cos, and tan. If we think of things in terms of a right-angled triangle, then sin θ = O H, cos θ = A H, tan θ = O A, (8) where H is the length of the longest side of the triangle (the hypotenuse), O is the length of the side opposite the angle, and A is the length of the side adjacent to the angle. I learned this as SOH CAH TOA, but why a string on nonsense syllables helps, I don t know. If we return to the view of a unit circle, and Figure, then we can substitute H =, O = y, and A = x, giving sin θ = y, cos θ = x, tan θ = y x. (9) For a more general circle, with a radius of r, this can be rearranged to show that (x, y) = (r cos θ, r sin θ), (0) for any point, (x, y) on a circle. The combination of these definitions with Pythagoras theorem (i.e., a +b = c, for a right angled triangle with sides of length a, b, and hypotenuse c) leads directly to the identity sin θ + cos θ =, () where the conventional notation is that sin θ = (sin θ), etc. 9

3.3 Plots of Trigonometric Functions Plots of the sine and cosine functions are shown in Figure 3. Note that both functions are periodic, so sin(θ + π) = sin θ. Also, cos(θ) = sin(θ + π ). 0.5 sin(x) cos(x) 0 0.5 3 0 3 4 5 6 Figure 3: Plot of sine and cosine functions. Figure 4 shows the tangent function. Like sine and cosine it is periodic, with a period of π. Note also that tan(θ) is undefined at π, and therefore at (k+)π for any integer, k. This comes about directly from the definition in Equation 8, since the length of the side adjacent to the angle is 0. It is important to remember that tan(θ) is undefined at these points, not infinite the function diverges so it gets larger (more positive) without bound from one side and smaller (more negative) from the other. 3.4 Inverse Functions When working with lines and vectors, we often end up computing the sine or cosine of an angle. In some cases we need to find the angle from these values, and so the inverse functions are needed. These are written as sin (x), etc. which means the angle which has a sine of x. This notation is a little unfortunate, since it conflicts a bit with the idea of sin (θ) being (sin(θ)). Following that logic, sin (x) could be interpreted as (sin(θ)) = sin(θ). This is not the usual convention, however. The inverse functions are often referred to as the arcsine, arccosine, and arctangent. This is why the corresponding functions in languages like C are 0

4 tan(x) 0 4 3 0 3 4 5 6 Figure 4: Plot of the tangent functions. called asin, acos, and atan. Note that the inverse is not unique, since the trigonometric functions are periodic. Furthermore, there are generally two angles in each cycle with the same value for each function. For example, cos( θ) = cos(θ) for any θ. By convention, cos (x) is treated as the value in the range [0, π], while sin (x) and tan (x) return a value in the range [ π, π ]. 3.5 Some Common Angles While in computation we will generally use any value for angles, computing trigonometric functions for an arbitrary angle by hand is not generally recommended. However, for a set of common angles, the trigonometric functions can be easily expressed. These arise from two special right-angled triangles one with two 45 angles, and one with a 30 and 60 angles. The 45 45 90 triangle has two equal sides, and the longest side has a length of. Pythagoras theorem tells us that if the length of the other two sides is a, then a + a =, so a =. The 30 60 90 triangle is half of an equilateral triangle with sides of length. This means that the shortest side (adjacent to the 60 angle) has length. Therefore the remaining side length, b is found from ( ) + b =, ()

which tells us that b = 3. These results, and the trigonometric functions that follow directly from them are summarised in Table. π θ 0 6 4 3 Degrees 0 30 45 60 90 Opposite 0 3 π π π 3 0 Adjacent Hypotenuse sin θ 0 3 cos θ tan θ 0 3 0 3 3 Table : Trigonometric function values for some common angles. Note that tan π is undefined.

4 Vectors A vector is a one-dimensional array of numbers, and we write them in bold face, such as v to distinguish them from scalars. Other notations you might see for vectors include an arrow or bar over or above a value, such as v, v, or v. These alternative notations are often used when writing by hand. A vector, being an array, has a size, which is the number of elements it contains. A vector with n elements is called an n-vector, so we talk about -vectors, 3-vectors, and so on. Note that the size of a vector refers to the number of elements in a vector, not how big or small the elements are. By convention, the ith element of a vector v is denoted v i, using the same letter or symbol as the vector, but as a scalar. Vectors may be written out in a number of different ways, and we will follow the common convention of using column vectors, where the elements are arranged vertically, v v v =.. (3) v n This convention will be useful when we come to combine matrices and vectors, but takes up a lot of room on the page. When writing vectors in text, it is convenient to convert them to rows. The transpose operator converts a column vector to a row, or vice-versa, and so we can write v T = [ ] [ ] T. v v... v n, or equivalently v = v v... v n The transpose is a matrix operation, and so will be discussed later, but other notations you might see are a prime, v, or an asterisk, v. 4. Visualising Vectors Just as scalars can be thought of as points or steps along a line, vectors can be thought of as points or steps in the plane (for -vectors), 3D space (for 3-vectors) and so on. Some -vectors are shown in Figure 5, and can be thought of as either steps along the x- and y-axes, or as points in D space. Likewise, a 3-vector can be thought of as steps along the x-, y-, and z-axes in 3D space. 4. Some Special Vectors A few special cases of vectors arise, and lead to some additional notation. The first is a vector where every element is zero. This is called a zero vector, 3

Y (,3) (-3,) (3,) (-,0) X (-,-) (½, -½) Figure 5: Some -vectors visualised in the plane. The vector v = [ x is seen as either the point (x, y), or the step from the origin to (x, y). y ] T and often written 0. There is a whole family of zero vectors of different length so we have the zero -vector, the zero 3-vector, etc. When the size of the vector is clear from context, we ll just talk about the zero vector. You might also see for a vector of all ones, but this is less common. Note that is not called the one-vector. A -vector is a vector with one element, and is equivalent to a scalar. 4.3 Vector Addition and Subtraction Vector addition and subtraction are defined simply by adding the corresponding elements of a vector, so [ ] [ ] [ ] [ ] 3 + 3 4 + = =. (4) 4 + 4 6 This only makes sense if there are the same number of elements in each vector. If the two vectors have different sizes, then their sum is undefined. 4

More generally, the sum of two n-vectors, u and v, is u v u + v u u + v =. + v. = u + v.. (5) u n v n u n + v n The difference of two vectors is defined in a similar way, u v u v u u v =. v. = u v.. (6) u n v n u n v n As with scalars, it is sometimes convenient to think of subtraction as adding a negative, so u v = u + ( v), where v = [ v v... v n ] T. The definitions of addition and subtraction can be represented more compactly if we think about the individual elements. If w = u + v, we can see that w i = u i + v i. Likewise if d = u v, then d i = u i v i. Vector addition has the same properties as scalar addition. Vector addition is commutative and associative, so u + v = v + u u, v, (7) and u + (v + w) = (u + v) + w u, v, w. (8) 4.4 Scalar Multiplication Multiplication of vectors is more complex than for scalars, and there are several different types of product that can be formed. The first, and simplest, is scalar multiplication, which is the result of multiplying a vector by a scalar. Like vector addition, this is done element-wise, so multiplying a vector by simply doubles each element. More formally, given a scalar, s, and an n-vector, v, their scalar product, u = sv is also an n-vector, with elements u i = s v i. Scalar multipliers are conventionally written on the left of the vector, but this is not essential, and vs = sv. Multiplication by several scalars is associative and commutative, so stv = tsv and s(tv) = (st)v. Finally, multiplication by 0 yields a zero vector of the same size as the original vector, 0v = 0. 5

4.5 The Dot Product The most common form of multiplication with vectors is the dot product, written u v. This is also called the scalar product (not to be confused with scalar multiplication) or sometimes the inner product. Given two vectors of the same size, their dot product is a scalar value. The corresponding elements of the two vectors are multiplied together, and these are added up to give the result. More formally, u v u u v =. v. = u v + u v + + u n v n, (9) u n v n which can be written more compactly as n u v = u i v i. (0) i= The dot product is commutative, so u v = v u. It is not associative, because the dot product of three vectors is undefined. Taking the dot product of two vectors gives a scalar result, and the dot product of a scalar and a vector is not defined. The dot product is distributive over vector addition, so u (v + w) = u v + u w u, v, w. () Scalar multiplication also factors out, so (ru) (sv) = rs(u v). 4.6 Vector Lengths and Normalisation The length of a vector is written v, or v, and can be computed as v = v + v + + v n = v v. () The length of the vector is also called its magnitude or norm, or more precisely its Euclidean norm or -norm. A unit vector is a vector with length. Any vector can be converted into a unit vector by dividing by its length, and we use a hat over a vector to denote this, ˆv = v v. (3) This process is called normalisation. 6

4.7 Vector Cross Product The final form of multiplication we need for vectors is the cross product. The cross product is only defined for 3-vectors. Given two 3-vectors, u and v, their cross product is also a 3-vector given by u v u v 3 u 3 v u v = u v = u 3 v u v 3 (4) u 3 v 3 u v u v It can be easily shown from this definition that v v = 0 for any 3-vector, v. The cross product is not associative, so (u v) w v (u w) in general. The cross product is anticommutative. This means that changing the order of the vectors does change the result, but in a predictable way, since u v = (v u). The cross product distributes over addition, so u (v + w) = uv + uw. Scalar multiplication also interacts in the expected manner so (ru) (sv) = rs(u v). 4.8 Geometric Interpretation of Products of Vector The various products of two vectors have useful geometric interpretations. These are related to the angle between the vectors, and we introduce some notation for special cases. If two vectors are at right angles to each other, they are perpendicular and we write u v. If two vectors point in the same direction, they are parallel, and we write u v. The zero-vector, 0, is not parallel or perpendicular to any vector, and is a special case in all of the discussions that follow. Two (non-zero) vectors are parallel if, and only if, one is a scalar product of the other. So if u v, then u = sv for some non-zero s, and vice versa. The dot product tells us about the angle between vectors, since u v = u v cos θ, (5) where θ is the angle between the two vectors. If we normalise the vectors, this simplifies to û ˆv = cos θ. (6) Since cos θ = 0 at ± π, this means that if the dot product of two non-zero vectors is 0 if, and only if, they are perpendicular to one another (or one of them is 0). 7

In general, the cross product of two vectors is perpendicular to both of them, so u (u v), and v (u v). There are two exceptions to this rule. Firstly, if either vector is 0, then their cross product is 0. This follows directly from the definition of the cross product (Equation 4). The second is if the two vectors are parallel, in this case u = sv, and it follows fairly directly that u v = 0. These facts help us to solve some common problems that come up in computer graphics, so to summarise, given two non-zero vectors u and v: ( ) The angle between the vectors is cos u v u v They are perpendicular if and only if u v = 0. They are parallel if and only if u v = 0. A vector perpendicular to both vectors is u v (unless u v). 8

5 Matrices Many of the properties of vectors are special cases of matrix operations, so we ll cover matrices first. A matrix is a two-dimensional array of numbers, and we write then in uppercase upright letters, such as A. The size of matrices is often important, and is expressed as [number of rows] [number of columns], so a 3 4 matrix has 3 rows and 4 columns. Since a matrix is an array of numbers, it is often convenient to talk about individual elements of a matrix. We ll use a i,j to refer to the element of a matrix A in the ith row and jth column. Following mathematical convention, we ll start indices from, although in code you ll often start from 0. When expressed in full, matrices are written inside square brackets, so a m n matrix can be written as a, a,... a,n a, a,... a,n A =....... (7) a m, a m,... a m,n Matrices have many of the same operators as scalars, although some function a little differently. One new operator is the transpose, which involves swapping the rows and columns of a matrix. Given the m n matrix A in Equation 7, its transpose is the n m matrix a, a,... a m, A T a, a,... a m, =....... (8) a,n a,n... a m,n 5. Matrix Addition and Subtraction Matrix addition and subtraction are defined element-wise. That is, you add (or subtract) two matrices by adding (or subtracting) the values at each 9

position in the matrix. Written out more formally we have a, a,... a,n b, b,... b,n a, a,... a,n A + B =...... + b, b,... b,n...... a m, a m,... a m,n b m, b m,... b m,n (a, + b, ) (a, + b, )... (a,n + b,n ) (a, + b, ) (a, + b, )... (a,n + b,n ) =...... (a m, + b m, ) (a m, + b m, )... (a m,n + b m,n ) (a, b, ) (a, b, )... (a,n b,n ) (a, b, ) (a, b, )... (a,n b,n ) A B =...... (a m, b m, ) (a m, b m, )... (a m,n b m,n ) (9) (30) (3) This only makes sense if all the values line up, so you can only add or subtract matrices of the same size. Addition and subtraction of matrices of different sizes is not defined. We can summarise addition and subtraction by considering how each element in the result is computed. If C = A + B, then the i, jth element of C is given be c i,j = a i,j + b i,j, (3) likewise, if D = A B, then d i,j = a i,j b i,j. (33) Addition of matrices is associative and commutative, just like addition of scalars. Given any matrices A, B and C all of the same size, then A + B = B + A (34) and A + (B + C) = (A + B) + C. (35) 0

5. Scalar Multiplication Multiplying a matrix by a scalar, like addition, is defined on a per-element basis. The product of a scalar, s, and an m n matrix, A is sa, sa,... sa,n sa, sa,... sa,n sa =......, (36) sa m, sa m,... sa m,n or in element-wise form, if B = sa, b i,j = sa i,j. (37) Note that it is customary to write scalars on the left, but this is not necessary since sa = As. (38) Scalar multiplication distributes over matrix addition, so s(a + B) = sa + sb s, A, B. (39) Likewise, it distributes over scalar addition, giving 5.3 Matrix Multiplication (s + t)a = sa + ta s, t, A. (40) Finally we come to multiplying two matrices. As with addition, this is not defined for all pairs of matrices, but the rules are a little more complicated. The product of two matrices is defined if, and only if, the number of columns in the left-hand matrix, and the number of rows in the right-hand matrix are the same. Given matrices A of size m p and B of size p n, their product is a m n matrix, A of size m p B of size p n = C of size m n. (4) As with scalar multiplication, we ll usually just write AB for the product of two matrices, and leave out the multiplication sign. The i, jth element of C = AB is found from the ith row of A and the jth column of B. The corresponding elements of the row and column are multiplied together then summed, giving p c i,j = a i,k b k,j. (4) k=

Note that this is only defined if the rows of A and columns of B have the same number of elements. This is why the number of columns in A must be the same as the number of rows in B. Figure 6: Matrices are multiplied by combining the rows of the left-hand side with the columns of the right-hand side Matrix multiplication is associative, but not commutative. that A, B, and C have appropriate sizes, Assuming but in general A(BA) = (AB)C, (43) AB BA. (44) Note that unless A and B are square, BA won t even be defined. Even if they are square, the products will usually be different in each case 5.4 Matrix Transpose The transpose of a matrix was introduced briefly when talking about vectors. Given an m n matrix, M, its transpose, M T is an n m matrix where the ith row of M T is the ith column of M. Alternatively we can think about individual elements and (with some abuse of notation), m T ij = m ji. The transpose undoes itself, so (M T ) T = M. Transposes of sums and scalar products are fairly straightforward, since (A + B) T = A T + B T, and (sa) T = s(a T ). The transpose of a product of two matrices is a little surprising, since the order of multiplication reverses, giving (AB) T = B T A T. (45) With this definition of the transpose and matrix multiplication, we can revisit the dot product. Viewing vectors as column matrices, it is easy to show that u v = u T v. The vectors are treated as n matrices, so u T is a n matrix. The matrix product u T v is then a matrix, which is equivalent to a scalar, and expanding out its definition shows that it is the same as the dot product.

5.5 Matrices as Transforms of Vectors We can think of a vector as a matrix with only one column. This means that we can multiply matrices by vectors, and the result is itself a vector, so multiplying a m n matrix by a n-vector gives a m vector as a result. Usually we ll use square matrices, which take n-vectors and transform them to new n-vectors, but this is not always the case. If we think about the vectors as points, then this lets us think about matrices in terms of how they transform (or move) those points. This is the key to the use of matrices in computer graphics they give us a mathematical way to represent transformations, and therefore a way to implement these in our programs. More detail about this will be covered in lectures, but as a simple example, consider the matrix [ ] 0 M =. (46) 0 If we apply this to some -vector, v = [ x y ] T, we get a transformed vector v = Mv = [ x y] T. So we can think about M as having the effect of stretching points by a factor of horizontally, and compressing it by half vertically. We can apply M to lines, curves, or shapes by applying it to each point on the line curve, or shape. Figure 7 shows the effect of applying M to a square positioned at the origin. Figure 7: Applying the matrix, M, from Equation 46 to a square (dotted lines), stretches it horizontally and squashes it vertically (dashed lines). 3

5.6 The Identity Matrix An important matrix is the identity matrix, written I. The diagonal entries of the identity matrix (those in the ith row and ith column) are, and the off-diagonal entries are zero. As with zero-vectors, it is more accurate to talk about an identity matrix because there is one for each size of matrix. Where it is not clear, a subscript can be used to indicate the size of an identity matrix, I = [ ] 0 0 0 0 0 0 0 I 3 = 0 0 I 4 = 0 0 0 0 0 0... (47) 0 0 0 0 0 The identity matrix is a transform with no effect, so Iv = v, for any v. Likewise, I has no effect when applied to other matrices, IM = M = MI for any matrix M. 5.7 Inverse Matrices The inverse of a square matrix, M, is a matrix, M, such that MM = M M = I. (48) Not all matrices have an inverse, but for those that do the inverse is unique. If a matrix has an inverse it is called invertible, nonsingular, or nondegenerate. Matrices without an inverse are called singular or degenerate. If we have a product of two n n matrices, say C = AB then the inverse of the product reverses the order of multiplication, giving This generalises to any number of matrices, so C = (AB) = B A. (49) (A A... A k A k ) = A k A k... A A. (50) For scalar multiplication we have (sa) = s A, for non-zero s. There is no general formula for the inverse of the sum of two matrices. The transpose and inverse can be applied in any order, since (A ) T = (A T ). This may be written as A T as a short-hand. In terms of transforms, the inverse matrix undoes the original transform. For example, the inverse of the matrix from Equation 46 is [ ] M = 0, (5) 0 4

which can be verified by computing MM and M M. While M stretches points horizontally and compresses vertically, M compresses horizontally and stretches vertically. As an example of a matrix that is not invertible, consider the matrix [ ] S =. (5) 4 Suppose this did have an inverse, say S = T, where [ ] t t T =. (53) t t Now we have ST = I (54) [ ] [ ] [ ] t t 0 = (55) 4 t t 0 [ ] [ ] t + t t + t 0 = (56) t + 4t t + 4t 0 The top left element tells us that t + t =, and the bottom left tells us that t +4t = 0. Dividing the second equation by gives us t +t = 0, but that has the same left-hand side as the first equation. Since 0, there can be no such matrix, and S has no inverse. There are many ways of expressing the general property that a n n matrix, M has to have to be invertible. Some of the more useful ones to us are: The columns (or rows) of M must be linearly independent it must not be possible to write one as a weighted sum of the others. The columns of M must span or form a basis for n-dimensional space. That is any n-vector can be written as a weighted sum of the columns of M. The equation Mx = 0 has only the trivial solution, x = 0. For the matrix S from Equation 5 these properties do not hold: The second column is twice the first. No weighted sum of the columns of S can give [ ] T. x = [ ] T is a non-trivial solution to Sx = 0. 5