Discrete Mathematics in Computer Science B8

Discrete Mathematics in Computer Science B8

Discrete Mathematics in Computer Science B8. Functions Malte Helmert, Gabriele R¨oger University of Basel October 19, 2020 Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 1 / 34 Discrete Mathematics in Computer Science October 19, 2020 | B8. Functions B8.1 Partial and Total Functions B8.2 Operations on Partial Functions B8.3 Properties of Functions Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 2 / 34 B8. Functions Partial and Total Functions B8.1 Partial and Total Functions Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 3 / 34 B8. Functions Partial and Total Functions Important Building Blocks of Discrete Mathematics Important building blocks: I sets I relations I functions In principle, functions are just a special kind of relations: 2 I f : N0 ! N0 with f (x) = x 2 I relation R over N0 with R = f(x; y) j x; y 2 N0 and y = x g. Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 4 / 34 B8. Functions Partial and Total Functions Functional Relations Definition A binary relation R over sets A and B is functional if for every a 2 A there is at most one b 2 B with (a; b) 2 R. a 1 a 1 b 2 b 2 B B A c 3 A c 3 d 4 d 4 e e functional not functional Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 5 / 34 B8. Functions Partial and Total Functions Functions { Examples 2 I f : N0 ! N0 with f (x) = x + 1 I abs : Z ! N0 with ( x if x ≥ 0 abs(x) = −x otherwise 2 2 I distance : R × R ! R with p 2 2 distance((x1; y1); (x2; y2)) = (x2 − x1) + (y2 − y1) Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 6 / 34 B8. Functions Partial and Total Functions Partial Function { Example Partial function r : Z × Z 9 Q with ( n if d 6= 0 r(n; d) = d undefined otherwise Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 7 / 34 B8. Functions Partial and Total Functions Partial Functions Definition (Partial function) A partial function f from set A to set B (written f : A 9 B) is given by a functional relation G over A and B. Relation G is called the graph of f . We write f (x) = y for (x; y) 2 G and say y is the image of x under f . If there is no y 2 B with (x; y) 2 G, then f (x) is undefined. Partial function r : Z × Z 9 Q with ( n if d 6= 0 r(n; d) = d undefined otherwise n 2 has graph f((n; d); d ) j n 2 Z; d 2 Z n f0gg ⊆ Z × Q. Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 8 / 34 B8. Functions Partial and Total Functions Domain (of Definition), Codomain, Image Definition (domain of definition, codomain, image) Let f : A 9 B be a partial function. Set A is called the domain of f , set B is its codomain. The domain of definition of f is the set dom(f ) = fx 2 A j there is a y 2 B with f (x) = yg. The image (or range) of f is the set img(f ) = fy j there is an x 2 A with f (x) = yg. f : fa; b; c; d; eg f1; 2; 3; 4g a 1 9 f (a) = 4; f (b) = 2; f (c) = 1; f (e) = 4 b 2 B domain fa; b; c; d; eg A c 3 codomain f1; 2; 3; 4g d 4 domain of definition dom(f ) = fa; b; c; eg e image img(f ) = f1; 2; 4g Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 9 / 34 B8. Functions Partial and Total Functions Preimage The preimage contains all elements of the domain that are mapped to given elements of the codomain. Definition (Preimage) Let f : A 9 B be a partial function and let Y ⊆ B. The preimage of Y under f is the set f −1[Y ] = fx 2 A j f (x) 2 Y g. −1 a 1 f [f1g] = b 2 f −1[f3g] = B A c 3 f −1[f4g] = d 4 −1 e f [f1; 2g] = Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 10 / 34 B8. Functions Partial and Total Functions Total Functions Definition (Total function) A (total) function f : A ! B from set A to set B is a partial function from A to B such that f (x) is defined for all x 2 A. ! no difference between the domain and the domain of definition a 1 b 2 B A c 3 d 4 e Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 11 / 34 B8. Functions Partial and Total Functions Specifying a Function Some common ways of specifying a function: I Listing the mapping explicitly, e. g. f (a) = 4; f (b) = 2; f (c) = 1; f (e) = 4 or f = fa 7! 4; b 7! 2; c 7! 1; e 7! 4g I By a formula, e. g. f (x) = x2 + 1 I By recurrence, e. g. 0! = 1 and n! = n(n − 1)! for n > 0 I In terms of other functions, e. g. inverse, composition Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 12 / 34 B8. Functions Partial and Total Functions Relationship to Functions in Programming def factorial(n): ifn ==0: return1 else: returnn* factorial(n-1) ! Relationship between recursion and recurrence Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 13 / 34 B8. Functions Partial and Total Functions Relationship to Functions in Programming def foo(n): value= ... while<some condition>: ... value= ... return value ! Does possibly not terminate on all inputs. ! Value is undefined for such inputs. ! Theoretical computer science: partial function Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 14 / 34 B8. Functions Partial and Total Functions Relationship to Functions in Programming import random counter=0 def bar(n): print("Hi! I got input", n) global counter counter +=1 return random.choice([1,2,n]) ! Functions in programming don't always compute mathematical functions (except purely functional languages). ! In addition, not all mathematical functions are computable. Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 15 / 34 B8. Functions Operations on Partial Functions B8.2 Operations on Partial Functions Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 16 / 34 B8. Functions Operations on Partial Functions Restrictions and Extensions Definition (restriction and extension) Let f : A 9 B be a partial function and let X ⊆ A. The restriction of f to X is the partial function f jX : X 9 B with f jX (x) = f (x) for all x 2 X . 0 0 A function f : A 9 B is called an extension of f 0 0 if A ⊆ A and f jA = f . The restriction of f to its domain of definition is a total function. What's the graph of the restriction? What's the restriction of f to its domain? Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 17 / 34 B8. Functions Operations on Partial Functions Function Composition Definition (Composition of partial functions) Let f : A 9 B and g : B 9 C be partial functions. The composition of f and g is g ◦ f : A 9 C with 8 <>g(f (x)) if f is defined for x and (g ◦ f )(x) = g is defined for f (x) :>undefined otherwise Corresponds to relation composition of the graphs. If f and g are functions, their composition is a function. Example: 2 f : N0 ! N0 with f (x) = x g : N0 ! N0 with g(x) = x + 3 (g ◦ f )(x) = Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 18 / 34 B8. Functions Operations on Partial Functions Properties of Function Composition Function composition is I not commutative: 2 I f : N0 ! N0 with f (x) = x I g : N0 ! N0 with g(x) = x + 3 I (g ◦ f )(x) = x 2 + 3 I (f ◦ g)(x) = (x + 3)2 I associative, i. e. h ◦ (g ◦ f ) = (h ◦ g) ◦ f ! analogous to associativity of relation composition Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 19 / 34 B8. Functions Operations on Partial Functions Function Composition in Programming We implicitly compose functions all the time. def foo(n): ... x= somefunction(n) y= someotherfunction(x) ... Many languages also allow explicit composition of functions, e. g. in Haskell: incrx=x+1 squarex=x*x squareplusone= incr. square Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 20 / 34 B8. Functions Properties of Functions B8.3 Properties of Functions Malte Helmert, Gabriele R¨oger(University of Basel)Discrete Mathematics in Computer Science October 19, 2020 21 / 34 B8. Functions Properties of Functions Properties of Functions I Partial functions map every element of their domain to at most one element of their codomain, total functions map it to exactly one such value.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    34 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us