0% found this document useful (0 votes)
15 views115 pages

Bagdasar - Concise Computer Mathematics Tutorials On Theory and Problems

This document is a publication in the SpringerBriefs in Computer Science series authored by Ovidiu Bagdasar, focusing on Concise Computer Mathematics. It serves as a tutorial resource for undergraduate computing students, covering essential mathematical concepts such as Discrete Mathematics, Logic, Linear Algebra, and Special Topics. The book aims to enhance students' understanding and problem-solving skills through a structured approach to learning mathematics relevant to their studies.

Uploaded by

gxsteph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views115 pages

Bagdasar - Concise Computer Mathematics Tutorials On Theory and Problems

This document is a publication in the SpringerBriefs in Computer Science series authored by Ovidiu Bagdasar, focusing on Concise Computer Mathematics. It serves as a tutorial resource for undergraduate computing students, covering essential mathematical concepts such as Discrete Mathematics, Logic, Linear Algebra, and Special Topics. The book aims to enhance students' understanding and problem-solving skills through a structured approach to learning mathematics relevant to their studies.

Uploaded by

gxsteph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 115

SPRINGER BRIEFS IN COMPUTER SCIENCE

Ovidiu Bagdasar

Concise Computer
Mathematics
Tutorials on Theory
and Problems
SpringerBriefs in Computer Science

Series Editors
Stan Zdonik
Peng Ning
Shashi Shekhar
Jonathan Katz
Xindong Wu
Lakhmi C. Jain
David Padua
Xuemin Shen
Borko Furht
V. S. Subrahmanian
Martial Hebert
Katsushi Ikeuchi
Bruno Siciliano

For further volumes:


http://www.springer.com/series/10028
Ovidiu Bagdasar

Concise Computer
Mathematics
Tutorials on Theory and Problems

123
Ovidiu Bagdasar
University of Derby
Derby
UK

ISSN 2191-5768 ISSN 2191-5776 (electronic)


ISBN 978-3-319-01750-1 ISBN 978-3-319-01751-8 (eBook)
DOI 10.1007/978-3-319-01751-8
Springer Cham Heidelberg New York Dordrecht London

Library of Congress Control Number: 2013947356

 The Author(s) 2013


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of
the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or
information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar
methodology now known or hereafter developed. Exempted from this legal reservation are brief
excerpts in connection with reviews or scholarly analysis or material supplied specifically for the
purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the
work. Duplication of this publication or parts thereof is permitted only under the provisions of
the Copyright Law of the Publisher’s location, in its current version, and permission for use must
always be obtained from Springer. Permissions for use may be obtained through RightsLink at the
Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this
publication does not imply, even in the absence of a specific statement, that such names are exempt
from the relevant protective laws and regulations and therefore free for general use.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for
any errors or omissions that may be made. The publisher makes no warranty, express or implied, with
respect to the material contained herein.

Printed on acid-free paper

Springer is part of Springer Science+Business Media (www.springer.com)


Foreword

It is a great pleasure for me to write a short Foreword to this publication which


adds to the SpringerBriefs in Computer Science series. The work has arisen from
the efforts of colleague Dr. Ovidiu Bagdasar in bringing together various topics
from past modules and developing material in order to deliver a new module made
available for our Year 1 computing students here at the University of Derby.
Entitled Computational Mathematics, it introduces fresher students to some fun-
damental concepts designed to give them a firm mathematical footing on which to
base their studies and from which Dr. Bagdasar has produced a programme of
tutorials in booklet form here. The chosen areas—Discrete Mathematics, Logic,
Linear Algebra and Special Topics—will lead any student through both theory and
a series of problems and exercises, consolidating some of the work typically
covered in class but also offering independent study options.
Concise Computer Mathematics: Tutorials on Theory and Problems should
prove to be a valuable resource for undergraduate students of computing in gen-
eral, and computer science in particular. As an educational tool its aim is to foster
knowledge, understanding and improved confidence in a systematic way, and I
wish the publication every success.

Derby, July 2013 Peter J. Larcombe

v
Preface

Overview and Goals

Through the ages, mathematics has established itself as ‘‘the door and key to the
sciences’’ (in the words of Roger Bacon, 1214-1294) and since antiquity it rep-
resented one of the key areas of interest for human knowledge. However, Euclid is
said (following Proclus) to have replied to King Ptolemy’s request for an easier
way of learning mathematics that ‘‘there is no Royal Road to geometry’’. To most
students’ despair, Mathematics still seems to be the tumbling stone for anyone
trying to understand the world around us. Computer Science students make no
exception as they need to master some key areas of mathematics.
This book has its origins in the delivery of a Computational Mathematics
module to a body of undergraduate Computing students. The aim of the module
was to deliver an easily accessible, self-contained introduction to the basic notions
of Mathematics necessary for their degree. The variety of Computing programmes
delivered (Computer Science, Computer Games Programming, Computer Forensic
Investigation, Computer Networks and Security, Information Technology) posed a
real challenge when developing the syllabus for Computational Mathematics. This
book reflects the need to quickly introduce students, from a variety of educational
backgrounds, to a number of essential mathematical concepts.

Organisation and Features

The material is divided into four units: Discrete Mathematics (Sets, Relations,
Functions), Logic (Boolean Types, Truth tables, proofs), Linear Algebra (Vectors,
Matrices and Graphics) and Special topics (Graph Theory, Number Theory, Basic
elements of Calculus). Each teaching unit was used in conjunction with specifi-
cally designed computer-based tests available through the whole semester.
The module was a success as the students engaged well with their work and
improved both the attitude towards their studies and their problem solving skills.
The flexibility offered by the computer-based tests was highly praised in the
feedback gathered from the students.

vii
viii Preface

How to Use the Book

Guidelines for Students

The chapters contain a brief theoretical presentation of the topic, followed by a


short further reading list. The problem section is divided into Essential problems
(which are direct applications of the theory) and Supplementary problems (which
may require a bit more work). Each chapter ends with a Problem Answers section
which either provides correct answers or worked solutions for all the problems.
Most problems are original, but there are some taken from past teaching materials
and whose source is uncertain.

Guidelines for Tutors

Teaching

Each chapter is covered in two hours of lectures and two hours of tutorial.

Assessment

The assessment is based on computer tests and consists of two parts, each worth
50 % of the final mark.

Part I: Four summative online tests covering


• Test 1: Sets, Relations and Functions
• Test 2: Logic and Boolean Types
• Test 3: Vectors, Matrices and Graphics
• Test 4: Graphs, Number Theory, Elements of Calculus.
Each test can be accessed at any time, for multiple times and only the highest
grades over all the valid attempts count towards the final mark.

Part II: Two hours summative test with questions from all subjects.
Acknowledgments

First of all, I would like to thank my family for bearing with a part-time husband
and father during my first year as a Lecturer. My appreciation goes to my mentors:
Richard and Nick—for their excellent guidance and tremendous support through
my first steps as an Academic, Peter and Stuart—for being by my side in every
step of the way. Finally to all my colleagues at the University of Derby.

June 2013 Ovidiu Bagdasar

ix
Contents

1 Sets and Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1


1.1 Brief Theoretical Background . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Essential Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Supplementary Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Problem Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2 Relations and Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9


2.1 Brief Theoretical Background . . . . . . . . . . . . . . . . . . . . . . . 9
2.2 Essential Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.3 Supplementary Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.4 Problem Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.1 Brief Theoretical Background . . . . . . . . . . . . . . . . . . . . . . . 19
3.2 Essential Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.3 Supplementary Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.4 Problem Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

4 Boolean Algebra, Logic and Quantifiers . . . . . . . . . . . . . . . . . . . 27


4.1 Brief Theoretical Background . . . . . . . . . . . . . . . . . . . . . . . 27
4.2 Essential Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.3 Supplementary Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.4 Problem Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

5 Normal Forms, Proof and Argument. . . . . . . . . . . . . . . . . . . . . . 35


5.1 Brief Theoretical Background . . . . . . . . . . . . . . . . . . . . . . . 35
5.2 Essential Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.3 Supplementary Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.4 Problem Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

6 Vectors and Complex Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . 45


6.1 Brief Theoretical Background . . . . . . . . . . . . . . . . . . . . . . . 45
6.2 Essential Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

xi
xii Contents

6.3 Supplementary Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . 52


6.4 Problem Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

7 Matrices and Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55


7.1 Brief Theoretical Background . . . . . . . . . . . . . . . . . . . . . . . 55
7.2 Essential Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
7.3 Supplementary Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . 60
7.4 Problem Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

8 Matrix Applications in Computer Graphics. . . . . . . . . . . . . . . . . 65


8.1 Brief Theoretical Background . . . . . . . . . . . . . . . . . . . . . . . 65
8.2 Essential Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
8.3 Supplementary Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . 69
8.4 Problem Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

9 Elements of Graph Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73


9.1 Brief Theoretical Background . . . . . . . . . . . . . . . . . . . . . . . 73
9.2 Essential Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
9.3 Supplementary Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.4 Problem Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

10 Elements of Number Theory and Cryptography . . . . . . . . . . . . . 81


10.1 Brief Theoretical Background . . . . . . . . . . . . . . . . . . . . . . . 81
10.2 Essential Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
10.3 Supplementary Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
10.4 Problem Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

11 Elements of Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
11.1 Brief Theoretical Background . . . . . . . . . . . . . . . . . . . . . . . 89
11.2 Essential Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
11.3 Supplementary Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
11.4 Problem Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

12 Elementary Numerical Methods . . . . . . . . . . . . . . . . . . . . . . . . . 95


12.1 Brief Theoretical Background . . . . . . . . . . . . . . . . . . . . . . . 95
12.2 Essential Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
12.3 Supplementary Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
12.4 Problem Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Chapter 1
Sets and Numbers

Abstract Sets are defined as collections of objects of the same kind and are among
the fundamental notions in many Mathematical subjects, including Algebra and Cal-
culus. Operations involving sets play a key role in many applications and sets of
numbers have been created for solving numerous problems inspired from real life.
In this chapter we present some of the key concepts in the theory of sets such as
basic set operations, Venn diagrams, set cardinals and motivations for the number
systems.

Keywords Sets · Set operations · Venn diagrams · Number systems

1.1 Brief Theoretical Background

Set Definitions and Notations

• set - collection of similar objects (numbers, symbols, etc)


• x ∈ A - element x is a member of set A
• x ∈ A - element x is not a member of set A
• U - universe - contains all the elements of a kind
• ∅ - empty set - contains no element
• B ⊆ A - inclusion - set B is included or equal to set A (for all x ∈ B, x ∈ A)
• B ⊂ A (or B  A) - strict inclusion - B is included, but not equal to A
• A = B - set equality - (sets A and B are equal when B ⊆ A and A ⊆ B)
• P(A) - power set - set of all subsets of A
• A × B - cartesian product - all pairs (a, b) s.t. a ∈ A and b ∈ B

O. Bagdasar, Concise Computer Mathematics, SpringerBriefs in Computer Science, 1


DOI: 10.1007/978-3-319-01751-8_1, © The Author(s) 2013
2 1 Sets and Numbers

Example: Let A = {a, b} and B = {a, b, c} be two sets. Then

c ∈ B, c ∈ A, A ⊂ B
P(A) = {∅, {a}, {b}, {a, b}},
A × B = {(a, a), (a, b), (a, c), (b, a), (b, b), (b, c)}.

Set Operations

For the sets A and B taken from the universe U we define


• A ∩ B = {x ∈ U : x ∈ Aandx ∈ B} (intersection)
• A ∪ B = {x ∈ U : x ∈ Aorx ∈ B} (union)
• A \ B = {x ∈ U 
: x ∈ Aandx ∈
/ B} (difference)
• AB = (A \ B) (B \ A) (symmetric difference)
• Ac = (A)c = ∼ (A) = (A) = U \ A = {x ∈ U : x ∈/ A} (complement)
Example: For subsets A = {1, 2}, B = {2, 3, 5} of universe U = {0, 1, 2, 3, 4, 5}
the previously defined operations give

A ∩ B = {2}, A ∪ B = {1, 2, 3, 5}, A \ B = {1}, AB = {1, 3, 5}, B c = {0, 1, 4}.

Venn Diagrams

Set and operations can be represented by Venn diagrams like in Fig. 1.1. The universe
U corresponding to two sets (Fig. 1.1a) is divided into four distinct regions, which
can be related to the previously defined set operations.
For example, in the above diagrams for two sets (Fig. 1.1a), A ∩ B represents
region 3, A ∪ B regions 1, 2 and 3, A \ B region 1, AB regions 1 and 2, while Ac
regions 2 and 4. Venn diagrams are also used to represent operations involving three
sets (Fig. 1.1b) or more.

Fig. 1.1 Venn diagrams for a two sets and b three sets
1.1 Brief Theoretical Background 3

Ways to Define Sets

A = {0, 1, 2, 3} (enumeration)
B = {n ∈ N : 3 ≤ n ≤ 100} (ellipsis)
C = {x = n 2 : n ∈ N} (formula)

Ellipsis should only be used when no confusion is possible.

Other Set Properties

• cardinal - the number of elements in a set.


Example: The set {0, 1, 2} has three elements (therefore cardinal is 3).
• finite vs infinite sets (sets have a finite or infinite number of elements)
Example: The set {0, 1, 2} is finite, while the set {0, 1, 2, 3, . . . } is infinite.
• ordered vs not ordered sets (any two elements are comparable or not)
Example: The set {0, 1, 2} (ordered), the set of students (not ordered).

Number Systems and Motivation

Most people know the natural numbers 0, 1, 2, 3, ..., but other number sets exist. The
German mathematician Leopold Kronecker (1850s) was quoted as having said “God
made natural numbers; all else is the work of man”.
His conclusion was based on the fact that starting with the set of natural numbers,
further number sets can be generated by using basic operations (such as subtraction
or division of natural numbers), and also by trying to define sets which accommodate
the solutions of simple equations.

Basic Number Sets

N = {0, 1, 2, 3, . . . } natural numbers (including zero)


N+ = {1, 2, 3, . . . } natural numbers (excluding zero)
Z = {..., −2, −1, 0, 1, 2, . . . } integers (positive and negative)
Q = {q = mn : m ∈ Z, n ∈ N+ } rational numbers (fractions)
R = {a.b : a, b ∈ Z} real numbers (infinite decimals)
R+ = {x ∈ R : x ≥ 0} positive real numbers √
C = {a + bi : a, b ∈ R} complex numbers (i = −1)
4 1 Sets and Numbers

Polynomials

The most simple equations are generated by polynomials.These are simple


mathematical expressions constructed from variables (also called indeterminates)
and constants (usually numbers), using only the operations of addition, subtraction,
multiplication, and non-negative integer exponents.

Motivation for the Number Systems

Examples of polynomials with roots in the various number sets are

1. x −1 =0⇒ x =1∈N
2. x +1 =0⇒ x = −1 ∈ N, x ∈Z
3. 4x − 1 =0⇒ x = 1/4
√ ∈ Z, x ∈Q
4. 2x 2 − 1 =0⇒ x = √2 ∈ Q, x ∈R
5. x2 + 1 =0⇒ x = −1 = i ∈ R, x ∈C

The above examples illustrate the strict inclusions

N  Z  Q  R  C.

The Fundamental Theorem of Algebra (Argand in 1806, Gauss 1816)

Every non-constant single-variable polynomial with complex coefficients has at least


one complex root (real coefficients and roots being within the definition of complex
numbers).
This theorem shows that the complex numbers are sufficient for solving all poly-
nomial equations (like the ones given in the above examples).

1.2 Essential Problems

E 1.1. What is the power set P(A) (set of all subsets) for A = {a, b, c}?

E 1.2. Find the cartesian product of sets A = {1, 2, 3, 6} and B = {a, b, c} (all
pairs whose first component is in A, and second component in B).

E 1.3. (Set operations) Let the sets A, B and C be defined as

A = {‘r ’, ‘e’, ‘s’, ‘t’}


B = {‘r ’, ‘e’, ‘l’, ‘a’, ‘x’}
C = {‘s’, ‘l’, ‘u’, ‘m’, ‘b’, ‘e’, ‘r ’}
1.2 Essential Problems 5

Find the sets and corresponding regions in the Venn diagram Fig. 1.1b

A∪ B A∪C B ∪C
A∩ B A∩C B ∩C
A \ (A ∩ C) B \ (A ∪ C).

E 1.4. Consider the Universe U = {0, 1, 2, 3, 4, 5, 6, 7} and the sets

D = {0, 1, 2}, E = {2, 4, 6}, F = {1, 3, 5, 7}.

Find the sets D \ F, E ∩ F, E ∪ F and (F ∪ D)c and their cardinal.

E 1.5. (a) List the elements of the sets (enumerate or give a formula).

(i) {x ∈ N+ : x is a multiple of 4}
(ii) {x ∈ Z : −2 ≤ x ≤ 3}

(b) List the elements of the following sets (enumerate or give a formula).

A = {x ∈ N+ : x is prime, x ≤ 45}
B = {x ∈ N+ : x is even}
C = {x ∈ N+ : x is odd}
D = {x ∈ N+ : x is a multiple of 6}

Are any of the above sets A, B, C or D subsets of one of the other sets?
(c) Determine which of the following sets are equal

E = {0, 1, 2, 3}
F = {x ∈ N : x 2 < 13}
G = {x ∈ N : (x 2 + 3) < 2}

E 1.6. Solve the following inclusion problems:


(i) If A ⊆ C and C ⊆ B, does it follow that A ⊆ B?

(ii) If A ⊆ C and B ⊆ C, what can you say about A and B?

E 1.7. If A and B are subsets of a universe U , show by constructing examples that


each of the following is true.

(i) (AB)c = (A ∩ B) ∪ (A ∪ B)c


(ii) (A \ B)c = Ac ∪ B
6 1 Sets and Numbers

E 1.8. Show that the following set identities are true, using Venn diagrams.

(i) (A ∩ B) ∪ (A ∩ B)c = U (universal set)


(ii) (Ac ∩ B) ∪ A = A ∪ B

1.3 Supplementary Problems

S 1.1. (Cartesian product)


(a) Find A × B × C for the sets A = {1, 2, 3}, B = {a, b, c} and C = {a, b, c}.
(b) If the sets A, B and C have m, n and p (m, n, p ∈ N+ ) elements respectively,
what is the number of elements of A × B × C?
S 1.2. Prove that if A, B, C are sets from the Universe U , then:

(i) A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) (distributivity of ∩)
(ii) A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C) (distributivity of ∪)
(iii) (A ∩ B)c = Ac ∪ B c (de Morgan law for ∩)
(iv) (A ∪ B)c = Ac ∩ B c (de Morgan law for ∪).

S 1.3. Find the sets A, B with the properties

(i) A ∪ B = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}


(ii) A ∩ B = {3, 4, 5}
(iii) A \ B = {2, 6}

S 1.4. Write the following numbers as fractions

(i) x = 0.333 · · · = 0.(3) = 0.3


(ii) x = 0.23232323 · · · = 0.(23) = 0.23
(iii) x = 0.1129292929 · · · = 0.11(29) = 0.1129

(iv) x = 3.
1.4 Problem Answers 7

1.4 Problem Answers

Essential Problems

E 1.1. P(A) = {∅, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}}.
E 1.2. A × B = {(1, a), (1, b), (1, c), (2, a), (2, b), (2, c), (3, a), (3, b), (3, c),
(6, a), (6, b), (6, c)}.

E 1.3. The set operations give the following sets

A∪B = {‘r ’, ‘e’, ‘s’, ‘t’, ‘l’, ‘a’, ‘x’}; A ∪ C = {‘r ’, ‘e’, ‘s’, ‘t’, ‘l’, ‘u’, ‘m’, ‘b’};
B ∪C = {‘r ’, ‘e’, ‘l’, ‘a’, ‘x’, ‘s’, ‘u’, ‘m’, ‘b’}; A ∩ B = {‘r ’, ‘e’};
A∩C = {‘r ’, ‘e’, ‘s’};
B ∩C = {‘r ’, ‘e’, ‘l’}; A \ (A ∩ C) = {‘s’, ‘t’}; B \ (A ∪ C) = {‘a’, ‘x’}.

The regions are A ∪ B (1, 5, 4, 7, 6, 3), A ∪ C (1, 5, 4, 7, 2, 6), B ∪ C (2, 5, 6, 7, 4, 3),


A ∩ B (4, 7), A ∩ C (5, 7), B ∩ C (6, 7), A \ (A ∩ C) (1, 4), B \ (A ∪ C) (3).

E 1.4. D \ F = {0, 2}; E ∩ F = ∅; E ∪ F = {2, 4, 6, 1, 3, 5, 7}; (F ∪ D)c = {4, 6}.


The cardinals are 2, 0, 7 and 2.

E 1.5. (a) (i){4, 8, 12, . . . } = {4n : n ∈ N+ }; (ii){−2, −1, 0, 1, 2, 3}.


(b) A = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43}
B = {2, 4, 6, 8, . . . } = {2n : n ∈ N+ }
C = {1, 3, 5, 7, . . . } = {2n − 1 : n ∈ N+ } = {2n + 1 : n ∈ N}
D = {6, 12, 18, . . . N+ } = {6n : n ∈ N+ } ⊂ B
(c) E = F = {0, 1, 2, 3}.

E 1.6. (i) yes; (ii) nothing; The inclusion can be true or false.
We may have the following situations:
• If A = {2n : n ∈ N+ }, B = {2n + 1 : n ∈ N+ } and C = N then A ⊂ C, B ⊂ C and
A ∩ B = ∅ (A and B have no common elements).
• If A = {2n : n ∈ N+ }, B = {4n : n ∈ N+ } and C = N then A ⊂ C, B ⊂ C and
A ∩ B = ∅ but B ⊂ A (B is included in A).
E 1.7. Let U = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} and A = {0, 1, 2, 3, 4, 5},
B = {4, 5, 6, 7}.

(i) (AB) = {0, 1, 2, 3, 6, 7}, (A ∩ B) = {4, 5}, (A ∪ B)c = {8, 9}


(AB)c = (A ∩ B) ∪ (A ∪ B)c = {4, 5, 8, 9}
(ii) (A \ B)c = Ac ∪ B = {4, 5, 6, 7, 8, 9}

E 1.8. The expressions correspond to the same regions of the Venn diagrams.
8 1 Sets and Numbers

Supplementary Problems

S 1.1. (b) mnp.

(a) A × B × C = {(1, a, a), (1, a, b), (1, a, c), (1, b, a), (1, b, b), (1, b, c), (1, c, a),
(1, c, b), (1, c, c), (2, a, a), (2, a, b), (2, a, c), (2, b, a), (2, b, b),
(2, b, c), (2, c, a), (2, c, b), (2, c, c), (3, a, a), (3, a, b), (3, a, c),
(3, b, a), (3, b, b), (3, b, c), (3, c, a), (3, c, b), (3, c, c)};

S 1.2. You may use the Venn diagrams to show that both sides determine the same region.
Otherwise, we can use the following argument.

(i) Let x ∈ A ∩ (B ∪ C) then x ∈ A and x ∈ (B ∪ C). This means that x ∈ A and


x ∈ B or x ∈ C. This means that x ∈ (A ∩ B) or x ∈ (A ∩ C). For the proof to be
complete one also need to start with a x from the right hand side, and show that it
belongs to the set at the left.
(ii) x ∈ A ∪ (B ∩ C) ⇔ x ∈ A or x ∈ (B ∩ C). If x ∈ A, it clearly belongs to (A ∪ B)
and (A ∪ C), therefore to their intersection. If x ∈ (B ∩ C), it belongs to both
B and C, therefore to (A ∪ B) and (A ∪ C) and (A ∪ B) ∩ (A ∪ C).
(iii) and (iv) - check with Venn diagrams.

S 1.3. One can use a Venn diagram to show that

A = {2, 3, 4, 5, 6}
B = {1, 3, 4, 5, 7, 8, 9, 10}

