# Functions A function, f, is a mechanism that relates (or maps) one set of elements to another set. –More specifically, f, is a special type of relation.

## Presentation on theme: "Functions A function, f, is a mechanism that relates (or maps) one set of elements to another set. –More specifically, f, is a special type of relation."— Presentation transcript:

Functions A function, f, is a mechanism that relates (or maps) one set of elements to another set. –More specifically, f, is a special type of relation which associates the element of its domain to a unique element of its range. Let S and T be two sets, then f is often written as: – f : S x T (f defined over a Cartesian Product just like relation) – f : S T –f (s) = t, for s in S (domain) and t in T (range) – s f t, for s in S and t in T Formally, a function, f, over S x T is defined as follows: – f = { \/ s: S; t 1, t 2 : T I ( s f t 1 /\ s f t 2 ) -> t 1 = t 2 }

Function, f, pictorially S = domain of f T = range of f s1 s2 s3 s4 t1 t2 t3 t4 f (s1) = t1 and f (s2) = t1 ----- ok f (s3) = t2 ----- ok f (s4) = t3 and f (s4) = t4 ----- NOT ok X X

Examples of functions f(x) = 2x + 2, for x : N f(x) = 2x + 2, for x : N –this is the same as y = 2x + 2, for x, y : N g = { (1,2), (2,5), (3,10), ------ (n, n 2 +1)}, for n: N 1 g = { (1,2), (2,5), (3,10), ------ (n, n 2 +1)}, for n: N 1 Let S = { tom, jane, maple, sam} and Emp_N = N 1, then f: S x Emp_N may be defined as: Let S = { tom, jane, maple, sam} and Emp_N = N 1, then f: S x Emp_N may be defined as: f = { (tom, 3), (jane, 423) } f = { (tom, 3), (jane, 423) } Is Square Root a function? NO! Sqrt (4) = +2 and Sqrt (4) = -2 Sqrt (4) = +2 and Sqrt (4) = -2 This violates the definition of a function because sqrt results in two values. Employee number is unique

Partial & Total Functions A function, f : S x T, is a partial function if dom f is a proper subset of S. – dom f S (e.g. f(x) = 10/x does not include x = 0) A function, f: S x T, is a total function if the dom f is the same as S. – dom f = S

Injection A function, f: S x T, is called an injection if – f(s1) = t1 and f(s2) = t1, then s1 = s2 Injective functions are also called 1-to-1 functions s1 s2 s3 s5 s4 t1 t2 t3 t4 f(s4) = t4 and f(s5) = t4 would not be allowed if f were an injection X X Note the inverse of f. If f is an injection, then f -1 is also a function

Examples of Injection f = {(1,3), (2,5), (3,2), (11,24) } is an injection f = {(1,3), (2,5), (3,2), (11,24) } is an injection –Note that f -1 = {(3,1), (5,2), (2,3), (24,11)} is also a function. g = {(1,3), (2,5), (3,5), (11,24)} is NOT an injection –Note that g -1 = {(3,1), (5,2), (5,3), (24,11)} is not a function with (5,2) and (5,3) as part of g -1 – So, when f is not an injection, f -1 will not be a function. Is “absolute value” function an injection? Is “absolute value” function an injection? I 4 I = 4 and I -4 I = 4 I 4 I = 4 and I -4 I = 4

Surjection A function, f: S x T, is called a surjection if ran f = T ran f = T A surjective function is also called an onto function For function, f, to be surjective, there can not be t5 in T t1 t2 t3 t4 t5 ST

Examples of Surjection Let A = { a1,a2,a3,a4,a5,a6,a7,a8,a9} and WK_day = { M,T,W,Th,F,S,Sn}, then g : A x WK_day defined below is a surjection g = {(a1, T),(a2, M),(a3,Th),(a4, F),(a5, Sn),(a6,Sn), (a7, W), (a8,S)} g = {(a1, T),(a2, M),(a3,Th),(a4, F),(a5, Sn),(a6,Sn), (a7, W), (a8,S)} but g -1 is not a surjection because a9 would not be included. (and what else can you say about g -1 ?) but g -1 is not a surjection because a9 would not be included. (and what else can you say about g -1 ?) –Is g -1 a total or partial function ? Is it even a function? Example: In a computing file system, the function, f, that maps file_owners to active_files should be a surjection because every active_file is owned by some file_owner. (But What do we have to look out for to make sure that f is even a function? May be it should be f -1 ? May be we should just leave “f” as a relation?)

Bijection A function, f, is called bijective if it both –injective and –surjective A bijective function is also known as isomorphic

Example of bijective function Let S = {0 and positive even integers} and T={positive odd integers}, then g: S x T defined below is a bijection. – g (s) = t = s + 1, for s in S and t in T – note that g -1 is also a bijection Let S = {positive integers} and then f : S x S defined below is NOT a bijection – f(s) = s +1, for s in S – note that range of f does not include 1, which is in S. – note also that the inverse function, f -1, can not include 1 as its domain because f -1 (1) = 0 which is not in S.

Predecessor and Successor functions Let pred stands for predecessor function defined as: – pred: N 1 x N (note that N includes 0 and N 1 does not) – pred = {(1,0); (2,1); (3,2); - - - - -} Let succ stands for successor function defined as: –Succ: N x N 1 –Succ = { (0,1); (1,2); (2,3); - - - - - - }

Higher-Order functions A higher order function is a function, f, whose domain or range is itself a function.

Example of Higher-order function Model a query that will display all the items in a warehouse –Let w = warehouse names = { Atl, Ny, SanFran, LA}, I = items = {shoes, boots, socks, pants, jackets}, and D = dozens of items = {0, 1, 2, 3}. –Let function f : I x D be defined as the total function that specifies quantity of each item. –Let g : w x P f be the higher-order function that specifies the amount of each item in the warehouses. {remember: P f stands for power set of f } –g = { [Atl, ( (shoes,1),(boots,2),(socks,2),(pants,0),(jackets,1))], [Ny, ( (boots,2),(pants3),(jackets,1) ) ], - - - - } g (Atl) = {(shoes, 1), (boots, 2), (socks, 2), (pants, 0), (jackets,1) } g (Atl) = {(shoes, 1), (boots, 2), (socks, 2), (pants, 0), (jackets,1) }

Higher-Order function example re-examine Look at the previous example: –Would you want to redefine function f : I x D ? –Would it be better to have a non-function, but just a relation for this model?

Download ppt "Functions A function, f, is a mechanism that relates (or maps) one set of elements to another set. –More specifically, f, is a special type of relation."

Similar presentations