4.1 CompSci 102© Michael Frank Today’s topics FunctionsFunctions – –Notations and terms – –One-to-One vs. Onto – –Floor, ceiling, and identity Reading: Sections 1.8Reading: Sections 1.8 UpcomingUpcoming –Algorithms
4.2 CompSci 102© Michael Frank On to section 1.8… Functions From calculus, you are familiar with the concept of a real-valued function f, which assigns to each number x R a particular value y=f(x), where y R.From calculus, you are familiar with the concept of a real-valued function f, which assigns to each number x R a particular value y=f(x), where y R. But, the notion of a function can also be naturally generalized to the concept of assigning elements of any set to elements of any set. (Also known as a map.)But, the notion of a function can also be naturally generalized to the concept of assigning elements of any set to elements of any set. (Also known as a map.)
4.3 CompSci 102© Michael Frank Function: Formal Definition For any sets A, B, we say that a function f from (or “mapping”) A to B (f:A B) is a particular assignment of exactly one element f(x) B to each element x A.For any sets A, B, we say that a function f from (or “mapping”) A to B (f:A B) is a particular assignment of exactly one element f(x) B to each element x A. Some further generalizations of this idea:Some further generalizations of this idea: –A partial (non-total) function f assigns zero or one elements of B to each element x A. –Functions of n arguments; relations (ch. 6).
4.4 CompSci 102© Michael Frank Graphical Representations Functions can be represented graphically in several ways:Functions can be represented graphically in several ways: A B a b f f x y Plot Bipartite Graph Like Venn diagrams AB
4.5 CompSci 102© Michael Frank Functions We’ve Seen So Far A proposition can be viewed as a function from “situations” to truth values {T,F}A proposition can be viewed as a function from “situations” to truth values {T,F} –A logic system called situation theory. –p=“It is raining.”; s=our situation here,now –p(s) {T,F}. A propositional operator can be viewed as a function from ordered pairs of truth values to truth values: e.g., ((F,T)) = T.A propositional operator can be viewed as a function from ordered pairs of truth values to truth values: e.g., ((F,T)) = T. Another example: →((T,F)) = F.
4.6 CompSci 102© Michael Frank A Neat Trick Sometimes we write Y X to denote the set F of all possible functions f:X Y.Sometimes we write Y X to denote the set F of all possible functions f:X Y. This notation is especially appropriate, because for finite X, Y, we have |F| = |Y| |X|.This notation is especially appropriate, because for finite X, Y, we have |F| = |Y| |X|. If we use representations F 0, T 1, 2: {0,1}={F,T}, then a subset T S is just a function from S to 2, so the power set of S (set of all such fns.) is 2 S in this notation.If we use representations F 0, T 1, 2: {0,1}={F,T}, then a subset T S is just a function from S to 2, so the power set of S (set of all such fns.) is 2 S in this notation.
4.7 CompSci 102© Michael Frank Some Function Terminology If it is written that f:A B, and f(a)=b (where a A & b B), then we say:If it is written that f:A B, and f(a)=b (where a A & b B), then we say: –A is the domain of f. –B is the codomain of f. –b is the image of a under f. –a is a pre-image of b under f. In general, b may have more than 1 pre-image.In general, b may have more than 1 pre-image. –The range R B of f is R={b | a f(a)=b }. We also say the signature of f is A → B.
4.8 CompSci 102© Michael Frank Range versus Codomain The range of a function might not be its whole codomain.The range of a function might not be its whole codomain. The codomain is the set that the function is declared to map all domain values into.The codomain is the set that the function is declared to map all domain values into. The range is the particular set of values in the codomain that the function actually maps elements of the domain to.The range is the particular set of values in the codomain that the function actually maps elements of the domain to.
4.9 CompSci 102© Michael Frank Range vs. Codomain - Example Suppose I declare to you that: “f is a function mapping students in this class to the set of grades {A,B,C,D,E}.”Suppose I declare to you that: “f is a function mapping students in this class to the set of grades {A,B,C,D,E}.” At this point, you know f’s codomain is: __________, and its range is ________.At this point, you know f’s codomain is: __________, and its range is ________. Suppose the grades turn out all As and Bs.Suppose the grades turn out all As and Bs. Then the range of f is _________, but its codomain is __________________.Then the range of f is _________, but its codomain is __________________. {A,B,C,D,E}unknown! {A,B} still {A,B,C,D,E}!
4.10 CompSci 102© Michael Frank Operators (general definition) An n-ary operator over (or on) the set S is any function from the set of ordered n- tuples of elements of S, to S itself.An n-ary operator over (or on) the set S is any function from the set of ordered n- tuples of elements of S, to S itself. E.g., if S={T,F}, can be seen as a unary operator, and , are binary operators on S.E.g., if S={T,F}, can be seen as a unary operator, and , are binary operators on S. Another example: and are binary operators on the set of all sets.Another example: and are binary operators on the set of all sets.
4.11 CompSci 102© Michael Frank Constructing Function Operators If (“dot”) is any operator over B, then we can extend to also denote an operator over functions f:A B.If (“dot”) is any operator over B, then we can extend to also denote an operator over functions f:A B. E.g.: Given any binary operator :B B B, and functions f,g:A B, we define (f g):A B to be the function defined by: a A, (f g)(a) = f(a) g(a).E.g.: Given any binary operator :B B B, and functions f,g:A B, we define (f g):A B to be the function defined by: a A, (f g)(a) = f(a) g(a).
4.12 CompSci 102© Michael Frank Function Operator Example ,× (“plus”,“times”) are binary operators over R. (Normal addition & multiplication.) ,× (“plus”,“times”) are binary operators over R. (Normal addition & multiplication.) Therefore, we can also add and multiply functions f,g:R R:Therefore, we can also add and multiply functions f,g:R R: –(f g):R R, where (f g)(x) = f(x) g(x) –(f × g):R R, where (f × g)(x) = f(x) × g(x)
4.13 CompSci 102© Michael Frank Function Composition Operator For functions g:A B and f:B C, there is a special operator called compose (“○”).For functions g:A B and f:B C, there is a special operator called compose (“○”). –It composes (creates) a new function out of f and g by applying f to the result of applying g. –We say (f○g):A C, where (f○g)(a) :≡ f(g(a)). –Note g(a) B, so f(g(a)) is defined and C. –Note that ○ (like Cartesian , but unlike +, , ) is non-commuting. (Generally, f○g g○f.) Note match here.
4.14 CompSci 102© Michael Frank Images of Sets under Functions Given f:A B, and S A,Given f:A B, and S A, The image of S under f is simply the set of all images (under f) of the elements of S. f(S) : {f(s) | s S} : {b | s S: f(s)=b}.The image of S under f is simply the set of all images (under f) of the elements of S. f(S) : {f(s) | s S} : {b | s S: f(s)=b}. Note the range of f can be defined as simply the image (under f) of f’s domain!Note the range of f can be defined as simply the image (under f) of f’s domain!
4.15 CompSci 102© Michael Frank One-to-One Functions A function is one-to-one (1-1), or injective, or an injection, iff every element of its range has only 1 pre-image.A function is one-to-one (1-1), or injective, or an injection, iff every element of its range has only 1 pre-image. –Formally: given f:A B, “x is injective” : ( x,y: x y f(x) f(y)). Only element of the domain is mapped to any given one element of the range.Only one element of the domain is mapped to any given one element of the range. –Domain & range have same cardinality. What about codomain? Memory jogger: Each element of the domain is injected into a different element of the range.Memory jogger: Each element of the domain is injected into a different element of the range. –Compare “each dose of vaccine is injected into a different patient.”
4.16 CompSci 102© Michael Frank One-to-One Illustration Bipartite (2-part) graph representations of functions that are (or not) one-to-one:Bipartite (2-part) graph representations of functions that are (or not) one-to-one: One-to-one Not one-to-one Not even a function!
4.17 CompSci 102© Michael Frank Sufficient Conditions for 1-1ness For functions f over numbers, we say:For functions f over numbers, we say: –f is strictly (or monotonically) increasing iff x>y f(x)>f(y) for all x,y in domain; –f is strictly (or monotonically) decreasing iff x>y f(x) y f(x)<f(y) for all x,y in domain; If f is either strictly increasing or strictly decreasing, then f is one-to-one. E.g. x 3If f is either strictly increasing or strictly decreasing, then f is one-to-one. E.g. x 3 –Converse is not necessarily true. E.g. 1/x
4.18 CompSci 102© Michael Frank Onto (Surjective) Functions A function f:A B is onto or surjective or a surjection iff its range is equal to its codomain ( b B, a A: f(a)=b).A function f:A B is onto or surjective or a surjection iff its range is equal to its codomain ( b B, a A: f(a)=b). Think: An onto function maps the set A onto (over, covering) the entirety of the set B, not just over a piece of it.Think: An onto function maps the set A onto (over, covering) the entirety of the set B, not just over a piece of it. E.g., for domain & codomain x 3 is onto, whereas x 2 isn’t. (Why not?)E.g., for domain & codomain R, x 3 is onto, whereas x 2 isn’t. (Why not?)
4.19 CompSci 102© Michael Frank Illustration of Onto Some functions that are, or are not, onto their codomains:Some functions that are, or are not, onto their codomains: Onto (but not 1-1) Not Onto (or 1-1) Both 1-1 and onto 1-1 but not onto
4.20 CompSci 102© Michael Frank Bijections A function f is said to be a one-to-one correspondence, or a bijection, or reversible, or invertible, iff it is both one-to-one and onto.A function f is said to be a one-to-one correspondence, or a bijection, or reversible, or invertible, iff it is both one-to-one and onto. For bijections f:A B, there exists an inverse of f, written f 1 :B A, which is the unique function such thatFor bijections f:A B, there exists an inverse of f, written f 1 :B A, which is the unique function such that –(where I A is the identity function on A)
4.21 CompSci 102© Michael Frank The Identity Function For any domain A, the identity function I:A A (variously written, I A, 1, 1 A ) is the unique function such that a A: I(a)=a.For any domain A, the identity function I:A A (variously written, I A, 1, 1 A ) is the unique function such that a A: I(a)=a. Some identity functions you’ve seen:Some identity functions you’ve seen: – ing 0, ·ing by 1, ing with T, ing with F, ing with , ing with U. Note that the identity function is always both one-to-one and onto (bijective).Note that the identity function is always both one-to-one and onto (bijective).
4.22 CompSci 102© Michael Frank The identity function:The identity function: Identity Function Illustrations Domain and range x y y = I(x) = x
4.23 CompSci 102© Michael Frank Graphs of Functions We can represent a function f:A B as a set of ordered pairs {(a,f(a)) | a A}.We can represent a function f:A B as a set of ordered pairs {(a,f(a)) | a A}. Note that a, there is only 1 pair (a,b).Note that a, there is only 1 pair (a,b). –Later (ch.6): relations loosen this restriction. For functions over numbers, we can represent an ordered pair (x,y) as a point on a plane.For functions over numbers, we can represent an ordered pair (x,y) as a point on a plane. –A function is then drawn as a curve (set of points), with only one y for each x. ← The function’s graph.
4.24 CompSci 102© Michael Frank Aside About Representations It is possible to represent any type of discrete structure (propositions, bit-strings, numbers, sets, ordered pairs, functions) in terms of virtually any of the other structures (or some combination thereof).It is possible to represent any type of discrete structure (propositions, bit-strings, numbers, sets, ordered pairs, functions) in terms of virtually any of the other structures (or some combination thereof). Probably none of these structures is truly more fundamental than the others (whatever that would mean). However, strings, logic, and sets are often used as the foundation for all else. E.g. in Probably none of these structures is truly more fundamental than the others (whatever that would mean). However, strings, logic, and sets are often used as the foundation for all else. E.g. in
4.25 CompSci 102© Michael Frank A Couple of Key Functions In discrete math, we will frequently use the following two functions over real numbers:In discrete math, we will frequently use the following two functions over real numbers: –The floor function · :R → Z, where x (“floor of x”) means the largest (most positive) integer x. I.e., x :≡ max({i Z|i≤x}). –The ceiling function · :R → Z, where x (“ceiling of x”) means the smallest (most negative) integer x. x :≡ min({i Z|i≥x})
4.26 CompSci 102© Michael Frank Visualizing Floor & Ceiling Real numbers “fall to their floor” or “rise to their ceiling.”Real numbers “fall to their floor” or “rise to their ceiling.” Note that if x Z, x x & x x Note that if x Z, x x & x x Note that if x Z, x = x = x.Note that if x Z, x = x = x. 0 1 22 3 1.6 =2 1.4 = 2 1.4 1.4 = 1 1.6 =1 33 3 = 3 = 3
4.27 CompSci 102© Michael Frank Plots with floor/ceiling Note that for f(x)= x , the graph of f includes the point (a, 0) for all values of a such that a 0 and a<1, but not for the value a=1.Note that for f(x)= x , the graph of f includes the point (a, 0) for all values of a such that a 0 and a<1, but not for the value a=1. We say that the set of points (a,0) that is in f does not include its limit or boundary point (a,1).We say that the set of points (a,0) that is in f does not include its limit or boundary point (a,1). –Sets that do not include all of their limit points are generally called open sets. In a plot, we draw a limit point of a curve using an open dot (circle) if the limit point is not on the curve, and with a closed (solid) dot if it is on the curve.In a plot, we draw a limit point of a curve using an open dot (circle) if the limit point is not on the curve, and with a closed (solid) dot if it is on the curve.
4.28 CompSci 102© Michael Frank Plots with floor/ceiling: Example Plot of graph of function f(x) = x/3 :Plot of graph of function f(x) = x/3 : x f(x) Set of points (x, f(x)) +3 22 +2 33