S 1.4. (i) x = 0.333 · · · = 0.(3).


Multiplying by 10 we obtain 10x = 3 + x, which shows that x = 13 .
(ii) x = 0.23232323 · · · = 0.(23).
Multiplying by 100 we obtain 100x = 23 + x, which shows that x = 2399 .
(iii) x = 0.1129292929 · · · = 0.11(29).
Multiplying by 100 the fraction becomes 100x = 11 + 0.(29) = 11 + 2999 .
In the end, we obtain x = 9900 .
1129

(iv) x = 3. This is actually not a rational number.
To prove this we√assume there are two natural numbers p, q s.t they have no common
divisors and qp = 3. This is equivalent to p 2 = 3q 2 and because p and q are natural
numbers, p 2 is a multiple of 3. This can only happen when p itself is a multiple of 3,
therefore p = 3 p1 . In this case, (3 p1 )2 = 3q 2 , which gives 3 p12 = q 2 . Using the same
argument as before, q = 3q1 . However, this would mean that 3 is a common divisor of
p and q, which is a contradiction. This ends the proof.
Chapter 2
Relations and Databases

Abstract In mathematics a binary relation between two sets A and B is a collection


of ordered pairs (a, b) belonging to the cartesian product A × B. In this chapter
we present some of the general properties of relations and their operations, as well
some special types of relations defined over the same set. A good command of these
basic notions is essential for understanding the relational database theory, where a
relation is a set of tuples (a1 , a2 , ..., an ) and each element aj is a member of An a data
domain (sometimes called an attribute). In the context of databases we also present
the notion of key.

Keywords Binary relations · Operations on relations · Databases · Keys

2.1 Brief Theoretical Background

Notations and Definitions


• A binary relation R is a triplet (X, Y , G), where G is a subset of X × Y
• X - domain, Y - co-domain
• G - graph (we usually identify the relation R with its graph G!!!)
• If the pair (x, y) is in G, x is in relation with y ((x, y) ∈ G, xRy or x ∼ y).

Representation

A relation is defined by the set of elements or by a diagram.


For the sets X = {1, 2, 3, 4} and Y = {a, b, c, d} one can define a relation as

R = {(1, a), (1, b), (2, e), (3, b), (3, c), (4, c)}, (2.1)

represented in the diagram (or sometimes in a table).

O. Bagdasar, Concise Computer Mathematics, SpringerBriefs in Computer Science, 9


DOI: 10.1007/978-3-319-01751-8_2, © The Author(s) 2013
10 2 Relations and Databases

In this relation we can see that (1, a) ∈ R (or 1Ra), while (1, c) ∈ R.
At the same time, no element in X is related to d ∈ Y .

Classification of Relations

Based on the number of links between elements of X and Y , a relation is


• many-one: each x related to at most one y
• one-many: each y related to at most one x
• one-one: both many-one and one-many (called functions in Chap. 3)
• neither of the above: this is the case for relation R defined in (2.1).

Operations on Relations

Let X, Y be two sets, A ⊂ X, B ⊂ Y be subsets, and let R, S ⊂ X × Y be two


