discrete mathematics in computer science
play

Discrete Mathematics in Computer Science B8. Functions Malte - PowerPoint PPT Presentation

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


  1. 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

  2. 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

  3. 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

  4. B8. Functions Partial and Total Functions Important Building Blocks of Discrete Mathematics Important building blocks: ◮ sets ◮ relations ◮ functions In principle, functions are just a special kind of relations: ◮ f : N 0 → N 0 with f ( x ) = x 2 ◮ relation R over N 0 with R = { ( x , y ) | x , y ∈ N 0 and y = x 2 } . Malte Helmert, Gabriele R¨ oger (University of Basel) Discrete Mathematics in Computer Science October 19, 2020 4 / 34

  5. B8. Functions Partial and Total Functions Functional Relations Definition A binary relation R over sets A and B is functional if for every a ∈ A there is at most one b ∈ B with ( a , b ) ∈ R . a 1 a 1 2 2 b b B B A c 3 A c 3 4 4 d d e e functional not functional Malte Helmert, Gabriele R¨ oger (University of Basel) Discrete Mathematics in Computer Science October 19, 2020 5 / 34

  6. B8. Functions Partial and Total Functions Functions – Examples ◮ f : N 0 → N 0 with f ( x ) = x 2 + 1 ◮ abs : Z → N 0 with � x if x ≥ 0 abs ( x ) = − x otherwise ◮ distance : R 2 × R 2 → R with ( x 2 − x 1 ) 2 + ( y 2 − y 1 ) 2 � distance (( x 1 , y 1 ) , ( x 2 , y 2 )) = Malte Helmert, Gabriele R¨ oger (University of Basel) Discrete Mathematics in Computer Science October 19, 2020 6 / 34

  7. B8. Functions Partial and Total Functions Partial Function – Example Partial function r : Z × Z � Q with � n if d � = 0 d r ( n , d ) = undefined otherwise Malte Helmert, Gabriele R¨ oger (University of Basel) Discrete Mathematics in Computer Science October 19, 2020 7 / 34

  8. B8. Functions Partial and Total Functions Partial Functions Definition (Partial function) A partial function f from set A to set B (written f : A � 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 ) ∈ G and say y is the image of x under f . If there is no y ∈ B with ( x , y ) ∈ G , then f ( x ) is undefined. Partial function r : Z × Z � Q with � n if d � = 0 d r ( n , d ) = undefined otherwise d ) | n ∈ Z , d ∈ Z \ { 0 }} ⊆ Z 2 × Q . has graph { (( n , d ) , n Malte Helmert, Gabriele R¨ oger (University of Basel) Discrete Mathematics in Computer Science October 19, 2020 8 / 34

  9. B8. Functions Partial and Total Functions Domain (of Definition), Codomain, Image Definition (domain of definition, codomain, image) Let f : A � 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 ) = { x ∈ A | there is a y ∈ B with f ( x ) = y } . The image (or range) of f is the set img( f ) = { y | there is an x ∈ A with f ( x ) = y } . f : { a , b , c , d , e } � { 1 , 2 , 3 , 4 } a 1 f ( a ) = 4 , f ( b ) = 2 , f ( c ) = 1 , f ( e ) = 4 b 2 domain { a , b , c , d , e } B A c 3 codomain { 1 , 2 , 3 , 4 } d 4 domain of definition dom( f ) = { a , b , c , e } e image img( f ) = { 1 , 2 , 4 } Malte Helmert, Gabriele R¨ oger (University of Basel) Discrete Mathematics in Computer Science October 19, 2020 9 / 34

  10. 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 � B be a partial function and let Y ⊆ B . The preimage of Y under f is the set f − 1 [ Y ] = { x ∈ A | f ( x ) ∈ Y } . f − 1 [ { 1 } ] = a 1 f − 1 [ { 3 } ] = b 2 B A c 3 f − 1 [ { 4 } ] = 4 d f − 1 [ { 1 , 2 } ] = e Malte Helmert, Gabriele R¨ oger (University of Basel) Discrete Mathematics in Computer Science October 19, 2020 10 / 34

  11. 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 ∈ 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

  12. B8. Functions Partial and Total Functions Specifying a Function Some common ways of specifying a function: ◮ Listing the mapping explicitly, e. g. f ( a ) = 4 , f ( b ) = 2 , f ( c ) = 1 , f ( e ) = 4 or f = { a �→ 4 , b �→ 2 , c �→ 1 , e �→ 4 } ◮ By a formula, e. g. f ( x ) = x 2 + 1 ◮ By recurrence, e. g. 0! = 1 and n ! = n ( n − 1)! for n > 0 ◮ 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

  13. B8. Functions Partial and Total Functions Relationship to Functions in Programming def factorial(n): if n == 0: return 1 else: return n * 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

  14. 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

  15. 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

  16. 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

  17. B8. Functions Operations on Partial Functions Restrictions and Extensions Definition (restriction and extension) Let f : A � B be a partial function and let X ⊆ A . The restriction of f to X is the partial function f | X : X � B with f | X ( x ) = f ( x ) for all x ∈ X . A function f ′ : A ′ � B is called an extension of f if A ⊆ A ′ and f ′ | A = 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

  18. B8. Functions Operations on Partial Functions Function Composition Definition (Composition of partial functions) Let f : A � B and g : B � C be partial functions. The composition of f and g is g ◦ f : A � C with  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: with f ( x ) = x 2 f : N 0 → N 0 g : N 0 → N 0 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

  19. B8. Functions Operations on Partial Functions Properties of Function Composition Function composition is ◮ not commutative: ◮ f : N 0 → N 0 with f ( x ) = x 2 ◮ g : N 0 → N 0 with g ( x ) = x + 3 ◮ ( g ◦ f )( x ) = x 2 + 3 ◮ ( f ◦ g )( x ) = ( x + 3) 2 ◮ 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

  20. 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: incr x = x + 1 square x = x * x squareplusone = incr . square Malte Helmert, Gabriele R¨ oger (University of Basel) Discrete Mathematics in Computer Science October 19, 2020 20 / 34

  21. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend