240 likes | 339 Views
CS1022 Computer Programming & Principles. Lecture 5.2 Functions (2). Plan of lecture. Inverse functions Composition of functions Pigeonhole principle Fundamentals of functional programming. Inverse functions (1).
E N D
CS1022Computer Programming & Principles Lecture 5.2 Functions (2)
Plan of lecture • Inverse functions • Composition of functions • Pigeonhole principle • Fundamentals of functional programming CS1022
Inverse functions (1) • Any function f : A B is also a relation, so we can form the inverse relation f–1 • If f–1 is a function, then f is an invertible function • We write f–1 : B Afor the inverse function • Function f consists of pairs (a, b), f(a) b • If f is invertible f–1 consists of pairs (b, a), f–1(b) a • That is, the inverse function “reverses” the effect of the original function, f(a) b, f–1(b) a CS1022
Inverse functions (2) • Which of the functions are invertible? a a a a a a 1 1 1 1 1 1 • Not a function! b b b b b b 2 2 2 2 2 2 c c c c c c 3 3 3 3 3 3 • A function! • Not a function! CS1022
Inverse functions (3) • Reversing arrows can be adapted for more complex scenarios (not graphic ones) • Let’s consider k : R R, k(x) = 4x 3 • Its “effect” can be described by the following diagram • Elementary operations “multiply by 4” and “add 3” reversed as “divide by 4” and “subtract 3”, respectively • Hence, k–1 : R R, k–1(x) ¼ (x 3) Multiply by 4 Add 3 x x 3 4x 4x 3 x Divide by 4 Subtract 3 ¼ (x 3) CS1022
Inverse functions (4) • Solution of previous slide can also be obtained algebraically: • Let y k(x), so x k–1(y) • Since y 4x 3, we can operate on it (y) – 3 (4x 3) – 3 (subtract 3 from both sides) ¼ (y – 3) ¼ (4x) (divide both sides by 4) ¼ (y – 3) x • Therefore k–1(y) ¼ (y – 3) • Since we usually use x as input parameter (a convention, but not essential), we have k–1(x) ¼ (x – 3) CS1022
Inverse functions (5) • Only bijective functions are invertible • Elements of co-domain must have a unique element in domain (injective) • Each element of co-domain is a value of the function (surjective) • Theorem: f is invertible if, and only if, it is bijective • Proof: in two parts • Show that a bijective function is invertible • Show that an invertible function must be bijective CS1022
Composition of functions (1) • Easier (more intuitive) than composition of relations • Let f : A B and g : B C be functions • Composite function gf : A C consists of (a, c) where for some b B, (a, b) f, and (b, c) g • Notice: • b f(a) uniquely determined by a since f is a function • c g(b) uniquely determined by b since g is a function • Hence, c g(f(a)) is uniquely determined by a • So, the composition gf is also a function • Therefore, gf : A C is function (gf )(x) g(f(a)) CS1022
Composition of functions (2) • Let f : R R, f(x) x2 and g : R R, g(x) 4x 3 Calculategf Calculatef g Calculatef f Calculategg gf(x) gf(x) g(f(x)) gf(x) g(f(x)) g(x2) gf(x) g(f(x)) g(x2) 4x2 3 f g(x) f g(x) f(g(x)) f g(x) f(g(x)) f(4x 3) f g(x) f(g(x)) f(4x 3) (4x 3)2 f g(x) f(g(x)) f(4x 3) (4x 3)2 16x2 24x 9 f f(y) f f(y) f(f(x)) f f(y) f(f(x)) f(x2) f f(y) f(f(x)) f(x2) (x2)2 f f(y) f(f(x)) f(x2) (x2)2 x4 gg(x) g(g(x)) g(4x 3) 4(4x 3) 3 16x 15 CS1022
The pigeonhole principle (1) • Let f : A B be a function over finite sets A and B • Suppose A a1, a2, , an (with n elements) • The pigeonhole principle states that if |A| |B| then at least one value of f occurs more than once • That is, f(ai) f(aj), for some i and j, i j CS1022
The pigeonhole principle (2) • Reality check: • Suppose f(ai) f(aj), for all i and j, i j • Then B contains n distinct elements f(a1), f(a2), ,f(an) • Hence |B| n, which contradicts |A| |B| Therefore there are at least two distinct elements ai, aj A with f(ai) f(aj) • Example: • Any group with 13 or more people, will have two (or more) people with a birthday in the same month something missing here? CS1022
Functional programming (1) • Functions neatly capture input/output relation • For instance, f(x) x2 maps input x onto output x2 • Functional programming languages explore • Recursion to achieve repetition • Function composition for modularity • We’ll look at these two features in the next slides CS1022
Functional programming (2) • Example: simple text-processing scenario • We use the following sets to define (co-)domains • The natural numbers N = 0, 1, 2, • The set C = {‘a’, ‘b’, , ‘z’} of lower-case characters in English (no special characters such as é, ö, etc.) • The set S of strings of characters from C • “bat” C • “ ” C (empty string) CS1022
Functional programming (3) • Suppose the following primitive (built-in) functions • CHAR : S C, where CHAR(s)is first character of s (non-empty) • REST : S S, where REST(s)is the string obtained by removing the first character of s (non-empty) • ADDCHAR : C S S, where ADDCHAR(c, s)is the string obtained by adding character c to the front of s • LEN : S N, where LEN(s)is the number of characters in the string s • “Built-ins”: don’t worry about their implementation • They are available for ready use CS1022
Function composition (1) • We can compose basic “built-in” functions to define more sophisticated computations • Composition by nesting functions f(g(h())) • Important: expected type of parameters • Evaluation from innermost function, outwards f(g(h())) f(g(h())) f(g(result)) f(g(result)) f(result) result CS1022
Function composition (2) • Example: suppose s “bat”, compute result of LEN(REST(s)) LEN(REST(“bat”)) LEN(“at”) 2 CS1022
Function composition (3) • Example: if s “bat”, compute result of ADDCHAR(CHAR(s), ADDCHAR(‘o’, REST(s))) ADDCHAR(CHAR(s), ADDCHAR(‘o’,REST(s))) ADDCHAR(CHAR(s), ADDCHAR(‘o’,“at”)) ADDCHAR(CHAR(s),ADDCHAR(‘o’,“at”)) ADDCHAR(CHAR(s),“oat”) ADDCHAR(CHAR(s),“oat”) ADDCHAR(‘b’,“oat”) ADDCHAR(‘b’,“oat”) “boat” CS1022
Function composition (4) • Notice: the value of s does not change • Parameters are input, processed (without changes) and results output • There is no “assignment” of values to variables • At least not in the “standard way”... • No “destructive” assignment as in “x := x + 1” CS1022
Recursion (1) • Repetition achieved with recursion • A function is defined in terms of itself • A function “calls” itself • Some non-functional programming languages also allow recursion • Recursion can be “most natural” way to define some computations • (Most) functional programming languages do not have while-, repeat-until- or for-loops CS1022
Recursion (2) • Let us consider again the factorial of numbers: • A functional solution in Python is • How does it work? CS1022
Recursion (3) Let’s “trace” how factorial(3) is computed CS1022
Recursion (4) • Notice: it requires a stack of function calls • If too many repetitions, then we run out of memory • In some programming languages (Haskell) when the recursive call is the last command (tail-recursive), then (in many cases) no stack is needed • Performance is comparable to conventional loops • Recursion can be indirect • Function f defined in terms of function g, • Function g defined in terms of function f • Termination, just like in other loops, is an issue • What would happen to factorial(1)? CS1022
Summary You should now know: • Inverse functions • Pigeonhole principle • Principles of functional programming • Recursion! CS1022
Further reading • R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd. 2002. (Chapter 5) • Wikipedia’s entry • Wikibooks entry CS1022