relations. The following operations are defined:
• R−1 = {(y, x) ∈ Y × X : (x, y) ∈ R} ⊂ Y × X (inverse)
• A  R = {(x, y) ∈ X × Y : (x, y) ∈ R and x ∈ A} (restriction)
• R  B = {(x, y) ∈ X × Y : (x, y) ∈ R and y ∈ B} (co-restriction)
• R(A) = {(y ∈ Y : there is a pair (a, y) ∈ R with a ∈ A} (image - set!!!)
• R ∪ S = {(x, y) : (x, y) ∈ R or (x, y) ∈ S} ⊂ X × Y (union)
• R ∩ S = {(x, y) : (x, y) ∈ R and (x, y) ∈ S} ⊂ X × Y (intersection).
Example: For X = {1, 2, 3, 4}, Y = {a, b, c, d}, A = {1, 2} ⊂ X, B = {a, b} ⊂ Y

R = {(1, a), (1, b), (2, e), (3, b), (3, c), (4, c)},
S = {(1, b), (2, a), (3, d), (4, c)},

the above operations give


• R−1 = {(a, 1), (b, 1), (e, 2), (b, 3), (c, 3), (c, 4)} ⊂ Y × X
• A  R = {(1, a), (1, b), (2, e)} ⊂ X × Y
• R  B = {(1, a), (1, b), (3, b)} ⊂ X × Y
• R(A) = {a, b, e} ⊂ Y (set!)
• R ∪ S = {(1, a), (1, b), (2, a), (2, e), (3, b), (3, d), (3, c), (4, c)} ⊂ X × Y
• R ∩ S = {(1, b), (4, c)} ⊂ X × Y .
2.1 Brief Theoretical Background 11

Composition of Binary Relations

Let R ⊂ X × Y and S ⊂ Y × Z be two relations.


The composition is a relation S ◦ R ⊂ X × Z defined as

S ◦ R = {(x, z) ∈ X × Z : there exists y ∈ Y with (x, y) ∈ R and (y, z) ∈ S}.

These are the pairs (x, z) from X × Z, connected through Y .

Example:
Consider the set X = {1, 2, 3, 4} and the relations R = {(1, 2), (1, 1), (1, 3), (2, 4),
(3, 2)} and S = {(1, 4), (1, 3), (2, 3), (3, 1), (4, 1)}. Find S ◦ R.
Solution: As (1, 2) ∈ R and (2, 3) ∈ S, one has (1, 3) ∈ S ◦ R.
Similarly we can proceed with the others:
• (1, 1) ∈ R and (1, 4) ∈ S ⇒ (1, 4) ∈ S ◦ R
• (1, 1) ∈ R and (1, 3) ∈ S ⇒ (1, 3) ∈ S ◦ R
• (1, 3) ∈ R and (3, 1) ∈ S ⇒ (1, 1) ∈ S ◦ R
• (2, 4) ∈ R and (4, 1) ∈ S ⇒ (2, 1) ∈ S ◦ R
• (3, 2) ∈ R and (2, 3) ∈ S ⇒ (3, 3) ∈ S ◦ R
This finally gives S ◦ R = {(1, 3), (1, 4), (1, 1), (2, 1), (3, 3)}.

Special Binary Relations Defined Over a Set

• reflexive: for all x ∈ X it holds that xRx (other notations: x ∼ y, (x, x) ∈ R)


• symmetric: For all x, y ∈ X it holds that if xRy then yRx
• antisymmetric: For all distinct x, y ∈ X, if xRy and yRx then x = y
• transitive: For all x, y, z ∈ X, if xRy and yRz then xRz
• total: For all x, y ∈ X, it holds that xRy or yRx
• equivalence: a relation that is reflexive, symmetric and transitive.

Examples:
1. X: all people in the world; xRy: x is a blood relative of y.
The relation is reflexive, symmetric, transitive (equivalence) and total.
2. X = N; xRy if x ≤ y.
The relation is reflexive, antisymmetric, transitive and total.
3. X = N; xRy if x > y
The relation is not reflexive, not antisymmetric, but transitive and total.
12 2 Relations and Databases

N-ary Relations (Databases) and Keys

Let n ≥ 2 be a number and A1 , . . . , An sets (attributes). A n-ary relation is a set


R ⊂ A1 × · · · × An of n-tuples (a1 , . . . , an ) s.t. aj ∈ Aj , j = 1, . . . , n.
• Relation scheme: table with its column headings (called attributes).
The entries in the table are called “tuples”.
• Key: Set K of attributes for relation R s.t.
(1) no relation in R can have two tuples with the same values in K, but different
on some other attribute,
(2) no subset of K has a determining property.
Examples: In the following table, the keys are {ID}, {Name}, {Surname, Pro-
gramme}.

ID Name Surname Programme


11 George Bush HND
12 Alan Turing PhD
13 Jimmy Carter BSc
14 Roger Smith MSc
15 Paul Smith HND
16 Greg Smith Phd

Each entry has four attributes, therefore this is a 4-ary database. The ID and Name
attributes can uniquely identify the entries. The surname or the programme cannot
uniquely identify the entries on their own, but they form a key when combined.
Remark: In the context of keys one can better understand why students or employees
of big companies are given an ID number!

2.2 Essential Problems

E 2.1. Let C be all characters and Y = {“apple”, “pie”}. Describe the relation

APPEARS ⊂ C × Y

where (c, s) ∈ APPEARS if the character c appears in the string s.


2.2 Essential Problems 13

E 2.2. A relation R ⊂ A × B, where A = {1, 2, 3, 4} and B = {‘a’, ‘b’ ,‘c’, ‘d’} is


shown below. (i) Give R as a table; (ii) Give R−1 as a set of pairs.

E 2.3. Let X = {1, 2, 3, 4}, Y = {A, B, C, D, E, F} and the relations

R ⊂ X × Y = {(1, A), (1, C), (2, A), (3, D), (4, D)}
S ⊂ Y × Y = {(E, A), (E, B), (B, D), (C, F), (A, F)}.

Calculate each of the following sets or relations:

(i) {1, 2}  R (ii) S  {A, C, D} (iii) R({1, 2}) (iv) S ◦ R


−1
(v) S ◦ S (vi) S ◦ S ◦ S (vii) R ◦ R

E 2.4. Suppose that T is a set of students, S is a set of sports, and LIKES ⊂ T × S


is a relation, as defined below

T = {Anne, Bill, Carol, Dave, Emma, Frankie}


S = {hockey, netball, tennis}
LIKES = {(Anne, tennis), (Anne, hockey), (Bill, tennis), (Carol, netball),
(Dave, hockey)}.

If A = {Bill, Dave, Frankie} ⊂ T and B = {hockey, tennis} ⊂ S then find ALIKES,


LIKES  B and LIKES (A).

E 2.5. Let A ⊂ X, B ⊂ Y be subsets and R, S ⊂ X × Y relations. Prove that

(A  R)  B = A  (R  B).

E 2.6. Let R ⊂ X × Y , S ⊂ Y × Z and T ⊂ Z × U be relations. Show that

(i) (S ◦ R)−1 = R−1 ◦ S −1


(ii) (T ◦ S) ◦ R = T ◦ (S ◦ R).

E 2.7. For each of the relations given below, decide whether or not it is:
(a) many-one; (b) one-many; (c) one-one; (d) onto.
14 2 Relations and Databases

(i) R1 = {(s, t) ∈ S × S : s = "", t is string s with its first character deleted}


(ii) R2 = {(c, s) ∈ C × S : LEN(s) ≥ 3 and c is the third character of string s}
(iii) R3 = {(s, n) ∈ S × N : s = "", n is the ASCII code of first character in s}.

E 2.8. Check whether the following relations R ⊂ X × X are equivalences


(i) X = N, (x, y) ∈ R if x = y.
(ii) X = R, (x, y) ∈ R if x ≥ y.
(iii) X = Z, (x, y) ∈ R if x is divisible by y.

E 2.9. Find the key(s) of the database

ID Name Surname Programme Stage


10 Jimmy Hendrix BSc 1
12 John Hendrix HND 2
16 Jerry Carter HND 2

2.3 Supplementary Problems

S 2.1. Let X and Y be sets of cardinalities m and n respectively. What is the minimum
cardinality of relation R ⊂ X × Y ? What is the maximal one ?
S 2.2. For A1 , A2 ⊂ X, B1 , B2 ⊂ Y sets and R, S ⊂ X × Y relations prove that

(A1 ∩ A2 )  R = (A1  R) ∩ (A2  R)


(A2 ∪ A2 )  R = (A1  R) ∪ (A2  R)
R  (B1 ∩ B2 ) = (R  B1 ) ∩ (R  B2 )
R  (B1 ∪ B2 ) = (R  B1 ) ∪ (R  B2 )
R(A1 ∩ A2 ) = R(A1 ) ∩ R(A2 )
R(A1 ∪ A2 ) = R(A1 ) ∪ R(A2 )

S 2.3. Let STUDENTS be the set of students in a college, COURSES be the set
of courses the college runs, and SESSIONS be the set of sessions (such as: June
21, a.m.) at which exam papers are taken. (Students may take more than one course,
and a course may have more than one exam paper.) Relations TAKING and EXAM
are defined as:

TAKING = {(s, c) ⊂ STUDENTS × COURSES : student s is taking course c}


EXAM = {(c, d) ⊂ COURSES × SESSIONS :
there is a paper for course c in session d}.
2.3 Supplementary Problems 15

Let F be the set of female students, and C the set of computing courses.
(i) Express in English each of the following:
(a) C  EXAM
(b) EXAM(C)
(c) EXAM ◦ TAKING
(d) EXAM −1 ◦ EXAM
(e) EXAM ◦ (TAKING  C)
(ii) Using F, C, TAKING and EXAM and operations on relations, express
(a) Relation (s, c) where s is a female student taking computing course c.
(b) The set of courses with no female students.
(c) The set of exam sessions when the student ‘A107X’ has a paper to sit.
S 2.4. HOBBIES is a relation which is defined below:

HOBBIES = {(Sarah, painting), (Tom, hockey), (Joe, football),


(Janet, football), (Susie, hockey), (Sarah, sketching),
(Craig, sketching)}

and also, the following subsets are considered

Professional = {Joe, Sarah}; Amateur = {Susie, Tom, Janet, Craig}


Arty = {sketching, painting}; Games = { football, hockey}

Write down the following:

(i) HOBBIES  Arty


(ii) Professional  HOBBIES
(iii) Amateur  HOBBIES  Games

S 2.5. Let R ⊂ X × Y be a binary relation. Express in English the statements:

(i) ∀x ∈ X[∃y ∈ Y [(x, y) ∈ R]]; (ii) ∀y ∈ Y [∃x ∈ X[(x, y) ∈ R]]

S 2.6. Prove whether the following binary relations are equivalences. If the relation
is not an equivalence relation, state why it fails to be one.

(i) X = Z : a ∼ b if ab > 0.
(ii) X = R : a ∼ b if a − b is rational.
(iii) X = R : a ∼ b if a ≥ b.
(iv) X = C : a ∼ b if |a| = |b|.
16 2 Relations and Databases

S 2.7. Find the key(s) of the database

ID Name Surname Programme Stage


10 Jimmy Hendrix BSc 1
11 John Hendrix HND 2

2.4 Problem Answers

Essential Problems

E 2.1. APPEARS =

C Y
‘a’ “apple”
‘p’ “apple”
‘l’ “apple”
‘e’ “apple”
‘p’ “pie”
‘i’ “pie”
‘e’ “pie”

E 2.2. (i)

1 ‘a’
1 ‘b’
2 ‘d’
3 ‘b’
4 ‘b’

(ii) R−1 = {(‘a’,1),(‘b’,1), (‘b’,3), (‘b’,4), (‘d’,1)}.

E 2.3. The operations give the following results

(i) {1, 2}  R = {(1, A), (1, C), (2, A)},


(ii) S  {A, C, D} = {(E, A), (B, D)},
(iii) R({1, 2}) = {A, C},
(iv) S ◦ R = {(1, F), (2, F)},
2.4 Problem Answers 17

(v) S ◦ S = {(E, F), (E, D)},


(vi) S ◦ S ◦ S = ∅,
(vii) R ◦ R−1 = {(A, A), (A, C), (C, A), (C, C), (D, D)}.

E 2.4. The answers are

A  LIKES = {(Bill, tennis), (Dave, hockey)}


LIKES  B = {(Anne, tennis), (Anne, hockey), (Bill, tennis), (Dave, hockey)}
LIKES(A) = {tennis, hockey}.

E 2.5. Both sides represent the set

(A  R)  B = A  (R  B) = {(a, b) ∈ R : a ∈ A, b ∈ B}.

E 2.6. Let R ⊂ X × Y , S ⊂ Y × Z and T ⊂ Z × U be relations. Show that

(i) (z, x) ∈ (S ◦ R)−1 ⇔ (x, z) ∈ (S ◦ R)


⇔ ∃y ∈ Y s.t (x, y) ∈ R, (y, z) ∈ S
⇔ ∃y ∈ Y s.t (y, x) ∈ R−1 , (z, y) ∈ S −1
⇔ (z, x) ∈ R−1 ◦ S −1
(ii) (x, t) ∈ (T ◦ S) ◦ R ⇔ ∃y ∈ Y s.t (x, y) ∈ R, (y, t) ∈ T ◦ S
⇔ ∃y ∈ Y , z ∈ Z s.t (x, y) ∈ R, (y, z) ∈ S, (z, t) ∈ T ,
⇔ ∃z ∈ Z s.t (x, z) ∈ S ◦ R, (z, t) ∈ T ,
⇔ (x, t) ∈ T ◦ (S ◦ R).

E 2.7. (i) many-one; (ii) one-many; (iii) many-one.


E 2.8.
(i) X = N, (x, y) ∈ R if x = y. Yes, because it is (a) reflexive: x = x; (b) symmetric:
x = y implies y = x (c) transitive: x = y, y = z implies x = z.
(ii) X = R, (x, y) ∈ R if x ≥ y. Not an equivalence, because it is
(a) reflexive: x ≥ x; (b) not symmetric (actually antisymmetric): x ≥ y does
not imply y ≥ x (ex. x = 3, y = 2) (c) transitive: x ≥ y, y ≥ z implies x ≥ z.
(iii) X = Z, (x, y) ∈ R if x is divisible by y. As above, it is not symmetric.
E 2.9. The keys are {ID}, {Name}, {Surname, Programme, Stage}.
18 2 Relations and Databases

Supplementary Problems
S 2.1. min = 0; max = mn.
S 2.2. One just needs to use the definitions of ∩, ∪, ,  and of the image of a
subset, and the double inclusion to prove the pairs of sets are equal.
S 2.3. (i) Express in English each of the following:
(a) C  EXAM =(c, d): there is a paper in computing c in the exam session d
(b) EXAM(C) = the sessions d having computing exams
(c) EXAM ◦ TAKING = (s, d) the students s taking an exam in session d
(d) EXAM −1 ◦EXAM = (c, e) the pairs of courses c,e having a common session
(e) EXAM ◦ (TAKING  C) = (s, d) the students s taking computing exam in
session d.
(ii)
(a) F  TAKING  C;
(b) TAKING(STUDENTS \ F);
(c) EXAM(TAKING( ‘A107X’)).
S 2.4. The answer is

(i) HOBBIES  Arty = {(Sarah, painting), (Sarah, sketching), (Craig, sketching)}


(ii) Professional  HOBBIES = {(Sarah, painting), (Joe, football), (Sarah, sketching)}
(iii) Amateur  HOBBIES  Games = {(Janet, football), (Tom, hockey), (Susie, hockey)}.

S 2.5. (i) for all x in X, there is y in Y s.t. (x, y) is in R;


(ii) for all y in Y, there is x in X s.t. (x, y) is in R.
S 2.6. The following answers are obtained:
(i) X = Z : a ∼ b if ab > 0.
Yes: reflexive, symmetric and transitive.

(ii) X = R : a ∼ b if a − b is rational.
Yes: reflexive, symmetric and transitive.

(iii) X = R : a ∼ b if a ≥ b.
No: reflexive, antisymmetric and transitive.

(iv) X = C : a ∼ b if |a| = |b|.


Yes: reflexive, symmetric and transitive.

E 2.7. The keys are {ID}, {Name}, {Programme}, {Stage}.


Chapter 3
Functions

Abstract Functions are very often the central objects of investigation in most fields
of modern mathematics. There are many ways to describe or to represent a function.
Functions can be defined by a formula or algorithm that indicates how to compute the
output for the given input. Sometimes they are given by a picture (called the graph
of the function), or by a table of values. In this chapter functions are introduced as
relations which link every input from a given set to a unique output from another set.
We then present some basic properties, examples and operations involving functions,
along with applications based on set numbers, data structures and polynomials.

Keywords Function properties and operations · Datatypes · Polynomials

3.1 Brief Theoretical Background

Notations and Definitions

• Function: Special type of relation s.t. each element has a unique image.
• Function elements: X -domain, Y -codomain, y = R(x) ∈ Y - image of x.

Examples of Useful Functions


• Car d (or ): P(X ) → N, Card(A) = the number of elements in subset A
• d : R × R → R+ , d(x, y) = |x − y| distance between numbers x and y
• Operations on N × N → N: well defined (+, ×) and ill defined (−, /).
– + : N × N → N, (x, y) −→ x + y (well defined: x + y ∈ N)
– × : N × N → N, (x, y) −→ x × y (well defined: x × y ∈ N)
– − : N × N → N, (x, y) −→ x − y (ill defined: (1, 3) −→ −2 ∈ N)
– / : N × N → N, (x, y) −→ x/y (ill defined: (1, 2) −→ 1/2 ∈ N)

O. Bagdasar, Concise Computer Mathematics, SpringerBriefs in Computer Science, 19


DOI: 10.1007/978-3-319-01751-8_3, © The Author(s) 2013
20 3 Functions

• power of a given exponent: f (x) = 2x ( f (1) = 2, f (2) = 4, etc)


• exp x, log x, sin x and cos x.

General Properties of Functions f : X → Y

• injective: different elements have different images

x1 , x2 ∈ X : x1 = x2 ⇒ f (x1 ) = f (x2 )
(Example: f : R → R, f (x) = −2x + 1)

• surjective: all elements in Y are image of elements from X .

y ∈ Y : there is x ∈ X s.t. f (x) = y


(Example: f : R → R, f (x) = −2x + 1)

• bijective (1-on-1): a function that is both injective and surjective


• a function f only has an inverse f −1 if it is bijective!
• A function f : R → R is increasing (decreasing if f (x1 ) ≥ f (x2 )) if

x1 , x2 ∈ R : x1 ≤ x2 ⇒ f (x1 ) ≤ f (x2 )
(Example: f : R → R, f (x) = 2x + 1)

• A function f : [−a, a] or R → R is even if

for all x ∈ X : f (x) = f (−x) (Examples: f (x) = x 2 , cos x)

• A function f : [−a, a] or R → R is odd if

for all x ∈ X : f (x) = − f (−x) (Examples: f (x) = x, x 3 , sin x, tan x).

Applications

• Theory of cardinals:
(1) Two sets have same cardinal if there is a 1–1 function between X ∼ Y
(2) Finite sets: have same cardinal is the same number of elements
(3) Infinite sets: N ∼ Z ∼ Q (countable), R (not countable).
• Datatype (set, together with the basic defining functions) string:
(1) Sets: C set of all characters, S set of all (ordered) sequences of characters
(2) Notation: ‘a’ - character, “string” - string
3.1 Brief Theoretical Background 21

(3) Functions defining the datatype string (all but L E N ):

– L E N : S → N - number of characters in string s


– F I RST : S → C - selects the first character in string s
The domain of F I RST is S \ {“ ”} (i.e. s is not an empty string).
– R E ST : S → S - deletes the first character from string s
The domain of R E ST is S \ {“ ”} (i.e. s is not an empty string)
– I S E M P T Y : S → {tr ue, f alse} - checks whether a string is empty
– AD D F I RST : C × S → S - adds character c at the front of string s
– ST R : C → S - makes the string “c” out of the character ‘c’.
• Functions ASC and CHR (for characters):
– ASCII code: number between 0 and 255 representing a keyboard character on
one byte (8 bit).
– ASC : C → {0, . . . , 255} - converts a character to its ASCII code
– C H R : {0, . . . , 255} → C - converts an ASCII code to a character
Example:ASC(‘a’) = 97, C H R(98) = ‘b’.
• Special notations and operations
– +R , =N , <C , =s : defined on R(real), N(natural), C(char), S(string)
Example:=C is well defined when both inputs are in set C.
– >c : orders two characters by comparing their ASCII codes.
– +s : concatenates two strings (“a’’ +s “xe’’ = “axe’’)

Composition of Functions

Let f ⊂ X × Y and g ⊂ Y × Z be two functions.

g ◦ f (x) = {z ∈ Z : there exists y ∈ Y with f (x) = y and g(y) = z}.

Note that in general g( f (x)) = f (g(x))!!!


Example: Let the functions f, g : R → R be f (x) = 2x + 1, g(x) = x 2 − 1. Then

f (g(x)) = 2 · g(x) + 1 = 2(x 2 − 1) + 1 = 2x 2 − 1,


g( f (x)) = ( f (x))2 − 1 = (2x + 1)2 − 1 = 4x 2 + 4x.

Polynomials and Polynomial Functions


Polynomials: Simple mathematical expressions constructed from variables (called
indeterminates) and constants (usually numbers), using the operations of addition,
subtraction, multiplication, and natural exponents.
22 3 Functions

Examples: The following expressions are polynomials:

P=1 (degree 0–constant)


Q = 2x − 1 (degree 1–linear)
R = 4x 2 − 2x + 1 (degree 2–quadratic)
T (x, y) = x − 2x y + 3y − 1
2
(degree 2 in x and 1 in y)

Operations with Polynomials

Let P = 2x + y + 1 and Q = 3x 2 + 2x y + 4x − 3. We can define the operations


• multiplication by a scalar

2P = 4x + 2y + 2

• addition:

P + Q = 3x 2 + 2x y + 6x + y − 2

• multiplication

P Q = (2x + y + 1)(3x 2 + 2x y + 4x − 3)
= 6x 3 + 7x 2 y + 11x 2 + 2x y 2 + 6x y − 2x − 3y − 3.

Polynomial function: Function defined by evaluating a polynomial.


Composing two polynomial functions generates a polynomial functions.

Root of a polynomial: A number r is called a root of polynomial P if the value of


the polynomial function at r is zero (P = 0).

Example: Polynomial P = x 2 − 3x + 2 = (x − 1)(x − 2) has roots 1 and 2.

3.2 Essential Problems


Hints: Make sure you understand the theory, before solving the problems!
When solving the problems one has to notice that the operations with index
(“ +s ’’, “ =c ’’, “ =N ’’, “ +R ’’) refer to operations on the sets: S (strings),
C (characters), N (natural numbers) and R real numbers.
The apostrophe notation ‘h’ denotes character h, “h” denotes string h.
E 3.1. Calculate ASC(x) +N 32 for x = ‘A’ and x = ‘ p’.

E 3.2. Which of the following are true? If false or invalid explain why.
3.2 Essential Problems 23

(a) ‘a’ <c ‘A’; (b) ‘2’ <c ‘Q’; (c) 3 =c ‘3’.

E 3.3. Which of the following are true, false (or invalid).


(a) ST R(‘c’) =s “c”; (b) “d” =c ‘d’; (c) ASC(‘c’) ∈ C;
(d) ST R(ASC(‘b’)) =s “98”.
E 3.4. Given s = “H al”, t = “P R I N C E” compute where possible:
(a) L E N (s);
(b) L E N (t);
(c) s +R t;
(d) F I RST (s);
(e) R E ST (t);
(f) t +s s;
(g) t +s ‘s’;
(h) R E ST (s) +s “ice”.

E 3.5. Find F I RST (“12345”) ; L E N (“take away”).


E 3.6. Compute where possible, or say if the expression is undefined.
(a) ASC(‘D’) +N ASC(‘d’);
(b) C H R(6.5);
(c) ST R(‘H ’);
(d) ST R(ASC(‘A’));
(e) R E ST (“Louise”);
(f) F I RST (“T H E L M A”);
(g) L E N (“time” +s “out”);
(h) R E ST (R E ST (“ f r og”));
(i) F I RST (R E ST (“toad”));
(j) F I RST (R E ST (“K ”));
(k) ST R(F I RST (“ f r og”)) +s R E ST (R E ST (“Xi”)).
E 3.7. Each of the following statements defines an element IMPLICITLY. Say which
element is uniquely defined or whether there is no such element and explain why.
Are there are many possible elements?
(a) c ∈ S such that c+s “ed” =s “pushed”;
(b) t ∈ S such that L E N (t) = 6;
(c) r ∈ S such that L E N (r ) = 0;
(d) v ∈ C such that ASC(v) = 109;
(e) w ∈ N such that L E N (w) = 1.
E 3.8. Let f, g, h : R → R be functions defined as

f (x) = x 2 − 1 g(x) = 2x 2 + x + 1 h(x) = 3x − 2.

Evaluate (a) (i) f ◦ f ; (ii) g ◦ f ; (iii) f ◦ h; (iv) h ◦ f ; (v) g ◦ h.


(b) (i) 2 f − 3g + 4h; (ii) f · g; (iii) h · f ; (iv) g · h.
24 3 Functions

3.3 Supplementary Problems

S 3.1. Find the maximum domain


√ and codomain of the functions f : X → Y :
(a) f (x) = x 2 ; (b) f (x) = x; (c) X = [2, 4] and f (x) = x + 2.
S 3.2. Define the function C A P I T AL_C O N V E RT : S → S that replaces a string
by the same string, with the first letter replaced by the upper case symbol for that
letter. i.e. f ish → Fish, ada → Ada. Hint: Check the online for the ASCII codes
of lower and upper letters.
S 3.3. Which of the following functions is injective, surjective or bijective:
(a) f : R → R, f (x) = 2x + 3;
(b) f : R → R, f (x) = 5;
(c) f : R → R, f (x) = x 2 − 2x + 3;
(d) f : R → R+ , f (x) = x 2 − 2x + 1;
(e) f : R+ → R+ , f (x) = x 2 − 2x + 1;
(f) f : N → N, f (n) = 2n;
(g) f : N → Z, f (n) = n/2 (n even), and f (n) = −(n + 1)/2 (n odd);
(h) f : N → 3N, f (n) = 3n. Prove that N, 2N and 3N have same cardinal.
S 3.4. Check which of the functions below is well defined

(i) f : R → R+ , f (x) = (x + 1)2 − 2


(ii) f : R → R+ , f (x) = 2x − 1
(iii) f : R → R, f (x) = x − 1
(iv) f : R → R+ , f : R × R → R+ , f (x, y) = x − 2y
(v) f : N × N → N+ , f (x, y) = 2x − y

S 3.5. Prove the following identities

x 2 ± 2x y + y 2 = (x ± y)2 ;
x 2 − y 2 = (x + y)(x − y);
x 3 ± y 3 = (x ± y)(x 2 ∓ 2x y + y 2 ).

S 3.6. Prove that every function f : R → R can be written as the sum of two
functions g, h : R → R, such that g is even and h is odd.
3.4 Problem Answers 25

3.4 Problem Answers

Essential Problems

E 3.1. ASC(‘A’) = 65, ASC(‘ p’) = 112, therefore we have 97 and 144.
E 3.2. (a) false; (b) true; (c) invalid.
E 3.3. (a) true; (b) invalid; (c) false; (d) invalid.
E 3.4. (a) 3; (b) 6; (c) invalid; (d) ‘H’; (e) “RINCE”; (f) “PRINCEHal”;
(g) invalid; (h) “alice”.
E 3.5. ‘1’, 9.
E 3.6. (a) 68 + 100 = 168; (b) invalid; (c) “H”; (d) STR(65) invalid;
(e) “ouise”; (f) “T”; (g) 7; (h) “og” ; (i) “o”; (j) FIRST(" ") - invalid - the function is
only defined for non-empty strings; (k) “ f ”+s " "= “ f ”.
E 3.7. (a) “push”; (b) any 6 character string; (c) " "; (d) ASC(‘m’) = 109, so
v = ‘m’; (e) w ∈ N is not a string, therefore L E N (w) is invalid.
E 3.8. (a)
(i) f ◦ f = x 4 − 2x 2 ;
(ii) g ◦ f = 2(x 2 − 1)2 + (x 2 − 1) + 1 = 2x 4 − 3x 2 + 2;
(iii) f ◦ h = (3x − 2)2 − 1;
(iv) h ◦ f = 3(x 2 − 1) − 2 = 3x 2 − 5;
(v) g ◦ h = 2(3x − 2)2 + (3x − 2) + 1 = 18x 2 − 9x + 7.
(b)

(i) 2 f − 3g + 4h = −4x 2 + 9x − 13;


(ii) f · g = 2x 4 + x 3 − x 2 − x − 1;
(iii) h · f = 3x 3 − 2x 2 − 3x + 2;
(iv) g · h = 6x 3 − x 2 + x − 2.

Supplementary Problems

S 3.1. (a) f (x) = x 2 : If X = R, then R + ⊂ Y ; (b) f (x) = x: X ⊂ R + ;
(c) X = [2, 4] and f (x) = x + 2: [4, 6] ⊂ Y .
S 3.2. The solution is: domain s ∈ S \ {“ ”} and 97 ≤N ASC(F I RST (s)) ≤N 122,
codomain: t = AD D F I RST (C H R(ASC(F I RST (s)) −N 32), R E ST (s)).
Remark: five other functions ASC, C H R, F I RST, R E ST, AD D F I RST have all
been used to make the function C A P I T AL_C O N V E RT .
S 3.3. (a) bijective; (b) neither; (c) neither; (d) surjective; (e) bijective; (f) bijective;
(g) bijective; (h) bijective. N, 2N and 3N have the same cardinal.
S 3.4. Functions (iii) and (iv) are well defined. The others are not.
26 3 Functions

S 3.5. You just have to multiply the brackets.


S 3.6. The two functions (whose sum is f (x)) can be defined as

1
g(x) = [ f (x) + f (−x)] (even)
2
1
h(x) = [ f (x) − f (−x)] (odd).
2
Chapter 4
Boolean Algebra, Logic and Quantifiers

Abstract Boolean algebra was introduced in 1854 by George Boole and has been
very important for the development of computer science. It operates with variables
which have the truth values true and false (sometimes denoted 1 and 0 respectively).
In this chapter the main operations of Boolean algebra (conjunction (AND, ∧),
disjunction (OR, ∨) and negation (NOT, ¬)) are defined, and statements involving
logical variables are studied with the aid of truth tables and Venn diagrams. Useful
formulae involving logical variables are then discussed (De Morgan’s laws), along
with the existential and universal quantifiers and their negation.

Keywords Boolean variables · Truth tables · Logical operations · Quantifiers

4.1 Brief Theoretical Background

Boolean Values: B = {tr ue, f alse} (sometimes the set {0, 1} is used instead).

Boolean Operations

• ¬:B→B NOT (standard notation, some books may use “∼”)


• ∨ : B × B → B OR
• ∧ : B × B → B AND
• ⇒: B × B → B IMPLIES
• ⇔: B × B → B EQUIVALENT

These operations are usually represented using truth tables like below

O. Bagdasar, Concise Computer Mathematics, SpringerBriefs in Computer Science, 27


DOI: 10.1007/978-3-319-01751-8_4, © The Author(s) 2013
28 4 Boolean Algebra, Logic and Quantifiers

p q ¬p p ∨ q p ∧ q p ⇒ q p ⇔ q
T T F T T T T
T F F T F F F
F T T T F T F
F F T F F T T

Equivalence Between Boolean Operations and Set Operations

Let A, B be two subsets of universe U and p and q be defined as

p(x) is true if and only if x ∈ A


q(x) is true if and only if x ∈ B

• ¬ p(x) is true if and only if x ∈ Ac = U \ A.


• p ∨ q(x) is true if and only if x ∈ A ∪ B.
• p ∧ q(x) is true if and only if x ∈ A ∩ B.

Definitions

• Sentence: statement that takes a value true or false.


• Proposition: sentence with no variables.
• Predicate: sentence which contains variables.
• Special predicates:
– Tautology: always true
– Contradiction: always false

Useful Formulae for Logical Operations

• De Morgan’s Laws

¬( p ∧ q) = ¬ p ∨ ¬q
¬( p ∨ q) = ¬ p ∧ ¬q

• Distributive Law

p ∧ (q ∨ r ) = ( p ∧ q) ∨ ( p ∧ r )
p ∨ (q ∧ r ) = ( p ∨ q) ∧ ( p ∨ r )
4.1 Brief Theoretical Background 29

• Associative Law

p ∧ (q ∧ r ) = ( p ∧ q) ∧ ( p ∧ r )
p ∨ (q ∨ r ) = ( p ∨ q) ∨ ( p ∨ r )

Observation: Applying ¬ to an expression has the following effect:


• p becomes ¬p (and of course, ¬p becomes p)
• ∧ becomes ∨, ∨ becomes ∧
Example: ¬( p ∧ (¬q ∨ r )) = ¬ p ∨ ( p ∧ ¬r ).

Quantifiers and Their Negation

• universal quantifier: ∀ (for all)


Example: ∀x ∈ N[x + 3 ≥ 2]
• existential quantifier: ∃ (there exists)
Example: ∃x ∈ N[x + 3 > 4]
• By negation ∀ becomes ∃ (the proposition negates): ¬(∀x[ p(x)]) ≡ ∃x[¬ p(x)]
Example: ¬(∀x ∈ N[x + 3 ≥ 2]) ≡ ∃x ∈ N[x + 3 < 2]
• By negation ∃ becomes ∀ (the proposition negates): ¬(∃x[ p(x)]) ≡ ∀x[¬ p(x)]
Example: ¬ (∃x ∈ N[x − 2 > 4]) ≡ ∀x ∈ N[x − 2 ≤ 4]

4.2 Essential Problems

E 4.1. Write the negations for each of the following statements


(a) John is six feet tall and he weighs at least 200 pounds.
(b) The bus was late or Tom‘s watch was slow.
E 4.2. Construct a truth table for the expressions: (i) ¬(¬p ∧ q); (ii) (p ∨¬q)∧r;

(iii) Construct a truth table for (p ∧ q)∨r and for (p ∨r) ∧ (q ∨r) hence show that
these two statements are logically equivalent.
(iv) Check whether (p ∨ q)∧¬r and (p ∧¬r)∨(q ∧¬r) are equivalent.

E 4.3. For x ∈ R, suppose that p, q and r are the propositions given below:

p: x = −4
q: x = +4
r: x 2 = 16

Which of the following are true?


30 4 Boolean Algebra, Logic and Quantifiers

(a) p ⇒ r ; (b) r ⇒ p; (c) q ⇒ r ; (d) r ⇒ q; (e) p ⇔ r ;


( f ) q ⇔ r ; (g) ( p ∨ q) ⇒ r ; (h) r ⇒ ( p ∨ q); (i) ( p ∨ q) ⇔ r ;

E 4.4. Decide which of the sentences below, are universally true, which are univer-
sally false, and which are neither. Then write the predicates below in symbolic form:
(i) (x + 3)2 > 8, x ∈ N; (ii) 2x = 9, x ∈ N.

E 4.5. Check (using truth tables) which statements are logically equivalent

(i) (a ∧ b) ∨ b and a ∧ b
(ii) (a ∧ b) ∨ a and a
(iii) (¬a ∧ b ∧ c) ∨ ((a ∧ b) ∨ c) and b ∧ c
(iv) a ⇒ b and ¬b ⇒ ¬a.

E 4.6. Below, y is a variable of type Nat, c is of type Char and s and t are of type
Str. Represent the following conditions formally, using only standard functions of
the data types Nat, Char, Str and ∧, ∨, ¬, ⇒, ⇔ .
(i)y is either less than 3, or greater than 6 but less than 10. y is not seven.
(ii)The first character of s is ‘b’ and the second character of s is ‘a’.
(iii)If s starts with ‘c’ then t starts with ‘c’.
(iv) s and t have similar starting characters, but their second characters are distinct.
(v) c is an upper case vowel, and the first two characters of t are the same if and
only if the first two characters of s are the same.
(vi) s is the string “fred” or y has the value 16, but not both.
E 4.7. Use quantifiers to express formally the following predicates:
(i) There is a natural number whose square is greater than 7
(ii) Whatever real number x, x 2 = −(x 2 + 1) is false.
E 4.8. Write in English the negations of the following statements
(i) All dogs bark;
(ii) Some birds fly;
(iii) No cat likes to swim;
(iv) No computer science student does not know mathematics.
E 4.9. Find the negation of the following statements and evaluate their truth values:
(i) ∀ x ∈ R [ x 2 − 6x + 10 ≥ 0]; (ii) ∀ x ∈ R [ x 2 ≥ x];
(iii) ∃ x ∈ N+ [ x 2 + 2x − 2 ≤ 0]; (iv) ∃ x ∈ Q [ x 2 = 3].

4.3 Supplementary Problems

S 4.1. Decide whether each of the following propositions is true or false:


4.3 Supplementary Problems 31
√ √
(i) 27 ≥ 5; (ii) 32 + 23 < 250; (iii) 34 < 43 .
S 4.2. Repeat question E 4.4. for x ∈ R.
S 4.3. Decide whether each of the following predicates is true or false.
(i) ∀x ∈ R[x 2 >R 0]; (ii) ∃t ∈ N[t 2 =N 7]; (iii) ∃s ∈ N[s 2 =N 9].
S 4.4. Decide which of the sentences below are universally true in R, which are
universally false and which are neither.
(i) (x + 3)2 > 6; (ii) x = x + 1; (iii) 2x = 7; (iv) (x − 1)2 = x 2 − 2x + 1.
S 4.5. Let R be the proposition “Roses are red” and B be the proposition “Violets
are blue”. Express the following propositions as logical expressions:
(a) If roses are not red, then violets are not blue;
(b) Roses are red or violets are not blue;
(c) Either roses are red or violets are blue (but not both);
Use a truth table to show that (a) and (b) are equivalent.
S 4.6. Interpret the following quantified propositions in English and state which are
true and which are false:
(i) ∀ s ∈ S [(¬(s = “ ”)) ⇔ (L E N (s) ≥ 1 )];
(ii) ∀ s ∈ S [∃ ‘c’ ∈ C [ c = FIRST(s)]];
(iii) ∀ s ∈ S [(s =“ ”) ∨ (∃ ‘c’ ∈ C s.t.‘c’ = FIRST(s))].

4.4 Problem Answers

Essential Problems

E 4.1. John is either not six feet tall, or he weighs less than 200 pounds.

E 4.2. In each case, one needs to build the table for 2 and 3 variables.
The final expressions are called H or G(to avoid wide tables).

(i) Let H= ¬(¬p ∧ q). The corresponding truth table is

p ¬p q ¬p ∧ q H
1 0 1 0 1
1 0 0 0 1
0 1 1 1 0
0 1 0 0 1

(ii) Let H=(p ∨¬q)∧r. The corresponding truth table is


32 4 Boolean Algebra, Logic and Quantifiers

p q r ¬q p ∨¬q H
1 1 1 0 1 1
1 1 0 0 1 0
1 0 1 1 1 1
1 0 0 1 1 0
0 1 1 0 0 0
0 1 0 0 0 0
0 0 1 1 1 1
0 0 0 1 1 0

(iii) Let G=(p ∧ q)∨r and H=(p ∨r)∧(q ∨r). The truth table for G and H is

p q r p ∧ q p ∨r q ∨r G H
1 1 1 1 1 1 1 1
1 1 0 1 1 1 1 1
1 0 1 0 1 1 1 1
1 0 0 0 1 0 0 0
0 1 1 0 1 1 1 1
0 1 0 0 0 1 0 0
0 0 1 0 1 1 1 1
0 0 0 0 0 0 0 0

hence the two statements are logically equivalent.


(iv) Let G=(p ∨ q)∧¬r and H=(p ∧¬r)∨(q ∧¬r).
The combined truth table for G and H is
hence the two statements are logically equivalent.

p q r ¬r p ∨ q p ∧¬r q ∧¬r G H
1 1 1 0 1 0 0 0 0
1 1 0 1 1 1 1 1 1
1 0 1 0 1 0 0 0 0
1 0 0 1 1 1 0 1 1
0 1 1 0 1 0 0 0 0
0 1 0 1 1 0 1 1 1
0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0

E 4.3. (a) T ; (b) F; (c) T ; (d) F; (e) F; ( f ) F; (g) T ; (h) T ; (i) T ;


E 4.4. (i) UT; (ii) UF.
E 4.5. (i) yes; (ii) yes; (iii) no; (iv) yes.
E 4.6. (i) ((y < 3) ∨ ((y < 10) ∧ (y > 6))) ∧ ¬(y = 6);
(ii) F I RST (s) = ‘b‘ ∧ F I RST (F I RST (s)) = ‘a‘;
(iii) (F I RST (s) = ‘c‘) ⇒ F I RST (t) = ‘c‘;
4.4 Problem Answers 33

(iv) F I RST (s) = F I RST (t)∧ = (F I RST (F I RST (s)) = F I RST


(F
 I RST (t)); 
(v) 65 ≤ ASC(c) ≤ 90 ∧ F I RST (t) = F I RST (F I RST (t)) ⇔
F I RST (s)
= F I RST (F I RST (s));
(vi) (s = " f r ed" ∨ y = 16) ∧ ¬(s = " f r ed" ∧ y = 16).

E 4.7. (i) ∃x ∈ N s.t. x 2 > 7; (ii) ∀x ∈ R, x 2 = −(x 2 + 1).

E 4.8. (i) There is at least one dog which can’t bark;


(ii) There is at least one bird which can’t fly;
(iii) There is at least one cat which likes to swim;
(iv) There is at least one computer science student who knows mathematics.

E 4.9. (i) ∃ x ∈ R [ x 2 − 6x + 10 < 0] F; (ii) ∃ x ∈ R [ x 2 < x] T;


(iii) ∀ x ∈ N+ [ x 2 + 2x − 2 > 0] T; (iv) ∀ x ∈ Q [ x 2 = 3] T.
Supplementary Problems
S 4.1. (i) T; (ii) F; (iii) F.
S 4.2. (i) UT; (ii) neither.
S 4.3. (i) F; (ii) F; (iii) T.
S 4.4. (i) UT; (ii) UF; (iii) neither; (iv) UT.
S 4.5. (a) ¬R⇒ ¬B; (b) R∨¬B; (c) (R∨B)∧¬(R∧B)

S 4.6. (i) For all strings s, the propositions “the string is not void” and “the string’s
length is at least one” are equivalent. True.
(ii) Every string s has a first character ‘c’.
False, as it doesn’t apply for the empty string s=“ ”.
(iii) Every string s is either empty, or has a first character ‘c’. True.
Chapter 5
Normal Forms, Proof and Argument

Abstract In boolean logic, logical formulae are usually reduced to standardised


(or normalised) forms, which are more appropriate for automated theorem proving.
Such an example are the disjunctive normal form (d.n.f), represented by conjunctive
clauses connected by the disjunction operator (OR, ∨), and the conjunctive normal
form (c.n.f), represented by disjunctive clauses connected by the conjunction operator
(AND, ∧). In this Chapter we present the formal definition and derivation of d.n.f and
c.n.f, along with some deduction rules. We then present some popular arguments such
as the proof by contradiction, the proof by induction and the pigeonhole principle.

Keywords Normal forms (d.n.f, c.n.f) · Deduction rules · Special arguments

5.1 Brief Theoretical Background

In this chapter we use the boolean variables {0, 1}.

Disjunctive Normal Form (d.n.f)

Equivalent way of writing the truth table, first using ∧, then using ∨.
Example 1: Find the d.n.f of ¬(¬ p ∧ q) (Fig. 5.1).
Step 1: Truth table
p ¬p q ¬q ¬ p ∧ q ¬(¬ p ∧ q)
1 0 1 0 0 1
1 0 0 1 0 1
0 1 1 0 1 0
0 1 0 1 0 1

O. Bagdasar, Concise Computer Mathematics, SpringerBriefs in Computer Science, 35


DOI: 10.1007/978-3-319-01751-8_5, © The Author(s) 2013
36 5 Normal Forms, Proof and Argument

Fig. 5.1 d.n.f using Truth


tables and Venn diagrams

Step 2: Identify 1 entries in the last column.


Step 3: For each entry, the 1s in the first 4 columns are connected by ∧.
Solution: d.n. f (¬(¬ p ∧ q)) = ( p ∧ q) ∨ ( p ∧ ¬q) ∨ (¬ p ∧ ¬q)

Example 2: For H : d.n. f (H ) = ( p ∧ q ∧ r ) ∨ ( p ∧ ¬q ∧ ¬r ) ∨ (¬ p ∧ ¬q ∧ r )


The corresponding regions in the Venn diagram are: 7, 1, 2.

Conjunctive Normal Form (c.n.f)

Conjunctive normal form (c.n.f.) combines the basic literals, first using ∨, then
using ∧.
Algorithm for finding the c.n.f of a formula H:

Step 1: Find the d.n.f of ¬H


Step 2: Find the negation of this d.n.f

Observation 1: The expressions d.n.f(H) and c.n.f(H) have the same truth value
(maybe less evident for c.n.f, where which is basically equivalent to ¬(¬ H)) as they
are just different ways of writing H .

Example 3: Find the c.n.f of H = ¬ p ∧ q.

Step 1: Truth table and d.n.f of ¬H

p q ¬p H ¬H
1 1 0 0 1
1 0 0 0 1
0 1 1 1 0
0 0 1 0 1

d.n. f (¬H ) = ( p ∧ q) ∨ ( p ∧ ¬q) ∨ (¬ p ∧ ¬q).


5.1 Brief Theoretical Background 37

Step 2: As c.n.f(H) is the negation of d.n.f(¬H) one obtains


 
c.n. f (H ) = ¬d.n. f (¬H ) = ¬ ( p ∧ q) ∨ ( p ∧ ¬q) ∨ (¬ p ∧ ¬q)

= (¬ p ∨ ¬q) ∧ (¬ p ∨ q) ∧ ( p ∨ q)

Example 4: For the variable H on the previous page, d.n. f (¬H ) is

d.n. f (¬H ) = ( p ∧ q ∧ ¬r ) ∨ ( p ∧ ¬q ∧ r ) ∨ (¬ p ∧ q ∧ r ) ∨ (¬ p ∧ q ∧ ¬r )
∨ (¬ p ∧ ¬q ∧ ¬r )
c.n. f (H ) = (¬ p ∧ ¬q ∧ r ) ∨ (¬ p ∧ q ∧ ¬r ) ∨ ( p ∧ ¬q ∧ ¬r )
∨ ( p ∧ ¬q ∧ r ) ∨ ( p ∧ q ∧ r )

Deduction Rules
Rules defining a valid argument from a fallacious argument.
When the conjunction of all the assumptions implies the conclusion (this can be
checked using truth tables), the argument is valid.

Proof of Modus Ponens


If both p and p ⇒ q are true (circled), and also q is true (underlined), then

p q p⇒q ¬q ¬p

1 1 
1 0 0
1 0 0 1 0
0 1 1 0 1
0 0 1 0 1

the argument is valid!

Common Deduction Rules


The other common deduction rules are summarised in the table below.

Valid Fallacious
Modus Ponens Converse error
p ⇒ q; p p ⇒ q; q
q p
Modus Tollens Inverse error
p ⇒ q; ¬q p ⇒ q; ¬ p
¬p ¬q
38 5 Normal Forms, Proof and Argument

Special Types of Argument

Proof by Contradiction
This argument is based on Modus Tollens.

Example. Prove that 2 is not rational (fraction).

Proof. Assume that 2 is rational. Then √ there are two natural numbers p, q s.t they
have no common divisors and qp = 2, therefore p 2 = 2q 2 . Because p and q are
natural numbers, p 2 is a multiple of 2, so p itself is a multiple of 2 ( p = 2 p1 ). In
this case, (2 p1 )2 = 2q 2 , which gives 2 p12 = q 2 . Using the same argument as before,
q = 2q1 . We have just proved that 2 is a common divisor of p and q, which is a
contradiction.

The Pigeonhole Principle


If n items are put into m pigeonholes with n > m, then at least one pigeonhole
must contain more than one item. This simple theorem has numerous applications
in Mathematics and Computer Science.
Example. (Hand-shaking problem) In a room there are n > 1 people who can shake
hands with one another. Show that there is always a pair of people who will shake
hands with the same number of people.
Proof. Each person can shake hands with anybody from 0 to n − 1 other people,
therefore we have n − 1 possible ‘holes’, because either the ‘0’ or the ‘n − 1’ hole
must be empty (we can’t have persons shaking hands with no one, if there is a person
who shook hands with everyone). This leaves n people to be placed in at most n − 1
non-empty holes, ensuring duplication.

5.2 Essential Questions

E 5.1. Find the d.n.f. of the following formulae:


(i) ¬(¬ p ∧ q); (ii) ¬(¬ p ∨ q); (iii) p ∧ (q ∨ r ).
E 5.2. Find the d.n.f and c.n.f of the following statements
(i) p ∨ (q ∧ r ); (ii) ¬(( p ∧ q) ∨ r ); (iii) p ⇔ (q ∧ r ).
E 5.3. Given the following truth table for a formula H, find the disjunctive normal
form for H and hence find the conjunctive normal form of H (Fig. 5.2).

p ¬p q ¬q H
1 0 1 0 1
1 0 0 1 0
0 1 1 0 0
0 1 0 1 1
5.2 Essential Questions 39

Fig. 5.2 d.n.f using Truth


tables and Venn diagrams

E 5.4. Find d.n.f(G) and c.n.f(G) for formula G in the truth table below and identify
the corresponding regions in the Venn diagram.
E 5.5. Use truth tables to prove the arguments Modus Ponens, Modus Tollens, Inverse
Error and Converse Error.

E 5.6. Prove that if the product ab is even, then either a or b has to be even.

E 5.7. In a box there are 3 blue balls, 5 red balls and 7 yellow balls. Calculate the
minimum number of balls needed to be drawn from the box before
(i) a pair of the same color can be made;
(ii) pairs of each color are made.

5.3 Supplementary Questions

S 5.1. Find the d.n.f. and c.n.f of the following formulae:


(i) ¬ p ∧ (q ⇒ p); (ii) ¬ p ∧ (¬q ∨ r ); (iii) ¬ p ∧ (q ∨ r ).

S 5.2. Find the d.n.f and c.n.f of the following expressions:


(i) ¬(p ⇒ q); (ii) p ⇒ (q∧r); (iii) ¬(p ⇔ q).

S 5.3. Find the c.n.f of the following formulae:


(i) ¬ p ∧ (q ⇒ p); (ii) ¬ p ∧ (¬q ∨ r ); (iii) ¬ p ∧ (q ∨ r ).

S 5.4. Use the deduction rules discussed before to prove which of the following
arguments are valid and which are fallacious (use truth tables).

(i) If the apple is ripe, then it will be sweet;


The apple is sweet.
Therefore the apple is ripe.
40 5 Normal Forms, Proof and Argument

(ii) If I go to the pub, I won’t finish my revision;


If I don’t finish my revision, I won’t do well in the exam tomorrow.
Thus if I go to the pub, I won’t do well in the exam tomorrow.

(iii) Gill is playing rugby if Tom is not in class;


If Gill is not playing rugby then Tom will be in class.
Therefore Tom is not in class or Gill is not playing rugby.

(iv) Sam is studying maths or Sam is studying economics;


Sam is required to take logic if he is studying maths.
So Sam is an economics student or he is not required to take logic.

(v) English men wear bowler hats;


The man is wearing a bowler hat.
Therefore he must be English.

S 5.5. Comment on the following argument.


Storing on floppy disk is better than nothing.
Nothing is better than a hard disk drive.
Therefore, storing on floppy disk is better than a hard disk drive.

S 5.6. Prove that if a, b ∈ Z, then a 2 − 4b = 3.

S 5.7. Prove that if six distinct numbers are selected from {1, 2, . . . , 9}, then some
two of these numbers sum to 10.

5.4 Problem Answers

Essential Problems

E 5.1. We first find the truth table for each expression.


Note: The truth tables for (i) and (ii) are combined to save time and space.
Let G= ¬(¬ p ∧ q) and H= ¬(¬ p ∨ q). The truth table for G and H is

p ¬p q ¬q ¬ p ∧ q ¬ p ∨ q G H
1 0 1 0 0 1 1 0
1 0 0 1 0 0 1 1
0 1 1 0 1 1 0 0
0 1 0 1 0 1 1 0
5.4 Problem Answers 41

One obtains (i) ( p ∧ q) ∨ ( p ∧ ¬q) ∨ (¬ p ∧ ¬q); (ii) p ∧ ¬q;


(iii) ( p ∧ q ∧ r ) ∨ ( p ∧ ¬q ∧ r ) ∨ ( p ∧ q ∧ ¬r ).

E 5.2. We first need to find the truth table for the expressions
A = p ∨ (q ∧ r ), B = ¬(( p ∧ q) ∨ r ) and C = p ⇔ (q ∧ r ) and their negations.

p q r q ∧ r p ∧ q ( p ∧ q) ∨ r A B C ¬A ¬B ¬C
1 1 1 1 1 1 1 0 1 0 1 0
1 1 0 0 1 1 1 0 0 0 1 1
1 0 1 0 0 1 1 0 0 0 1 1
1 0 0 0 0 0 1 1 0 0 0 1
0 1 1 1 0 1 1 0 0 0 1 1
0 1 0 0 0 0 0 1 1 1 0 0
0 0 1 0 0 1 0 0 1 1 1 0
0 0 0 0 0 0 0 1 1 1 0 0

(i) Using the columns corresponding to A and ¬A we obtain

d.n.f(A) = ( p ∧ q ∧ r ) ∨ ( p ∧ q ∧ ¬r ) ∨ ( p ∧ ¬q ∧ r ) ∨ ( p ∧ ¬q ∧ ¬r )
∨ (¬ p ∧ q ∧ r ).
c.n.f(A) = ¬d.n.f(¬A) = ( p ∨ ¬q ∨ r ) ∧ ( p ∨ q ∨ ¬r ) ∧ ( p ∨ q ∨ r ).

(ii) Using the columns corresponding to B and ¬B we obtain

d.n.f(B) = ( p ∧ ¬q ∧ ¬r ) ∨ (¬ p ∧ q ∧ ¬r ) ∨ (¬ p ∧ ¬q ∧ ¬r ) and
c.n.f(B) = (¬ p ∨ ¬q ∨ ¬r ) ∧ (¬ p ∨ ¬q ∨ r ) ∧ (¬ p ∨ q ∨ ¬r ) ∧ ( p ∨ ¬q ∨ ¬r )
∧ ( p ∨ q ∨ ¬r ).

(iii) Using the columns corresponding to C and ¬C we obtain

d.n.f(C) = ( p ∧ q ∧ r ) ∨ (¬ p ∧ q ∧ ¬r ) ∨ (¬ p ∧ ¬q ∧ r ) ∨ (¬ p ∧ ¬q ∧ ¬r ).
c.n.f(C) = (¬ p ∨ ¬q ∨ ¬r ) ∧ (¬ p ∨ q ∨ ¬r ) ∧ (¬ p ∨ q ∨ r ) ∧ ( p ∨ ¬q ∨ ¬r ).

E 5.3. As seen above, we add the column corresponding to ¬H and obtain


d.n.f(H) = ( p ∧ q) ∨ (¬ p ∧ ¬q); c.n.f(H) = ¬d.n.f(¬H ) = (¬ p ∨ q) ∧ ( p ∨ ¬q).

E 5.4. As seen above, we add the column corresponding to ¬G and obtain

d.n.f(G) = ( p ∧ q ∧ ¬r ) ∨ (¬ p ∧ q ∧ ¬r ) ∨ (¬ p ∧ ¬q ∧ r ) ∨ (¬ p ∧ ¬q ∧ ¬r ).
c.n.f(G) = (¬ p ∨ ¬q ∨ r ) ∧ (¬ p ∨ q ∨ ¬r ) ∧ (¬ p ∨ q ∨ r ) ∧ ( p ∨ ¬q ∨ ¬r ).

E 5.5. Check if the conjunction of all assumptions implies the conclusion.


Modus Tollens: If both ¬q and p⇒q are true (circled), then ¬p is true.
42 5 Normal Forms, Proof and Argument

p q p⇒q ¬q ¬ p
1 1 1 0 0
1 0 0 1 0
0 1 1 0 1
0 0 1 1 1

The argument is valid!


Converse error: If both q and p⇒q are true (circled), then p is true or false!

p q p ⇒q ¬q ¬p
1 1 
1 0 0
1 0 0 1 0
0 1 
1 0 1
0 0 1 1 1

The argument is fallacious, because of line 3 (true does not imply false)!
The proof for Inverse error is similar and left for the reader.

E 5.6. Suppose that none of a or b is even, therefore there exist c, d ∈ Z s.t. a = 2c+1
and b = 2d + 1, whose product is ab = (2c + 1)(2d + 1) = 4cd + 2c + 2d + 1.
As the latter number is odd, and there was no error in the proof, the only problem
seems to have been the initial assumption. This proves that either a or b is even.
E 5.7. (i) For this to happen one can split the balls into 3 holes containing balls of
the same colour. We surely have a repetition if we extract 4 balls.
(ii) In worst case scenario all the balls of two colours have been extracted, but
just one of the remaining colour. The minimum number of balls is then
7 + 5 + 2 = 16.
Supplementary Problems

S 5.1–S 5.3. The solutions are similar to E 5.1–E 5.4.

S 5.4. The arguments are as follows:

(i) Defining p: “apple is ripe” and q: “apple is sweet” the proposition becomes

p ⇒ q; p
q (Modus Ponens, true)

(ii) For p: “go to the pub”, q: “finish my revision” and r: “do well in the exam” we
have
p ⇒ ¬q; ¬q ⇒ ¬r ; p
p ⇒ ¬r (true)
5.4 Problem Answers 43

(iii) For p: “Gill is playing rugby”, q: “Tom is in class” we have

¬q ⇒ p; ¬ p ⇒ q
¬q ∨ ¬ p (false)

(iv) For p: “Sam is studying maths”, q: “Sam is studying economics” and r: “Sam is
required to take logic” the argument is written as

p ∨ q; p ⇒ r
q ∨ ¬r (false)

(v) For p: “man is English”, q: “man wars bowler hat” we have

p ⇒ q; q
p (Converse Error, false)

S 5.5. Storing on floppy disk is better than nothing.


Nothing is better than a hard disk drive.
Therefore, storing on floppy disk is better than a hard disk drive.

S 5.6. Suppose this proposition is false. This conditional statement being false means
there exist numbers a and b for which a, b ∈ Z is true but a 2 − 4b = 3 is false. Thus
there exist integers a, b ∈ Z for which a 2 − 4b = 3, or equivalently a 2 = 4b + 3
(odd). This shows that a is odd, so there is c ∈ Z s.t. a = 2c + 1. From the equation
we obtain that a 2 = (2c + 1)2 = 4c2 + 4c + 1 = 4b + 3, which can be written as
c2 + c − b = 21 .

However, this is a contradiction as c2 + c − b is an integer. As all the logic after


the first line of the proof is correct, it must be that the first line was incorrect. In other
words, we were wrong to assume the proposition was false. Thus the proposition is
true.

S 5.7. Since six numbers have to be selected, the Pigeonhole Principle guarantees that
at least two of them are selected from one of the five sets {1, 9}, {2, 8}, {3, 7}, {4, 6},
{5, 5}, and therefore their sum is 10.
Chapter 6
Vectors and Complex Numbers

Abstract A vector is a geometric object that has magnitude (or length) and direction
and can be added to other vectors or multiplied by a scalar. Vectors play an important
role in many branches of science, such as physics or engineering. They allow us
to represent the position of objects in two, three or more dimensions and to store
complex data having multiple components, such as velocity, temperature, pressure,
etc. In this chapter we define vectors and their basic operations, such as addition,
multiplication by a scalar, dot and cross products, together with some direct applica-
tions. The complex numbers in algebraic (vectors having one real and one imaginary
component in the Argand diagram) and polar (vectors having radius and argument)
form of are then presented, along with some of key operations and results.

Keywords Vector operations · Dot and cross product · Complex numbers ·


Algebraic and polar form · Operations with complex numbers

6.1 Brief Theoretical Background

In this section are presented notions about vectors and complex numbers.

Vectors

A geometric has direction as well as magnitude.


A vector is an ordered collection of n elements (components).


→ −→
a , a , AB, a, â
a = (a1 , a2 , a3 ), a = (ax , a y , az ), a = [ax , a y , az ]

O. Bagdasar, Concise Computer Mathematics, SpringerBriefs in Computer Science, 45


DOI: 10.1007/978-3-319-01751-8_6, © The Author(s) 2013
46 6 Vectors and Complex Numbers
⎡ ⎤
a1
a = ⎣a2 ⎦ (column), a(length of vectora)
a3
−→
A(a1 , a2 , a3 ), B(b1 , b2 , b3 ) ⇒ AB = (b1 − a1 , b2 − a2 , b3 − a3 ).

Properties and Operations

• Equality: Two vectors are equal if they have same modulus and direction.
• Zero vector: 0 = (0, 0, 0) ∈ R3 (3D) or 0 = (0, 0) ∈ R2 (2D)
• Unit vectors: a. Sometimes denoted by â.
• Addition:
−→ −→ −→
– AB + BC = AC (triangle rule for geometric vectors)
– Let a = (a1 , a2 , a3 ) and b = (b1 , b2 , b3 ) be two vectors. Their sum is

a + b = (a1 + b1 , a2 + b2 , a3 + b3 ).

• Multiplication by a scalar: Let a = (a1 , a2 , a3 ) be a vector and λ ∈ R. Then

λa = λ(a1 , a2 , a3 ) = (λa1 , λa2 , λa3 ).

• Subtraction: a − b = a + (−1)b.
• Polygon addition rule: Let A, B, C, D be points in plane (space). Then
−→ −→ −→ −→
AB + BC + C D + D A = 0. (same is true for a polygon A1 , . . . , An )

Centre of Mass

The system of points A1 , . . . , An has the centre of mass M given by


−−→ −−→ −−→
−−→ O A1 + O A2 + · · · + O An
OM = .
n
If the points A1 , . . . , An of the system have masses m 1 , . . . , m n then the centre of
mass M is given by
−−→ −−→ −−→
−−→ m 1 O A1 + m 2 O A2 + · · · + m n O An
OM = .
m1 + m2 + · · · + mn
6.1 Brief Theoretical Background 47

Dot (Scalar) Product

Let a = (a1 , a2 , a3 ) and b = (b1 , b2 , b3 ) be two vectors. The dot product is

a · b = a1 · b1 + a2 · b2 + a3 · b3
= ab cos θ

Applications of the Scalar Product

• The modulus of vector a = (a1 , a2 , a3 ) is obtained from the formula

√ 
a = a · a = a12 + a22 + a32

• The angle θ between vectors a and b is

a·b
cos θ =
ab

• Vectors a and b are perpendicular if and only if

θ = π/2 ⇔ cos θ = 0 ⇔ a · b = 0

• Unit vectors for a (same direction, modulus one):

a = aâ

Cross Product (Vector Product)

Let a = (a1 , a2 , a3 ), b = (b1 , b2 , b3 ) be vectors. The cross (vector) product is

a × b = ab sin θ n̂ (n̂ is the normal vector).

The normal vector on a, b is the unit vector perpendicular on both a and b.


Standard base: i = (1, 0, 0); j = (0, 1, 0); k = (0, 0, 1).
For these vectors we have
48 6 Vectors and Complex Numbers

i × j = k; j × i = −k
j × k = i; k × j = −i
k × i = j; i × k = −j
i×i=j×j=k×k =0

Vector product in coordinate notation: For coordinate vectors we have

a = (a1 , a2 , a3 ) = a1 i + a2 j + a3 k.
b = (b1 , b2 , b3 ) = b1 i + b2 j + b3 k.

the vector product is defined as

a × b = (a1 i + a2 j + a3 k) × (b1 i + b2 j + b3 k)
= (a2 b3 − a3 b2 )i − (a1 b3 − a3 b1 )j + (a1 b2 − a2 b1 )k

Applications of the Cross (Vector) Product

• The area of a triangle between vectors a = (a1 , a2 , a3 ) and b = (b1 , b2 , b3 ) is


obtained from the formula

S = a × b

• The angle θ between vectors a and b is

a × b
sin θ =
ab

• Vectors a and b are parallel if and only if

θ = 0 ⇔ sin θ = 0 ⇔ a × b = 0

• Find a vector perpendicular to a plane. Take points A, B, C ∈ R3 :


−→ −→ −→
AB × AC ⊥ AB
−→ −→ −→
AB × AC ⊥ AC
6.1 Brief Theoretical Background 49

Fig. 6.1 Algebraic and polar


representation of a complex
number

Complex Numbers

A complex number√can be expressed in the form a + bi, where a and b are real
numbers and i = −1 is the imaginary number representing the solution of the
equation x 2 + 1 = 0 (the notation j is used in Engineering) (Fig. 6.1).
Notation

z = a + bi, a, b ∈ R (algebraicform)
z = re iθ
= r (cos θ + i sin θ ) r, θ ∈ R (polar form)
a = (z) = r cos θ (real part)
b = (z) = r sin θ (imaginary part)

r = |z| = a 2 + b2 (modulus)
θ ∈ [0, 2π ) (argument)

Modulus Property

|z|2 = zz = (a + bi)(a − bi) = a 2 + b2 .

The Complex Conjugate


The conjugate of z = a + bi is the number z = a − bi.
Properties of the Conjugate

z1 + z2 = z1 + z2
z1 z2 = z1 z2
z 1 /z 2 = z 1 /z 2
1
(z) = (z + z),
2
1
(z) = (z − z)
2i
50 6 Vectors and Complex Numbers

Basic Operations with Complex Numbers

Let z 1 = a + bi and z 2 = c + di.


The following operations are defined:
• Addition:

z 1 + z 2 = (a + bi) + (c + di) = (a + c) + (b + d)i

• Substraction:

z 1 − z 2 = (a + bi) − (c + di) = (a − c) + (b − d)i

• Multiplication (remember that i 2 = −1)

z 1 z 2 = (a + bi)(c + di)
= ac + adi + bci + bdi 2
= (ac − bd) + (ad + bc)i

• Division (remember that i 2 = −1 and zz = |z|2 )

z1 a + bi (a + bi)(c − di)
= =
z2 c + di (c + di)(c − di)
ac + bd bc − ad
= 2 + 2 i
c +d 2 c + d2

Example: Let z 1 = 3 + 2i and z 2 = 1 − 4i. The following operations give

(1) z 1 + z 2 = 4 − 2i;
(2) z 1 − z 2 = 2 + 6i;
(3) z 1 z 2 = 11 − 10i;
 √
(4) |z 1 | = 32 + 22 = 13;
5 14
(5) z 1 /z 2 = − + i.
17 17
De Moivre’s formula
For the natural number n ≥ 2 and z = r eiθ = (cos θ + i sin θ ) we have

z n = r n einθ = r n (cos nθ + i sin nθ )

Remark: This formula is useful for computing powers of complex numbers.


6.2 Essential Questions 51

6.2 Essential Questions

E 6.1. Decide which of the following are vector quantities: Velocity, mass, acceleration,
weight, area, temperature, force, potential energy, volume.
E 6.2. In a system of vectors , a force of 10 newtons is represented by a line 5cm
long. Find the magnitude of the force vector represented in the system by a line 2cm
long.

E 6.3. (i) Find the magnitude of the following vectors:

a = −2i + 3j + 4k; b = i + 2j − 4k; c = 3i − 2j + k; d = 2j + 3k.

(ii) Find unit vectors parallel to the following vectors

u = i − 2j + 4k; v = 0.5i + 2j − k; w = i − 2j;

(iii) Find x ∈ R such that c = (x, 1, 1) is perpendicular on u, v and w.

E 6.4. (i) Evaluate the dot products of the following pairs of vectors:

(a) p = 2i − 3j + k, q = 3i + 4j − 3k
(b) m = 2i − 3j + 5k, l = 3i + 2j − 2k
(c) v = i + 2j + 2k, u = −2i − j + 2k

(ii) Use the definition of the dot product and your answers to part (i) to deter-
mine the angles between the pairs of vectors in part (i).

(iii) For each pair in (i), find a vector perpendicular on both vectors.

E 6.5. Evaluate the cross products of the following vectors:

(a) p = 2i − j + k, q = 3i + 4j − k
(b) a = i + 2j − 3k, b = −2i + 4j + k
(c) r = i + 2j − 3k, s = 2i + j + 3k.

E 6.6. Let z 1 = 2 + 3i and z 2 = 3 − 4i. Compute the following

(1) z 1 + z 2 ; (2) z 1 − z 2 ; (3) z 1 z 2 ; (4) |z 1 |; (5) z 1 /z 2 ;


(6) z 1 − z 2 ; (7) z 1 z 2 ; (8) z 2 z 2 ; (9) z 13 ; (10) z 14 .
52 6 Vectors and Complex Numbers

6.3 Supplementary Questions

S 6.1. Let ABCD be a quadrilateral. Find the single vectors equivalent to


−→ −→ −→ −→ −→ −→ −→ −→ −→ −→ −→
(i) AB + BC; (ii) AB + BC + C D; (iii) AD + DC; (iv) BC + C D; (v) AB + D A.
−→ −→ −→
S 6.2. O, A, B, C, D are five points such that O A = a, O B = b, OC = a + 2b and
−−→ −→ −→ −→ −→ −→
O D = 2a − b. Express AB, BC, C D, AC and B D in terms of a and b.
S 6.3. Let ABCD be a quadrilateral whose vertices are A(1,1), B(7,3), C(10,12),
−→ −→ −→ −→
D(8,2). Find the vectors: (i) AB; (ii) AC; (iii) AD; (iv) B D;
−→
S 6.4. For the quadrilateral ABCD in S 6.3., check whether the diagonals AC and
−→
B D are perpendicular. If not, what is the angle between them ?
S 6.5. Let ABCD be a quadrilateral of vertices A(2,1), B(5,5), C(9,12), D(8,2).

(i) Find the centre of mass G for the quadrilateral ABCD.


(ii) Find the centres of mass G A , G B , G C , G D corresponding to each of the trian-
gles BC D, AC D, AB D, ABC.
(iii) Find the centre of mass G 0 for the quadrilateral G A G B G C G D . What is the
relation between G 0 and G?
(iv) Let n ≥ 4, and let a1 , . . . , an and b1 , . . . , bn be real numbers. Formulate state-
ments similar to (i)–(iii) for the polygon A1 (a1 , b1 ), . . . , An (an , bn ).

S 6.6. Answer same questions as in S 6.5. (i)–(iii), when masses 1, 2, 3 and 4 kg are
attached to A, B, C and D, respectively.
S 6.7. Compute the following expressions:
(i) (1 + i) + (3 − 4i); (ii) 1+i
1−i ; (iii) i
2012 ; (iv) i 2013 ; (v) i 2014 ; (vi) (1 + i)2013 .

S 6.8. Solve in C the following equations:


(i) z 2 + 4z + 2 = 0;
(ii) z 2 + 2z + 4 = 0;
(iii) z 2 + 2z + 5 = 0.
(iv) i z 2 + (1 − 4i)z + 4i − 1 = 0.
S 6.9. Let z = 3 + 2i. Find the complex number u = a + bi s.t. u 2 = z.
S 6.10. Given that z is not real and |z| = 1, prove that the number w = z−1
z+1 is a pure
imaginary number.
6.4 Problem Answers 53

6.4 Problem Answers

Essential Problems
E 6.1. Vectors: velocity, acceleration, force.
E 6.2. 4 newtons. √ √ √ √
E 6.3. (i) a = 29; b = 21; c = 18; d = 13;
(ii) û = √1 (i − 2j + 4k); v̂ = √2 (0.5i + 2j − k); ŵ = √1 (i − 2j);
21 21 5
(iii) c ⊥ u: x = −2; c ⊥ v: x = −2; c ⊥ w: x=2.
E 6.4. (i) (a) p · q = −9; (b) m · l = −10; (c) v · u = 0.
(ii) (a) cos θp,q = √ −9
√ ; (b) cos θm,l = √ −10
√ ; (c) cos θv,u = 0.
14 34 38 17
(iii) In each case we have to find a vector a = (x, y, z) s.t.
(a) a · p = a · q = 0. Solution: a = (t, 9/5t, 17/10t), t ∈ R.
(b) a · m = a · l = 0. Solution: a = (t, −7/2t, −16/13t), t ∈ R.
(c) a · v = a · u = 0. Solution: a = (t, −t, 1/2t), t ∈ R.
E 6.5. (a) p×q = −3i +5j+11k; (b) a ×b = 14i +5j+8k; (c) r ×s = 9i −9j−3k.

E 6.6. (1) 5 − i; (2) −1 + 7i; (3) 18 + i; (4) 13; (5) (−6 + 17i)/25; (6)
−1 − i; (7) −6 + 17i; (8) 25; (9) −46 + 9i; (10) 119 − 12i.
Supplementary Problems
−→ −→ −→ −→ −→
S 6.1. (i) AC; (ii) AD; (iii) AC; (iv) B D; (v) D B.
−→ −→ −→ −→ −→
S 6.2. AB = b − a; BC = a + b; C D = a − 3b; AC = 2b; B D = 2a − 2b.
−→ −→ −→ −→
S 6.3. (i) AB = (6, 2);(ii) AC = (9, 11); (iii) AD = (7, 1); (iv) B D = (1, −1).
−→ −→
S 6.4. The two diagonals are perpendicular if the dot product is zero, i.e. AC · B D = 0.
−→ −→
However, in this case the dot product is AC · B D = 9 · 1 + 11 · (−1) = −2 = 0,
which shows that the two vectors are not perpendicular. The cosine of the angle can
obtained from the formula
−2 −1
cos θ−
→ −→ = √
AC, B D
√ =√ .
202 2 101

S 6.5. (i) Using the formula for the centre of mass one obtains
−→ −→ −→ −−→
−−→ O A + O B + OC + O D
OG = ,
4
therefore G(6, 5).
(ii) The centres are G A = ( 22
3 , 3 ), G B = ( 3 , 5), G C = (5, 3 ), G D =
19 19 8

3 , 6).
( 16
54 6 Vectors and Complex Numbers

(iii) One obtains


 22
+ 19
+5+ 16 19
+5+ 8
+6
G0 = 3 3 3
, 3 3
= (6, 5) = G.
4 4

(iv) Let n ≥ 4 be a natural number and let the polygon A1 (a1 , b1 ), . . . , An (an , bn )
have the center of mass G. For the polygons Ai , i = 1, . . . , n (obtained
by deleting the i-th vertex from the original polygon) having centre of
mass G i the following property holds: the centre of mass G 0 of the points
G 1 , . . . , G n coincides with the center of mass G.

S 6.6. In this case the weighted formula for the center of mass yields

1(2, 1) + 2(5, 5) + 3(9, 12) + 4(8, 2) 71 55
Gw = )= , .
4 4 4

S 6.7. (i) 5 − 3i; (ii) i; (iii) 1; (iv) i; (v) −1; (vi) 1 + i = 2eiπ/4 therefore
√ 2013 i 2013π √ 5π
(1 + i)2013 = 2 e 4 = 2 2013 e i(126·2π + 4 )
√ 5π 5π √
= 2 2013 (cos + i sin ) = 2 2013 (−1/2 − i/2).
4 4
S 6.8. Applying the normal quadratic formula one obtains the solutions
√ √
(i) z 1,2 = −4± 2·1
42 −4·2
= −2 ± 2 ∈ R (both solutions are real).
√ √
(ii) z 1,2 = −2± 2·1
22 −4·4
= −1 ± 3i ∈ C \ R (both solutions are complex).

−2± 22 −4·5
(iii) z 1,2 = 2·1 √ = −1 ± 2i ∈ C \ R (both solutions are complex).

−(1−4i)± (1−4i) −4i·(4i−1)
2 √
(iv) z 1,2 = 2·i = 21 (4 + i) ± 4i − 1 .

S 6.9. One needs to solve (a + bi)2 = a 2 − b2 + 2abi = 3 + 2i which is equivalent


to a 2 − b2 = 3 and ab = 1 which gives a 2 − 1/a 2 = 3. Substituting x = a 2
the problem reduces
√ to solving the quadratic x 2 − 1 − 3x = 0 with the√solutions
x1,2 = 2 (3 ± 13). As x = a 2 the only solutions are a1,2 = ± 21 (3 + 13) and
1

b1,2 = 1/a1,2 .
S 6.10. Let z = a + bi. By amplification with z + 1 one obtains

z−1 (z + 1)(z − 1) zz − z + z − 1 2bi 2b


w= = = = = i.
z+1 (z + 1)(z + 1) |z + 1| 2 |z + 1| 2 |z + 1|2
Chapter 7
Matrices and Applications

Abstract A matrix is a rectangular array of numbers, symbols, or expressions (called


entries or elements) arranged in rows and columns. Matrices are direct generalisations
of vectors and play a key role in many mathematical areas such as linear algebra or
computer graphics (where they are used to define linear transformations). In this
chapter we define matrices and illustrate their properties through examples. We then
present some basic matrix operations such as addition and multiplication, as well as
the determinant and inverse of square matrices. Finally, matrices are used for solving
systems of linear equations. These results prepare the introduction of matrix based
linear transformations in computer graphics, discussed in the next chapter.

Keywords Matrix operations · Determinant · Inverse · Systems of equations

7.1 Brief Theoretical Background

A matrix is an array of numbers called elements. Horizontal components are called


rows (or lines) while the vertical components are called columns. A matrix with m
rows and n columns is of dimension (size, order) m × n. Two matrices are equal if
they have same dimension and elements.

Example: The following matrix has 2 rows and 4 columns


 
2013
.
1312

Special Types of Matrices (Orderwise)


   
• row matrix: 1 0 1 or 1 0 2

O. Bagdasar, Concise Computer Mathematics, SpringerBriefs in Computer Science, 55


DOI: 10.1007/978-3-319-01751-8_7, © The Author(s) 2013
56 7 Matrices and Applications
⎛ ⎞ ⎡ ⎤
1 1
• column matrix: ⎝0⎠ or ⎣0⎦
2 2
   
20 20
• square matrix (i.e. m = n): or
13 13

Matrix Notation

• Matrices are usually denoted by capital letters.


 
A= 102

• a matrix of order m × n is denoted by


⎡ ⎤
a11 ··· a1 j · · ·a1n
⎢ .. .. .. . . .. ⎥
⎢ . . . . . ⎥
⎢ ⎥
A=⎢
⎢ ai1 ··· ai j · · · ain ⎥

⎢ .. .. .. . . .. ⎥
⎣ . . . . . ⎦
am1 · · · am j · · · amn

• ai j denotes the element in row i and column j.

• A matrix of order m × n in X is a also a function

A : {1, . . . , m} × {1, . . . , n} → X, A(i, j) = ai j .

Special Matrices

• The zero matrix: all its elements are zero. The zero 2 × 2 matrix is
 
00
0=
00

• Diagonal matrix: Square, elements outside the main diagonal are zero.
⎡ ⎤
 
−1 0 0
−1 0
A= , B = ⎣ 0 3 0⎦ , . . .
0 2
0 07

• The identity matrix: Diagonal matrix with 1 on the main diagonal.


7.1 Brief Theoretical Background 57
⎡ ⎤
 100
10
I2 = , I3 = ⎣0 1 0⎦ , . . .
01
001

• Lower/Upper triangular matrix: All Elements below/above main diagonal are


zero.
⎡ ⎤
  224
30
L= , U = ⎣0 4 3⎦ , . . .
12
001
Matrix Operations
• Matrix transpose: changes columns with rows.
First column becomes first row, second column becomes second row...
   
ab a c
If A = , then A =
T
cd bd

Remark 1: The transpose of an m × n matrix is an n × m matrix.


Remark 2: A matrix for which A = AT is called symmetric.
• Addition and subtraction:
– element by element operation
– matrices need to be of the same order (dimension, size)
– addition and subtraction are done element by element.
• Multiplication by a scalar: Let A be a matrix and λ ∈ R a scalar (number).
The matrix λA is obtained by multiplying each element of A by λ.
   
ab λa λb
λA = λ =
cd λc λd

• Multiplication of two matrices: the rule of row-column multiplication.


(Fig. 7.1) Let A, B be matrices of sizes m × n and n × p respectively. The element
cij in C = AB is obtained by multiplying
– the i-th row of A by
– the j-th column of B.
Example: A(3 × 2) × B(2 × 4) = C(3 × 4).
 
ab
• Determinant (2 × 2 matrix): The determinant of matrix A = is
cd
 
a b 
|A| =  = ad − bc = det A
c d
58 7 Matrices and Applications

Fig. 7.1 Matrix multiplica-


tion diagram

 
ab
• Inverse (2 × 2 matrix): The inverse of matrix A = is
cd
 
−1 1 d −b
A =
|A| −c a

Remark 3: The inverse only exists when |A| = 0!


Remark 4: A matrix for which A−1 = AT is called orthogonal.
Remark 5: Determinants exist for any square matrix of higher order.

Solution of a Linear System of Equations

Example: Consider the system of linear equations

2x + 3y = 13
3x + 4y = 18

This can be written in matrix form as


    
23 x 13
=
34 y 18

The solution is
      
x −1 4 −3 13 2
= x = A b = (−1) =
y −3 2 18 3

Remark 6: Any linear systems of equations has a matrix representation.


7.2 Essential Questions 59

7.2 Essential Questions

E 7.1. State the dimension of each of the following matrices:


⎡ ⎤ ⎡ ⎤
  2 4 1
123  
(a) A = , (b) B = ⎣2 3⎦ , (c) C = 1 2 , (d) D = ⎣2⎦.
212
3 1 3

E 7.2. Indicate whether the following statements are true or false, for the matrices
below
 
531  
(a) A = , (b) B = 5 3 1 ,
213
   
  12 32
(c) C = 10/2 6/2 1 , (d) D = , (e) E = .
23 21

(i) D = A; (ii) D = E; (iii) B = C; (iv) A = B; (v) D − C = 0.


E 7.3. Let A, B and C be the matrices defined below
     
531 123 011
A= , B= , C= .
213 432 232

Compute each of the following


(i) A + C; (ii) A − B; (iii) 2A; (iv) − 3C; (v) A − 2B + 4C; (vi) BT ;
(vii) B + CT .
E 7.4. Form the product of the following matrices, stating whether the result is a
column or row vector:
⎡ ⎤
 
  23   231
(i) 5 3 , (ii) 5 3 1 ⎣1 2 4⎦ ,
12
312
⎡ ⎤
   5 
53 2 
(iii) , (iv) ⎣3⎦ 1 2 4 .
23 3
1

 
53
E 7.5. Find the determinant of the matrix , and find its inverse.
42
Use the result to solve the pair of simultaneous equations

5x + 3y = 19
4x + 2y = 14

E 7.6. Let the matrices A, B, C be


60 7 Matrices and Applications
⎡ ⎤ ⎡ ⎤ ⎡ ⎤
531 123 011
A = ⎣2 1 3⎦ , B = ⎣4 3 2⎦ , C = ⎣2 3 2⎦.
312 213 521

Compute (i) AB; (ii) AC; (iii) CA. Show that AC  = CA, therefore matrix
multiplication is generally not commutative.

7.3 Supplementary Questions

S 7.1. Compute (i) A + B; (ii) AT + BT ; (iii) (A + B)T for the matrices


⎡ ⎤ ⎡ ⎤
5 3 1 3
Let A = ⎣2 1⎦ and B = ⎣4 2⎦ .
3 1 2 3

What is the connection between the results for parts (ii) and (iii) above?
S 7.2. Consider the matrices defined below
⎡ ⎤ ⎡ ⎤
23 4 2
C = ⎣4 2⎦ and D = ⎣3 1⎦.
31 2 3

Compute (i) C + D; (ii) C − D; (iii) (C − D)T ; (iv) C + CT ; (v) (CT )T for


S 7.3. Compute AI and IA for the matrices

  
53 10
A= and I = .
21 01

What does this suggest about the multiplication by the identity matrix?
S 7.4. Evaluate the result of the operations
 (i) AT ; (ii) AAT ; (iii) AT A; (iv) AA and
531
(v) AT AT for the matrix A = . In general, if A is an m × n matrix, what
213
can you say about the size of matrices AAT and AT A?
S 7.5. Given the following matrices
⎡ ⎤
1    
 01 232
⎣ ⎦
A= 531 , B= 2 , C= , D= .
23 521
3
7.3 Supplementary Questions 61

State the size of the following products and compute the ones that exist.
(i) AB; (ii) BA; (iii) CD; (iv) DB; (v) DC; (vi) BD; (vii) A2 ; (viii) C2 .

S 7.6. Evaluate the determinants of the following matrices:


⎡ ⎤
      232
 12 26 42
A= 53 , B= , C= , D= , E = ⎣5 2 1⎦.
35 13 35
143

Find the inverses of the 2 × 2 matrices B, C and D.


S 7.7. Solve the following systems of linear equations using the inverse matrix
method.
  
3x − y = 4 4x − 3y = 6 −2x + y = −4
(i) (ii) (iii)
2x − y = −11 3x − 4y = −9 x + 2y = 2

S 7.8. State the reason why the inverse matrix method cannot be applied to the
system 
4x + 6y = 12
2x + 3y = 14

S 7.9. Write the following system in matrix form




⎨4x + 6y + 3z = 28
2x + 3y + 2z = 15


3x + 2y + 5z = 19

7.4 Problem Answers

Essential Problems
E 7.1. (a) 2 × 3; (b) 3 × 2; (c) 1 × 2; (d) 3 × 1.
E 7.2. (a) F; (b) F; (c) T; (d) F; (e) F.
     
542 4 1 −2 10 6 2
E 7.3. (i) A + C = ; (ii) A − B = ; (iii) 2A = ;
445 −2 −2 1 4 26
⎡ ⎤
    14
0 −3 −3 3 3 −1
(iv) −3C = ; (v) A − 2B + 4C = ; (vi) BT = ⎣2 3⎦;
−6 −9 −6 27 7
32
(vii) operation not defined.
   
E 7.4. (i) row matrix: 13 21 ; (ii) row matrix: 16 22 19 ;
62 7 Matrices and Applications
⎡ ⎤
 5 10 20
16
(iii) column matrix: ; (iv) square matrix: ⎣3 6 12⎦ .
13
1 2 4
 
−1 1.5
E 7.5. det A = −2; A−1 = ; x = 2; y = 3.
2 −2.5
⎡ ⎤ ⎡ ⎤ ⎡ ⎤
19 20 24 11 16 12 5 2 5
E 7.6. (i) AB = ⎣12 10 17⎦ ; (ii) AC = ⎣17 11 7 ⎦ ; (iii) CA = ⎣22 11 15⎦.
11 11 17 12 10 7 32 18 13
Supplementary problems
S 7.1. The results are the following
⎡ ⎤
66    
⎣ ⎦ 665 665
(i) A + B = 6 3 ; (ii) A + B =
T T
; (iii) (A + B) =
T
.
634 634
54

One can see that AT + BT = (A + B)T , therefore the transpose can be applied at
any stage when adding matrices.

S 7.2. The results are


⎡ ⎤ ⎡ ⎤
65 −2 1  
⎣ ⎦ ⎣ ⎦ −2 1 1
(i) C + D = 7 3 ; (ii) C − D = 1 1 ; (iii) (C − D) =
T
;
1 1 −2
54 1 −2

(iv) The addition is not compatible; (v) (CT )T = C.

S 7.3. By doing the multiplication one can check that AI = IA = A, which is valid
for any square matrix A and the corresponding identity matrix.

S 7.4. The matrix operations give


⎡ ⎤ ⎡ ⎤
52   29 17 11
35 16
(i) AT = ⎣3 1⎦ ; (ii) AAT = ; (iii) AT A = ⎣17 10 6 ⎦ .
16 14
13 11 6 10

The operations defined in (iv) and (v) are not compatible. For a matrix A of size
m × n, the size of matrix AAT is m × m while the size of matrix AT A is n × n. Also,
one may note that these two matrices are symmetric.

S 7.5. The answers are


⎡ ⎤
5 31    
⎣ ⎦ 5 2 1 14
(i) AB = 14; (ii) BA = 10 6 2 ; (iii) CD = ; (iv) DB = .
19 12 7 12
15 9 3
7.4 Problem Answers 63

(vii) A2 are not compatible.


The operations (v) DC, (vi) BD and 
2 3
The answer for (viii) is CD = .
6 11
S 7.6. Clearly, det A = |A| does not exist. For the other matrices we have

|B| = −1; |C| = 0; |D| = 14; |E| = −2.

Matrix C is not invertible, while for the other ones we have


   
−1 −5 2 −1 1 5 −2
B = , D = .
3 −1 14 −3 4

S 7.7. The solutions can be obtained from the formulae


      
x −1 −1 1 4 15
(i) = x = A b = ( −1) =
y −2 3 −11 41
      
x 1 −4 3 6 1 51
(ii) = x = A−1 b = =
y −7 −3 4 −9 7 54
      
x 1 2 −1 −4 2
(iii) = x = A−1 b = =
y −5 −1 −2 2 0

S 7.8. The inverse matrixmethod


 may not be applied to the system because the
46
determinant of the matrix is zero.
23
S 7.9. The system can be written as
⎧ ⎛ ⎞⎛ ⎞ ⎛ ⎞

⎨4x + 6y + 3z = 28 463 x 28
2x + 3y + 2z = 15 ⇐⇒ ⎝2 3 2⎠ ⎝ y ⎠ = ⎝15⎠.


3x + 2y + 5z = 19 325 z 19
Chapter 8
Matrix Applications in Computer Graphics

Abstract Column matrices can be used to represent points in 2D or 3D, while


matrices of dimension 2 × n and 3 × n can be used to represent sets of points in 2D or
3D. Matrices allow arbitrary linear transformations to be represented in a consistent
format (T (x) = Ax for some 2 × n (or 3 × n) matrix A, called the transformation
matrix of T), suitable for computation. This format allows transformations to be
conveniently combined with each other by multiplying their matrices. In this chapter
we first use matrices to represent points, lines and polygons. We then discuss in
detail some linear transformations such as translation, scaling, rotation, reflections
and shearing in 2D, and examine how transformations can be concatenated using
matrix multiplication.

Keywords Points · Linear transformations · Rotations · Reflections · Shearing.

8.1 Brief Theoretical Background

Here we use matrices to represent linear transformations in 2D.

Matrix Representation

• Points: represented by a column matrix (position vector)


 
x
X= .
y

• Lines: defined
 by two points and represented by a 2 × 2 matrix.
 
3 −1 3 −1
The matrix represents a line through points and .
2 4 2 4

O. Bagdasar, Concise Computer Mathematics, SpringerBriefs in Computer Science, 65


DOI: 10.1007/978-3-319-01751-8_8, © The Author(s) 2013
66 8 Matrix Applications in Computer Graphics

• Polygons: Represented
  by a 2 × n matrix.      
3 −1 1 3 −1 1
The matrix represents the triangle , and .
2 4 1 2 4 1

Matrix Transformations of Points

Consider the point X and a transformation matrix T given by


   
x ab
X= , T = .
y cd

Matrix T pre-multiplies point X , generating another point X ∗


      ∗
ab x ax + by x
TX = = = ∗ = X∗
cd y cx + dy y
   
−1 12
Example: The point X = is transformed by the matrix T = .
2 34
       
∗ x∗ 1 2 −1 1(−1) + 2(2) 3
X = ∗ = TX = = =
y 34 2 3(−1) + 4(2) 5

Transformation of Lines

• Take two points X , Y and a transformation T


• Apply the transformation T to both X , Y (they determine a line XY)
• The transformed points X ∗ , Y ∗ determine a new line X ∗ Y ∗

Remark: Matrix transformations change lines into lines.


Example: A straight line joins the points (2, 2) and (4, 5). 
42
Find the new line produced by the transformation matrix T = .
31
Solution: The new line is determined by the two points representing the columns of
the matrix
    
42 24 12 26
= .
31 25 8 17

Linear Transformations in 2D

Consider the point X , vector V and a transformation matrix T given by


8.1 Brief Theoretical Background 67
     
x u ab
X= , V = , T = .
y v cd

The following linear transformations can be defined.


Translation: The translation of point X by vector V is defined by
 ∗  
∗ x x +u
X = ∗ = X +V = .
y y+v

Scaling:Scales horizontal and vertical coordinates.

  
     
α 0 ∗ x∗ α 0 x αx
T = ; X = ∗ = TX = = .
0β y 0β y βy

Rotation: The line from (0, 0) to the point (x, y), is rotated by an angle θ .
By convention anticlockwise movement is considered positive.
We say that (x, y) has rotated about the origin to (x ∗ , y ∗ ).
 
cos θ − sin θ
T = .
sin θ cos θ

Reflections
• in the X axis: changes the sign of the y co-ordinate
      
1 0 1 0 x x
T = ; X∗ = T X = =
0 −1 0 −1 y −y

• in the Y axis: changes the sign of the x co-ordinate


      
−1 0 ∗ −1 0 x −x
T = ; X = TX = =
0 1 0 1 y y

• in the y = x line: interchanges the x and y co-ordinates.

     
01 ∗ 01 x y
T = ; X = TX = =
10 10 y x

• in the y = −x line: interchanges x and y co-ordinates and changes signs.

      
0 −1 0 −1 x −y
T = ; X∗ = T X = =
−1 0 −1 0 y −x
68 8 Matrix Applications in Computer Graphics

Shearing: A shear is a distortion. The transformation matrix


 
ab
T =
cd

The ‘off-diagonal’ elements b and c determine the kind of shear produced.


• b produces x-direction shear,
• c produces y-direction shear.

Combinations of Linear Transformations

Translations, rotations, scalings or shears are very often combined to generate more
complex transformations. In the language of matrices, this translates into computing
the product of the matrices corresponding to each individual transformation.
If the point X is subject to two consecutive transformations T1 and T2 , then the
composed transformation T = T2 T1 generates the new point

X ∗ = T2 (T1 X ) = (T2 T1 )X = T X.
 
30
Example: The point (3, 2) is transformed by the scaling , then reflected in Y
02
axis and finally rotated by 45◦ . Find the coordinates of the new point.
Solution: Using the notations

       
30 −1 0 cos 45◦ − sin 45◦ 1 cos 1 −1
T1 = , T2 = , T3 = = √ ,
02 0 1 sin 45◦ cos 45◦ 2 sin 1 1

the coordinate of the new point is given by


      
∗ 1 1 −1 −1 0 3 0 3 1 −13
X = T3 T2 T1 X = √ =√ .
2 1 1 0 1 02 2 2 −5

Remark: Matrices of size 3 × 3 can represent 3D linear transformations.

8.2 Essential Questions

E 8.1. A straight line joins the points (3, 2) and (4, 5).  
4 2
Describe the transformed line produced by the matrix T = .
3 −1
8.2 Essential Questions 69

E 8.2. Write down a transformation matrix which


(a) scales vertical lines by a factor of 3 leaving horizontal lines unchanged.
(b) scales horizontal lines by a factor of 2 and vertical lines by a factor of 5.
E 8.3. The point (3, 1) is rotated anticlockwise about the origin through an angle of
45◦ . Calculate the position of the new point.
E 8.4. Write down the following matrices.
(a) Rotation about the origin, anti-clockwise through π/6 radians.
(b) Rotation about the origin, clockwise through π/6.
(c) Use the appropriate matrix to find the new position of the point (2, 3) after it has
been rotated anti-clockwise through π/6 about the origin.
(d) Calculate the product of the two matrices found in (a) and (b). Explain your
answer.

E 8.5. Consider the 2D unit square having coordinates (0, 0), (1, 0), (0, 1), (1, 1).
Determine the new co-ordinates of the figure after the transformations:
(1) Translation of vector (1, 2);
(2) horizontal scaling of factor 2;
(3) vertical scaling of factor 3;
(4) mixed scaling of factors 3 and 4;
(5) Rotation of angle π/6;
(6) Rotation of angle 90◦ ;
(7) Reflection in the X axis;
(8) Reflection in the Y axis;
(9) Reflection in the y = x line;
(10) Reflection in the y = −x line.
E 8.6. Point (2, 3) is reflected in Y axis, then reflected in y = x, and then rotated by
90◦ . Find the co-ordinates of the final point.

8.3 Supplementary Questions

S 8.1. Show that any rotation matrix R has the property R T = R −1 (i.e. the transpose
of a rotation matrix is equal to its inverse).
S 8.2. Let R be the rotation matrix about the origin through angle θ .

(a) State the rotation matrix S for a clockwise rotation through an angle θ .
(b) Calculate R −1 using the result of problem S 8.1. above.
(c) What can be concluded from parts (a) and (b) above.
S 8.3. Write down transformation matrices which will:
70 8 Matrix Applications in Computer Graphics

(a) scale vertical lines by a factor of 5 and leave horizontal lines unchanged.
(b) scale horizontal lines by a factor of 4 and vertical lines by a factor 0.25.
(c) Use the above matrices to find the new co-ordinates of the points (2, 1) and
(−2, −1), performing each transformation separately.
S 8.4. Point A(2, −1) is reflected in X axis, then rotated by 90◦ and finally reflected
in y = −x. Find the co-ordinates of the final point.
S 8.5. Find the coordinates of the unit square defined in E 8.5., subject to the shear
transformations
  given by the  matrices:
    
13 10 12 23
(a) T = ; (b) T = ; (c) T = ; (d) T = .
01 21 31 45

8.4 Problem Answers

Essential Problems
      
34 4 2 34 16 26
E.8.1 The first line is . The transformed line is = .
25 3 −1 2 5 7 7
  
30 50
E 8.2. (a) T = ; (b) T = .
01 02
      √ 
1 cos 45◦ − sin 45◦ 3 2
E 8.3. The new point is given by: T = = √ .
3 sin 45◦ cos 45◦ 1 2 2

E 8.4. Using the formulae for the rotation matrix we obtain


  √ 
cos π/6 − sin π/6 3/2 √−1/2
(a) R = = .
sin π/6 cos π/6 1/2 3/2
  √ 
cos(−π/6) − sin(−π/6) 3/2 √1/2
(b) S = = .
sin(−π/6) cos(−π/6) −1/2 3/2
      √    
2 cos π/6 − sin π/6 2 3/2 √ −1/2 2 0.2321
(c) R = = = .
3 sin π/6 cos π/6 3 1/2 3/2 3 3.5981
(d) The product is the identity matrix, as the two transformations cancel each other.
 
0101
E 8.5. First, the unit square is represented by the matrix .
0011
     
0101 1111 1212
(1) + = .
0011 2222 2233
     
20 0101 0202
(2) T = ; T = .
01 0011 0011
8.4 Problem Answers 71
     
10 0101 0101
(3) T = ; T = .
03 0011 0033
     
30 0101 0303
(4) T = ; T = .
04 0011 0044
 
cos π/6 − sin π/6
(5) T = ;
sin π/6 cos π/6
 
0 −1
(6) T =
1 1
     
1 0 0101 01 0 1
(7) T = ; T = .
0 −1 0011 0 0 −1 −1
     
−1 0 0101 0 −1 0 −1
(8) T = ; T = .
0 0 0011 0 0 1 1
     
01 0101 0011
(9) T = ; T = .
10 0011 0101
     
0 −1 0101 0 −1 0 −1
(10) T = ; T = .
−1 0 0011 0 0 −1 −1
E 8.6. The following matrix transformations are involved
     
−1 0 01 0 −1
T1 = , T2 = , T3 =
0 1 10 1 0

The final transformation is


     
−1 0 0 1 0 −1 0 −1
T = T3 T2 T1 = =
0 1 10 1 0 −1 0

The co-ordinates of the transformed point are (−3, −2).

Supplementary Problems
 
cos θ − sin θ
S 8.1. The transformation matrix for a rotation of angle θ is R(θ ) = ,
sin θ cos θ
whose inverse is the rotation of angle −θ , which satisfies
   
cos(−θ ) − sin(−θ ) cos θ sin θ
R(−θ ) = = = R(θ )T ,
sin(−θ ) cos(−θ ) − sin θ cos θ
72 8 Matrix Applications in Computer Graphics

because sin(−θ ) = − sin(θ ) and cos(−θ ) = cos(θ ).


S 8.2. The answer can easily be obtained from S 8.3. and E 8.4.
   
50 1/4 0
S 8.3. (a) T = ; (b) T = . (c) (10, 1), (−10, −1); (1/2, 8), (−1/2, −8).
01 0 4

S 8.4. The following matrix transformations are involved

     
1 0 0 −1 0 −1
T1 = , T2 = , T3 =
0 −1 1 0 −1 0

The final transformation is


     
0 −1 0 −1 1 0 −1 0
T = T3 T2 T1 = =
−1 0 1 0 0 −1 0 −1

The co-ordinates of the transformed point are (−2, 1).


       
0134 0101 0123 0235
S 8.5. (a) ; (b) ; (c) ; (d) .
0011 0213 0314 0459
Chapter 9
Elements of Graph Theory

Abstract Graphs are mathematical structures used to model and visualize relations
between certain objects. One of the first formulations of a graph theory problem was
the famous Konigsberg Problem solved by Leonhard Euler. He proposed a model
which reduced the problem to a schematic diagram and then concluded that the graph
needed to satisfy some general conditions for the problem to be solved affirmatively.
His studies highlighted the importance of understanding graphs and their properties,
which later lead to the creation of Graph Theory as a distinct mathematical discipline.
In this chapter we first introduce graphs through some illustrative examples and then
describe the basic elements of graphs, as well some important graph properties.
We then present special types of graphs like trees or networks, together with some
specific techniques, problems and algorithms.

Keywords Graph elements · Trees · Networks · Adjacency matrix · Cost matrix.

9.1 Brief Theoretical Background

A graph is a mathematical structure used to model pairwise relations between objects


from a certain collection. Before exploring specific properties, two illustrative exam-
ples are presented.
Example 1: The Konigsberg Bridge Problem (Eighteenth Century) In Fig. 9.1a are
sketched seven bridges (lines) which connect four islands (nodes). The question is
whether it possible to start on one island, walk over each of the seven bridges exactly
once and then return to the starting point?
Solution: The answer is no, and was found by Euler in 1736. His idea was to represent
the problem using 4 nodes (land masses) and 7 arcs (bridges). The graph should have
had exactly zero or two nodes of odd degree for a solution to exist.

O. Bagdasar, Concise Computer Mathematics, SpringerBriefs in Computer Science, 73


DOI: 10.1007/978-3-319-01751-8_9, © The Author(s) 2013
74 9 Elements of Graph Theory

Fig. 9.1 a Konigsberg bridges diagram; b Utility problem diagram

Example 2: The utility problem (Fig. 9.1b)


We have 3 houses and 3 utility companies - say, gas, electric, and water.
Is it possible to connect each utility to each house without any crossovers ?
Solution: The answer to the puzzle posed in Fig. 9.1b is NO.
The problem asks whether the complete bipartite graph K 3,3 is planar.
Basic elements of a graph G
• vertices: (nodes) V (G): the set of points
• edges: (arcs) E(G): lines connecting two vertices (directed or not)
• loops: edge that connects a vertex to itself
• order (degree) of a node: number of ‘incident’ edges.
• trail: sequence of arcs s.t. the end node of one arc is the start of the next.
• path: trail in which no node is passed through more than once
• cycle: path with an extra arc joining the final node to the initial node.
Example:

Special Graphs
• directed: Some edges have a direction.
• connected: It is possible to reach any node from any node
• complete: Simple, undirected, arcs between all nodes (Notation: K n )
• planar: It can be drawn so that the arcs do not cross
• simple: A graph with no loops or multiple arcs
• tree: Simple graph with no cycles
9.1 Brief Theoretical Background 75

• spanning tree (for a connected, undirected graph): subgraph that is a tree and
connects all the vertices together.
• network: A graph with weighted arcs (could be distance, cost, time).
Adjacency matrix: For a graph G with n vertices this is a n × n matrix s.t.
• non-diagonal entry ai j : number of edges from vertex i to vertex j
• diagonal entry aii : number of edges from vertex i to itself taken once for directed
graphs or twice for undirected graphs
Example: For the graph depicted in the previous page we have

1 2 3 4 5 6
1 2 1 0 0 1 0
2 1 0 1 0 1 0
3 0 1 0 1 0 0
4 0 0 1 0 1 1
5 1 1 0 1 0 0
6 0 0 0 1 0 0

Trees: classification and basic operations


• sequence: each node has 1 predecessor (or 0) and 1 successor (or 0)
• binary tree: each node has 1 predecessor (or 0) and 2 successors (or 1, 0)
• general tree: can have any number of successors
Standard operations on binary trees
The set of all trees with vertices in set X is denoted by BT R E E(X ).
For a given tree t ∈ BT R E E(X ) the following operations are defined
• ROOT: BT R E E(X ) → X (finds root)
• LEFT: BT R E E(X ) → BT R E E(X ) (finds left branch)
• RIGHT: BT R E E(X ) → BT R E E(X ) (finds right branch)
• ISEMPTYTREE: BT R E E(X ) → B (checks if tree is empty)

For tree t in (a) below, R O OT (t) = 6, L E F T (t) is tree (b) while R I G H T (t) = 2.
For t and r trees in BT R E E(X ) and s ∈ X one can define the function:
• MAKE: BT R E E(X ) × X × BT R E E(X ) → BT R E E(X ); (t, s, r )  → u
This output u is a tree with left branch t, root s and right branch r .
76 9 Elements of Graph Theory

Network problems
Networks can be represented using diagrams or a distance matrix.

Kruskal’s algorithm (minimum spanning tree)


• List the arcs in order of increasing weight
• Choose the arc with least weight
• Build a tree by working down the list choosing arcs provided they do not form a
cycle when added to the arcs already chosen
• Stop when no more arcs can be chosen.

Example: The segments selected when using Kruskal’s algorithm are


AD = 5, C E = 5, D F = 6, AB = 7, B E = 7 (not BC = 8, F E = 8, B D = 9)
and E G = 9. Segments F G = 11 and D E = 15 do not contribute.

Prims’s algorithm (minimum spanning tree)


• Choose a node
• Build a tree by choosing the minimum weight arc joining a node not yet chosen
to one that has. Add this arc and the end node to the tree
• Repeat the tree building process until all the nodes have been chosen.

Example: The segments selected when using Prim’s algorithm are


AD = 5, D F = 6, AB = 7, B E = 7, C E = 5 and E G = 9.
Shortest path problem: Minimal cost path between two vertices in a graph.
For large networks this is solved using computers and algorithms.
Example: The shortest path between A and G is A − D − F − G of cost 22.

9.2 Essential Problems


E 9.1. Prove that the sum of degrees of vertices in any finite graph is even.
E 9.2. Draw the graphs represented by the following adjacency matrices:
E 9.3. (a) Write down the information in E 9.2. as a relation R ⊂ X × X where
X = {a, b, c, d, e} i.e. as a set of ordered pairs.
9.2 Essential Problems 77

a b c d e
a 0 1 1 0 1
(i) b 1 0 1 1 1
c 1 1 0 0 0
d 0 1 0 0 1
e 1 1 0 1 0
a b c d e
a 0 1 0 0 1
(ii) b 1 0 1 1 0
c 0 1 1 1 0
d 0 1 1 0 1
e 1 0 0 1 1
a b c d e
a 0 1 0 0 1
(iii) b 0 0 0 0 0
c 0 0 0 0 0
d 0 0 0 0 1
e 0 0 0 0 0

(b) Which if any of the graphs in E 9.2. have the following properties: con-
nected, complete, directed, undirected, contain a cycle, contain a loop, is
a tree, contains a vertex which is even, contains a vertex which is odd?

E 9.4. In a group of people John likes Mary, Brian and Emma; Brian likes Mary and
Sue; Mary likes John and Sue; Emma likes Mary, John and Brian; Sue likes nobody.
Draw a graph showing who likes who.
E 9.5. Given the trees p and q from BTREE (BIRDS) defined below

Write down the following:


(i) LEFT(RIGHT(p));
(ii) ISEMPTYTREE(RIGHT(RIGHT(q)));
(iii) ROOT(LEFT(p));
(iv) MAKE(q, gull, q);
(v) MAKE (LEFT(p), ROOT(q), RIGHT(p));
(vi) MAKE(LEFT(LEFT(q)), ROOT(p), MAKE(q, puffin, RIGHT(p))).

E 9.6. Given the tree u from BTREE(S) defined below:


78 9 Elements of Graph Theory

Evaluate each of the following:


(i) LEFT(RIGHT(LEFT(u)));
(ii) RIGHT(LEFT(RIGHT(u)));
(iii) ROOT(u) + S ROOT(LEFT(RIGHT(u)).

9.3 Supplementary Problems

S 9.1. Show that a tree with n vertices has exactly n − 1 edges.


S 9.2. Prove that a complete graph with n nodes (K n ) has n(n − 1)/2 edges.
S 9.3. Show that every simple graph has two vertices of the same degree.
S 9.4. Using standard functions of BTREE(X), describe the functions.
(i) F that inputs a vertex and a binary tree, and replaces the root of the tree by the
input vertex.
(ii) G that inputs a binary tree and exchanges left and right subtrees. Give an example
in each case to illustrate what the function does.
S 9.5. Use standard functions of BTREE(X) to give a formal description of two
functions that input a binary tree and an item from X with the actions:
(i) R E PL – replaces the root of the left subtree by the input item.
(ii) R E PR – replaces the root of the right subtree by the input item.
S 9.6. Let a network be given by the distance matrix

A B C D E F G
A – 10 5 4 3 – –
B 10 – 8 9 7 – 2
C 5 8 – – 5 – –
D 4 9 – – 10 6 4
E 3 7 5 10 – 8 9
F – – – 6 8 – 11
G – 2 – 4 9 11 –

Find the minimum spanning tree using


(a) Kruskal’s algorithm (node and arc list, start with A).
(b) Prim’s algorithm (arc list).
(c) Find the shortest path between A and G.
9.4 Problem Answers 79

9.4 Problem Answers

Essential problems
E 9.1. Each arc contributes with 2 to the sum of degrees, therefore this sum should
be twice the number of arcs.
E 9.2. Draw the vertices {a, b, c, d, e} and directed segments from x to y if (x, y) = 1
in each of the adjacency matrices.
E 9.3. We write down the directed segments corresponding to each matrix
(i) G 1 = {(a, b), (a, c), (a, e), (b, a), (b, c), (b, d), (b, e), (c, a), (c, b), (d, b),
(d, e), (e, a), (e, b), (e, d)};
(ii) G 2 = {(a, b), (a, e), (b, a), (b, c), (b, d), (c, b), (c, c), (c, d)(d, b), (d, c),
(d, e)(e, a), (e, d), (e, e)};
(iii) G 3 = {(a, b), (a, e), (d, e)}.
E 9.4. The adjacency matrix is

John Mary Brian Emma Sue


John 0 1 1 1 0
Mary 1 0 0 0 1
Brian 0 1 0 0 1
Emma 1 1 1 0 0
Sue 0 0 0 0 0

Drawing the graph is straightforward.

E 9.5. (i) swan; (ii) False; (iii) hawk;


iv) Tree whose left branch is q, root is "gull" and right branch is q;
v) Tree whose left branch is LEFT(p), root is "rook" and right branch is
RIGHT(p);
vi) Tree whose left branch is empty, root is "eagle" and right branch is another
tree; this smaller tree has left branch q, root "puffin" and right branch
RIGHT(p).

