COMP11120 (2013–14) Mathematical Techniques for Computer Science (Vectors and Matrices)

Transcription

COMP11120 (2013–14) Mathematical Techniques for Computer Science (Vectors and Matrices)
COMP11120 (2013–14)
Mathematical Techniques for Computer Science (Vectors and Matrices)
These notes accompany the part of the course given by Mark Kambites.
Why Do I Need Vectors and Matrices?
Computers process information primarily in the form of numbers, but many applications involve spatial
information. For example, a computer controlling a robotic arm needs to keep track of where it is and work
out how to move it around. A 3d computer game needs to work out how objects moving in space should be
displayed on a (2d) screen.
To make these tasks possible, the spatial information must be given a numerical representation so it can
be processed. Vectors and matrices are the mathematical concepts which do this: they give a way to encode
spatial information as numbers. An understanding of them is essential for computation involving spatial
information.
• Vectors give a numerical way to describe, record and process spatial information: for example, position, velocity or acceleration.
• Matrices give a numerical way to describe, record and process information about transformations of
space: for example, rotation, reflection, projection (e.g. projecting a 3-dimensional model onto a
2-dimensional screen).
Within mathematics, vectors and matrices have many other applications and are important for many other
reasons. But for computer science the ones described above are probably the most important, and they will
be the focus of this course.
Practicalities
Lecturer. Dr. Mark Kambites (email [email protected]).
Notes and Lectures. Notes will be provided on Mark’s webpage, which is accessible via Moodle. The
notes form the definitive content of this part of the course. The lectures will explain the material more
informally.
Exercises. Examples sheets will be provided on Mark’s webpage; solutions to these will be made available
when you have had a chance to attempt the questions yourself. There are also a few additional exercises in
the notes, to which formal solutions will not be made available. Those marked “Exercise*” aim to give
a greater degree of insight into the underlying mathematics; they are not directly examinable and can in
principle be ignored, but if you can master them it will help with the rest of the material.
Office Hour. My office is room 2.144 in the Alan Turing Building (across the square from Kilburn). My
office hour is generally 09:30-10:30 on Thursday mornings during termtime; any exceptions will be
announced in lectures. If you can’t make it to the office hour but need a personal meeting then please email
for an appointment.
Coursework Tests. This part of the course will have two take-home coursework tests. If all goes as
planned, the first will be made available on 27th March and due in on 2nd April. The second will be made
available on 3rd April and due in on 30th April.
Examination. Two of the four questions on the examination will examine this part of the course.
Feedback. Please let me know how you are finding the course, and especially if you think there are any
problems with it. Feel free to use the Moodle forum, speak to me after a lecture, email me, come along in
my office hour, or even slip an anonymous note under my office door!
Books. This part of the course is self-contained and full notes will be supplied, so you should not need to
refer to any books. But if you would like an alternative viewpoint, any of the countless introductory texts
on vectors and matrices should be suitable.
1
1
Revision of Trigonometry
Trigonometry tells us about the relationship between the side lengths and angles of triangles. This is more
generally useful than you might think, because many other shapes can be broken up into triangles. Here we
shall be mostly interested in right-angled triangles.
Recall that in a right-angled triangle,
opposite
y
=
r
hypotenuse
x
adjacent
cos θ = =
r
hypotenuse
y
opposite
tan θ = =
x
adjacent
2
2
r = x + y2
sin θ =
r
y
θ
x
Example. Find x in the following triangle
Solution. Here, cos θ =
3.5
x
so
r
x = r cos θ = 3.5 cos 35◦ = 3.5 × 0.819 = 2.87.
35◦
x
Example. Find θ in the following triangle
Solution. Here, tan θ =
1.1
1.1
y
=
= 0.688. So
x
1.6
θ = tan−1 0.688 = 34.5◦ .
θ
1.6
Note. If tan θ = p, then θ = tan−1 p. Similarly, if sin θ = q, then θ = sin−1 q, or if cos θ = r, then
θ = cos−1 r.
Relationships. There are various properties of and relationships between the trigonometric functions.
Some of the most useful are:
cos(−θ) = cos θ,
sin(−θ) = − sin θ,
tan θ =
sin θ
,
cos θ
− cos θ = cos(180◦ − θ),
sin θ = cos(θ − 90◦ ).
Calculations. In general, you’ll need a calculator (or a look-up table) to compute trigonometric functions
and their inverses. But there are a few standard values of sin θ and cos θ that you should know by heart:
√
1
1
3
◦
◦
◦
◦
, sin 90◦ = 1.
sin 0 = 0, sin 30 = , sin 45 = √ , sin 60 =
2
2
2
◦
◦
cos 0 = 1, cos 30 =
√
1
1
3
, cos 45◦ = √ , cos 60◦ = , cos 90◦ = 0.
2
2
2
Note that these are exact values, and so are better than the rounded approximations you would get from
a calculator (or a computer doing floating point arithmetic).
Exercise. Use these values and the relationships above to work out exact values for: (i) tan 60◦ , (ii)
cos(−30◦ ), (iii) sin 315◦ and (iv) cos−1 (−1/2).
2
2
Coordinate Systems
Y
Recall that coordinates are used to describe the position of a point. In two dimensions (e.g. on a computer
screen), the position of a point can be determined by
its x-coordinate (the distance “along”) and its ycoordinate (the distance “up”). These are measured
from a fixed point, called the origin O.
y
O
x
X
Y
x negative
x and y
y positive
both positive
x and y
x positive
both negative
y negative
Points in the each of the four “quadrants” of the plane
can be distinguished by the signs of their x- and ycoordinates (whether they are positive or negative):
X
Y
Here are 5 points in two-dimensional space:
• Point A has coordinates (3, 6) as it is
3 units to the right of the origin and
6 units up from the origin.
C
A
5
• Point B has coordinates (7, 2) as it is
7 units to the right of the origin and
2 units up from the origin.
B
• Point C has coordinates (−4, 6) as it
is 4 units to the left of the origin and
6 units up from the origin.
-5
X
5
D
• Point D has coordinates (−4, −3) as
it is 4 units to the left of the origin
and 3 units down from the origin.
-5
E
• Point E has coordinates (4, −7) as it
is 4 units to the right of the origin and
7 units down from the origin.
Z
In three dimensions, three coordinates are required
to give the position of a point. Conventionally, the
x and z axes are drawn in the plane of the paper/screen/board while the y axis is pointing into the
board, perpendicular to the other two, as shown on
the right.
Y
X
3
Z
Of course the axes would look a bit
different if viewed from different angles. For example, they could look as
shown on the left.
Y
X
This is not an issue, but there is an important distinction between right-handed and left-handed sets of
axes. Both of the above two are right-handed: you should find that you can arrange your right-hand so
the thumb points in the direction of the x-axis, the forefinger in the direction of the y-axis and the middle
finger in the direction of the z-axis. But (unless perhaps you are double-jointed!) you won’t be able to do
this with your left hand. Swapping the y- and z-axes would give a left-handed system.
To avoid confusion, it is conventional to use right-handed axes unless there is a good reason to do
otherwise (which won’t happen in this course).
3
Introduction to Vectors
A vector is an entity which gives the location of one point relative to another. It is also used to describe a
physical quantity with an associated direction, such as “2 km north-east”, or “5ms−1 downwards”.
a
a
a
a
a
a
a
Intuitively, a vector can be visualised as an “arrow”.
The position of the arrow is unimportant — what matters is the length (or magnitude) of the arrow and its
direction. Two vectors are the same (termed equal)
if they have both the same magnitude and the same
direction. Here, all the vectors marked a are the same
(even though they are drawn in different places). The
vector marked b is different.
b
Notation. To distinguish vectors from ordinary numbers (or scalars) we usually underline their names,
and in print we also use bold type: for example a or v. The magnitude of a vector v is written |v|.
Parallel Vectors. Two vectors in the same direction are called
parallel. The three vectors on the right are all parallel to each
other.
3.1
The Zero Vector
There is one special vector which is difficult to draw as an arrow. It is called the zero vector and denoted
0. It has magnitude 0 and does not have a direction at all. (Intuitively, this is because if you haven’t
travelled any distance, it doesn’t make sense to ask in which direction you went!).
Note that the zero vector 0 is not the same thing as the zero scalar 0.
4
3.2
Adding Arrows
Vectors can be added by the triangle rule. To work out the vector sum a + b, place the tail of vector b at
the head of the vector a. The vector a + b is the directed arrow from the tail of a to the head of b:
Vector addition is commutative,
that is, a + b = b + a. This is
illustrated in the picture on the
left.
b
a
c
In addition, vector addition is
associative:
a + (b + c) = (a + b) + c
b
a+b+c
a
The zero vector 0 acts as an identity for vector addition:
0 + a = a = a + 0.
3.3
Scaling Vectors
Any vector can be multiplied (or scaled) by a scalar (number). Scaling by a positive number λ gives a
vector in the same direction as the original one, but with magnitude multiplied by λ. Scaling by a negative
number −λ gives a vector pointing in the opposite direction, with magnitude multiplied by λ.
−2a
3a
a
The Zero Vector. Scaling any vector by the scalar 0, or scaling the zero vector 0 by any scalar, gives 0:
0a = 0 = λ0.
Negation. We write −a as a shorthand for (−1)a, that is, the arrow with the same magnitude as a and
the opposite direction.
5
3.4
Subtracting Arrows
Now that we have addition and scaling we can also
define subtraction:
a − b = a + (−b) = a + (−1)b.
a−b
Intuitively, if a and b are drawn with their tails at the
same point, then a − b goes from the head of b to the
head of a.
3.5
b
a
Vectors in Component Form
Z
Arrows give a useful way to visualise vectors, but
they do not give us the numerical representation we
wanted for spatial data. For this, we consider the three
vectors i, j and k of magnitude (length) 1 in the directions of the three coordinate axes:
k
Y
j
i
X
Every vector can be expressed as a sum of scalings of these vectors. Specifically, the vector which goes a
units in the x-direction, b units in the y-direction and c units in the z-direction can be written as ai + bj+ ck.
Z
2i + 1j + 4k
Example. The vector going 2 units to the right, 1
unit back (into the board/paper) and 4 units up, can
be written as 2i + 1j + 4k or 2i + j + 4k.
4
Y
2
1
X
Y
In two dimensions, only the vectors i and j are needed.
Any vector can be expressed as a sum of multiples of
i and j.
2i + 3j
3
X
2
6
3.6
Displacement and Position Vectors
If A and B are points, the displacement vector from
A to B is the arrow which fits with its tail at A and
−
−
→
its head at B. It is usually written as AB.
In component form, if A = (ax , ay , az ) and B =
(bx , by , bz ) then
B
−
−
→
AB
−
−
→
AB = (bx − ax )i + (by − ay )j + (bz − az )k.
A
Example. The displacement vector from (2, 4) to (5, 2) is given by (5 − 2)i + (2 − 4)j, that is, 3i − 2j. The
vector from (1, 1, 2) to (5, 7, 3) is given by 4i + 6j + k.
We can represent a point P as a vector, by
considering the displacement vector from
the origin to P . The position vector of P
is the arrow which fits with its tail at the
origin and head at P .
In component form, the position vector of
the point (x, y, z) is the just vector xi +
yj+ zk. For example, the position vector of
(2, 4, 3) is the vector 2i + 4j + 3k. Similarly
in two dimensions: the position vector of
the point (2, 6) is the vector 2i + 6j.
3.7
Vector Operations in Component Form
The vector operations we saw earlier all have simple descriptions in component form:
Addition. To add together two vectors in component form, add together the relevant components. For
example:
(2i − 4j + 5k) + (2i + 6j − 1k) = 4i + 2j + 4k
Subtraction. To subtract one vector from another, subtract the relevant components. For example:
(2i − 4j + 5k) − (2i + 6j − 1k) = 0i − 10j + 6k = −10j + 6k
Scaling. To scale a vector (multiply it by a scalar), we multiply each component of the vector by that
scalar. For example
5 × (2i + 5j − 1k) = 10i + 25j − 5k
Notice that we are multiplying a vector by a scalar to get a vector.
Magnitude. How do we recover the magnitude (length) of a vector from its component form? The
magnitude of a = ax i + ay j + az k is given by
q
|a| = a2x + a2y + a2z
For example, the magnitude of −2i + 6j + 3k is given by
p
√
(−2)2 + 62 + 32 = 49 = 7.
7
3.8
Unit Vectors
A vector with magnitude 1 is called a unit vector. There is exactly one unit vector in every possible
direction.
q
q
4
4 2
3 2
16
3
9
+ 25
= 1.
− 5 + 5 = 25
Example. The vector − 5 i+ 5 j is a unit vector as its magnitude is given by
The vector 12 i + 13 j + 41 k is not a unit vector as its magnitude is
s 2 2 r
61
1 2
1
1
6= 1
+
+
=
2
3
4
144
Finding Unit Vectors. Given a non-zero vector, we can find a unit vector parallel to it by scaling it to
have the right length. Specifically, we multiply by the scalar 1/m where m is the magnitude of the vector.
√
Example. To find a unit vector parallel to a = i + 8j + 4k, note that |a| = 12 + 82 + 42 = 9 so a unit
vector parallel to a is
1
1
8
4
× (i + 8j + 4k) = i + j + k.
9
9
9
9
Exercise. Check that this is a unit vector!
3.9
Further Problems
In the figure above, composed of a series of parallelograms, all points refer to the corners of parallelograms
except for G which is at the centre of a parallelogram.
−−→
−−→ −→ −−→ −−→ −−→ −−
→
−−
→
• If vector AB = p and AD = q, find, in terms of p and q, vectors BC, AC, DC, HF , EC, F G and
−−→
DG.
• Give the position vector of the point (−2, 4, 1).
• Give the displacement vector from (2, 5, 4) to (7, 1, 2)
• Find the magnitude of the vector a = 3i − 4j + 12k
• Which of the following is a unit vector ?
1.
2.
1
3i
1
3i
+ 23 j + 32 k
+ 13 j + 32 k
For the one which is not, find a unit vector parallel to it.
• For a = 2i − 3j + 7k, b = −i + 2j + 1k and λ = 4, find
1. a + b
2. a − b
3. a + λb
• For a = 3i − 4j + 4k, b = 2i + 7j − 3k and c = 2i + 6j + 5k, show that (a + b) + c = a + (b + c).
8
4
4.1
The Scalar and Vector Products
Scalar Product of Arrows
The scalar product (also called the dot product)
is an operation which combines two vectors to give
a scalar (a number). In terms of arrows, the scalar
product is defined by
a · b = |a| |b| cos θ
where θ is the angle between the directions of vectors
a and b.
Thus, the scalar product is the magnitude of the first vector, times the magnitude of the second vector,
times the cosine of the angle between the two vectors. Notice that the angle is always measured by putting
the tails of the two vectors together, and measuring the angle in between.
Example. Find the scalar product of the two vectors a = 4i and b = i + j.
Solution.
b
45◦
a
√
√
√
The magnitude of vector a is 42 + 02 = 4. The magnitude of vector b is 12 + 12 = 2. The angle
between a and b is 45◦ . So, the scalar product a · b is given by
a · b = |a| |b| cos θ
√
= 4 × 2 × cos 45◦
√
1
= 4× 2× √
2
= 4.
Thus, a · b = 4.
The Zero Vector. The definition of a · b given above does not make sense if a = 0 or b = 0 (or both),
since 0 has no direction. By convention, the scalar product of the zero vector with anything is defined to
be 0 (the zero scalar):
a · 0 = 0 = 0 · a.
4.2
Sign of the Scalar Product
Since magnitudes are always positive, the value of the angle θ between two vectors determines whether their
scalar product is positive, negative or zero:
9
Acute Angles. If θ is an acute angle (between 0 and 90◦ ) the scalar product is positive. For example,
Here the scalar product is
3 × 2 × cos 25◦ = 6 × 0.91 = 5.4
which is positive.
Obtuse Angles. If θ is an obtuse angle (between 90◦ and 180◦ ) the scalar product is negative. For example,
Here the scalar product is 3 × 2 × cos 130◦ = 6 × (−0.64) = −3.9 which is negative.
Right Angles. If θ is exactly 90◦ then the scalar product is 0. Conversely, if the scalar product of two
non-zero vectors is zero then we can immediately deduce that they are perpendicular:
Here the scalar product is
3 × 2 × cos 90◦ = 6 × 0 = 0.
4.3
The Scalar Product in Component Form
How do we calculate the scalar product of two vectors from their component forms? This turns out to be
easy!
Two dimensions. If a = ax i + ay j and b = bx i + by j then
a · b = ax bx + ay by .
10
Three dimensions. If a = ax i + ay j + az k and b = bx i + by j + bz k then
a · b = ax bx + ay by + az bz .
Remark/Exercise. You can check that these formulae work even if a = 0 or b = 0 (see Section 4.1).
Example. Find the scalar product of the two vectors a = 4i and b = i + j.
Solution. Here ax = 4, ay = 0, bx = 1 and by = 1. The scalar product is 4 × 1 + 0 × 1 = 4. (Notice that
this is the same example we worked out at the start of the section, and of course we get the same answer.
But this time we got there without having to work out the angle between the vectors.)
Example. Find the scalar product of the two vectors a = 4i − 3j + 2k and b = i + j + 6k.
Solution. Here ax = 4, ay = −3, az = 2, bx = 1, by = 1 and bz = 6. The scalar product is 4 × 1 + (−3) ×
1 + 2 × 6 = 13.
4.4
Calculating Angles Between Vectors
The two different ways to calculate the scalar product can be combined to give a way of calculating the
angle between two vectors. We know that
a · b = |a| |b| cos θ.
Assuming neither vector is 0 we can divide through by |a| |b| to get:
cos θ =
a·b
|a| |b|
If we know the component forms of a and b, then we know how to compute a · b, |a| and |b|, so we can
compute cos θ and hence θ itself.
Example. Find the angle between the two vectors a = 2i − j + 3k and b = i + 6j − 2k.
Solution. Here
a · b = 2 × 1 + (−1) × 6 + 3 × (−2) = −10,
p
√
|a| = 22 + (−1)2 + 32 = 14, and
p
√
|b| = 12 + 62 + (−2)2 = 41.
Thus
−10
cos θ = √ √ = −0.417
14 41
and
θ = 115◦ .
4.5
Algebraic Properties of Scalar Products
The following laws apply to scalar products.
a·b=b·a
(“commutativity”)
a · (λb) = λ(a · b) = (λa) · b
(“compatibility with scaling”)
a · (b + c) = a · b + a · c
(“distributivity over addition”)
a · (b − c) = a · b − a · c
(“distributivity over subtraction”)
Exercise*. Prove these.
11
4.6
Calculating Vector Components in a Given Direction
If a and b are non-zero vectors, then it is possible to express a as the sum of two vectors, one of which is
parallel to b, and the other of which is perpendicular to b. Specifically, we can write
a = aparallel + aperp
where
aparallel =
is a vector parallel to b, and
a·b
b
|b|2
aperp = a − aparallel = a −
is a vector perpendicular to b.
a·b
b
|b|2
Exercise*. Prove that aparallel really is parallel to b, that aperp really is perpendicular to b, and that their
sum really does equal a. (Hint: you will need the definition scalar product in terms of angle, and some
elementary trigonometry. It might also help to consider a unit vector parallel to b).
Example. Express the vector a = 7i + 4j as the sum of a vector parallel to b = 2i + 3j and a vector
perpendicular to b.
√
√
Solution. Here a · b = 2 × 7 + 4 × 3 = 26 and |b| = 22 + 32 = 13.
26
aparallel = √
(b) = 2(2i + 3j) = 4i + 6j and
( 13)2
aperp = a − aparallel = 7i + 4j − (4i + 6j) = 3i − 2j
so that
7i + 4j = (4i + 6j) + (3i − 2j).
4.7
Further Exercises
1. Find the scalar products between the following pairs of vectors:
2. Find the scalar product (i + 6j − 2k) · (−2i − j + 4k).
3. Find the angle between the two vectors a = 2i + 3j and b = −i + 6j.
4.8
The Vector Product
We have seen that the scalar product gives a way to multiply two vectors and get a scalar. In three
dimensions, there is another way to multiply two vectors which gives a vector. The vector product (also
called the cross product) of a and b is written a × b. Provided a 6= 0 and b 6= 0 it has the following
properties:
1. It is a vector.
12
2. It has magnitude |a| |b| sin θ where θ is the angle between a and b
3. Its direction is perpendicular (or normal) to both a and b (and hence also to any plane containing
a and b — more on this later).
4. a, b and a × b form a right-handed system. (As before with axes, this means that you can arrange
your right-hand so that the thumb points in the direction of a, the forefinger in the direction of b and
the middle finger in the direction of a × b.
If a = 0 or b = 0 (or both) then a × b = 0.
4.9
The Vector Product in Component Form
As with the scalar product, it is straightforward to calculate the vector product of two vectors in component
form. If a = ax i + ay j + az k and b = bx i + by j + bz k then
a × b = (ay bz − az by )i + (az bx − ax bz )j + (ax by − ay bx )k.
Example. If a = −2i + 3j + 4k and b = 5i − j − 6k, find the vector product a × b.
Solution. Here ax = −2, ay = 3, az = 4, bx = 5, by = −1, bz = −6. So:
a × b = (ay bz − az by )i + (az bx − ax bz )j + (ax by − ay bx )k
= [3(−6) − 4(−1)]i + [4 × 5 − (−2)(−6)]j + [(−2)(−1) − 3 × 5]k
= −14i + 8j − 13k.
4.10
Further Exercises
• Using both the a · b = |a| |b| cos θ formula and the component formula, evaluate a · b where a = 5i
and b = −2i − 2j.
• For c = 2i − 5j − 2k and d = −i + 4j − 3k, evaluate c · d.
• For p = 5i + 2j, q = 7i + 1j and r = −2i + 5j which of the pairs of vectors make (i) acute angles, (ii)
right angles or (iii) obtuse angles?
• Find the angle between 2i − 3j and 4i + j − k.
• For a = 2i − j, b = 3i + 2j and c = 5i − 4j, verify the identity a · (b + c) = a · b + a · c.
• Express a = 13i+11j+7k as the sum of a vector parallel to b = 1i+2j+4k and a vector perpendicular
to b.
5
Introduction to Matrices
A matrix is a rectangular array of numbers, for example:
A=
2 5
3 8
or B =
1 6 3
2 1 9


1
 2 

or C = 
 3 .
4
Rows and Columns. A matrix has clearly defined numbers of rows and and columns. A matrix with p
rows and q columns is called a p × q matrix. Of the above examples:
• A is a 2 × 2 matrix (2 rows and 2 columns – we say that it is square);
• B is a 2 × 3 matrix (2 rows and 3 columns);
13
• C is a 4 × 1 matrix (4 rows and 1 column).
Matrix Entries. If a matrix is called A, we write Aij to denote the entry in the ith row and jth column
of A. For example, in the matrix A above, the entries are A11 = 2, A12 = 5, A21 = 3 and A22 = 8.
Equality of Matrices. Two matrices are equal if
• they have the same number of rows and
• they have the same number of columns and
• corresponding entries are equal.
Example. Find the circumstances under which
2 b
3 9
and
a 5
3 d
are equal.
Solution. Both have 2 rows and 2 columns so the potential for equality exists. For the matrices to be equal
it is necessary (and sufficient) for corresponding entries to be equal, that is: 2 = a, b = 5, 3 = 3 and 9 = d.
So the matrices are equal if a = 2, b = 5, d = 9.
 
2
2 b
Example. Find the circumstances under which
and  c  are equal.
3 9
d
Solution. The first matrix has 2 rows and 2 columns; the second matrix has 3 rows and 1 column. The
two will never be equal regardless of the entries of b, c and d. There are thus no circumstances under which
the matrices are equal.
5.1
Addition of Matrices
Two matrices can be added if they have the same number of rows (as each other) and the same number of
columns (as each other). If so, to get each entry of A + B just add the corresponding entries of A and B.
Formally,
(A + B)ij = Aij + Bij .




2 1
7 2
Example. Can A =  6 1  and B =  1 4  be added? If so, what is the sum?
7 1
3 9
Solution. A and B are both 3 × 2 matrices so they can be added. The sum is


 
9 3
2+7 1+2
A + B =  6 + 1 1 + 4  =  7 5 .
10 10
7+3 1+9
5.2
Scaling Matrices
Any matrix can be multiplied by any scalar, to give another matrix of the same size. To do this, just
multiply each entry of the matrix by the scalar. Formally,
(λA)ij = λ(Aij ).
Example. Find 7
−1 4
.
3 2
Solution. Each entry is multiplied by the scalar 7, so
−1 4
7 × (−1) 7 × 4
−7 28
7
=
=
.
3 2
7×3
7×2
21 14
Notation. Just as for vectors, we write −A as a shorthand for (−1)A.
14
5.3
Subtraction of Matrices
Two matrices of the same size (the same number of rows and same number of columns) can be subtracted.
Again, the operation is performed by subtracting corresponding components. Formally, (A−B)ij = Aij −Bij .




2
7
−1 3
Example. Find A − B if A =  7 −1  and B =  0 4 
−1 2
2 0
Solution. A has 3 rows and 2 columns. B has 3 rows and 2 columns. As the numbers of rows are equal
and the numbers of columns are equal, the subtraction A − B is meaningful and we have

 

2 − (−1) 7 − 3
3
4
A−B =  7−0
−1 − 4  =  7 −5 
−1 − 2
2−0
−3 2
Remark. Just as for vectors, we always have
A − B = A + (−1)B.
Exercise*. Prove this.
5.4
Multiplication of Matrices
Two matrices A and B can, under certain circumstances, be multiplied together to give a product AB.
The condition for this to be possible is that the number of columns in A should equal the number of rows
in B.
If A has m rows and n columns, and B has n rows and p columns, then AB can be formed and will have
m rows and p columns.
A
×
B
=
AB
m× n
n× p
m× p
The entry in the ith row and the jth column of AB depends on the entries in the ith row of A and the
entries in the jth column of B. Formally,
(AB)ij = Ai1 B1j + Ai2 B2j + Ai3 B3j + . . . + Ain Bnj =
n
X
Aik Bkj .
k=1
The way entries of the factors combine to form entries of the product can be visualised as

 


..
..
B1j
.
.

 


B
2j
 Ai1 Ai2 Ai3 . . . Ain  


(AB)
ij

 


...  =  ... ...
...

  . . . . . . B3j
..
..





..
.
.

 


.
..
..
.
.
Bnj
i − th row
j − th column
Element in i − th row
and j − th column
follows:







Example. Consider the matrices

Verify that AB exists, and find it.

2 1
5
A =  −3 7  and B =
.
−2
1 5
Solution. A has 3 rows and 2 columns; B has 2 rows and 1 column. As the number of columns in A is the
same as the number of rows in B, AB exists. AB will have 3 rows and 1 column. The three entries of AB
are computed as follows:
15
• The top entry of AB will be computed from top row of A and the single column of B: it is 2 × 5 +
1 × (−2) = 8.
• The middle entry of AB will be computed from the middle row of A and the single column of B: it is
−3 × 5 + 7 × (−2) = −29.
• The bottom entry of AB will be computed from the bottom row of A and the single column of B: it
is 1 × 5 + 5 × (−2) = −5.


8
So AB =  −29 .
−5
Example. Consider the matrices
C=
2 1
−5 3
and D =
4 −2
3 7
.
Verify that CD exists and find it.
Solution. Both C and D have 2 rows and 2 columns. As the number of columns in C equals the number
of rows in D, CD exists. CD will also have 2 rows and 2 columns.
CD =
2 1
−5 3
4 −2
3 7
2×4+1×3
2 × (−2) + 1 × 7
=
−5 × 4 + 3 × 3 (−5) × (−2) + 3 × 7
11
3
=
−11 31
Example. Consider the matrices

2
6
 1
2


E =  −3
7
 1
2
−2
0
−4
8
2
2
7
3
−5
9
4
4
Then EF will be a 5 × 3 matrix of the form

Find the element y.


EF = 




7


 −1
 and F = 

 2

2
.
.
.
y
.
.
.
.
.
.
.
.
.
.
.
8
4
−3
2

−1
1 
.
2 
−2



.


Solution. y is (EF )42 , that is, the element in the 4th row and the 2nd column. The calculation will
8
4
involve the 4th row of E, which is 1 2 2 4 , and the 2nd column of F , which is
. y will equal
−3
2
1 × 8 + 2 × 4 + 2 × (−3) + 4 × 2 = 18.
Tip. The only way to master matrix operations, and especially multiplication, is to practice them! Try
making up lots of examples of matrices and multiplying them. Get a friend to do the same ones and check
you get the same answer.
16
5.5
Further Exercises
1. Find the number of rows and columns in A =
2 2
2. Find
+
−1 3
1 5
2
3. Find
0 3
−3
5.6
6 −1
2 2
6
,
−
2 4
−1 3
2
1 8
2
and 1 5
2 7
−3
2 3 6
. What is A12 ?
−1 4 7
−1
2 2
and 5
.
4
−1 3
1 8
.
2 7
Vectors as Matrices
A matrix with only one column is called a column matrix. A matrix with only one row is called a row
matrix. It is sometimes helpful to write vectors as column matrices1 . For example, the vector ai + bj + ck
 
a

b . Because of this, column matrices are sometimes also
can be considered as the 3 × 1 column matrix
c
called column vectors.
Regarding a vector as a matrix allows us to multiply vectors together with matrices. Specifically, if v
is an n-dimensional vector and A is a matrix with n columns, then we can compute the product Av. This
will be a column matrix. Most interesting is the case where A is an n × n square matrix, in which case Av
will be another n × 1 matrix, that is, another n-dimensional vector.
Example. Form the product Ax where
A=
2 7
1 5
and x = −1i + 4j.
Solution. Expressed as a column matrix,
x = −1i + 4j =
−1
4
so the product is just the matrix product
2 7
−1
2 × (−1) + 7 × 4
26
Ax =
=
=
= 26i + 19j.
1 5
4
1 × (−1) + 5 × 4
19
Exercise. Form the product By where

2
 −1
B=
 0
−1
5.7
6
2
4
3



3
1
1 
 and y =  2  .
4 
−1
0
Identities and Inverses
Identity Matrices. A square matrix with 1’s on the main diagonal (the diagonal from top left to bottom
right) and 0’s everywhere else is called an identity matrix. The n × n identity matrix is denoted In . For
example:


1 0 0
1 0
I2 =
while I3 =  0 1 0  .
0 1
0 0 1
It has the property that AIn = A and/or In A = A whenever A is the right size for these products to be
defined.
1
In some contexts it is also useful to consider a vectors as a row matrices, but we shall not need to do this in this course.
17
Inverses. Let A be an n × n square matrix. An inverse of A is an n × n matrix A−1 such that
A A−1 = A−1 A = In .
Not every matrix has an inverse. Matrices without an inverse are called singular.
Exercise. What is the inverse of the identity matrix In ?
Exercise*. Prove that AIn = A whenever the product is defined.
5.8
Transpose and Symmetry
The transpose of a p × q matrix is the q × p matrix obtained by swapping over the rows and columns. The
transpose of a matrix A is written AT . For example:
T


1
2
6
 7 
  = 1 7 2 4 while if A =  −1 3  then AT = 2 −1 1 .
 2 
6 3 1
1 1
4

A (necessarily square!) matrix which is equal to its transpose is called symmetric. For example:


−1 4 3
2 −1
or  4 8 0  .
−1 4
3 0 7
Notice the “symmetry” of the matrices about the main diagonal. Another way of saying the matrix A is
symmetric, is that Aij = Aji for all i and j.
A (necessarily square!) matrix which is equal to minus its transpose is called skew-symmetric. For
example,


0 4 2
 −4 0 −1 
−2 1 0
is skew-symmetric as its transpose is


0 −4 −2
 4 0
1 .
2 −1 0
Another way of saying the matrix A is skew-symmetric is that Aij = −Aji for all i and j. (In particular
setting i = j this means that Aii = −Aii for all i, which means the entries on the main diagonal must all be
0!)
The Zero Matrix. A matrix is called a zero matrix if all of its entries are 0. For example:
 
0
 0 
0
0
  or
.
 0 
0 0
0
Exercise. Find an example of a 3 × 3 matrix which is both symmetric and skew-symmetric? How many
such matrices are there?
5.9
Further Exercises
2 3
−1 4
• Find the product CD where C =
and D =
.
7 −1
2 −2
1 2 7
2 3
exist? If so, find it.
• Does the product
6 2 3
5 6
18

4
• Does the product 1 4 5 2  2  exist? If so, find it.
1

• Consider the following matrices:


0 5 −2
4 2
4 6 7
1 3


,
and
,
−5 0 6
.
2 7
6 7 2
6 7
2 0 −6
For each one, decided whether it is (i) square, (ii) symmetric and/or (iii) skew-symmetric.
5.10
Commuting and Non-Commuting Matrices
It is easy exercise to find two matrices A and B such that AB and BA are both defined, but AB 6= BA.
We say that matrix multiplication is not commutative.
Example. Consider the matrices


2 −1
−1 4 0
A =  2 −1  and B =
.
2 3 6
1 4
In this case the products AB and BA can be both be formed, but AB is a 3 × 3 matrix while BA is a 2 × 2
matrix. So AB cannot possibly be equal to BA.
Exercise. Find two 2 × 2 matrices C and D such that CD 6= DC.
Sometimes, however, pairs of matrices do commute.
Example. Find values of b and d such that the matrices
E=
2 6
1 7
and F =
3 b
1 d
commute.
Solution. We have
EF =
FE =
2 6
1 7
3 b
1 d
3 b
1 d
2 6
1 7
=
=
12 2b + 6d
10 b + 7d
and
6 + b 18 + 7b
2 + d 6 + 7d
.
For these to be equal we need each of the entries to be equal, that is:
12 = 6 + b, 10 = 2 + d, 2b + 6d = 18 + 7b, and b + 7d = 6 + 7d.
It is easy to see that these equations are all satisfied by taking b = 6 and d = 8, so these are the required
values. In other words, if
3 6
2 6
and F =
E=
1 8
1 7
then we will have EF = F E.
Terminology Warning. Note that because multiplication is not commutative, phrases like “multiply A
by B” can be ambiguous, and should not be used!
19
5.11
Laws of Matrix Multiplication
While matrix multiplication is not commutative, it does satisfy various other important laws. Here are some
you should know:
(AB)C = A(BC)
(“associativity”)
A(B + C) = AB + AC
(“left distributivity”)
(B + C)A = BA + CA
(“right distributivity”)
T
T
T
(AB) = B A
Exercise. Consider the matrices
1 2
2 6
−2 1
A=
, B=
and C =
.
3 4
1 7
2 3
Check that (AB)C = A(BC).
5.12
Further Exercises
−2 1
1. If A =
and B =
, show that AB 6= BA.
2 4
45752
462467
2757527
2. If C =
and D =
, show that the statement CD = DC cannot be
464647 14641477
5277528
true.
−1 −1
−1 1
2 1
show that A(B + C) = AB + AC
and C =
,B=
3. If A =
4
1
3 7
1 2
5.13
7 1
1 4
Homogeneous Coordinates


x
The column matrix  y  in three dimensions can be thought of as representing either a vector between
z
two points, or the position vector of a particular point. Sometimes it is important to distinguish carefully
between these cases, which we can do by adding a fourth component. This will be equal to ‘1’ for a point
and equal to ‘0’ for a vector. Thus
 
x
 y 

• 
 z  represents the point (x, y, z), while
1
 
x
 y 

• 
 z  represents the vector xi + yj + zk.
0
Coordinates of this form are called homogeneous coordinates. We shall see later that they are extremely
useful when considering geometric transformations.
Example. Represent the point (2, 5) and the vector i − 3j using homogeneous coordinates.
Solution. The point and the vector are 2-dimensional and so the homogeneous coordinates will have three
components. Now the point (2, 5) and the vector i − 3j will be represented as



1
2
 5  and  −3 
1
0

20
respectively.
Note. Subtracting the homogeneous coordinates of two points yields the vector from one point to the other,
while adding a vector to an existing point yields a new point displaced from the original point by the given
vector.
−−→
Example. Given the points A = (2, 5, 4), B = (2, −1, 3) and the vector BC = 2i + 4j + k, use homogeneous
−−
→
coordinates to find vector AB and point C.
Solution. In homogeneous coordinates we have
 



2
2
 5 
 −1 
−−→ 




A=
 4  , B =  3  and BC = 
1
1
−−
→
The vector AB is given by

 
2
2


−−
→ 
−1   5
AB = 
 3 − 4
1
1
which is the vector −6j − k. The point C is given by

 
2
2
 −1   4

 
 3 + 1
1
0
which is the point (4, 3, 4).
5.14



2
4 
.
1 
0


0
  −6 
=

  −1 
0


4
  3 
= 
  4 
1
Further Exercises
• For A =
1 5
2 −1
and B =
2 10
, find x and y such that AB = BA.
x y
• When A has 2 rows and 3 columns, B has 3 rows and 5 columns, C has 5 rows and 4 columns and
D has 4 rows and one column, find the dimensions of AB, BC, CD, ABC, BCD and ABCD. Show
that all steps in finding ABCD are meaningful, that is, the final and intermediate products exist.

 
 
2
2 3 4
2
2 3 4  
• Find the products  −1 5 6   4  and
4 .
−1 5 6
−2 2 3
5
5
• Using homogeneous coordinates (and traditionally), find the vector from (2, 6) to (4, −1).
• In four dimensions, express the following in homogeneous coordinates.
Points : (1, 2, 3, 4), (0, 0, 5, 0), (−2, −6, 2, 5)
Vectors : 2x − 3y + 7z − 3w, 7x + y + z − 4w.
Note, for this example w may be taken as the unit vector in the 4th dimension.
21