Date post: | 31-Dec-2015 |
Category: |
Documents |
Upload: | fallon-waller |
View: | 40 times |
Download: | 0 times |
CS1022 Computer Programming &
Principles
Lecture 1Functions
Plan of lecture• Why functions?• Inverse relations and composition of relations
(background to functions)• Functions• Terminology• Digraphs and graphs of functions• Properties of functions
2CS1022
Why functions?• Functions capture transformations– Input arguments used to compute output results
• Input/output is key to computing• Advantages:– No commitments to computers/architectures– Abstract, clean, compact, focussed
• Functions represent computations– Underpinned by -calculus (a kind of “interpreter”)– Programming paradigm: functional languages
• What is x's age? Given a value for x, output the result: What is x's age?(Bill) => 34
3CS1022
Procedural vs. functional programs• Factorial: 5! = 5 4 3 2 1 = 120• Formally: or
4CS1022
Functional Solutionfac 0 1 fac n fac(n – 1) * n
Procedural solutionbegin input n fac := 1 for k 1 to n do fac := fac * k output facend
Inverse relations• Given a binary relation R between sets A and B• The inverse relation R–1 between A and B is
R–1 (b, a) : (a, b) R• For instance, the inverse of relation is a parent of on
the set of People is the relation is a child of– Graphically, the inverse of a binary relation is obtained
by reversing all arrows in the digraph of original relation
5CS1022
R
p
q
1
2
R–1
p
q
1
2
R (p, 1), (p, 2), (q, 2) R–1 (1, p), (2, p), (2, q)
Composition of relations (1)• Let R be a binary relation between sets A and B• Let S be a binary relation between sets B and C• The composition of R and S, represented as S R, is
a binary relation between A and C given by S R (a, c) : a A, c C, a R b, b S c for some b B
• The new relation relates elements of A and C using elements of B as “intermediaries”
6CS1022
Composition of relations (2)• If R is a relation “is a sister of” on a set of people• If S is a relation “is the mother of” on the same set• The composition S R is
S R (a, c) : a is a sister of b, b is mother of cS R (a, c) : a is a sister of the mother of c
S R (a, c) : a is an aunt of c• What about S S?
S S (a, c) : a is mother of b, b is mother of cS S (a, c) : a is mother of the mother of c
S S (a, c) : a is a grandmother of c7CS1022
Composition of relations (3)• Let R and S be binary relations defined as
Then a R 1 and 1 S y implies (a, y) S R a R 2 and 2 S x implies (a, x) S R a R 3 and 3 S x implies (a, x) S R b R 2 and 2 S x implies (b, x) S R
8CS1022
R
a
b
1
32
S x
y1
23
S R
a
b
x
y
R (a, 1), (a, 2), (a, 3), (b, 2)
S (1, y), (2, x), (3, x)
Composition of relations w/ matrix• Relations represented as a matrices for – It easy to see relationship properties by inspecting the
matrix, e.g. relationship is reflexive where it is 1 on the middle diagonal.
– Computers view relations as a two dimensional array• How to compute compositions using matrices? • Problem: – Given the logical matrices of two relations R and S
– Compute the logical matrix of their composition S R
• Result is logical (or Boolean) matrix product
9CS1022
Composition with matrix (2)• Let
A a1,a2,,an B b1,b2,,bm C c1,c2,,cp • Relations
R relates A and B S relates B and C• The logical matrix M representing R is given by– M(i, j) T (true) if (ai, bj) R
– M(i, j) F (false) if (ai, bj) R
• The logical matrix N representing S is given by– N(i, j) T (true) if (bi, cj) S
– N(i, j) F (false) if (bi, cj) S10CS1022
Composition with matrix (3)• Logical matrix P represents S R– If there is bk B with ai R bk and bk S cj then
M(i, k) = T and N(k, j) T Since ai (S R) cj then P(i, j) is also T
– Otherwise M(i, k) = F or N(k, j) F, for all k,
and P(i, j) F
• Logical matrix P representing S R is given by– P(i, j) T (true) if M(i, k) T and N(k, j) T, for some k– P(i, j) F (false) otherwise
11CS1022
bk is the bridge between the relations that allows the product relation.
12
Matrix Boolean product• A Boolean product of two matrices, where the matrices have T/F values (1/0) –– It is the conjunction (and) of the disjunctions (ors), where a, b, and c reference the matrix and xi,j is the row and column of x in the matrix:
etc...etc...
– Find a pair with a bridge between relations and mark 1/0 in result.
1110
1110
0110
0110
0011
1100
0110
0110
0101
0101
0010
0001
13
Matrix Boolean productR bill jill phil marybill 1 0 0 0jill 0 1 0 0phil 1 0 1 0mary 1 0 1 0
S bill jill phil marybill 0 1 1 0jill 0 1 1 0phil 0 0 1 1mary 1 1 0 0
R S R S
1110
1110
0110
0110
0011
1100
0110
0110
0101
0101
0010
0001
c1,2 is the same row in R, but the next column in S.
Composition with matrix (4)• Alternatively,
P(i, j) (M(i, 1) and N(1, j)) or (M(i, 2) and N(2, j))
... or (M(i, n) and N(n, j))
• We write P MN
For this Boolean matrix product
14CS1022
• Functions: special kind of (restricted) relation• Function from A to B is a binary relation in which– Every element of A is associated with a uniquely
specified element of B• That is, for all a A, there is exactly one pair (a, b)• In terms of digraphs, a function is a relation with
precisely one arc leaving every element of A
Functions (1)
15CS1022
Are these relations functions? Justify.• Relation x is a sibling of y on the set of People– No, since a person x may have several siblings (or none)
• R = (x, x2) : x Z (defined on Z)– Yes, since for any integer x, the value x2 is unique
• S = (x, y) : x y2 (defined on R)– No, because (2, 2) and (2, 2) belong to S
Functions (2)
16CS1022
• Let f be a function from set A to set B • For each x A there exists a unique y B, (x, y) f• We write y f(x)• We refer to f(x) as the image of x under f• We also write f : A B and it reads:– f is a function which transforms/maps each element of A
to a uniquely determined element of set B
Terminology of functions (1)
17CS1022
• A is the domain of f and B is the co-domain of f • The range of f is the set of images of all elements of
A under f, and is denoted as f(A) f(A) f(x) : x A
• Venn diagram:
Terminology of functions (2)
18CS1022
A B
f(A)
f
• When f : A B and sets A, B are infinite, we cannot draw a digraph of the function
• We can plot the pairs to build a picture of f• For instance, f : R R, f(x) x2 is as follows:
Digraphs of functions on infinite sets
19CS1022
y
x
(2, 4)
• Horizontal axis x is the domain R• Vertical axis y is the co-domain R• Curve consists of points (x, y) in
the Cartesian product R R for which f(x) x2
• Let f : A B be a function• We say f is injective (or one-to-one) function if
a1a2 ((a1, a2 A) and f(a1) f(a2)) a1 a2
• That is,If output is the same then input must be the same
• An injective function never repeats values• Different inputs give different outputs:
a1a2 ((a1, a2 A and a1 a2) f(a1) f(a2))
Properties of functions (1)
20CS1022
• Let f : A B be a function• f is surjective (or onto) if its range coincides with its
co-domain• For every b B there is an a A with b f(a)
b a (b B and a A and b f(a))• In other words:– Each element of the co-domain is a value of the function
Properties of functions (2)
21CS1022
• Let f : A B be a function• f is bijective if it is both injective and surjective
Properties of functions (3)
22CS1022
a
b
1
3
2
c
• Consider the functions below (as graphs)Properties of functions (3)
23CS1022
a
b
1
3
2
c
a
b
1
3
2
c
• Not injective as f(a) f(b) 1• Not surjective as 2 in codomain is not a
value of any f(x)
• Is injective as f(x) are all different• Is surjective as range and codomain are equal• Is bijective (injective and surjective)
• Is injective as f(x) are all different• Not surjective as 2 in codomain is not a
value of any f(x)
You should now know:• Inverse relations and composition of relations• Functions as a (special type) of relations• Properties of functions and some terminology
Summary
24CS1022
Further reading• R. Haggarty. “Discrete Mathematics for
Computing”. Pearson Education Ltd. 2002. (Chapter 5)
• Wikipedia’s entry• Wikibooks entry
25CS1022