E 9.6. (i) empty; (ii) empty; (iii) "adamwitch".


Supplementary Problems
S 9.1. A tree with n vertices has to be connected, but also to have no cycles. We can
imagine that initially there is just one node. To add another node to this one, one has
80 9 Elements of Graph Theory

to draw a segment. This step is repeated with the addition of any new node and in
the end one obtains exactly n − 1 edges.
S 9.2. In a complete graph each vertex is connected to all the others. This means that
in total we have n(n − 1) edges. As using this formula
  each node was counted twice,
the total number of edges has to be n(n − 1)/2 or n2 .
S 9.3. Let G be any finite simple graph with n = 2 vertices. The maximal degree of
any vertex in G is less than equal to n − 1. Also, if our graph G is not connected,
then the maximal degree is strictly less than n − 1.
Case 1: Assume that G is connected. We can not have a vertex of degree 0 in G, so
the set of vertex degrees is a subset of S = {1, 2, , n − 1}. Since the graph G has n
vertices, by pigeon-hole principle we can find two vertices of the same degree in G.

Case 2: Assume that G is not connected. G has no vertex of degree n − 1, so the


set of vertex degrees is a subset of S0 = 0, 1, 2, , n − 2. By pigeon-hole principle
again, we can find two vertices of the same degree in G.
S 9.4. The functions are defined as:

