CS1022 Computer Programming & Principles
260 likes | 567 Vues
CS1022 Computer Programming & Principles. Lecture 5.1 Functions (1). Plan of lecture. Why functions? Inverse relations and composition of relations (background to functions) Functions Terminology Digraphs and graphs of functions Properties of functions. Why functions?.
CS1022 Computer Programming & Principles
E N D
Presentation Transcript
CS1022Computer Programming & Principles Lecture 5.1 Functions (1)
Plan of lecture • Why functions? • Inverse relations and composition of relations (background to functions) • Functions • Terminology • Digraphs and graphs of functions • Properties of functions CS1022
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 CS1022
Procedural vs. functional programs • Factorial: 5! = 5 4 3 2 1 = 120 • Formally: or CS1022
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 R (p,1), (p,2), (q,2) R–1 (1, p), (2, p), (2, q) R R–1 1 1 2 2 p p q q CS1022
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 SR, is a binary relation between A and C given by SR(a,c) : aA, cC, aR b, bS c for some bB • The new relation relates elements of A and C using elements of B as “intermediaries” CS1022
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 compositionSR is SR(a,c) : ais a sister of b, bis mother of c SR(a,c) : ais a sister of themother of c SR(a,c) : ais an aunt of c • What about SS? SS(a,c) : ais mother ofb, bis mother of c SS(a,c) : ais mother ofthe mother of c SS(a,c) : ais a grandmother of c CS1022
Composition of relations (3) • Let R and S be binary relations defined as Then aR 1 and 1 Sy implies (a,y) SR aR 2 and 2 Sx implies (a,x) SR aR 3 and 3 Sx implies (a,x) SR bR 2 and 2 Sx implies (b,x) SR R(a,1), (a,2), (a,3), (b,2) 2 1 2 3 S(1,y), (2,x), (3,x) R SR S 1 x x y y 3 a a b b CS1022
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 SR • Result is logical (or Boolean) matrix product CS1022
Composition with matrix (2) • Let Aa1,a2,,an Bb1,b2,,bm Cc1,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) S CS1022
Composition with matrix (3) • Logical matrix P represents SR • If there is bk B with aiRbkandbkScj then M(i, k) = T andN(k, j) T Since ai (SR)cj then P(i, j) is also T • Otherwise M(i, k) = F orN(k, j) F, for all k, and P(i, j) F • Logical matrix P representing SR is given by • P(i, j) T (true) if M(i, k) T andN(k, j) T, for some k • P(i, j) F (false) otherwise bk is the bridge between the relations that allows the product relation. CS1022
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,jis 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.
Matrix Boolean product R bill jillphilmary bill 1 0 0 0 jill 0 1 0 0 phil 1 0 1 0 mary 1 0 1 0 S bill jillphilmary bill 0 11 0 jill 0 1 1 0 phil0 0 1 1 mary 1 100 R S R S 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) andN(1, j)) or (M(i, 2) andN(2, j)) ... or (M(i, n) andN(n, j)) • We write PMN For this Boolean matrix product CS1022
Functions (1) • Functions: special kind of (restricted) relation • Function from A to Bis 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 CS1022
Functions (2) 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 CS1022
Terminology of functions (1) • 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 CS1022
Terminology of functions (2) • 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: A B f f(A) CS1022
Digraphs of functions on infinite sets • 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: y • Horizontal axis xis the domain R • Vertical axis yis the co-domain R • Curve consists of points (x, y) in the Cartesian product R R for which f(x) x2 (2, 4) x CS1022
Properties of functions (1) • 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)) CS1022
Properties of functions (2) • Let f : A B be a function • f is surjective (or onto) if its range coincides with its co-domain • For every bBthere is an a A with b f(a) b a (bB and a A and b f(a)) • In other words: • Each element of the co-domain is a value of the function CS1022
Properties of functions (3) • Let f : A B be a function • f is bijective if it is both injective and surjective CS1022
Properties of functions (3) • Consider the functions below (as graphs) • Not injective as f(a) f(b) 1 • Not surjective as 2 in codomain is not a value of anyf(x) a a a 1 1 1 b b b 2 2 2 c c c 3 3 3 • 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 anyf(x) CS1022
Summary You should now know: • Inverse relations and composition of relations • Functions as a (special type) of relations • Properties of functions and some terminology CS1022
Further reading • R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd. 2002. (Chapter 5) • Wikipedia’s entry • Wikibooks entry CS1022