Professional Documents
Culture Documents
Mathematics for
Computer Science
Part 1
Dr Yvonne Fryer
September 2021
MATH1179
Course topics
A variety of topics will be introduced. Some may be familiar to you, others possibly less so. You will
be encouraged to read around and find out more about the topics.
Topics will include sets, number theory, linear algebra, calculus, graph theory… and more!
Tutorials
The tutorials allow you the opportunity to really make use of the tutors help and guidance. The tutors
will be monitoring your completion of the tutorial exercises in the workbook.
There will be tutorial exercises accompanying every week of lectures - you must try to complete these
exercises - the most important reason is to develop your understanding. If you cannot do an exercise
then ask for help!
Working together on tutorial exercises is encouraged and strongly recommended – you can learn from
each other!
You should work through a selection of exercises and ask for help on ones you don’t understand how
to do.
Solutions will appear on Moodle a little while after the tutorial, after you have had time to try all the
exercises for yourself.
Attendance
It is expected that you attend all lectures and tutorials. Records of attendance are kept. There is always
a very strong correlation between attendance and marks.
Books
We do not “lecture from a book” and are therefore not expecting you to go out and buy a book.
There are some books and sites listed that may be useful for you, but it is not compulsory to buy
anything.
If you find any really good books or sites, let me know, so that I can recommend them to other
students!
www.mathcentre.ac.uk has useful resources and practice material, often referred to on the course.
Finally …
Finally, we want you to enjoy this course! We want to get you thinking in different ways.
Put the work in and contribute to lectures, and you will learn more and hopefully enjoy it while you
are doing it!
And ask me anything you want to ask!
ii Term 1
MATH1179
Contents
iv Term 1
MATH1179
1: Sets
The first lines of a computer program often contain type declarations so that we know what sort of
storage we are going to need to fit our data.
Example
String name
Dim sum as Single
double x1,x2
If we try to use a different set of values to those we have declared we get a ‘type mismatch’ error
and our program will not run. In constructing and using databases we can also have sets of permitted
data as inputs or outputs. Names, addresses, dates and telephone numbers are often specified in a
certain style and trying to use the wrong format will cause an ‘inadmissible input’ error.
These are examples where a certain set or type is defined with clear rules so that it can be seen whether
a given value is in or out of the set.
As a formal structure a set is a collection of objects with some criteria of membership. The objects of
the set are called elements or members of the set.
Example
We could define D = the set of students on Computing courses
L = the set of letters in the alphabet
N = the set of natural numbers
To specify a set we must describe its members. This is usually done in one of two ways.
(i) Listing all the members of the set, e.g., L = {a, b, c, … y, z}.
(ii) Giving a property which distinguishes the members of the set from objects not in the set.
e.g., P = {x: x is a whole number, 4 ≤ x ≤ 7}
(We read this as: P is the set of elements x such that x is a whole number greater than or equal
to 4 and less than or equal to 7.)
Example
T = {x: 3x = 6}
K = {x: x is a whole number, 2 < x < 6}
H = the set of even numbers
F = the set of colours in the rainbow
E = {x: x is a letter in the alphabet, x is a vowel}
Note that sets are normally denoted by capital letters and the list of elements is enclosed in ‘curly
brackets’
A set is called finite if it contains only a limited number of elements. Otherwise it is called infinite.
If a set A is finite then the number of elements in A is called the order of A.
We denote the order of A by A.
In the last example T = 1, K = 3, H is infinite.
We will use the symbol ∈ to mean 'is an element of' or ' belongs to'
and ∉ to mean 'is not an element of' or 'does not belong to'.
Example
W = {1 ,2,4,5,8}. Then 4 ∈ W, 6 ∉ W.
Two sets A and B are equal if they contain exactly the same elements.
Example
If D = set of letters in the word 'members', E = {m, e, m, b, e, r, s} and
F = {b, e, m, r, s},
then D = E = F. Note sets do not normally contain duplicates.
1.1 Subsets
Let A and S be sets. If every element in A is also in S then A is called a subset of S.
We write: A ⊆ S (or S ⊇ A).
If every element in A is also in S, but there are elements of S which are NOT in A then A is a proper
subset of S.
We then write: A ⊂S (or S ⊃A).
Example
(i) B = {2}, C = {2, 4, 6}, D = {2, 3, 4, 5, 6}. Then B ⊂C, B ⊂D, C ⊂D, B ⊆ B, etc.
The empty set ∅ is the set containing no elements. For any set A, ∅ ⊆ A.
Example
D = the set of students taking a course in Computing, aged under 10; D = ∅
Page 2 Sets
MATH1179
For any problem we define a universal set U as the boundary of all things under consideration and
assume all sets to be subsets of U. For example if we are dealing with numbers the universal set
might be R ; if we were dealing with sets of students the universal set might be the set of all students
in the University of Greenwich.
Example
Let U = {a,b,c,d,e,f,g,h,i}, P = {b, d, f}, Q = {a, b, c, d}, S = {a, c, e}
P ∩ Q = { b,d } P ∪ Q = { a, b, c, d, f}
Q ∩ S = {a,c} Q ∪ S = {a,b,c,d,e}
P ∪ (Q ∩ S) = {a,b,c,d,f} (P ∪ Q) ∩ (P ∪ S) = {a,b,c,d,f}
P ∪ (Q ∪ S) = {a,b,c,d,e,f} (P ∪ Q) ∪ S = {a,b,c,d,e,f}
P' = {a,c,e,g,h,i} Q' = {e,f,g,h,i}
P' ∪ Q' = {a,c,e,f,g,h,i} (P ∩ Q)' = {a,c,e,f,g,h,i}
P \ Q = {f} Q \ P = {a,c}
Example
Let U, = {a,b,c,d,e,f,g,h,i}, P = {b, d, f}, Q = {a, b, c, d}, S = {a, c, e}.
On a Venn diagram we can see their relationship and where each element is placed:
Q
P S
b a
e
f d c i
h
g
Calculations with sets usually involve understanding when elements in an intersection may have been
counted more than once.
Example
Out of 100 people questioned, 54 read the Morning News and 67 the Evening News. If 40 read both,
how many read neither?
Note in the diagram below 40 represents |P ∩ Q| whereas in the previous diagram we show b and d
as elements of P ∩ Q.
U
|P|= 54 |Q| = 67
40
19
Let P be the set of people who read the Morning News and Q the set of people who read the Evening
News.
Page 4 Sets
MATH1179
Formula for A ∪ B
A B
Let A = x, B = y, and A ∩ B = r.
From the Venn diagram, it can be seen that x-r r y-r
A ∪ B = (x - r) + (y - r) + r
= x + y -r.
A ∪ B = A + B - A ∩ B
Example
• 112 students sat a certain examination.
• Question 1 was attempted by 70 students, question 2 was attempted by 66 students
and question 3 was attempted by 38 students.
• 32 students attempted questions 1 and 2, and 20 attempted questions 1 and 3.
• Only 8 students attempted all three questions.
Assuming all students attempted at least one of these three questions find the number who attempted
only two questions.
Let Q1 be the set of students who attempted question 1, let Q2 be the set of students who attempted
question 2 and let Q3 be the set of students who attempted question 3.
When these are placed on a Venn diagram we use the most general form of three intersecting circles.
U
Q2
Q1
24
26 34-x
12 x
18-x
Q3
We can demonstrate the truth of statements about sets by using Venn diagrams and/or prove it with
laws.
A A B
B
C
C
A ∪ (B ∩ C) (A ∪ B) ∩ (A ∪ C)
Page 6 Sets
MATH1179
Example
Show that S ∩ (S ∩ T')' = S ∩ T
S T S T S T S T
Product Set
Let A and B be sets.
The Cartesian product set A × B is the set of all ordered pairs (a, b) where a ∈ A and b ∈ B.
Example
Let A = {1, 2}, B = {s, t, u}
A × B = {(1,s), (1,t), (1,u), (2,s), (2,t), (2,u)}
If C = {Mary, Tom, Albie, John, Leroy } and I = {measles, chicken pox, mumps}
then we can express the information as a subset of the cartesian product set C × I,
i.e. as only the following pairs from the 15 possible in the product set
Product Set
{(Mary, measles), (Tom, measles), (Albie, measles), (Tom, chicken-pox),
(John, chicken-pox), (Leroy,chicken pox), (John,mumps), (Albie,mumps)} Name Disease
Mary Measles
We can see the application of this idea in queries in databases.
Tom Measles
Illness Query
People Albie Measles
Name Disease
Disease John Measles
Name
Mary Measles
Disease
Mary Leroy Measles
Tom Measles
Measles
Tom Mary Chickenpox
Albie Measles
Chicken Pox
Albie Tom Chickenpox
Tom Chicken Pox
Mumps
John Albie Chickenpox
John Chicken Pox
Leroy John Chickenpox
Leroy Chicken Pox
Leroy Chickenpox
This structure can be extended to 3 or more sets or John Mumps
use the same set more than once Mary Mumps
Albie Mumps
Tom Mumps
Albie Mumps
John Mumps
Leroy Mumps
Example
If you are trying to describe the position of a 3D shape in space then its coordinates are from the set
RxRxR
e.g. a cube could be (0,0,0), (0,1,0), (0,0,1), (0,1,1), (1,0,0), (0,1,0), (1,0,1), (1,1,1),
Page 8 Sets
MATH1179
2: Numbers
N Z Q R
the set of natural numbers N = {1, 2, 3,...} (note 0 is included in some books)
the set of integers Z = { ..., -2, -1, 0, 1, 2,...} extended to include negatives
the set of rational numbers Q = {p/q : p,q ∈ Ζ, q ≠ 0} extended to include fractions
the set of real numbers R extended to include √2 e.t.c.
In computing it is important when declaring numerical variables to know what type of number they
can be.
Remember for Addition and Subtraction you need to have ‘like with like’
Example
To add 2/5 and 3/8 you must change them both to a common denominator
2 3 16 15 31
+ = + =
5 8 40 40 40
byte
short float
‘integers’ int double ‘reals’
The type you use depends on the purpose of your program and the precision you require.
In dealing with computer storage, data transfer speeds, bandwidth and many other variables we often
need to write very large and very small numbers so we make use of this shorter form using powers of
10.
Page 10 Numbers
MATH1179
Example
2.3 x 102 + 4.1x103 = 4.33 x 103 (0.23 x 103 + 4.1x103 )
We can describe the range of some of the Java number types using this notation
Remember: 1
10 − n =
10 n
Because using indices you are dealing with powers of 10, they allow some short cuts in the arithmetic.
Remember:
102 × 103 = (10×10) × (10×10×10) = 105 so we add the indices ( or exponents)
when multiplying
10 × 10 × 10 × 10 × 10 × 10
106 ÷ 102 = = 104 here we subtract the indices when
10 × 10
dividing
Naming conventions
We are used to a centimetre being one hundredth of a metre. In general there are several words which
indicate a quantity has been multiplied or divided by a power of 10.
Smallest largest
×10-12 ×10-9 ×10-6 ×10-3 ×10-2 ×10-1 1=100 ×101 ×102 ×103 ×106 ×109 ×1012
Pico nano micro milli centi deci deca hecto kilo mega giga terra
one thousandth of a
a million millionth
One millionth of a
one thousandth
one hundredth
one millionth
thousandth
a thousand
a thousand
a hundred
a million
one tenth
millionth
thousand
Original
ten
Example
A snail moves at a rate of 5 millimetres a second. How many hours will it take the snail to finish the
Olympic 100 metres race? (answers to 2dp)
100m 1x10 2 m
= = 0.2 × 105 seconds = 2.0 × 104 = seconds.
5mm / sec 5x10 −3 m / sec
2.0×104
There are 360 seconds in an hour, therefore in hours the time is = 0.5555 × 102 = 55.56
3.6×102
hours or over 2 days!
In computing it is common to have to work in binary (base 2) arithmetic or even in hexadecimal (base
16). To be confident with these you need to go back to the basics of the decimal system (base 10) we
normally use and recognise some of the other bases in common use.
You should understand the value of the 4 in each of the following numbers is different:
number 2431 3.04 543.29 9.0045 4
value of 4 4 hundred 4 hundredths forty or 4 tens four thousandths 4 or 4 units
Page 12 Numbers
MATH1179
Understand the importance of place values and ‘carrying’ and ‘borrowing’ in addition and subtraction
when a whole 10 is made from 10 in one column.
14 31 4 234 2 31 4 3204 2 . 3 47 82 . 32
8+ 8- 718+ 118- 1348 - 0 . 015+ 9 . 70 -
22 26 952 116 1856 2 . 362 76 . 62
1 1
Example
A teams’ total times for the relay are obtained by adding up each 48.6
individual leg given in seconds.
54.3
52.7
Example
A boy of 5ft 11 inches grows 3 inches in a year making him 5 ft 14 inches????
No, we collect whole 12’s to make feet so 11 inches is the most we can count and his new height as
6ft 2 inches.
So if we were in base 5 the largest number possible in any place is 4 and the arithmetic would look
like this
5 + 2 = 7,
14 4+2 = 6 32
2+ 4- => 7 - 4 = 3to
21 23
So in base 2 (binary) the largest number possible in any place is 1 and a typical binary number looks
like
11
Binary numbers are widely used in electronics and computing because they only consist of two
values, 0 and 1, which can be represented in terms of electrical current.
2 )55 1
2 )27 1 5510 is converted to binary by successive divisions by 2 so becoming 1101112
2 )13 1
2 )6 0
2 )3 1
2 )1 1
0
Octal
This is another name for base 8 so that the digits used are 0-7.
OCTAL 512’s 64’s 8’s 1’s 1063 base 8 (binary) becomes 512+48+3 = 563 in base 10
(decimal)
1 0 6 3
we can write 1063 8 = 56310
8 )135 7
8 )16 0 13510 is converted to octal successive divisions by 8 becoming 2078
8 )2 2
0
Page 14 Numbers
MATH1179
Hexadecimal
This is another name for base 16 and now we have difficulty with our notation for digits
In hexadecimal we need to represent the digits 0-15 before we change columns
Example
In this addition using base 16, the 9 and 3 in the units column have not made a whole 16 to carry but
now the answer looks like 11216
1 9
3 + We overcome this by using letters for the digits between 10 and 15. So the digits used are
1 1 2 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F and the answer to our previous sum becomes 1C16
16 )570 A
16 )35 3 57010 is converted to hexadecimal by successive divisions by 16 becoming 23A16
16 )2 2
0
Example
Given 110011011001102 we can convert it to octal by grouping the figures in threes from the right
and converting them individually as 3 digit binary numbers.
1 1 0 0 1 1 0 1 1 0 0 1 1 0
3 1 5 4 6
110011011001102 = 315468
Similarly we can convert 110011011001102 to hexadecimal by grouping the figures in four from the
right and converting them individually as 4 digit binary numbers.
1 1 0 0 1 1 0 1 1 0 0 1 1 0
3 3 6 6
110011011001102 = 336616
This same procedure can be reversed by converting each of the digits to a binary group of three or
four
Example
60358
6 0 3 5
1 1 0 0 0 0 0 1 1 1 0 1
so 60358 = 1100000111012 (you must put in all the three 0’s for the 0 in base 8)
2D516
2 D =13 5
0 0 1 1 1 1 0 1 0 1 0 1
so 2D516 = 11110101012 (you can omit the first two 0’s but not any in the middle)
Modular arithmetic
In programming languages we often use several types of division, Java has two operators for dividing
2 numbers, these are written as p/q and p%q.
Example
In Java if p = 13 and q = 5
then p/q = 2 ( the number of 5’s in 13) and p%q = 3 ( the remainder when all whole 5’s are taken out)
The % operator is called Modulo Division and we can use this idea in general with the set of integers.
Modular arithmetic is an idea closely linked to number bases and is arithmetic only concerned with
‘remainders’.
Page 16 Numbers
MATH1179
Example
A dial can be turned to any one of four positions, the diagram shows that once you have done 4 turns
you are repeating the sequence again, so in this sense 5 = 1 and 6 = 2 and 7= 3 and 8= 4 = 0
This example illustrates arithmetic MODULO 4 i.e. where any whole 4’s are discounted. We write
6 = 2 mod 4 or
11 = 3 mod 4 (if you take all the whole 4’s from 11 you are left with a remainder of 3)
Example
We can complete tables for arithmetic modulo 4 for addition and multiplication, the sins for these are
sometimes written ⊕4 and ⊗4 to avoid confusion with ordinary + and ×. We note that the only digits
we need are {0,1,2,3} (you can see the link with number bases)
⊗4 0 1 2 3
⊕4 0 1 2 3 0 0 0 0 0
3×3 =9
0 0 1 2 3 3+3 = 6 1 0 1 2 3 9 = 1 mod 4
1 1 2 3 0 6 = 2 mod 4 2 0 2 0 2
2 2 3 0 1 3 0 3 2 1
3 3 0 1 2
The set {0,1,2,3} is known as Z4 and in general Zn = {0,1,2,3,……n-1}
Now Backwards!
,
This gives us the idea that 3 = -1 mod 4 so we could replace a negative number by a positive one and
so do addition instead of subtraction modulo 4
(3 -1) mod4 = (3 +3) mod4 = (6) mod4 = 2 mod 4 as expected
This idea uses addition by the complement of 1. Instead of subtraction by 1, we add 3.3 is the
complement of 1 as together they make 4.
(We have already used this ideas when dividing fractions by multiplying by its multiplicative inverse
3 4 3 4
e.g. ÷ = × where × = 1 )
4 3 4 3
00 00 00 00
11 01 11 01 11 01
11 01
10 10 10
10
So 3-1 = 3 +3 in binary becomes 112 – 012 = 112+112 = 1102 We seem to have got an extra digit as
we are no longer using modular arithmetic and ignoring complete 4’s so we have 6 (1102 ) instead of
2 (102)
Using binary 2’s complement we normally fix the length of the numbers stored so that this leading 1
is automatically lost.
For larger binary numbers the complement is found by reversing the digits and adding 1.
Example
The 2’s complement of 100011102 is 011100012+1 = 011100102
11110000 11110000
so that 10001110 − is equivalent to 01110010 +
01100010 1 01100010
which gives the same answer ignoring the first 1.
Remember if you use 2’s complement you must work to the same number of digits
Page 18 Numbers
MATH1179
3: Algebra
As software developers and system builders the crucial first step is in understanding the clients’ needs
and expressing them correctly.
By now you will be familiar with the concept of a variable in programming. You will try to use
suitable names for quantities that may change in value during the running of the program.
Example
Bill ← Bill + Vat
You often need to identify these variables from a large amount of information and work out the
relationships between them. This is a skill known as modelling and will be developed in semester 2.
The algebra that you learnt at school makes this modelling more abstract by using letters rather than
names to represent variables.
Note that this is now an equation not an assignment. An equation by signifying that we have two
equal sides allows us to manipulate it.
Example
Given the relationship 𝒏𝒏 = 𝒃𝒃 + 𝒗𝒗 can you make b the subject? (derive an expression for b on its
own)
𝒏𝒏 = 𝒃𝒃 + 𝒗𝒗
𝒏𝒏 − 𝒗𝒗 = 𝒃𝒃 (subtracting v from both sides)
as both sides are equal this can also be read 𝒃𝒃 = 𝒏𝒏 − 𝒗𝒗
In terms of our model we can interpret this as saying the original Bill can be found by taking off the
VAT from the new Bill.
Linear Equations
One of the simplest relationships between variables is an equation which only involves simple terms
and an equality, like the previous example. The modeller’s skill is in trying to get that relationship
from the client’s specification or from the information presented to them. The final job puts this skill
in reverse as results from the relationships back into meaningful words.
The two unknowns or variables here are the sale price and the original. We need to connect them
using the information given and so try and get an equation.
Let 𝑠𝑠: sale price and 𝑝𝑝: original price then sale is the original less 10% or sale is 90% of the original.
So,
90
𝑠𝑠 = × 𝑝𝑝
100
We can simplify this as 𝑠𝑠 = 0.9 × 𝑝𝑝
Now we can solve the problem where we know the sale price is £63. We substitute this in for s in the
general equation and rearrange it to solve for 𝑝𝑝.
𝑠𝑠 = 0.9 × 𝑝𝑝
So
63 = 0.9 × 𝑝𝑝
63
0.9
= 𝑝𝑝 (dividing both sides by 0.9)
Therefore
70 = 𝑝𝑝 (or more correctly: 𝑝𝑝 = 70)
The final part of the modelling is to interpret the result in terms of the real life situation i.e.
The original price of the article was £70
Example
The capacity of a big green Arriva bus is 70 people whereas a small single decker London Transport
red bus holds 40. If I have to transport 280 people on an outing to Broadstairs next month how many
buses will I need if I must hire them all from the same firm and use only one type?
This text has a lot of information which is not needed to solve the problem. We need to find the
variables (like Bill and Vat). One way to start modelling the variables is to work out what your
answer will be first!
Page 20 Algebra
MATH1179
Now we have to express the information we know in terms of our variables.
I have to transport 280 people on an outing to Broadstairs next month this problem is about
numbers of people
The capacity of a big green Arriva bus is 70 If I have v buses I can take 70 x v people written
70v people
A small single decker London Transport red bus holds 40. If I have t buses I can take 40t
people
If I use v Arriva buses then equating the people 70v = 280 so (dividing both sides by 70) v = 4
If I use t London Transport buses then equating the people 40 t = 280 so (dividing both sides by 40)
t=7
Answers v = 4 or t = 7
This is NOT the answer to the problem it is only the answer to the equations!
Answer
I will need 4 Arriva buses or 7 London Transport buses to take the 280 people to Broadstairs next
month.
One of the main problems is sorting out which information is vital to the solution and which is only
included for background. With experience you can see only the bold type information is essential.
The capacity of a big green Arriva bus is 70 people whereas a small single decker London
Transport red bus holds 40. If I have to transport 280 people on an outing to Broadstairs next month
how many buses will I need if I must hire them all from the same firm and use only one type?
Solving equations
We have used the balancing approach to solving the equations we have so far come across (do the
same to both sides). This can be because we want to solve the equation or sometimes just to rearrange
it into a different form.
Example
The total pay, p, of a worker is given in terms of the number of hours, n, he works as a formula
p = 4.50n
p p 87.75
=n or n = so to solve our problem n = = 19.50
4.50 4.50 4.50
Answer NOT n = 19.50 but, if his pay is 87.75 then he works 19.5 hours
Example
The total pay, p, of a second worker is given in terms of the number of hours, n, he works and a
weekly bonus, b, he receives as a formula
p = 4.50n +b
rearrange this to make b the subject and then use your new equation to work out how much his bonus
is if his pay is 181.25 for a 38 hour week
p = 4.50n +b
p - 4.50n = b (subtracting 4.50n from both sides)
b = p - 4.50n Now we know the values of p and n so this becomes
b = 181.25 – 4.50x38 = 181.25 – 171.00 = 10.25
Answer: The bonus is 10.25
Example
The temperature in Fahrenheit, F, is found from the temperature in Celsius, C, by using the formula
9C
F= + 32
5
What formula should we use to get the temperature in Celsius from the temperature in Fahrenheit?
9C
F= + 32
5
9C
F-32 = (subtracting 32 from both sides)
5
5(F-32) = 9C (multiplying both sides by 5)
5
(F − 32) = C (dividing both sides by 9)
9
5
C= (F − 32)
9
Page 22 Algebra
MATH1179
Example
The annual amount of pension due to an employee is given first by working out how many
pensionable years service an employee has (how many years they have worked). This is then divided
by 80 and the result used to give the fraction of their final salary they will receive as pension. Find
out what pension an employee gets if they leave at 58 with a final salary of £35 000 and started work
when they were 25
The answer will be of the form: pension will be **** . So for the variable let p = pension
y
p= × f If you want more detail y = l-s (last age – start age)
80
l−s
p= × f This is the sort of formula you can enter into a spreadsheet
80
58 − 25
For this actual case p = × 35000 = 14 437.50
80
The annual pension will be £14 437.50
If the final salary stays the same how many more years must they work so that the pension is over
£16 000?
80p
80 p = (l-s)f then = l−s
f
80p 80p
+s = l so l= +s
f f
putting our p = 16000 as required and putting f = 35000 and s = 25 gives
80 × 16000
l= + 25 = 61.57 so, they must work until they are 62 to be sure of a pension of £16 000
35000
4.1 Sequences
A sequence is essentially an ordered list of numbers, following some pattern. In most cases they are
infinite but finite sequences can also exist.
You might be able to define a sequence directly with a formula, or via previous elements in the
sequence.
We normally use notation such as 𝑥𝑥𝑛𝑛 to mean the n’th term of a sequence. Normally they follow some
pattern.
What is the next term in the following sequences?
1, 4, 9, 16, ….?
This is the sequence of square numbers (starting from 1). We can just describe this directly. The n’th
term is just given by 𝑛𝑛2 . We might write this slightly more mathematically as 𝑓𝑓𝑛𝑛 = 𝑛𝑛2
2, 3, 5, 7, 11, ….?
This one is the sequence of prime numbers. Note that we don’t count 1 as a prime number – there are
good reasons for this! So the next term is the next prime number, which is 13. Hence 𝑓𝑓(𝑛𝑛) = n’th
prime number. Is there a formula for this? As far as we know, there is no formula for the n’th prime
number.
In some sense, the prime numbers are the “building blocks” of all the natural numbers. Yet we know
so little about them.
This illustrates the fact that in maths, we are constantly learning and there is so much more for us to
discover
1, 1, 2, 3, 5, 8, 13, ….?
This is a special type of sequence.
Each term (after the first two 1’s) is obtained by
adding the two previous terms together.
So the next term is 8 + 13 = 21
This is called the Fibonnaci sequence
You might define it by saying that 𝑓𝑓1 = 𝑓𝑓2 = 1 and
then 𝑓𝑓𝑛𝑛 = 𝑓𝑓𝑛𝑛−1 + 𝑓𝑓𝑛𝑛−2
This is a surprisingly useful series.
It is used for example in banking, where the expected share price, say, is obtained by using the
previous two values.
Golden ratio
1+√5
The term that appears here is often referred to as the golden ratio. It is considered one of the
2
most aesthetically pleasing numbers. Many buildings, pictures, cars etc are designed according to the
golden ratio. Have a look around the campus.
Many of the buildings are built roughly to the golden ratio.
That is, the height and width are in the golden ratio (approximately 1: 1.618)
Why is this so aesthetically pleasing? I don’t know the answer – we can discuss in the class!
6, 7, 9, 8, 6, …..?
This one is trickier. There is no obvious pattern. You might be able to come up with lots of
possibilities depending on how you interpret the sequence
The number of letters in the days of the week (Monday = 6, Tuesday = 7 etc). So the next number
would be 8 (Saturday)
Note that by my definition, this could be a finite or infinite sequence – repeating the seven days to
make an infinite series. You may have thought of something else. So, we need to define sequences
carefully and unambiguously.
Sequence notation
Generally we use notation like 𝑠𝑠𝑛𝑛 to represent the n’th term of a sequence.
So, for example, with the sequence we had before, 1, 4, 9, 16, …. we have 𝑠𝑠𝑛𝑛 = 𝑛𝑛2
For the Fibonacci sequence we have 𝑠𝑠𝑛𝑛 = 𝑠𝑠𝑛𝑛−1 + 𝑠𝑠𝑛𝑛−2 . Note that we need to specify 𝑠𝑠1 = 𝑠𝑠2 = 1
4.2 Series
Sequences are useful in recognising patterns in numbers.
Things get more interesting, though, when you try to add up the numbers in a sequence.
If you add up (or sum) the terms then it is called a series.
The notation is called sigma notation – you may have seen this before.
Pattern spotting
Gauss essentially solved this by spotting patterns in the numbers. A lot of maths is pattern-spotting!
When you see the answer, it’s obvious, but it takes intuition, practice and experience to be able to
actually spot a pattern. Which is why most mathematical researchers spend half their life being stuck!
Sigma notation
Sigma notation uses a capital Greek letter ∑ (sigma) to denote summation.
You need to specify the start and end points of your sum (it is possible for these to be infinite).
Then you need to put a formula for each term of the sum after the sigma.
You work out the formula for the start point 𝒔𝒔, then the next term 𝒔𝒔 + 𝟏𝟏, and so on until you reach
the end point.
For example, our sum from 1 to 100 could be written in sigma notation as:
100
∑i
End point
Formula
Start point
i =1
This gives 1 + 2 + ⋯ + 100 as required
As another equation, consider the sum of the first ten squares 1 + 4 + 9 + 16 + ⋯ + 100
This could be written in sigma notation as
10
� 𝑖𝑖 2
𝑖𝑖=1
Note that the use of the letter 𝒊𝒊 is entirely arbitrary.
It is sometimes referred to as a dummy variable, we could use any symbol we like.
Page 26 Sequences and Series
MATH1179
Arithmetic progressions
An arithmetic progression is a sequence where each term differs from the previous term by a
common amount.
Example
1, 4, 7, 10, 13, 16, 19, 22
Infinite arithmetic progressions are not particularly useful as they will always sum to infinity.
Hence we will generally assume arithmetic progressions to be finite.
An arithmetic progression can be written by giving the starting term (usually denoted by 𝒂𝒂), the
common difference (usually denoted by 𝒅𝒅) and the number of terms (usually denoted by 𝒏𝒏)
An arithmetic progression is therefore of the form
𝑎𝑎, 𝑎𝑎 + 𝑑𝑑, 𝑎𝑎 + 2𝑑𝑑, … , 𝑎𝑎 + (𝑛𝑛– 1)𝑑𝑑
Check this has 𝑛𝑛 terms!
Hence the 𝑘𝑘’th term is given by:
𝑎𝑎 + (𝑘𝑘– 1)𝑑𝑑
It is possible to derive a formula for the sum of an arithmetic progression.
The common proof technique relates strongly to the idea that the young Gauss used in the example
we did previously. Hence the reason for mentioning it.
For simplicity, let’s define the last term in the sequence as 𝒍𝒍 (remember, we are assuming arithmetic
progressions are finite so there must be a last term)
Hence our arithmetic progression is of the form
𝑎𝑎, 𝑎𝑎 + 𝑑𝑑, 𝑎𝑎 + 2𝑑𝑑, … , 𝑙𝑙– 2𝑑𝑑, 𝑙𝑙– 𝑑𝑑, 𝑙𝑙
Let’s call the sum of this 𝑆𝑆𝑛𝑛 . So we have
𝑆𝑆𝑛𝑛 = 𝑎𝑎 + (𝑎𝑎 + 𝑑𝑑) + (𝑎𝑎 + 2𝑑𝑑) + ⋯ + (𝑙𝑙– 2𝑑𝑑) + (𝑙𝑙– 𝑑𝑑) + 𝑙𝑙
Now let’s use a little trick. Writing this in reverse gives
𝑆𝑆𝑛𝑛 = 𝑙𝑙 + (𝑙𝑙– 𝑑𝑑) + (𝑙𝑙– 2𝑑𝑑) + ⋯ + (𝑎𝑎 + 2𝑑𝑑) + (𝑎𝑎 + 𝑑𝑑) + 𝑎𝑎
Now, what happens if we add together these two equations?
In particular, what happens to all the 𝒅𝒅?
All the 𝒅𝒅 cancel and we get
2𝑆𝑆𝑛𝑛 = (𝑎𝑎 + 𝑙𝑙) + (𝑎𝑎 + 𝑙𝑙) + ⋯ + (𝑎𝑎 + 𝑙𝑙) + (𝑎𝑎 + 𝑙𝑙)
There are 𝒏𝒏 copies of 𝒂𝒂 + 𝒍𝒍 on the right hand side, and then dividing by 2 gives us:
The sum of the arithmetic progression with n terms, given first term a and last term l is:
1
𝑆𝑆𝑛𝑛 = 𝑛𝑛(𝑎𝑎 + 𝑙𝑙)
2
We often don’t know the last term, just the number of terms and the common difference
Substituting in for 𝑙𝑙 = 𝑎𝑎 + (𝑛𝑛– 1)𝑑𝑑 gives us:
The sum of the arithmetic progression with n terms, given first term a and common difference
d is:
1
𝑆𝑆𝑛𝑛 = 𝑛𝑛(2𝑎𝑎 + (𝑛𝑛 − 1)𝑑𝑑)
2
Mathematics for Computer Science Page 27
MATH1179
Note that if the terms are integers then this will always give an integer solution.
Note that we can use this to check Gauss’ answer for 1 + 2 + ⋯ + 100. We have first term 𝑎𝑎 = 1,
last term 𝑙𝑙 = 100, common difference 𝑑𝑑 = 1 and number of terms 𝑛𝑛 = 100
Use both versions of the formula to check you do indeed get the answer 5050!
We could of course write the sum of an arithmetic progression using sigma notation
In general the sum of an arithmetic progression can be given by
𝑛𝑛
� 𝑎𝑎 + (𝑖𝑖 − 1) 𝑑𝑑
𝑖𝑖=1
Note that you could choose to write this in other ways, for example
𝑛𝑛−1
� 𝑎𝑎 + 𝑖𝑖𝑖𝑖
𝑖𝑖=0
Choose the way that you feel is easiest for the problem you are dealing with!
Example
So, for example, the sum of the arithmetic progression 1, 4, 7, 10, 13, 16, 19, 22 could be written as
8 8
� 1 + 3(𝑖𝑖 − 1) = � 3𝑖𝑖 − 2
𝑖𝑖=1 𝑖𝑖=1
Calculate this using both versions of the formula
Example
What the arithmetic progression using sigma notation, 1, 5, 9, 13, 17, 21
6 6
� 1 + 4(𝑖𝑖 − 1) = � 4𝑖𝑖 − 3
𝑖𝑖=1 𝑖𝑖=1
� 𝑖𝑖
𝑖𝑖=1
Using either version of the formula, we have
𝑚𝑚
𝑚𝑚(𝑚𝑚 + 1)
� 𝑖𝑖 =
2
𝑖𝑖=1
Geometric progressions
In an arithmetic progression, each term differs by a common amount (the common difference).
In a geometric progression, each term differs by a multiple of a common amount.
To check this formula works, can you use it to work out (you can use a calculator!) the sum of the
geometric progression we had before?
This was 1 + 2 + 4 + 8 + 16 + 32
Write down 𝑎𝑎, 𝑟𝑟 and 𝑛𝑛 and use the formula!
Hopefully you will get the answer 63
What would it be for similar geometric progressions with a different number of terms?
Example
Find the sum of the first 5 terms of the geometric progression where the first term 𝑎𝑎 = 3 and the
common ratio 𝑟𝑟 = 4
𝑎𝑎(1 − 𝑟𝑟 𝑛𝑛 )
𝑆𝑆𝑛𝑛 =
1 − 𝑟𝑟
5)
3(1 − 4 3(1 − 1024)
𝑆𝑆5 = = = 1023
1−4 1−4
Zeno’s paradox
Zeno’s paradox is from ancient Greece
Suppose a hare was to run against a tortoise in, let’s say, a 100m race
The tortoise is obviously slower, so the hare agrees to let the tortoise have a head start, say 10m
Who is going to win this race?
The tortoise claims that it will win. In order for the hare to catch the tortoise, it firstly has to catch up
to the point where the tortoise started. But by then, the tortoise will have moved on a bit (say 1m)
So the hare still has to catch up.
So it has to catch up to the point where the tortoise is now
But in that time, the tortoise will have moved on a bit further
So the hare has to catch up that bit more
When catching up, the tortoise will move on a little bit more
And so on…
So the hare can never catch the tortoise
So the tortoise will win
Is this true?
Cutting wood
As another example, suppose you have a 2m long piece of wood. You cut it in half and keep 1m,
leaving you with 1m left over
1m 1m
Now from the 1m you have left, you cut in half again, keeping 1/2m and having 1/2m left over
And keep going like this, constantly cutting the remainder in half.
1m ½m ½m
Bouncing balls
A similar example is to think of dropping a ball. At each bounce it bounces to half of its previous
heights, so it (theoretically at least) bounces infinitely many times.
Yet it will stop bouncing after a finite amount of time.
Conceptualisation
Conceptually, this is quite a difficult thing to comprehend
Adding up infinitely many things to get towards a finite limit is a difficult thing to fully grasp.
Arithmetic Progressions
An arithmetic progression is a sequence of the form
𝑎𝑎, 𝑎𝑎 + 𝑑𝑑, 𝑎𝑎 + 2𝑑𝑑, … , 𝑎𝑎 + (𝑛𝑛 − 1)𝑑𝑑
Where 𝑎𝑎 is the first term, 𝑑𝑑 is the common difference and 𝑛𝑛 is the number of terms.
The j’th term is given by 𝑎𝑎 + (𝑗𝑗 − 1)𝑑𝑑
If 𝑎𝑎, 𝑑𝑑 and 𝑛𝑛 are defined as above, and 𝑙𝑙 is the last term, the sum of an arithmetic progression can be
given by either of the formulae:
1 1
𝑆𝑆𝑛𝑛 = 2 𝑛𝑛(𝑎𝑎 + 𝑙𝑙), or 𝑆𝑆𝑛𝑛 = 2 𝑛𝑛(2𝑎𝑎 + (𝑛𝑛 − 1)𝑑𝑑)
Geometric progressions
A geometric progression is a sequence 𝑎𝑎, 𝑎𝑎𝑎𝑎 𝑎𝑎𝑟𝑟 2 , 𝑎𝑎𝑟𝑟 3 , …
where 𝑎𝑎 is the first term and 𝑟𝑟 is the common ratio
The 𝑗𝑗’th term is given by 𝑎𝑎𝑟𝑟 𝑗𝑗–1
If 𝑎𝑎 and 𝑟𝑟 are defined as above, the sum of the first 𝑛𝑛 terms of a geometric progression is given by
𝑎𝑎(1 − 𝑟𝑟 𝑛𝑛 )
𝑆𝑆𝑛𝑛 =
1 − 𝑟𝑟
Infinite progression
An infinite arithmetic progression will always diverge.
If |𝑟𝑟| ≥ 1 an infinite geometric progression will always diverge.
𝑎𝑎
If |𝑟𝑟| < 1 an infinite geometric progression will converge to 1−𝑟𝑟
5: Logic
Logic is the area of mathematics used for reasoning and constructing proofs. It has applications in
computing science to networking, reasoning about and writing programs and to program
specification. It is also necessary in the wider context of data modelling and system building.
Propositions
A proposition is a statement which can be given the value true or false
Example
'two plus two = five', 'it is Tuesday today' are propositions
Example
Consider the following argument:
If the program syntax is faulty or if program execution results in division by zero, then the
computer will generate an error message. Therefore if the computer does not generate an
error message, then the program syntax is correct and program execution does not result in
division by zero.
We also need 'the program syntax is correct'. This is the same as 'the program syntax is not faulty'
and we shall call this ‘not p’. Similarly we have not q, not r.
If p is the proposition 'I eat chips' and q the proposition 'I eat salad'
then the conjunction of the propositions is 'I eat chips AND I eat salad' ( I eat chips and salad)
In symbols we write this p ∧ q.
The new proposition is true precisely when p and q are both true.
The disjunction of the propositions is 'I eat chips OR I eat salad' ( I eat chips or salad)
In symbols we write this p ∨ q.
In symbolic logic p ∨ q will always mean p or q or both. The proposition p ∨ q is true when either
p or q or both p and q are true. (Otherwise we have to use the exclusive or.)
The negation of a proposition p, written ¬p,( or sometimes ~ p) is the proposition which is false
whenever p is true and true whenever p is false.
Example
p : it is raining q : it is cold r : it is Monday
P q p∧q p q p∨q p ¬p
T T T T T T T F
T F F T F T F T
F T F F T T
F F F F F F
Two compound propositions are logically equivalent if they always have the same truth value.
Page 34 Logic
MATH1179
Example
p : the programming team is happy;
¬p : it is not true that the programming team is happy
¬¬p : it is not true that it is not true that the programming team is happy
which is equivalent to the programming team is happy.
So, ¬¬p and p are logically equivalent. We write p ≡ ¬¬p.
Example
Using a truth table show that ¬(p ∧ q) ≡ ¬p ∨ ¬q.
We have to show that for all the different truth values for p and q, the two compound propositions
have the same truth value.
We construct a table as shown below. In the columns headed p and q we put all the different
combinations there are for truth values for p and q. In the next column we write down the resulting
truth values for p ∧ q; in the next column the truth values for ¬(p ∧ q). Similarly we find the truth
values for ¬p ∨ ¬q.
To show the compound propositions ¬(p ∧ q) and ¬p ∨ ¬q are equivalent we compare the truth
values.
In this case they are identical and so the compound propositions are equivalent.
p q p∧q ¬(p ∧ q) ¬p ¬q ¬p ∨ ¬q
T T T F F F F
T F F T F T T
F T F T T F T
F F F T T T T
This means that 'it is not true that I got over 40% and failed' is equivalent to 'I did not get over 40%
or I passed'.
Example
Show that p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r)
p Q R q∧r p ∨ (q ∧ r) p∨q p∨r (p ∨ q) ∧ (p ∨ r)
T T T T T T T T
T T F F T T T T
T F T F T T T T
T F F F T T T T
F T T T T T T T
F T F F F T F F
F F T F F F T F
F F F F F F F F
A tautology - t - is a proposition which is always true eg p ∨ ¬p; it is wet or dry.
A contradiction - f - is a proposition which is always false eg p ∧ ¬p; it is wet and dry.
Example
Show that ¬a ∨ (a ∧ b) ≡ ¬a ∨ b
a B ¬a a∧b ¬a ∨ (a ∧ b) ¬a ∨ b
T T F T T T
T F F F F F
F T T F T T
F F T F T T
Implication
This logical connective is of the form ' p implies q ', or ' if p then q '.
p is called the premise and q the conclusion. We write this p → q.
To construct the truth table for the implication consider the example
'if I want some bread then I shall go to the supermarket'
Page 36 Logic
MATH1179
The only time this statement is false is when the conclusion (I go to the supermarket) is false but the
premise (I want some bread) is true.
The last but one line of this table may seem confusing (the premise is false but the conclusion is true),
but it fits in with our example. An alternative equivalent formula is sometimes easier to work with.
p q ¬p ¬p ∨ q
T T F T
T F F F
F T T T
F F T T
An argument is valid if whenever all the premises p1, p2, ..., pn are true then the
conclusion q is true.
An argument is a fallacy if there is a case when all the premises p1, p2, ... , pn are
true and the conclusion q is false.
Notation We replace the word therefore by the symbol ├ , so the argument becomes
w→s, w∨s s
To test the validity of the argument we construct a truth table showing the original propositions, each
of the premises and the conclusion
We can see from the table that whenever the premises are true the conclusion is true.
Here we have a VALID argument
Example
If Harry doesn't practice the piano he will fail Grade 8. Harry fails Grade 8 therefore he didn't practise.
Identify the propositions: p: Harry practises the piano and g: Harry fails grade 8
We can see from the table that although the conclusion is true when the premises are true in the third
line, in the first line the conclusion is false. An argument is only valid if whenever the premises are
true the conclusion is true so this argument is a FALLACY.
Example
If I study then I will not fail Mathematics. If I do not play football then I study.
But I failed Mathematics. Therefore I played football
Take as the propositions
s : I study; m : I fail Mathematics; b : I play football
premise 1 If I study then I will not fail Mathematics. s → ¬m
premise 2 If I do not play football then I study. ¬b → s
premise 3 But I failed Mathematics. m
Conclusion Therefore I played football. b
and or complement
p∧q p∨q ¬p ~p f t
P∩Q P∪Q P P´ ∅ U
P.Q P+Q P 0 1
(P∩Q)' = P′ ∪ Q'
¬(p∧q) = ¬ p ∨ ¬ q
(P.Q)´ = P´+Q´
Electronic Gates
Electronic switches and gates are designed to operate the same way as logical connectives. These
gates have inputs from wires carrying voltages; the convention is that a low voltage is treated as 0
and a high voltage as 1. Truth tables for propositions are written in terms of T and F however when
dealing with circuits we shall replace T by 1 and F by 0.
Page 40 Logic
MATH1179
The OR gate receives inputs from two wires carrying voltages and works likes the logical connective
∨. If the inputs are p and q, we write the output as p+q rather than p∨q.
The AND gate works like the logical connective ∧. The output is written p.q instead of p∧q.
The NOT gate has one input and behaves like ¬. If the input is p, the output is p .
Example
p r
q
p p
q q
The following circuit finds the sum and carry p q carry sum
0 0 0 0
s = ( p + q ) .( p .q ) 1 0 0 1
c = p.q 0 1 0 1
1 1 1 0
x + 3 = 8 is a one place predicate, it may or may not be true depending on the value of one object x. If
we give x a specific value then the statement becomes a proposition.
For example, 2 + 3 = 8 F or 5 + 3 = 8 T
We can see that when x has the value 5 the proposition is true and we say 5 satisfies the predicate. The
predicate can also be thought of as a propositional function using the notation p(x).
Example
q(x) : x is an even number r(x) : x is less than 8
Page 42 Logic
MATH1179
Quantification
Universal quantifiers are used to express the concept that a predicate is being stated for all possible
values of the object x.
Example
Everyone loves maths, is the extension of x loves maths to include all possible people and can be
expressed
For every x, x loves maths.
We use the symbol ∀ to denote for every or for all. There is an implied universal set called the
universe of discourse underlying the use of this quantifier. For instance when we say everyone we
are implying a universe of discourse that might be all the people in the world, everyone we know or
all the students in a class.
Example
p(x) : x has red hair. ∀ x p(x) would mean everyone has red hair, this is more likely if the universe
of discourse underlying this statement was not the world population but rather a particular family.
Example
All mathematicians are highly intelligent has to use 2 predicates
Existential quantifiers are used to state that at least one object exists that satisfies the predicate.
It is used to translate sentences such as ' there is a number that is greater than 8 '. Note than unless the
universe of discourse here were to be natural numbers less than 10 there would be more than one value
of x satisfying the predicate. The symbol used to state existential quantification is ∃ for there exists or
at least one.
Example
If s(x) : x has a sports car , then ∃ x s(x) could be translated as
there exists an x such that x has a sports car
or equivalently if we are considering x from a set of people – there is someone with a sports car, or at
least one person has a sports car or even some people have sports cars
Two-place predicates
The same ideas can be used to generate predicates with more than one variable. These are written in the
same way using an ordered pair.
Consider the proposition 'Programming team A is happy with specification 2.' Suppose we want to
change this so that it deals with a variety of programming teams and a variety of specifications.
We create a function h(p,s) : Is Happy with, which takes two values, 'p' from the set of programming
teams and 's' from the set of specifications and gives truth values T and F dependent on both these
variables It can be thought of in a similar way to a programming function
IsHappyWith(p,s) returns the boolean value true or false (i.e has truth values T or F).
So if team A is happy with spec 2 but unhappy with spec 1 we would write
IsHappyWith(teamA, spec2) = true IsHappyWith(teamA, spec1) = false
Examples
Let S be a set of programs under discussion. We can define the predicate Compiles(s) which takes
the value T, true
if program s ∈ S compiles.
Example
We define k(x,y) : x knows y. Take the universe of discourse as a village, we could say
Sentences written like this will have truth values dependent on the values each variable takes.
Page 44 Logic
MATH1179
Example
Given the universe of discourse is N the natural numbers, ∀ x ∀ y(x = y) is clearly false i.e ≡ f
but ∀ x ∃ y(y>x)≡ t.
Applications to Programming
Prolog (PROgramming in LOGic) is a logic programming language which uses Horn clauses and
resolution calculus and may be covered in detail later in the AI course in the final year of the degree. The
definitions given here are just for interest.
Definition A Horn clause is a formula in clausal form restricted to having just one non negative
literal.
6.1 Relations
Consider the set of students S = {Simina, Helen, Jawed, Stephen, Aysha, Jo } on the second year of
a programme of study.
They choose up to two electives from the set of courses, C = {G23, G67, N45, H43} as follows:
SIMINA
HELEN G23 The relation is many-many as each
student may take many courses and
JAWED G67
each course may be taken by many
STEPHEN N45 students.
AYSHA H43
JO
In general terms, a relation between sets A and B is a rule which associates some of the elements in
the set A with some of those in B. It is a subset of the product set A × B (as discussed in semester
one).
If you are familiar with database concepts you will see a one-many relation illustrated here in Access
Page 46 Functions
MATH1179
There are problems in Access when modelling many-many relations.
Given a relation we can define two mappings, called the projection mappings or projection
functions. They are designed to pick out one half of the ordered pair so that
p1
A
A×B p1 : A × B → A ,
p2 : A × B → B ,
p 1 (a, b) = a,
p 2 (a, b) = b
p2 B
In the example above, p 1 (Simina, G67) = Simina, p 2 (Stephen, H43) = H43
Now p 2 (Simina, G67) = G67, p 2 (Helen, G67) = G67 and p 2(Aysha, G67) = G67
Therefore
p 2 –1(G67) = {(Simina,G67),(Helen, G67), (Aysha, G67)}
Using the ideas of composition of functions which we shall deal with next,
pair
p1-1 p2-1
s
p1 p2
S C
student course
s relation
Similarly p2 ο p1-1 (Aysha) finds the subset of courses that Aysha has chosen.
So p2 ο p1-1 (Aysha) = p 2 { (Aysha, G23), (Aysha, G67) }
= {G23, G67}
This sort of operation is accomplished in a database with queries to pick out the fields you want using
the fields you know, e.g. From the above Access tables we can see that we could start with a customer
and find all the second hand cars they have owned.
Mathematics for Computer Science Page 47
MATH1179
6.2 Functions
A function is a rule which associates with each element of a set A, called the domain of the function,
exactly one element of another set B.
Example
A = subset of lecturers in the School of Computing and Mathematical Sciences, B = subset of rooms
at Greenwich Maritime Site. Each of the lecturers in the set A has a room which is in the set B and
so we have a rule linking the elements of A with the elements of B. We can see this clearly in a
diagram.
Finney 237
Fryer 305
Domain A Taft Co-domain B
523
Petridis
100
This is an example of a function, since every lecturer has a room, so every element in A is related to
an element in B - and no lecturer can have two rooms.
In the example above if r is the function from the set A to the set B,
then
the image of Fryer is 305
or r : Fryer → 305,
or r(Fryer) = 305
Example k u
1
v
Let A = { 1, 2, 3, 4}, B = { u, v, w, x, y, z}. w
2
3 x
A function k : A → B is defined by y
4
k(1) = w, k(2) = x, k(3) = x, k(4) = z. z
Page 48 Functions
MATH1179
The set of values in the co-domain B which are images of f is called the range or the image set of
the function f.
In the previous example, the range of k is {w, x, z}. In the example of the lecturers’ rooms, 100 is in
the co-domain but not in the range of r as none of the lecturers have that room.
The use of the co-domain and range allow you to declare types (e.g. currency) for the output of your
functions in programming before the computer produces actual values for the output (e.g. £3.45).
Classification of Functions
Let f be a function from the set A to the set B.
f is one-one (or injective) if no two elements in the domain are sent to the same element in the
co-domain. If this is not the case f is called many-one.
Example
A function which maps every student to their address is many to one (assuming there are at least 2
students sharing accommodation.)
Example
A function which maps every student to their registration number is one to one.
f is onto (or surjective) if every element in B is the image of an element in A. In this case the range
of f and the co-domain B are the same sets. If the range of the function is not the same as the co-
domain then f is said to be into.
range
A f B co-domain
A f B
f is onto f is into
range = co-domain range ⊂ co-domain
Example
A function from UoG students to the set of home addresses which maps every student to their home
address is onto (assuming an address is held when a student lives there.)
We note that to decide whether a function is onto or into, the domain and co-domain have to be stated
clearly.
From a programming point of view we can think of the function as a ‘black box’ turning inputs into
outputs.
inputs outputs
A function ran f
Composition of Functions
When programming or using formulae in databases or spreadsheets we often need to nest our
functions.
e.g. E1 =AVERAGE(MAX(C1:D1),MAX(A1:B1))
We have already considered the use of brackets to ensure that arithmetic operations are carried out in
the desired order, now we look at ways of describing putting 2 or more functions together.
A f B g C
1
w p
2
x q
3
y r
4
z
Note the order for g ο f, f first then g. Also the range of f must be a subset of the domain of g.
Page 50 Functions
MATH1179
Inverse of a function
Suppose that f is a 1-1 function from A onto B, we can define a function from B to A which "undoes"
f. We call this function the inverse of f and write it f -1
Dom -Domain
inputs outputs
f Ran - Range
dom f ran f =
-
= ran f dom f -
f -1
inputs
outputs
Example
A = { 1, 2, 3}, B = { x, y, z}. Define the function f : A → B, f(1) = z, f(2) = x, f(3) = y
Then
1 z
f -1 : B → A, f -1(x) = 2, f -1(y) = 3, f -1(z) = 1 2 x
3 y
Examples
1 f(n) = 5n - 4, n ∈ N. So f(1) = 5 × 1 - 4 = 5 - 4 = 1
f(2) = 5 × 2 - 4 = 10 - 4 = 6
f(5) = 5 × 5 - 4 = 25 - 4 = 21
2 g(n) = 4n 2 + 1, n ∈ N. So g(1) = 4 × 1 2 + 1 = 4 + 1 = 5
g(3) = 4 × 3 2 + 1 = 36 + 1 = 37
g(6) = 4 × 6 2 + 1 = 144 + 1 = 145
inputs outputs
×2 +1
f
dom f ran f =
= ran f -1 dom f -1
f -1 ÷2 -1
outputs inputs
To find the inverse function, think of the inverse operations in reverse order.
So here we have f: x→ 2x + 1
Graphs of Functions
co-domain
We can illustrate a function by using a graph.
u
We put the elements of the domain on the
horizontal axis and the elements of the co- v
domain on the vertical axis.
w
Let A = { 1, 2, 3, 4}, B = { u, v, w, x, y, z}.
The function k : A → B is defined by x
k(1) = w, k(2) = x, k(3) = x, k(4) = z.
y
The graph of this function is shown.
Note We cannot ‘join the dots’ as we only know z
the values of the function for 1,2,3 & 4 (we have
a discrete set of data points) 1 2 3 4 domain
With numerical data you can use a spreadsheet chart facility to draw the graph.
The one shown is f : A→B where A = {1,2,3,4} and B = {1,2,3,4,5,6,7,8,9,10}.
Page 52 Functions
MATH1179
Here f : x→2x or f(x) = 2x. As the domain of f only contains four members there are only four points
on the graph.
Note do not use a chart type which gives a continuous line when you have discrete data.
Example
f : R → R, f(x) = 2x + 1.
Using y to represent the image of x, this is usually written
y = 2x + 1, x ∈ R
To draw the graph, first find some points on the line by choosing values of x and working out the
corresponding values of y.
y
x 0 1 2 5
y 1 3 5 4
2
1
0 1 2 x
Note These points can be joined because the function is defined for all real values of x, i.e. the whole
axis, so we can calculate values like f(1.73) = 4.46
The graph of any function of the form f: R → R where f(x) = mx + c where m and c are given
numbers and R is the set of real numbers, is a straight line with m as the measure of the slope or
gradient and c the intercept where the line crosses the y axis.
y f(x) = 2x+1
or y = 2x+1
slope = 2/1 = 2
5
4 intercept = 1
2
1
0 1 2 x
Page 54 Functions
MATH1179
Prediction and inversion
Graphs can be used to predict values outside those from the given domain, and also to find values
from the inverse function f -1
y = x2 – 3x + 2
These are of the form f(x) = ax 2 + bx + c, where a, b and c are given numbers.
Note: There might not be an x term or a constant, c, but there must be an x2 term in a quadratic
function.
So, f(x) = x2 + 3, f(x) = 5x2 + 7x, f(x) = 2x2 +5x - 7.5 are all quadratic functions
But, f(x) = x3-2 and f(x) =2x +7 are not.
y = 2x
y = 2-x
Page 56 Functions
MATH1179
Logarithms
(You will occasionally need logs in other parts of the programme)
8 = 23 We say that the logarithm of 8 to base 2 is 3 log 2 8 = 3
32 = 2 5 So logarithm of 32 to base 2 is 5 log 2 32 = 5
81 = 3 4 So logarithm of 81 to base 3 is 4 log 3 81 = 4
1 1 1 1
= 4 = 2 − 4 So logarithm of to base 2 is – 4 log 2 = -4
16 2 16 16
Logarithms to base 10 are on your calculator usually near 10x under log. Logarithms to base e,
sometimes known as natural logarithms, are on your calculator usually near ex under ln.
log b x
To find logs to any other base you need the rule: log a x =
log b a
Using a calculator,
log 10 3 = 0.4771 and log 10 2 = 0.3010. So log 2 3 = log10 3 = 0.4771 = 1.585
log10 2 0.3010
This enables us to draw the graph of y = ln x simply using reflection of y = ex in the line y = x.
Trigonometric Functions
360 2π
degrees radians
These are used in Computer Systems and Networking in the discussion of data transmission.
Radian Measure
Angles can be measured in degrees or radians. There are 360 degrees in a circle and 2π radians in a
circle.
So 2π radians = 360 degrees
∴ π radians = 180 degrees
180 π
So 1 radian = degrees = 57.3 degrees and 1 degree = radians = 0.017 radians.
π 180
You will find both these systems on your calculator so make sure you know which one you should
be using.
Example
(i) Convert to degrees 3.2 radians : π radians = 180 degrees
180
∴ 1 radian = degrees
π
180
So 3.2 radians = 3.2 × degrees = 183.3 degrees
π
3π
(ii) Convert to degrees radians : π radians = 180 degrees
4
Page 58 Functions
MATH1179
180
∴ 1 radian = degrees
π
3π 3π 180
So radians = × degrees = 125 degrees
4 4 π
(iii) Convert to radians 30 degrees 180 degrees = π radians
π
1 degree = radians
180
π π
So 30 degrees = 30 × radians = radians = 0.52 radians
180 6
Note:
arc length 1:2π
Suppose we have a circle of radius 1. The
circumference of the circle is 2πr in this case
this is the same as 2π and the angle at the 1
angle 1: 2π radians
centre is 2π radians. So if we have an arc of
length 1, using ratios then the angle at the 1
centre is 1 radian.
Graph of y = sin t
-2π -π 0 π 2π 3π 4π t
Graph of y = cos t
-2π 0 π 2π 3π 4π t
-π
The maximum value is +1 and the minimum value–1. The amplitude of the sine function is half the
difference between its maximum and minimum values. The amplitude of sin t is 1.
These functions are periodic since they repeat themselves over and over again. The period is the
length of one cycle (or time interval if t is time), that is, 2π.
Given the basic sine curve we can manipulate it to represent different functions.
y = sint
0 2π 4π
-1
Putting a scaling factor, A, in front of the function alters the amplitude i.e. the peaks and troughs go
between ±A.
Page 60 Functions
MATH1179
Adding a factor, m, in front of the variable t scales the function along the x axis, compressing the sine
curve so that it oscillates more if m>1 and elongates if m<1
y = A sin(mt)
0
2π 4π
-A
If we take m = 2π then the values of t, which for signals is usually time in seconds, will be in exact
units at the critical points of the curve.
y = sin(2πt)
0 1 2
t in sec
-1
Amplitude = 1 period = 1 (1 cycle) frequency = =1
Combining the transformations from the last two examples, the usual way of writing a signal
mathematically is to put in the form s(t) = A sin(2πft), where s(t) is the signal at time t.
A
t in sec
s = Asin2πft
0
-A
measured in Hertz)
3
t in sec
1 2 s = 3sin2πt
0
-3
Amplitude = 3 period = 1 frequency =1
3
t in sec
s = 3sinπt
0 2 4
-3
3
t in sec
1 s = 3sin4πt
0 0.5
-3
Amplitude = 3 period = 1/2 frequency =2
Page 62 Functions
MATH1179
Examples
As the signal can be written as s = A sin (2πft) where A is the amplitude and f the frequency.
So s = 6 sin (8πt) has amplitude 6 and frequency 4,
s = 4 sin (πt) has amplitude 4 and frequency 0.5.
Phase angle
Sometimes the curve is shifted along the axis by a constant amount, θ, which moves all the points
along.
The graph shows the comparison of s = sin 2πt and a second curve s = sin 2π(t +0.25)
which has been moved back along the axis by 0.25
0.5
s 0
0 0.5 1 1.5 2 2.5 3
-0.5
-1
-1.5 t
7: Complex Numbers
We have already covered the solving of simple quadratic equations by factorisation and using the
formula. Plus we have also looked a number systems: integers, real, etc.
Examples
x2 +7x +10 = 0 2x2 -7x -4 = 0 2x2 +5x +1 = 0 Formula for solving a
(x+2 )(x+5) = 0 (2x+1 )(x-4) = 0 a= 2, b= 5, c=1 quadratic
either either −5± 25 − 8 𝑎𝑎𝑥𝑥 2 + 𝑏𝑏𝑏𝑏 + 𝑐𝑐 = 0
x=
x+2 = 0 2x+1 = 0 4 Therefore
x = -2 x = -1/2 − 5 ± 17 −𝑏𝑏 ± √𝑏𝑏 2 − 4𝑎𝑎𝑎𝑎
x=
4 𝑥𝑥 =
or or 2𝑎𝑎
− 5 + 4.12 = -0.22
x+5 = 0 x-4 = 0 x=
4
x = -5 x=4 or
− 5 − 4.12 =-2.28
x=
4
Example 0
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4
x2 + x + 1 = 0 so a = 1, b = 1, c = 1 -10
−1± 1− 4 so x =
−1± − 3 50
x=
2 2
40
Here we see b2 – 4ac (known as the discriminant)
is -3 which cannot have a real square root.
30
0
It was to deal with situations like this that a new
-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
extension to the number system was defined.
Definition
A complex number is an expression of the form 𝑧𝑧 = 𝑎𝑎 + 𝑏𝑏 𝑖𝑖 where 𝑎𝑎 and 𝑏𝑏 are real numbers and
𝑖𝑖 = √−1. The number 𝑎𝑎 is called the real part, denoted Re(z) and 𝑏𝑏 is called the imaginary part,
denoted Im(z). Note: 𝑖𝑖 2 = −1, 𝑖𝑖 is generally used for √−1 by mathematicians and 𝑗𝑗 commonly used
by engineers. For example, simplify the following:
√(−1) = 𝑖𝑖, √4 = ±2, √25 = ±5
and
√−36 = √36 × −1 = √36 × √−1 = ±6𝑖𝑖 .
Complex numbers are added, subtracted, multiplied and divided in the manner that you would think
from elementary algebra.
Example
Multiplication
Here you must remember to multiply all parts of the two complex numbers together.
Example
= 15 + 6i – 5i – 2i2 = 15 + i – 2(–1) = 15 + i +2 = 17 + i
Division
As we have already seen with fractions and matrices sometimes a different technique is used to deal
with division turning it into a multiplication.
When two complex numbers are divided we multiply top and bottom of the division by the complex
conjugate of the divisor. The complex conjugate of a + bi is a – bi. This has the effect of making the
denominator real.
Example 5 − 2i 5 − 2i 3 − 6i
= ×
3 + 6i 3 + 6i 3 − 6i
Argand Diagram
Imaginary Axis
Given a complex number 𝑧𝑧 in its Cartesian form,
𝑧𝑧 = 𝑥𝑥 + 𝑖𝑖𝑖𝑖, the Real part of z is x, and the Imaginary
y z
part of z is y. This point can be plotted on an Argand
diagram
θ is called the argument of z and is written as:
−1
θ = arg(z) = tan ( y / x)
r is called the modulus of z and is written as:
r = z = ( x2 + y 2 )
x Real Axis
6
-3 + 5i 5
4 2 + 3i 4 + 3i
3
r
2
1
0 θ
-6 -5 -4 -3 -2 -1 1 2 3 4 5 6
-1
-2
-3
4 - 2i
-5 - 3i -4
-5
-6
We can see 5 complex numbers marked here are −3 + 5𝑖𝑖, 2 + 3𝑖𝑖, 4 + 3𝑖𝑖, 4 − 2𝑖𝑖 and −5 − 3𝑖𝑖.
Definition
Given a complex number 𝑧𝑧 = 𝑎𝑎 + 𝑖𝑖𝑖𝑖 the modulus, 𝑟𝑟, is given by a 2 + b2
𝑏𝑏
the tangent of the argument, 𝑡𝑡𝑡𝑡𝑡𝑡 𝜃𝜃 = 𝑎𝑎
𝑎𝑎 𝑏𝑏
𝑧𝑧 can then be rewritten using the fact that cos 𝜃𝜃 = 𝑟𝑟
and sin 𝜃𝜃 = 𝑟𝑟
Leading to
𝑧𝑧 = 𝑟𝑟 𝑐𝑐𝑐𝑐𝑐𝑐 𝜃𝜃 + 𝑟𝑟 𝑖𝑖 𝑠𝑠𝑠𝑠𝑠𝑠 𝜃𝜃 = 𝑟𝑟 (𝑐𝑐𝑐𝑐𝑐𝑐 𝜃𝜃 + 𝑖𝑖 𝑠𝑠𝑠𝑠𝑠𝑠 𝜃𝜃)
this is called the polar form of a complex number sometimes written rcisθ for short or r∠θ o
The exponential (Euler’s) Form of z is 𝑧𝑧 = 𝑟𝑟𝑒𝑒 𝑖𝑖𝑖𝑖 Use radions for 𝜃𝜃.
Example
We have marked z = 4 + 3i,
3
so r = 4 2 + 32 = 16 + 9 = 25 = 5 , tan θ = so, θ = tan-1 (0.75) = 36.9o
4
z = 5(cos36.9 + isin36.9)
Complex Conjugate
Let𝑧𝑧 = 𝑥𝑥 + 𝑖𝑖𝑖𝑖, then the conjugate of z is denoted as 𝑧𝑧̅ and is written as 𝑧𝑧̅ = 𝑥𝑥 − 𝑖𝑖𝑖𝑖
Example
If z = 3 − 2i , the conjugate of z, z is given as z = 3 + 2i y
Here r = z = (3) 2 + (−2) 2 = 13 3
x
y −2 o
θ = arg( z ) = tan ( ) = tan −1 ( ) = 326.3
−1
x 3
-2
z is sometimes written in the polar form r∠θ o
o
In polar form, we can write, 13 ∠ 326.3 [ θ o in Degrees]
2
Note: 1) zz = ( x + iy )( x − iy ) = x 2 + y 2 = r 2 = z 2) z is always positive
Example z1-z2 y
If z1 = 3 + 2i and z2 = 5 − 9i , find
(i) z1 + z2 , (ii) z1 − z2 , (iii) z2 − z1 -z2
Solution
(i) z1 + z2 = (3 + 2i ) + (5 − 9i )
z1
= 3 + 5 + 2i − 9i
x
= 8 − 7i
(ii) z1 − z2 = (3 + 2i ) − (5 − 9i ) -z1
= 3 − 5 + 2i − −9i
= − 2 + 11i
(iii) z2 − z1 = (5 − 9i ) − (3 + 2i ) z1+z2
z2
= 5 − 3 − 9i − 2i
= 2 − 11i z2-z1
Now find − z2 − z1 and roughly plot on the
Argand diagram.
Example
2
If z1 = 3 + 2i and z2 = 5 − 9i , find (i) 3z1 , (ii) z1 z2 , (iii) z1
Example
1) Show that (2 + 3i )(3 + 4i ) = −6 + 17i
(2 + 3i )(3 + 4i ) = 6 + 9i + 8i − 12
= − 6 + 17i
Example
To find 4 − 3i the division can be written as: 4 − 3i 4 − 3i 5 + 2i
= ×
5 − 2i 5 − 2i 5 − 2i 5 + 2i
𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁
In General, if we have to do 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷
, this can be performed by:
𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝑂𝑂𝑂𝑂 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷
= ×
𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝑂𝑂𝑂𝑂 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝑛𝑛𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎
Hence, 4 − 3i = 4 − 3i × 5 + 2i = 4 × 5 − 3i × 5 + 4 × 2i − 3 × 2i
5 − 2i 5 − 2i 5 + 2i 5 × 5 − 2i × 2i
26 7
= − i
29 29
Example
Find 7 + 2i
8 − 5i
7 + 2i 7 + 2i 8 + 5i 7 × 8 + 2i × 8 + 7 × 5i + 2i × 5i
= × =
8 − 5i 8 − 5i 8 + 5i 8 × 8 − 5i × 5i
Quadratic Equations
Consider the following example of a quadratic equation of the form ax 2 + bx + c = 0 , and solve to
find the two roots.
Example
z2 + z + 1 = 0 , here a=1; b= 1; c=1.
− b ± b 2 − 4ac
Using z = substituting the values of a, b and c, we get
2a
−1± 1− 4 −1 3
z= = ± i
2 2 2
−1 3 −1 3
Which gives the two complex roots of the quadratic equation as z1 = + i and z2 = − i.
2 2 2 2
Example
Sketch on an Argand diagram and express in Polar and Exponential Forms the following complex
numbers z1 = 1 + i , z2 = −2 − 4i .
[ ] [ ]
1 1
r1 = (1) + (1) r2 = (− 2 ) + (− 4 )
2 2 2 2 2 2
= 2 = 20
z1
1 −4 r1
θ1 = tan −1 = 45° θ 2 = tan −1 = 63.4°
1 −2 θ1
θ2 is in the 3rd quadrant θ2
θ2 = 180+63.4=243.4°
θ1 = π radians θ2 = 4.25 radians (2d.p.)
4
r2
In Polar form, r∠θ , z1= 2 ∠ 45o , z2= 20 ∠ 243.4o
iπ
In Exponential form, z1= 2e 4 , z2= 20e 4.25i
z2
Pattern matching
Given a constant term and a pattern term, where the pattern term has variables, pattern matching
involves finding a variable assignment that will make the two terms match.
Starting with terminology, terms are built from constants, variables and function applications:
- A lowercase letter represents a constant (such as an integer or a string),
- An uppercase letter represents a variable,
- 𝑓𝑓(. . . ) is a function 𝑓𝑓 with some parameters.
Example
Constant term: 𝑓𝑓(𝑎𝑎, 𝑏𝑏, 𝑔𝑔(𝑡𝑡)) Pattern term: 𝑓𝑓(𝑎𝑎, 𝑌𝑌, 𝑋𝑋)
A simple assignment, a substitution mapping variables to the assigned values, works
𝑌𝑌 = 𝑏𝑏 and 𝑋𝑋 = 𝑔𝑔(𝑡𝑡).
A variable in the pattern is allowed to match anything in the other structure.
Variables can appear multiple times in a pattern:
Constant term: 𝑓𝑓(𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓(𝑎𝑎), 𝑎𝑎, ℎ(𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓(𝑎𝑎)), 𝑥𝑥) Pattern term: 𝑓𝑓(𝑌𝑌, 𝑎𝑎, ℎ(𝑌𝑌), 𝑥𝑥)
Here the right substitution is 𝑌𝑌 = 𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓(𝑎𝑎).
If a variable occurs more than once in the pattern it must match the same thing in each case.
Unification
In logic and computer science, unification is a process of automatically solving equations between
symbolic terms. Unification has several interesting applications, notably in logic programming and
type inference. Unification can be seen as a generalization of pattern matching.
Example
Consider a function such that 𝑓𝑓(𝑥𝑥, 𝐴𝐴) = 𝑓𝑓(𝐵𝐵, 𝑦𝑦).
What must we map to variables 𝐴𝐴 and 𝐵𝐵 so that the two sides have the same value?
Clearly {𝐴𝐴 → 𝑦𝑦, 𝐵𝐵 → 𝑥𝑥}
𝑓𝑓(𝑥𝑥, 𝐴𝐴) = 𝑓𝑓(𝑥𝑥, 𝑦𝑦), 𝑓𝑓(𝐵𝐵, 𝑦𝑦) = 𝑓𝑓(𝑥𝑥, 𝑦𝑦)
Sometimes there’s no mapping possible: 1 = 2 will never unify, nor will 𝑓𝑓(𝑋𝑋) = 𝑓𝑓(𝑋𝑋, 𝑌𝑌).
Example
Consider variables a and b
[a, b] = [1, 2] # succeeds and assigns a=1, b=2
[a, b] = [1, 2, 3] # fails
This example makes unification look a lot like pattern matching, but there are important differences.
What underlies the differences is this: unification is two-sided and works on logical variables, while
pattern matching is one-sided and works on normal programming-language variables or normal
single-assignment variables.
A logical variable is like a single-assignment variable but it can be used before it is assigned a value.
In other words, it can be put in a structure, passed to a function, returned from a function, or have a
predicate applied to it before ever being assigned. Pattern matching works on normal programming-
language variables--mutable variables, or normal single-assignment variables.
Matches a syntactic pattern to a data object. Matches a data object to a data object.
one-sided: Only assigns to variables on the left. two-sided: Assigns in both directions.
lexically restricted: Only assigns to variables that lexically unrestricted: Assigns to variables that do
occur syntactically in the statement. not occur syntactically in the statement.
A single pattern can match different terms with A term can only match another term with the same
different structures. structure.