(i) F : BT R E E(X ) × X → BT R E E(X )


 
F(T, x) = M AK E L E F T (T ), x, R I G H T (T )

(ii) G : BT R E E(X ) → BT R E E(X )


 
G(T ) = M AK E R I G H T (T ), R O OT (T ), L E F T (T ) .

S 9.5. (i) The function R E PL : BT R E E(X ) × X → BT R E E(X ) is defined as

REP L (T, x) = MAKE (M AK E (L E F T (L E F T (T )), x, R I G H T (L E F T (T ))),


R O OT (T ), R I G H T (T ))

(ii) The function R E PR : BT R E E(X ) × X → BT R E E(X ) is defined as



R E PR (T, x) = M AK E L E F T (T ), R O OT (T ),
 
M AK E L E F T (R I G H T (T )), x, R I G H T (R I G H T (T ))

S 9.6. (a) Nodes used by Kruskal’s algorithm are A − E − D − G − B − C − F.


Arcs: AE = 3, AD = 4, DG = 4, BG = 2, AC = 5 and D F = 6 with
cost 24.
(b) The arcs used by Prim’s algorithm are BG = 2, AE = 3, AD = 4,
DG = 4, AC = 5 (or C E = 5) and D F = 6 for a total of 24.
(c) The shortest path between A and G is A − D − G of length 8.
Chapter 10
Elements of Number Theory and Cryptography

