6. Functions Solutions Exercises A. 1. Define a predicate is_function that takes as input a vector whose components are vectors of dimension two. Then is_function has the following values: is_function([]) = 1, is_function([ [1, 2] ]) = 1, is_function([ [1, 2], [3, 2] ]) = 1, is_function([ [0, 5], [1, 4], [3, 0], [1, 4], [2, 5] ]) = 1, and is_function([ [1, 2], [3, 2], [1, 4] ] = 0. Test your function and write a description of the definition. Hint: You may want to define an auxiliary function with input two vectors of dimension two. What is important about such vectors when testing to see whether these vectors violate the definition of function? Solution The DERIVE expressions below define the function is_function and provide tests for this function and the auxiliary function test. CaseMode := Sensitive not(p) := 1 - p test(v,w) := not(IF(v SUB 1 = w SUB 1 AND v SUB 2 /= w SUB 2)) test([1, 2], [1, 3]) test([1, 2], [3, 3]) is_function(v) := PRODUCT(PRODUCT(test(v SUB i, v SUB j), i, 1, j), j, 1, DIMENSION(v)) [is_function([]), is_function([[1, 1]]), is_function([[1, 2], [1, 3]]), is_function([[1, 2], [2, 1]])] v := [[1, 6], [3, 5], [2, 6]] w := [[1, 6], [3, 5], [1, 4], [2, 8]] 1 [is_function(v), is_function(w)] The function test determines whether or not two ordered pairs are acceptable in the definition of a function as a set of ordered pairs. The function is_function tests every pair of ordered pairs in its argument to determine if they violate the definition of function or not. The product of these tests is 1 if and only if every pair is acceptable. 2. Define a function domain that computes the domain of a finite function. Some values of domain are shown below: domain([]) = [], domain([ [5, 2] ]) = [5], and domain([ [4, 2], [8, 3], [16, 4] ]) = [4, 8, 16]. Test your function and write a description of the definition. Solution The function domain defined below returns the first components of the vectors in a vector of vectors. domain(f) := VECTOR(f SUB i SUB 1, i, 1, DIMENSION(f)) [domain([]), domain([[1, 2]]), domain(v)] 3. Define a function range that computes the range of a finite function. Test your function and write a description of the definition. Solution The function range returns the second components of the vectors in a vector of vectors. range(f) := VECTOR(f SUB i SUB 2, i, 1, DIMENSION(f)) [range([]), range([[1, 2]]), range(v)] 4. Define a function restriction that computes the restriction of a function f to a subset of the domain of f. Your function should return the values shown below: f := [ [1, 1], [2, 4], [3, 9], [4, 16]], restriction(f, []) = [], restriction(f, [1, 2]) = [ [1, 1], [2, 4] ], restriction(f, domain(f)) = f, and 2 restriction(f, [0, 1]) = undefined. Solution Functions defined earlier can be used as auxiliary functions in solving this problem and later problems. Open a file in which these function definitions have been saved. FIRST(v) := v SUB 1 TAIL(v) := VECTOR(v SUB i, i, 2, DIMENSION(v)) CaseMode := Sensitive in(a, v) := IF(DIMENSION(v) = 0, 0, IF(a = FIRST(v), 1, in(a, TAIL(v)))) set(v) := IF(DIMENSION(v) = 0, v, IF(in(FIRST(v), TAIL(v)) = 1, set(TAIL(v)), APPEND([FIRST(v)], set(TAIL(v))))) union(s, t) := set(APPEND(s, t)) delete(a, s) := IF(DIMENSION(s) = 0, s, IF(a = FIRST(s), delete(a, TAIL(s)), APPEND([FIRST(s)], delete(a, TAIL(s))))) inter(s, t) := set(IF(DIMENSION(s) = 0, s, IF(in(FIRST(s), t) = 1, APPEND([FIRST(s)], inter(TAIL(s), t)), inter(TAIL(s), t)))) diff(s, t) := set(IF(DIMENSION(t) = 0, s, IF(in(FIRST(t), s) = 1, diff(delete(FIRST(t), s), TAIL(t)), diff(s, TAIL(t))))) is_subset(s, t) := PRODUCT(in(s SUB i, t), i, 1, DIMENSION(s)) and(p, q) := p*q eq(s, t) := and(is_subset(s, t), is_subset(t, s)) disjoint(s, t) := eq(inter(s, t), []) restrict(f, s) := IF(DIMENSION(f) = 0, [], IF(in((FIRST(f)) SUB 1, s) = 1, union([FIRST(f)], restrict(TAIL(f), s)), restrict(TAIL(f),s ))) [restrict([], []), restrict(v, []), restrict(v, [1, 3]), restrict(v, domain(v))] restriction(f, s) := IF(is_subset(s, domain(f))=1, restrict(f, s ), "The set s is not a subset of the domain of f.") [restriction(v, [2, 1]), restriction(v, [1, 4])] 3 The recursive function restrict adds the first element of f to the restricted function’s set only when the element’s first component is in the set s. The function restrict returns the restriction of the function f to the set s when s is a subset of the domain of f. The function restriction checks that s is a subset of the domain of f. 5. Determine which of the statements below are true and which are false. Prove any true statement, and give a counterexample (using DERIVE) for any false statement. Let f: X Y be a function. a. For all subsets A and B of X, if A B, then f(A) f(B). Solution The statement is true. Proposition: For all sets X and Y, subsets A and B of X, and functions f: X Y, if A B, then f(A) f(B). Proof: Suppose that X and Y are sets, A and B are subsets of X, f: X Y is a function, and A B. Further assume that y f(A). Then there exists a A such that f(a) = y. Since A is a subset of B, a B. Thus f(x) f(B). Therefore, f(A) f(B). b. For all subsets A and B of X, f(A B) = f(A) f(B). Solution The counterexample below shows that the statement is false. Define f, A, and B, and then simplify the next two expressions. [f := [[1, 3], [2, 3]], A := [1, 2], B := [1]] range(restriction(f, diff(A, B))) diff(range(restriction(f, A)), range(restriction(f, B))) c. For all subsets A and B of X, f(A B) = f(A) f(B). Solution The statement is false by the counterexample below. [f := [[1, 5], [2, 6], [3, 5]], A := [1, 2], B := [2, 3]] 4 range(restriction(f, inter(A, B))) inter(range(restriction(f, A)), range(restriction(f, B))) d. For all subsets A and B of X, f(A B) = f(A) f(B). Solution The statement is true. Proposition. For all sets X and Y, subsets A and B of X and functions f: X Y, f(A B) = f(A) f(B). Proof: Suppose that X and Y are sets, A and B are subsets of X, and f: X Y is a function. Then an element y f(A B) iff there exists x A B such that f(x) = y iff f(x) = y for some x A or f(x) = y for some x B iff y f(A) f(B). Therefore, f(A B) = f(A) f(B). Exercises B. 1. Define predicates is_oneone, is_onto, and is_bijective that perform the indicated tests on finite functions. Consider the functions: f := [ [1, 6], [3, 5], [2, 6] ] and g := [ [1, 5], [2, 6], [3, 12] ] The predicates should yield the following values: is_oneone( [] ) = 1, is_oneone( f ) = 0, is_oneone( g ) = 1, is_onto( [], [] ) = 1 is_onto( [], [1, 2]) = 0 is_onto( f, [5, 6] ) = 1, is_onto( f, [4, 5, 6] ) = 0 is_onto( g, [12, 5, 6] ) = 1 is_bijective( [], [] ) = 1, is_bijective( f, [5, 6] ) = 0, and is_bijective( g, [5, 6, 12] ) = 1. Test your predicates and write descriptions of their definitions. Solution The first four DERIVE expressions below define the functions. The expressions that follow can 5 be entered to define examples and to test these functions. The function test2 is an auxiliary function that determines whether or not a pair of twodimensional vectors violates the definition of injection. The product of the results for all ordered pairs in the definition of a function f is 1 if and only if there are no violations, and this product provides the definition of the function is_oneone. The functions is_onto and is_bijective directly implement the definitions of these properties. test2(v, w) := not(IF(v SUB 2 = w SUB 2 AND v SUB 1 /= w SUB 1)) is_oneone(f) := PRODUCT(PRODUCT(test2(f SUB i, f SUB j), i, 1, j), j, 1, DIMENSION(f)) is_onto(f, s) := eq(range(f), s) is_bijective(f, s) := and(is_onto(f, s), is_oneone(f)) g := [[1, 4], [2, 5], [3, 4]] [domain(g), range(g), is_oneone(g), is_onto(g, [4, 5]), is_bijective(g, [4, 5]) [is_function(g), domain(g), range(g), is_oneone(g), is_onto(g,[4,5]), is_bijective(g, [4, 5])] The function g with codomain [4,5] is onto but not one-to-one. [domain(g), range(g), is_oneone(g), is_onto(g, [4, 5, 6]), is_bijective(g, [4, 5, 6])] The function g with codomain [4,5,6] is not onto and not one-to-one. h := [[1, 4], [2, 5], [3, 6]] [domain(h), range(h), is_oneone(h), is_onto(h, [4, 5, 6]), is_bijective(h,[4, 5, 6])] The function h with codomain [4,5,6] is onto and one-to-one. 2. Define a DERIVE function val that evaluates a finite function. For the finite functions f and g of question #1, we want the following results: val(f, 1) = 5, val(f, 2) = 6, val(f, 4) = undefined, and val(g, 3) = 12. Test your function val and write a description of the definition. 6 Solution The function val1 is an auxiliary function that computes the sum of those second components where the first component equals n. The function val returns “undefined” if n is not in the domain of the function f, and it removes multiple occurrences of vectors from the definition of f by applying the function set to f. Except for the three function definitions, the other DERIVE expressions below can be used to test the functions. val1(f, n) := SUM(IF(f SUB i SUB 1 = n, f SUB i SUB 2, 0), i, 1, DIMENSION(f)) [val1(f, 3), val1(h, 3)] val(f, n) := IF(in(n, domain(f)) = 1, val1(set(f), n), "undefined") val(f, 3) [val(f, 4), val(h, 4)] Exercises C. 1. Define a functional inverse that returns the inverse function of a one-to-one finite function. For example, the following values should be returned: inverse([ [1, 5], [2, 6] ] ) = [ [5, 1], [6, 2] ], inverse( [] ) = [] inverse( [[1, 5], [2, 5] ] ) = undefined. Test your functional inverse and write a description of its definition. Solution The function inverse1 defined below is an auxiliary function that interchanges the two components of every ordered pair in the definition of f. The function inverse checks that the result is a function by checking that f is injective. inverse1(f) := VECTOR([f SUB i SUB 2, f SUB i SUB 1], i, 1, DIMENSION(f)) inverse1(h) inverse(f) := IF(is_oneone(f) = 1, inverse1(f), "undefined") [inverse(g), inverse(h)] 7 2. Define DERIVE functionals whose two arguments are finite functions (expressed as vectors with vector components of two dimensions) with real values. The values of the functionals are finite functions. The new DERIVE functionals to be defined are: a. sum b. product c. composition Here are some examples of finite functions and the values of the functionals evaluated on these functions. f := [ [1, 6], [2, 8], [3, 10] ], g := [ [1, -2], [3, 0], [2, -3] ], h := [ [6, 5], [10, 11], [4, 3], [8, 4] ], sum(f, g) = [ [1, 4], [2, 5], [3, 10] ], sum(f, h) = undefined, product(f, g) = [ [1, -12], [2, -24], [3, 0] ] product(g, h) = undefined, composition(h, f) = [ [1, 5], [2, 4], [3, 11] ], and composition(h, g) = undefined. These functionals should test that the domains of the input functions are suitable. Test your functionals and write a description of the definitions. Solution The functionals sum, product, and composition are direct translations of the mathematical concepts to DERIVE expressions. In each case auxiliary functions are first defined to implement the operation, and then checks for validity of arguments are included in the required functions. The expressions that are not definitions can be simplified to test the functionals. sum1(f, g) := VECTOR([f SUB i SUB 1, f SUB i SUB 2 + val(g, f SUB i SUB 1)], i, 1, DIMENSION(f)) sum1(g, h) sum(f, g) := IF(eq(domain(f), domain(g)) = 1, sum1(f, g),"undefined") k := [[0, 4], [2, 10]] [sum(h, k), sum(g, h), sum(h, g)] product1(f, g) := VECTOR([f SUB i SUB 1, f SUB i SUB 2*val(g, f SUB i SUB 1)], i, 1, DIMENSION(f)) product(f, g) := IF(eq(domain(f), domain(g)) = 1, product1(f, g), "undefined") [product(h, k), product(g, h), product(h, g)] 8 composition1(f, g) := VECTOR([g SUB i SUB 1, val(f, g SUB i SUB 2)] ,i, 1, DIMENSION(g)) m := [[1, 2], [2, 3], [3, 3], [0, 1], [4, 2]] [composition1(g, m), composition1(h, m)] composition(f, g) := IF(is_subset(range(g), domain(f)) = 1, composition1(f,g ), " undefined") [composition(g, m), composition(h, m), composition(h, g)] 3. Determine which statements below are true and which are false. Prove any true statement, and give a counterexample (using DERIVE) for any false statement. Let X and Y be sets. a. If f: X Y and g: X Y are one-to-one functions, then so is their sum. b. If f: X Y and g: X Y are one-to-one functions, then so is their product. Solution Statements a and b are shown to be false by the following counterexamples. f := [[1, 0], [2, 5]] g := [[1, 5], [2, 0]] [and(is_oneone(f), is_oneone(g)), is_oneone(sum(f, g)), is_oneone(product(f, g))] c. If the functions f: X Y and g: X Y are onto a set S, then so is their sum. d. If the functions f: X Y and g: X Y are onto a set S, then so is their product. Solution Statements c and d are shown to be false by simplifying the DERIVE expression below. [and(is_onto(f, [0, 5]), is_onto(g, [0, 5])), is_onto(sum(f, g), [0,5]), is_onto(product(f, g), [0, 5])] 9 e. If the composition fg is one-to-one, so is the function f. Solution The counterexample below shows that statement e is false. f := [[2, 5], [3, 5]] g := [[1, 2]] [is_oneone(composition(f, g)), is_oneone(f)] f. If the composition fg is one-to-one, then so is the function g. Solution Statement f is true. Proposition: For all functions g: X Y and f: Y Z, if the composition fg is one-to-one, then so is the function g. Proof: Suppose that g:X Y and f: Y Z are functions and the composition fg is one-to-one. Assume that g(a) = g(b) for some elements a and b in X. Then f(g(a)) = f(g(b)). Thus, since the composition fg is one-to-one, a = b. Therefore, g is one-to-one. g. If the composition fg is onto a set S, then so is f. Solution Statement g is true. Proposition: For all functions g: X Y and f: Y Z, if the composition fg is onto Z, then so is the function f. Proof: Suppose that g: X Y and f: Y Z are functions and the composition fg is onto Z. Let z be an element of Z. Then there exists x in X such that f(g(x)) = z since fg is onto. Let y = g(x). Then f(y) = f(g(x)) = z. Therefore f is onto Z. h. If the composition fg is onto range(fg), then g is onto domain(f). 10 Solution Since every function is onto its range, the hypothesis is true in every case. Simplify the DERIVE expression to show that the functions f and g provide a counterexample. [is_onto(composition(f, g), range(composition(f, g))), is_onto(g,domain(f))] 11