Abstract Number theory is an important mathematical domain dedicated to the


study of numbers and their properties. As discussed in Chap 1, the number sys-
tems N, Z, Q, C emerged from the need of solving more complicated equations.
However, numerous fundamental (some still unanswered) questions in mathematics
refer to prime numbers and their properties (Goldbach conjecture, Rieman hypothe-
sis). Cryptography studies techniques for a secure communication in the presence of
adversaries which involves coding (encrypting) and decoding (decrypting). From the
rudimentary tools used since antiquity (letter shifting, sticks), modern cryptography
makes extensive use of mathematics, information theory, computational complex-
ity, statistics, combinatorics, abstract algebra and number theory. In this chapter
we present basic elements of number theory including prime numbers, divisibility,
Euler’s totient function and modulo arithmetic, which are used to describe the Caesar
cypher and the RSA algorithm.

Keywords Prime factorisation · Least common multiple · Greatest common


divisor · Totient function · Modulo arithmetic · Caesar cypher · RSA algorithm

10.1 Brief Theoretical Background

Prime Numbers and Divisibility

A number n is prime if his only positive divisors are 1 and itself.


Fundamental theorem of arithmetic (unique factorization theorem):
Every integer n ≥ 1 is either prime or is the product of prime numbers.
Every integer n > 1 is represented uniquely as a product of prime powers

n = p1a1 p2a2 · · · pkak , (canonical representation)

where p1 < p2 < ... < pk are primes and ai are positive integers.

O. Bagdasar, Concise Computer Mathematics, SpringerBriefs in Computer Science, 81


DOI: 10.1007/978-3-319-01751-8_10, © The Author(s) 2013
82 10 Elements of Number Theory and Cryptography

Greatest common divisor: gcd(a, b) or (a, b)


Example: What is the greatest common divisor of 54 and 24?
Solution: The divisors of 54 are: 1, 2, 3, 6, 9, 18, 27, 54.
The divisors of 24 are: 1, 2, 3, 4, 6, 8, 12, 24.
The common divisors of 54 and 24: 1, 2, 3, 6.
The greatest of these is 6, written as gcd(54, 24) = 6.

Least common multiple: l cm (a, b) or [a, b]


Example: What is the LCM of 4 and 6?
Solution: The multiples of 4 are: 4, 8, 12, 16, 20, 24, 28, 32, 36, ...
The multiples of 6 are: 6, 12, 18, 24, 30, 36, 42, 48, 54, ...
Common multiples of 4 and 6 are 12, 24, 36, 48, 60, 72, ...
The least common multiple is 12.

Definition Numbers a and b are relatively prime if gcd(a, b) = 1.


Properties of gcd(a, b) and l cm(a, b): If a = p1a1 p2a2 · · · pkak , b = p1b1 p2b2 · · · pkbk
where p1 < p2 < ... < pk are primes and ai , bi are non-negative integers.
The following properties hold:

a · b = p1a1 +b1 p2a2 +b2 · · · pkak +bk


min(a1 ,b1 ) min(a2 ,b2 ) min(ak ,bk )
gcd(a, b) = p1 p2 · · · pk
max(a ,b ) max(a ,b ) max(ak ,bk )
lcm(a, b) = p1 1 1
p2 2 2
· · · pk
a · b = lcm(a, b) · gcd(a, b)

Euler’s Totient Function ϕ(n)

The number of integers 1 ≤ k ≤ n relatively prime with n.


Example: The set of numbers prime with 8 are 1, 3, 5, 7, therefore ϕ(8) = 4.
Formula properties: If p is prime, gcd(m, n) = 1 and k > 1 then

ϕ( p) = p − 1
ϕ(mn) = ϕ(m)ϕ(n).

Formula (Euler): If the factorisation of n is n = p1a1 p2a2 · · · pkak then


    
1 1 1
ϕ(n) = n 1 − 1− ··· 1 − .
p1 p2 pk
    
Example: ϕ(36) = ϕ 22 32 = 36 1 − 21 1 − 13 = 36 · 1
2 · 2
3 = 12.
10.1 Brief Theoretical Background 83

Modular Arithmetic

System of arithmetic for integers (clock arithmetic), where numbers “wrap around”
upon reaching a certain value called modulus.

Definition For n ∈ N, two integers a and b are called congruent modulo n:

a≡b (mod n),

if their difference a − b is an integer multiple of n.


Modulo operations
If a1 ≡ b1 (mod n) and a2 ≡ b2 (mod n) we can define
• Addition: a1 + a2 ≡ b1 + b2 (mod n)
– 2 + 1 (mod 6) ≡ 3 (mod 6)
– 2 + 4 (mod 6) ≡ (2 + 4) (mod 6) = 0 (mod 6)
• Substraction a1 − a2 ≡ b1 − b2 (mod n)
– 2 − 1 (mod 6) ≡ 1 (mod 6)
– 2 − 4 (mod 6) ≡ (2 − 4) (mod 6) = 4 (mod 6)
• Multiplication a1 a2 ≡ b1 b2 (mod n).
– 2 × 2 (mod 6) ≡ 4 (mod 6)
– 2 × 3 (mod 6) ≡ 6 (mod 6) = 0 (mod 6)
• Inversion a1 /a2 ≡ b1 /b2 (mod n) (only defined when n is prime!).
– 2/3 (mod 5) ≡ 4 (mod 5) (as 3 × 4 ≡ 2 (mod 5))
Results b (mod n) are usually expressed using numbers b ∈ {0, 1, . . . , n − 1}.

Cryptography: Vocabulary and Notations


• plaintext m: string of characters from an alphabet A (m ∈ M)
– typically A..Z, printable ASCII, 0 and 1, 0,…, 9, etc.
• f encrypting algorithm - converts m to cyphertext c ∈ C
– typically f considered public knowledge (bribery, moles, etc)
• encrypting key e ∈ E (knowledge of this may be restricted)
• encrypt equation c = f(e,m)
– To avoid ambiguity: m= m ⇒ c = f(e,m) = c’ = f(e,m’)
• g decrypting algorithm - converts c to m
– considered public knowledge
84 10 Elements of Number Theory and Cryptography

– used a decrypt key d ∈ D (knowledge of this may be restricted)


– for each e ∈ E there must be d ∈ D such that m = g(d, c) = g(d, f (e, m))
– Usually d and e are related: d = h(e).

Caesar Cypher
• Alphabet: A = {A, . . . , Z } so n = 26 (or numbers 0.25).
• m and c are single characters
• encrypt key e ∈ {0, . . . , 25}
• encrypt equation: c = f (e, m) = m + e (mod 26)
• decrypt equation: m = g(d, c) = c + d (mod 26) ( f = g)
• clearly d + e = 0 (mod 26) (both need to be kept secret!)
• Method:

g(d, c) = g(d, f (e, m)) = g(d, m + e (mod 26))


= ((m + e) (mod 26)) + d) (mod 26) = m

• Example: e = 23 and n = 26, d = 3


• look-up table (e = 3):

plaintext : A, B, C, D, . . .
cypher text : D, E, F, G, . . .

RSA Algorithm (1977)

This is an efficient algorithm which uses the properties of large primes, as well as
Euler’s totient function to produce encryption and decryption key.
The RSA algorithm involves: Key generation, Encryption, Decryption.

Step 1. Key generation

1. Choose two distinct prime numbers p and q.


2. Compute n = pq.
3. Compute ϕ(n) = ( p − 1)(q − 1), where ϕ is Euler’s totient function.
4. Choose an integer e such that 1 < e < ϕ(n) and gcd(e, ϕ(n)) = 1
• e is released as the public key exponent.
5. Determine d as: d · e = 1 (mod ϕ(n))
• d is kept as the private key exponent.
Remark: p, q and ϕ(n) must be kept secret (they give d).
Step 2. Encryption
10.1 Brief Theoretical Background 85

John sends his public key (n, e) to Ana and keeps the private key secret. Ana then
sends message M to John. She first turns M into an integer m s.t. 0 ≤ m < n by
using a padding scheme (agreed-upon reversible protocol). She then computes the
cyphertext corresponding to
c = m e (mod n).

Ana then transmits c to John.


Step 3. Decryption
John can recover m from c using his private key exponent d by computing

m = cd (mod n).

Given m, he recovers the original message M by reversing the padding scheme.


Summary
“A user of RSA creates and then publishes the product of two large prime numbers,
along with an auxiliary value, as their public key. The prime factors must be kept
secret. Anyone can use the public key to encrypt a message, but with currently
published methods, if the public key is large enough, only someone with knowledge
of the prime factors can feasibly decode the message” (Rivest: 1978).

10.2 Essential Problems

E 10.1. Find 5 pairs of Pythagorean triples a, b, c ∈ N s.t. a 2 = b2 + c2 .


E 10.2. Factorize the numbers 96, 144, 286, 777 and 1001.
E 10.3. Find gcd(144, 96), gcd(1001, 777) and gcd(1001, 286).
E 10.4. Find lcm(144, 96), lcm(1001, 777) and lcm(1001, 286).
E 10.5. Compute (find results in the set {0, . . . , n − 1} for (mod n)):

5 + 2 (mod 2), 3 × 5 (mod 2)


3 + 7 (mod 5), 3 − 7 (mod 5), 3 × 7 (mod 5).
3 + 7 (mod 11), 3−7 (mod 1)1, 3×7 (mod 1)1.

E 10.6. Find the ϕ(n) function for the numbers n = 11, 21, 24, 36, 49, 81, 100.
E 10.7. Code the message “NOBLI EXS” using a Caesar cipher of key e = 16.
86 10 Elements of Number Theory and Cryptography

10.3 Supplementary Problems

S 10.1. Find the largest prime number smaller than 1000.


S 10.2. Goldbach conjecture:
For n even there are a, b primes s.t. n = a + b. Check this results for n ≤ 100.
S 10.3. Find 7 pairs of twin primes (Their difference is 2. Ex: (3, 5), etc.).
S 10.4. Find 5 Mersenne primes of the form M p = 2 p − 1.
S 10.5. Factorize the numbers 703, 779, 968, 1002, 1440, 1547, 1763, 2261.
S 10.6. Find gcd(1440, 968), gcd(1002, 703), gcd(2261, 1547) and gcd(779, 1763).
S 10.7. Find lcm(1440, 968), lcm(1002, 703), lcm(2261, 1547) and gcd(779, 1763).
S 10.8. Solve the following equations (in the set {0, . . . , n − 1} for (mod n)):

1+x =0 (mod 2), 3 × x = 1 (mod 2)


3 + x = 1 (mod 8), 3 − x = 6 (mod 8), 4 × x = 7 (mod 8).
5 + x = 3 (mod 11), 3 − x = 7 (mod 11), 4 × x = 8 (mod 11).

S 10.9. Find the ϕ(n) function for the numbers n = 19, 31, 28, 48, 144, 169, 1001.
S 10.10. Find the RSA key d for the public key e = 5, for n = 65.

10.4 Problem Answers

Essential Problems

E 10.1. (3, 4, 5), (5, 12, 13), (7, 24, 25), ( 8, 15, 17), ( 9, 40, 41).
E 10.2. The factorisations are
96 = 25 · 31 ; 144 = 24 · 32 ; 286 = 2 · 11 · 13; 777 = 3 · 7 · 37; 1001 = 7 · 11 · 13.
E 10.3. The greatest common divisors are
gcd(96, 144) = 24 · 3 = 48; gcd(1001, 777) = 7; gcd(1001, 286) = 11 · 13 = 143.
E 10.4. The least common multiples are
lcm(96, 144) = 25 · 32 = 288; lcm(1001, 777) = 3 · 7 · 11 · 13 · 37 = 111111;
lcm(1001, 286) = 2 · 7 · 11 · 13 = 2002.
E 10.5. The modulo operations give the following results:
10.4 Problem Answers 87

1 (mod 2), 1 (mod 2)


0 (mod 5), 1 (mod 5), 1 (mod 5).
10 (mod 11), 7 (mod 11), 10 (mod 11).

E 10.6. One has to factorise the numbers and apply the multiplicity of ϕ(n).

ϕ(11) = 10;
ϕ(21) = ϕ(3 · 7) = ϕ(3) · ϕ(7) = 2 · 6 = 12;
ϕ(24) = ϕ(23 · 3) = ϕ(23 ) · ϕ(3) = 4 · 2 = 8;
ϕ(36) = ϕ(22 · 32 ) = ϕ(22 ) · ϕ(32 ) = 2 · 6 = 12;
ϕ(49) = ϕ(72 ) = 49(1 − 1/7) = 42;
ϕ(81) = ϕ(34 ) = 81(1 − 1/3) = 54;
ϕ(100) = ϕ(22 · 52 ) = ϕ(22 ) · ϕ(52 ) = 2 · 20 = 40.

E 10.7. Each letter in the string is shifted to the right by 16 positions.


If the transformed letter is after Z, we use the order:
A, B, C, D, . . . , X, Y, Z , A, B, C, . . . .
Finally we obtain “DERBY UNI”.

Supplementary Problems

S 10.1. By consulting a prime numbers list on finds the number 997.


S 10.2. The verification of the Goldbach conjecture yields the following pairs

4 = 2 + 2; 6 = 3 + 3; 8 = 3 + 5; 10 = 3 + 7; 12 = 5 + 7; 14 = 3 + 11;
18 = 5 + 13; 20 = 3 + 17; . . . ; 96 = 7 + 89; 98 = 19 + 79; 100 = 3 + 97.

S 10.3. The first few twin primes are

(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73), (101, 103).

S 10.4. The first five Mersenne primes are 3, 7, 31, 127, 8191.
S 10.5. The prime factorisations are

703 = 19 · 37; 779 = 19 · 41;


88 10 Elements of Number Theory and Cryptography

968 = 23 · 112 ; 1002 = 2 · 3 · 167;


1440 = 2 · 3 · 5;
5 2
1547 = 7 · 13 · 17;
1763 = 41 · 43; 2261 = 7 · 17 · 19.

S 10.6. The greatest common divisors are

gcd(1440, 968) = 23 = 8;
gcd(1002, 703) = 1;
gcd(2261, 1547) = 7 · 17 = 119;
gcd(779, 1763) = 41.

S 10.7. The least common multiples are

lcm(1440, 968) = 25 · 32 · 5 · 112 ;


lcm(1002, 703) = 2 · 3 · 19 · 37 · 167;
lcm(2261, 1547) = 7 · 13 · 17 · 19;
lcm(779, 1763) = 19 · 41 · 43.

S 10.8. Using definitions of the modulo operations one obtains:

x = 1 (mod 2), x = 1 (mod 2)


x = 6 (mod 8), x = 5 (mod 8), there is no such x.
x = 9 (mod 11), x = 7 (mod 11), x = 2 (mod 11).

S 10.9. Using the formula for ϕ(n) one obtains

ϕ(19) = 18; ϕ(31) = 30; ϕ(28) = 12; ϕ(48) = 16;


ϕ(144) = 36; ϕ(169) = 156; ϕ(1001) = 720.

S 10.10. n = 65 then p = 5, q = 13 and ϕ(65) = 48. To find d one needs to solve


5 ∗ d = 1 (mod 48) which gives d = 30.
Chapter 11
Elements of Calculus

Abstract Calculus is one of the important subjects of mathematics, with


applications in various domains of knowledge such as Finance, Engineering or Archi-
tecture. The major branches of Calculus are differential calculus (concerning rates
of change and slopes of curves), and integral calculus (concerning accumulation of
quantities and the areas under curves), linked together through the Fundamental The-
orem of Calculus. In this Chapter we present some key elements of Calculus such
as sequences, limits, convergence, as well as definitions and rules for the differenti-
ation and integration of basic functions such as polynomials and basic trigonometric
functions.

Keywords Sequences · Limits · Continuity · Derivative · Integral

11.1 Brief Theoretical Background

Sequences and Limits

Sequence: ordered list of objects. Discrete function x : N → X, x(n) = xn .


Limit of a sequence: sequence terms "eventually get close to" a value.
• We say that the sequence converges to the limit
• If a sequence does not converge it is divergent
Examples: The following sequences have the limits:
• even numbers: 2, 4, 6, ..., 2n (diverge)
• inverse numbers: 1, 21 , 13 , . . . , n1 , . . . (converge to zero)
• geometric progression: 1, 21 , 212 , . . . , 21n , . . . (converges to zero)
• 0.3, 0.33, 0.333, 0.3333, ... converges to 1/3.

Limit of a function: Consider the function f : R → R and numbers p, L ∈ R.

O. Bagdasar, Concise Computer Mathematics, SpringerBriefs in Computer Science, 89


DOI: 10.1007/978-3-319-01751-8_11, © The Author(s) 2013
90 11 Elements of Calculus

We say "the limit of f as x approaches p is L" and write

lim f (x) = L ,
x→ p

if for every convergent sequence xn → p we have f (xn ) → L.


Examples: The following functions have the limits:
• f (x) = x 2 lim x→2 f (x) = 22 = 4.
• f (x) = 1/x lim x→0 f (x) = ∞.
• f (x) = sin x lim x→0 sin x = 0.

Continuous Functions

A function f : R → R is continuous if
• for every point x and convergent sequence xn → x we have f (xn ) → f (x).
• the graph is a single unbroken curve with no "holes" or "jumps".
The intermediate value theorem: If f is continuous on [a, b] and f (a) f (b) < 0
then there is c ∈ [a, b] s.t. f (c) = 0.
Examples: Polynomials, exp, sin, cos are all continuous functions.
The cubic in Fig. 11.1a crosses line y = 0 between −2 and 0, changing sign.
Signum (has the values ±1) (Fig. 11.1b) changes sign but is not continuous.

The Derivative

Measure of how a function changes as its input changes.


For x and x + h we define the ratio

 f (x) f (x + h) − f (x) f (x + h) − f (x)


m= = = .
x (x + h) − (x) h

Fig. 11.1 a f (x) = x 3 /3 − x 2 /4 + 1/2; b f (x) = sgn(x)


11.1 Brief Theoretical Background 91

Fig. 11.2 Graph of function f (x) = x 3 sin(x) between a = 1 and b = 3

The derivative of the function f : R → R at x is the limit

f (x + h) − f (x)
f  (x) = lim
h→0 h

Differentiation of elementary functions


• powers: f (x) = x n (n ∈ N) : f  (x) = nx n−1
• polynomials: (ax 2 + bx + c) = 2ax + b (a, b, c: constants)
• trigonometric functions:

f (x) = sin(x) : f  (x) = cos(x)


f (x) = cos(x) : f  (x) = − sin(x)

The Definite Integral

Let the function f defined on [a, b]. The definite integral denoted by
 b
f (x) d x
a

represents the area of the region in the xy-plane bounded by:


• the graph of f
• the x-axis,
• vertical lines x = a and x = b.
92 11 Elements of Calculus

Remark: The integral may not exist in general (for example, functions which have
singularities, such as f : [0, 1] → R, f (x) = 1/x), but it does exist when the
function f defined on [a, b] is continuous!
Fundamental theorem of calculus (FTC): If f is a continuous real-valued function
defined on [a, b] and a function F : [a, b] → R s.t. F  (x) = f (x), then the definite
integral of f over [a, b] is
 b
f (x) d x = F(b) − F(a)
a

The function F : [a, b] → R s.t. F  (x) = f (x) is called antiderivative.


Remark: The FTC states that for finding the integral of a function f over an interval
[a, b], it is sufficient to find the values of its antiderivative F at the ends of the
interval!
Integration of elementary functions
 x n+1
• powers: f (x) = x n (n ∈ N) : F(x) = f (x)dx = n+1 + C (constant)
 2
• polynomials: f (x) = ax + b; F(x) = f (x)dx = a x2 + bx + C
(a, b, C: constants)
• trigonometric functions:

f (x) = sin(x) : F(x) = f (x)dx = − cos(x) + C

f (x) = cos(x) : F(x) = f (x)dx = sin(x) + C

11.2 Essential Problems

E 11.1. Find the limit of the following sequences


(1) xn = 1; (2) xn = 1 + 1/n; (3) xn = 1/n 2 ; (4) xn = 1/2n ;
(5) 0.9, 0.99, 0.999, 0.9999, . . .;
(6) 0.23, 0.23, 0.2323, 0.232323, . . .;
(7) 0.423, 0.42323, 0.4232323, 0.423232323, . . .

E 11.2. Prove that f (x) = 2x 2 + 2x − 5 has a root in [1, 2] and [−3, −2].
E 11.3. Prove that the cubic f (x) = 2x 3 + 2x 2 − 3 has a root x0 ∈ [0, 1].
E 11.4. Differentiate the functions: (1) f (x) = 2x + 1; (2) f (x) = 2x 2 + 3.
E 11.5. Integrate: (1) f (x) = 1; (2) f (x) = 2x + 3; (3) f (x) = 2x 2 + 3.
2
E 11.6. Evaluate 0 f (x)d x for f (x) = x 2 + 2 using the FTC.
E 11.7. Find the area between the curves f (x) = −x 2 + 6x − 2, g(x) = 2x + 1.
11.3 Supplementary Problems 93

11.3 Supplementary Problems

S 11.1. Find the limit of the following sequences

2n 2n
(1) xn = ; (2) xn = (−1)n ;
n−4 n!
√ √
(3) xn = n 2 − 5n; (4) xn = n + 1 − n.

S 11.2. How many roots does the cubic f (x) = 8x 3 + 12x 2 − 2x − 3 have?
 
S 11.3. Prove that f (x) = x 2 − 3 cos π2 x + 2 has a root x0 ∈ [0, 1].
S 11.4. Differentiate the function f (x) = 2x 3 + 7x + 3.
S 11.5. Integrate: (1) f (x) = 2x + 7 cos(x) + 3 sin(x); (2) f (x) = 2x 3 + 1.

S 11.6. Consider f (x) = −10x 2 + 50x − 40 and g(x) = 5x − x 2 . Find


(a) the area under f (x) and above y = 0;
(b) the area under g(x) and above y = 0;
(c) the x1 < x2 values where the curves f (x) and g(x) intersect;
(d) the area between f (x) and g(x).

11.4 Problem Answers

Essential problems
E 11.1. (1) 0; (2) 0; (3) 0; (4) 0; (5) 1; (6) 23/99; (7) 423/990.
E 11.2. f (1) f (2) = (−1)7 < 0; f (−3) f (−2) = (7)(−1) < 0.
E 11.3. f (0) f (1) = (−3)1 < 0.
E 11.4. (1) f  (x) = 2; (2) f  (x) = 4x.
  
E 11.5. (1) f (x)d x = x + C; (2) f (x)d x = x 2 + 3x + C; (3) f (x)d x =
2x 3
3 + 3x + C.
 3
E 11.6. F(x) = f (x)d x = x3 + 2x + C. The integral is therefore given by
 2
f (x)d x = F(2) − F(0) = 8/3 + 4 + C − C = 20/3.
0

E 11.7. We first solve f (x) = g(x) and obtain the solutions x1 = 1, x2 = 3.


3 3
A = 1 f (x)−g(x)d x = 1 −(x 2 −4x +3)d x = −(x 3 /3−4x 2 +3x +C)|31 = 4/3.
94 11 Elements of Calculus

Supplementary problems
S 11.1. (1) 2; (2) 0; (3) ∞; (4) 0.
S 11.2. f (−2) = −11; f (−1) = 3; f (0) = −3; f (1) = 15. We have f (−2) f (−1)
< 0, f (−1) f (0) < 0 and f (0) f (1) < 0.
S 11.3. f (0) f (1) = (−1)3 < 0.
S 11.4. f  (x) = 6x 2 + 7.
 
S 11.5. (1) f (x)d x = x 2 + 7 sin(x) − 3 cos(x); (2) f (x)d x = x 4 /2 + C.

S 11.6. (a) 45.00; (b) 125/6 = √ 20.83; (c) x1 = 1/6(15 − 65) = 1.15, √x2 =
√  1/6(15+ 65)
1/6(15 + 65); (d) √ (−10(x − 1)(x − 4) + x(x − 5))d x = 651865 ∼
1/6(15− 65)
29.1137.
Chapter 12
Elementary Numerical Methods

Abstract Very often in real applications, one has to solve some complicated
equations, where a formula for the result is either impossible to obtain, or it is
too complicated for any practical use. In this case one has to rely on a wide range of
numerical methods, which very often represent approximations to the real results.
These methods are very important in practice and usually offer both an algorithm
generating increasingly exact approximations and an approximation for the error. In
this chapter we shall discuss the Lagrange polynomial for interpolation data given
by a table of values, basic iterative methods used for the numerical integration of
real functions and some iterative methods for root finding.

Keywords Interpolation · Trapezium rule · Bisection method · Newton-Raphson

12.1 Brief Theoretical Background

In this section we present the basic basic notions regarding polynomial interpolation,
numerical integration and root finding algorithms.

Polynomial Interpolation

When working with data on given by (x, y) coordinates, it is often required to find
a curve passing through multiple points. Polynomials are usually the first choice for
such a curve, as they are smooth, easy to work with and have a formula. The basic
idea of the Lagrange polynomial is to fit a minimum degree polynomial to a set of
points.

O. Bagdasar, Concise Computer Mathematics, SpringerBriefs in Computer Science, 95


DOI: 10.1007/978-3-319-01751-8_12, © The Author(s) 2013
96 12 Elementary Numerical methods

Lagrange Polynomial
For n ≥ 2 and a set of points P1 (x1 , y1 ), P2 (x2 , y2 ), . . . , Pn (xn , yn ) s.t. x1 < x2 <
· · · < xn one can find a polynomial function L(x) of degree n − 1 passing through
the points P1 , . . . , Pn (i.e. L(x1 ) = y1 , L(x2 ) = y2 , . . . , L(xn ) = yn ).
Examples: Given the points P1 (1, 2), P2 (2, 5) and P3 (3, 3) we can:
1. Find a polynomial passing through point P1 .
Solution: This could be any polynomial L(x) for which L(1) = 2. The polynomial
of minimum degree is the constant polynomial L(x) = 2.
2. Find a polynomial passing through points P1 and P2 .
Solution: The interpolating polynomial is:

x −2 x −1
L(x) = 2 · +5·
1−2 2−1
= −2(x − 2) + 5(x − 1) = 3x − 1.

3. Find a polynomial passing through point P1 , P2 and P3 .


Solution: The interpolating polynomial is:

x −2 x −3 x −1 x −3 x −1 x −2
L(x) = 1 · · +5· · +3· ·
1−2 1−3 2−1 2−3 3−1 3−2
= −3x 2 + 13x − 9.

In general, one finds the polynomials L 1 , . . . , L n such that

L(x) = L 1 (x)y1 + L 2 (x)y2 + · · · + L n (x)yn ,


= an−1 x n−1 + an−2 x n−2 + a1 x + a0

which satisfy 
1, x = x j
L j (x) =
0, x ∈ {x1 , . . . , xn } \ {x j }.

The degree of the polynomial is justified by the fact that a polynomial of degree n − 1
depends on n coefficients, equal to the number of available points.

Numerical Integration

The problem of finding the area between the graph of the function f : [a, b] → R
b
and the horizontal axes was linked in Chap. 11 to evaluating the integral a f (x)d x.
However, in real applications the functions to be integrated are very complicated or
the function might be known at some discrete points of the interval [a, b]. In such
cases, the area is approximated by numerical integration methods.
12.1 Brief Theoretical Background 97

Fig. 12.1 Approximations for f (x) = x 2 sin x using (a) N = 1; (b) N = 4 intervals

Trapezium rule
The graph of function f : [a, b] → R is approximated by a linear function (Fig. 12.1)
 b f (a) + f (b)
f (x) d x ≈ (b − a)
a 2

Composite trapezium rule (iterative method): To improve accuracy, the interval


[a, b] can be divided into N smaller intervals where the trapezium rule is applied.
The following notions are defined:
• Step: h = b−a
N (step);
• Partition: x0 = a, x1 = a + h, x2 = a + 2h,..., x N = a + N h = b.
We apply the trapezium rule and obtain the following approximation for the area


h
b N
I = f (x) d x ≈ TN = ( f (xk−1 ) + f (xk ))
a 2
k=1
b−a
= ( f (x0 ) + 2 f (x1 ) + 2 f (x2 ) + . . . + 2 f (x N −1 ) + f (x N )).
2N
Error bound: The absolute error E TN of this approximation is bounded by

(b − a)h 2 (b − a)3
E TN = |I − TN | ≤ M= M (M = max | f  (ξ )|).
12 12N 2 ξ ∈[a,b]

Number of intervals needed: If the desired accuracy is ε (say 0.01) the number of
intervals needed satisfies the inequality

(b − a)3 M
N≥ .
12ε
98 12 Elementary Numerical methods
2
Example: Evaluate the integral 0 f (x)d x for f (x) = x 2 + 2 using the trapezium
rule for N = 2, 4 intervals. What is the minimum value of N which ensures an
accuracy of ε = 0.01?
Solution: When N = 2 we have h = 2/2 = 1 and the formula gives

1 1
T2 = ( f (0) + 2 f (1) + f (2)) = (2 + 2 · 3 + 6) = 7.
2 2
When N = 4 we have h = 2/4 = 1/2 and the formula gives
   
1 1 3
T4 = ( f (0) + 2 f + 2 f (1) + 2 f + f (2))
4 2 2
1 9 17
= (2 + 2 + 2 · 3 + 2 + 6) = 27/4.
4 4 4
 x3
The antiderivative of f is F(x) = f (x)d x = 3 + 2x + C therefore
 2
I = f (x)d x = F(2) − F(0) = 8/3 + 4 + C − C = 20/3.
0

The error analysis for this problem yields


• The absolute error is E TN = |I − TN |. For N = 2, E T2 = |20/3 − 7| = 1/3.
• The error bound is E B = (b−a)h
2
12 maxξ ∈[a,b] | f  (ξ )|.
In this example f (x) = 2, therefore M = maxξ ∈[0,2] | f  (ξ )| = 2.


2·12
For N = 2, h = (b − a)/N = 1, so E B2 = 12 · 2 = 1/3.
2
For N = 4, h = (b − a)/N = 0.5, so E B2 = 2·0.5
12 · 2 = 1/12.
As f  (x) = 2 (so M = 2), the number N of intervals required is given by the
formula 
(2 − 0)3 · 2
N≥ ∼ 11.53, therefore N = 12.
12 · 0.01

Root Finding Algorithms


Few equations can be solved by a simple formula and a numerical method is often
required to approximate the solution. Many times, these approximations can be
improved by successive iterations. In this section we shall briefly present the bisec-
tion method, along with the Newton-Rapson and Secant iterations.

Root finding: The problem of finding f (x) = 0, where f : R → R.


Examples: Find the roots of the equations f (x) = 0:
12.1 Brief Theoretical Background 99

f (x) = x 2 − 4x + 3
f (x) = x 3 − 4x + 3
f (x) = sin(3x) − sin(x)
f (x) = e x − 5 sin(3x) + x 2

The usual starting point is to plot the graph of the function and then guess...
Corollary of the intermediate value theorem If f is continuous on [a, b] and
f (a) f (b) < 0 then there is c ∈ [a, b] s.t. f (c) = 0.

Bisection method algorithm


• Inputs: f (x) (function), interval [a0 , b0 ] s.t. f (a0 ) f (b0 ) < 0
• Output: An approximation of the root of f (x) = 0 in [a0 , b0 ].
• Algorithm For k = 0, 1, 2, ..., do until satisfied:

– Compute ck = ak +b 2 .
k

– Test if ck is the desired root, if so, stop.


– If ck is not the desired root, test if f (ck ) f (ak ) < 0
(1) If so, set bk+1 = ck and ak+1 = ak .
(2) Otherwise, set ak+1 = ck and bk+1 = b.
Bisection method example
Let f (x) = x 3 − 6x 2 + 11x − 6. Let a0 = 2.5, b0 = 4. Then f (a0 ) f (b0 ) < 0 so
there is a root in [2.5, 4].
Iteration 1. k = 0:
a0 + b0 2.5 + 4
c0 = = = 3.25.
2 2
Since f (a0 ) f (c0 ) = f (2.5) f (3.25) < 0, set a1 = a0 and b1 = c0 .
Iteration 2. k = 1:
a1 + b1 2.5 + 3.25
c1 = = = 2.875.
2 2
Since f (a1 ) f (c1 ) = f (2.5) f (3.25) > 0, set a2 = c1 and b2 = b1 .
Iteration 3. k = 2:
a2 + b2 2.875 + 3.250
c2 = = = 3.0625.
2 2
Since f (a2 ) f (c2 ) = f (2.875) f (3.250) < 0, set a3 = a2 and b2 = c2 . It is clear
that the iterations are converging towards the root x = 3.
100 12 Elementary Numerical methods

Table 12.1 Successive approximations using Newton’s algorithm


Iteration xn f (xn ) f  (xn )
0 0.25 0.125 5
1 0.225 0.00125 4.9
2 0.224745 0.0000001 4.89898
3 0.224745 0 4.898979

Bisection method: number of iterations needed


Starting from the initial interval [a0 , b0 ], the minimum number of iterations which
ensure that
bn − an b0 − a0
|cn − x ∗ | ≤ = n+1 ≤ ε
2 2
is give by the formula
log(b − a) − log(2ε)
n≥ .
log 2

Examples: Suppose we want to know a priori the minimum number of iterations


needed in the bisection algorithm, for a0 = 2.5, b0 = 4 and ε = 10−3 .

log(1.5) − log(0.002)
n≥ = 9.5507 =⇒ n = 10.
log 2

Newton’s Method
This is an often used method, with good accuracy and having the elements
• Starting point: x0 (the choice of this point is important)
• Iteration:
f (xn )
xn+1 = xn − 
f (xn )

Examples: Find the roots of the equation f (x) = 2x 2 + 4x − 1 = 0. We first


compute f  (x) = 4x + 4 and then write the iteration formula

2xn2 + 4xn − 1
xn+1 = xn − ,
4xn + 4

Starting from x0 = 0.25 we obtain


Remark: Different roots may be found by starting from different values.
12.2 Essential Problems 101

12.2 Essential Problems

Polynomial Interpolation
E 12.1. Find the Lagrange polynomial interpolating the data

x1 = 1 x2 = 2
y1 = 1 y2 = 3

E 12.2. Find the Lagrange polynomial interpolating the data

x1 = 1 x2 = 2 x3 = 3
y1 = 1 y2 = 2 y3 = 5

Numerical Integration
2
E 12.3. Evaluate 0 f (x)d x for f (x) = x 2 + 2 using N = 2 or 4 trapeziums.
2
E 12.4. Evaluate the integral 0 f (x)d x for f (x) = x 3 + 4x 2 − 5x − 2 exactly and
by the trapezium rule for 2, 4 and 8 intervals. What can you notice?
1
E 12.5. Evaluate the integral 0 f (x)d x for f (x) = 4x 2 − 5x − 2 exactly and by
the trapezium rule for N = 2, 4, 8. What is the error in each case?
E 12.6. Evaluate the composite trapezium approximation for the integral
 π/2
x 2 cos xdx
0

(a) with N = 2 intervals; (b) with N = 4 intervals.

Root Finding
E 12.7. (Existence of roots)
(a) Find the number of roots for the cubic f (x) = 8x 3 + 12x 2 − 2x − 3.
(b) Prove that f (x) = x 2 − 3 cos π2 x + 2 has a root x0 ∈ [0, 1].
E 12.8. (The bisection algorithm) (a) Apply 4 iterations of the bisection algorithm
(c0 , c1 , c2 , c3 ) to the function

f (x) = 2x 3 + 2x 2 − 3, x ∈ [0, 1].

(b) How many iterations are needed to ensure a precision of 10−4 ?


E 12.9. (Newton’s algorithm) Apply 3 iterations of Newton’s method (find x1 , x2 , x3 )
for the function
f (x) = 6x 2 + x − 2,

starting from the points (a) x0 = −1; (b) x0 = 2 and find the exact roots.
102 12 Elementary Numerical methods

12.3 Supplementary Problems

S 12.1. Find the Lagrange interpolation polynomial for the coordinates

x1 = 2 x2 = 3
y1 = 1 y2 = 5

S 12.2. Find the Lagrange interpolation polynomial for the coordinates

x1 = 1 x2 = 3 x3 = 4,
y1 = −5 y2 = −1 y3 = 10,
1
S 12.3. Evaluate the integral −1 f (x)d x for f (x) = x 2 − x + 1 exactly and by
using the trapezium rule for N = 2, 4, 8. Which value of N ensures an accuracy of
0.01? π
S 12.4. Evaluate the integral 0 f (x)d x for f (x) = sin x exactly and by using the
trapezium rule for N = 2, 4, 8. How many intervals N are required for ensuring an
accuracy of 0.01? But for an accuracy of 0.0001?
S 12.5. (Limitations of the bisection algorithm) Apply a few iterations of the bisection
algorithm on the interval [0, 2] to

1
(i) f (x) = ; (ii) g(x) = x 3 − x 2 − x + 1.
x −1

How many iterations are needed to ensure a precision of 10−4 ?

12.4 Problem Answers

Essential Problems

E 12.1. The interpolating polynomial is:

x −2 x −1
L(x) = 1 · +3· = −(x − 2) + 3(x − 1) = 2x − 1.
1−2 2−1

E 12.2. The interpolating polynomial is:

x −2 x −3 x −1 x −3 x −1 x −2
L(x) = 1 · · +2· · +5· ·
1−2 1−3 2−1 2−3 3−1 3−2
= x 2 − 2x + 2.
12.4 Problem Answers 103

Table 12.2 Successive approximations using Newton’s algorithm


Iteration xn f (xn ) f  (xn ) xn f (xn ) f  (xn )
0 2 24 25 −1 3 −11
1 1.04 5.5296 13.48 −0.7272 0.4462 −7.7272
2 0.6297 1.0096 8.5575 −0.6695 0.0200 −7.0342
3 0.5118 0.0835 7.1417 −0.6666 0.0000 −7.0000

E 12.3. When N = 2 we have h = 2/2 = 1, and the approximation

1 1
T2 = ( f (0) + 2 f (1) + f (2)) = (2 + 2 · 3 + 6) = 7.
2 2
When N = 4 we have h = 2/4 = 1/2, and the approximation
     
1 1 3
T4 = f (0) + 2 f + 2 f (1) + 2 f + f (2)
4 2 2
 
1 9 17
= 2 + 2 + 2 · 3 + 2 + 6 = 27/4.
4 4 4

E 12.4. The composite trapezium formula generates the approximations

T2 = 3; T4 = 1.25; T8 = 0.81; T16 = 0.70; T32 = 0.67,


2
which approach the exact value of the integral 0 f (x)d x = 2/3.
1
E 12.5. The approximations are T2 = 1, T4 = 0.875, T8 = 0.843 ∼ 0 f (x)
d x = 5/6. The error upper bounds are E T2 = 0.1666, E T4 = 0.0416, E T8 = 0.0104.
E 12.6. (a) 0.342 ; (b) 0.435.
E 12.7. (a) f (−2) = −11, f (−1) = 3, f (0) = −3, f (1) = 15 therefore we have
at least three roots. As a cubic is continuous and has at most three roots, the answer
is 3.
(b) The function is continuous, f (0) = −1 and (1) = 3, so f (0) f (1) < 0.
E 12.8. (a) c0 = 0.5, c1 = 0.75, c2 = 0.875, c3 = 0.9375; (b) n = 13.
E 12.9. The derivative of f (x) is f  (x) = 12x + 1, therefore the iteration is

6xn2 + xn − 2
xn+1 = xn −
12xn + 1

The analysis suggests that the roots are 1/2 and −2/3 (Table 12.1).
104 12 Elementary Numerical methods

Supplementary Problems
S 12.1. The interpolating polynomial is:

x −3 x −2
L(x) = 1 · +5·
2−3 3−2
= −(x − 3) + 5(x − 2) = 4x − 7.

S 12.2. The interpolating polynomial is:

x −3 x −4 x −1 x −4 x −1 x −3
L(x) = −5 · · + −1 · · + 10 · ·
1−3 1−4 3−1 3−4 4−1 4−3
= 3x 2 − 10x + 2.
1
S 12.3. The exact integral is −1 x 2 − x + 1d x = 83 ∼ 2.666. The approximations
are T2 = 3; T4 = 2.75; T8 = 2.687. The number of required intervals for a 0.01
accuracy is N = 12.
2
S 12.4. The exact integral is 0 sin xd x = − cos x|π0 = 2. The approximations are
T2 = 1.570; T4 = 1.896; T8 = 1.974. N = 17 intervals for a 0.01 accuracy and
N = 161 intervals for 0.0001.
S 12.5. (i) The function changes sign on [0, 2] but the approximations converge to
an asymptote. The method is not reliable because the function is not continuous on
the interval [0, 2].
(ii) The change of sign test is not relevant f (0) f (2) = 3, therefore the method
cannot be applied. The method is not able to pick the root x = 1 (g(1) = 0).
Further Reading

1. Lipschutz, S.: Schaum’s Outline of Theory and Problems of Set Theory and Related Topics,
2nd edn. McGraw-Hill, New York (1998)
2. Lipschutz, S., Lipson, M.: Schaum’s Outline of Discrete Mathematics, 3rd edn. McGraw-Hill,
New Year (2007)
3. Haggerty, R.: Discrete Mathematics for Computing. Addison Wesley, New York (2002)
4. Stroud, K.A., Dexter, J.B.: Foundation Mathematics. Macmillan, London (2009)
5. Tunnicliffe, W.R.: Mathematics for Programmers. Prentice Hall, Englewood Cliffs (1991)
6. Grassmann, W.K., Tremblay, J.P.: Logic and Discrete Mathematics: A Computer Science Per-
spective. Prentice Hall, Englewood Cliffs (1996)
7. Kelly, J.: The Essence of Logic. Prentice Hall, Englewood Cliffs (1997)
8. Neville, D.: The Essence of Discrete Mathematics. Prentice Hall, Englewood Cliffs (1997)
9. Lang, S.: Algebra. Graduate Texts in Mathematics 211, revised 3rd edn. Springer, New York
(2002)
10. Lipschutz, S.: Schaum’s Outline of Theory and Problems of Essential Computer Mathematics.
McGraw-Hill, New York (1982)
11. Marsden, J.E.: Basic Complex Analysis, 2nd edn. W.H. Freeman, New York (1987)
12. Van Verth, J.M., Bishop, L.M.: Essential Mathematics for Games and Interactive Applications:
A Programmer’s Guide, 2nd edn. Elsevier Science, Amsterdam (2008)
13. Wunsch, D.A.: Complex Variables with Applications, 3rd edn. Pearson, Boston (2005)
14. Burton, D.M.: Elementary Number Theory, 7th edn. McGraw-Hill, New York (2011)
15. Churchhouse, R.: Codes and Ciphers. Cambridge University Press, Cambridge (2002)
16. Dangerfeld, J.: Decision 1 for AQA. Pearson Longman, Boston (2005)
17. Goodaire, E.G., Parmenter, M.M.: Discrete Mathematics with Graph Theory. Prentice Hall,
Englewood Cliffs (1998)
18. Hill, R.: A First Course in Coding Theory. Oxford University Press, Oxford (1997)
19. Rivest, R., Shamir, A., Adleman, L.: A method for obtaining digital signatures and public-key
cryptosystems. Commun. ACM 21(2), 120–126 (1978)
20. Spivak, M.: Calculus, 4th edn. Publish or Perish, Houston (2008)
21. Stewart, J.: Calculus. Brooks/Cole, Pacific Grove (2003)

O. Bagdasar, Concise Computer Mathematics, SpringerBriefs in Computer Science, 105


DOI: 10.1007/978-3-319-01751-8, © The Author(s) 2013
Index

A Deduction rules, 37
Adjacency matrix, graph, 75 Disjunctive normal form, d.n.f, 35
Associative Law, 29 Distributive Law, 29
Domain, 9

B
Bisection method, root finding, 99 E
Boolean values, 27 Edges, graph, 74
Encryption, 84
Euler’s totient function, 82
C
Caesar cypher, 84
F
Cartesian product, 1
Finite set, 3
Centre of mass, 46
Function, 19
Co-domain, 9
Function, bijective, 20
Complete, graph, 74
Function, composition, 21
Complex numbers, 3, 49
Function, continuous, 90
Complex numbers, algebraic, 49
Function, derivative, 91
Complex numbers, conjugate, 49
Function, even, 20
Complex numbers, modulus, 49
Function, injective, 20
Complex numbers, operations, 50
Function, integral, 92
Complex numbers, polar, 49
Function, limit, 90
Conjunctive normal form, c.n.f, 36
Function, odd, 20
Connected, graph, 74
Function, surjective, 20
Contradiction, 28
Fundamental theorem of calculus, 92
Converse error, 37
Cost matrix, network, 75
Cycle, graph, 74 G
Greatest common divisor, 82
D
Database, 12 I
Datatype string, 21 Infinite set, 3
De Moivre’s formula, 50 Integers, 3
De Morgan’s Law, 29 Interpolation, Lagrange polynomial, 96
Decryption, 84 Inverse error, 37

O. Bagdasar, Concise Computer Mathematics, SpringerBriefs in Computer Science, 107


DOI: 10.1007/978-3-319-01751-8, © The Author(s) 2013
108 Index

K Prime number, 81
Key, 12 Private key, 84
Kruskal’s algorithm, 75 Proof by contradiction, 38
Proposition, 28
Public key, 84
L
Least common multiple, 82
Linear system of equations, 58 Q
Logical operations, and, 27 Quantifiers, existential, 29
Logical operations, equivalent, 27 Quantifiers, negation, 29
Logical operations, implies, 27 Quantifiers, universal, 29
Logical operations, not, 27
Logical operations, or, 27
Loops, graph, 74 R
Rational numbers, 3
Real numbers, 3
M Reflections, transformation, 68
Many-one, 10 Relations, 9
Matrix, 55 Relations, antisymmetric, 11
Matrix, column, 56 Relations, composition, 11
Matrix, diagonal, 57 Relations, equivalence, 11
Matrix, identity, 57 Relations, image, 10
Matrix, inverse, 58 Relations, intersection, 10
Matrix, row, 56 Relations, inverse, 10
Matrix, square, 56 Relations, reflexive, 11
Matrix, transformation, 66 Relations, symmetric, 11
Matrix, transpose, 57 Relations, total, 11
Matrix,multiplication, 57 Relations, transitive, 11
Modulo congruence, 83 Relations, union, 10
Modus ponens, 37 Relatively prime numbers, 82
Modus Tollens, 37 Root of polynomial, 22
Rotation, transformation, 67
RSA algorithm, 84
N
Network, graph, 74
Newton’s method, root finding, 100 S
Number set, 3 Scaling, transformation, 67
Sentence, 28
Sequence, 89
O Sequence, convergent, 89
One-many, 10 Sequence, divergent, 89
One-one, 10 Sequence, limit, 89
Order, graph nodes, 74 Set, 1
Set cardinal, 3
Set inclusion, 1
P Sets, complement, 2
Path, graph, 74 Sets, difference, 2
Pigeonhole principle, 38 Sets, empty, 1
Planar, graph, 74 Sets, intersection, 2
Polynomials, 4 Sets, symmetric difference, 2
Power set, 1 Sets, union, 2
Predicate, 28 Sets, universe, 1
Prim’s algorithm, 75 Shearing, transformation, 68
Prime factorisation, 81 Shortest path, network, 75
Index 109

Simple, graph, 74 Truth table, 27


Spanning tree, graph, 74

V
T Vectors, 46
Tautology, 28 Vectors, addition, 46
Trail, graph, 74 Vectors, dot product, 47
Translation, transformation, 67 Vectors, magnitude, 46
Trapezium rule, 97 Vectors, scalar product, 47
Trapezium rule, composite, 97 Vectors, zero, 46
Trapezium rule, error bound, 97 Venn diagram, 2
Tree, graph, 74 Vertices, graph, 74

You might also like