> #Weiji Zheng, 09/22/2020, Assignment 4 ; > #OK to post homework ; > ; > #Q1 ; > ; > #CP(A,B): The Cartesian product of finite sets A and B ; > CP:=proc(A,B) local a,b: > {seq(seq([a,b],a in A),b in B)}: > end: > CP({a,b,c},{b,c,d}); {[a, b], [a, c], [a, d], [b, b], [b, c], [b, d], [c, b], [c, c], [c, d]} ; > ; > > > #Mynops(A): Inputs a finite set A and outputs the number of elements using the > #Fundamental Theorem of Enumeration (see http://sites.math.rutgers.edu/~zeilberg/mamarim/mamarimPDF/enu.pdf, p.1, bottom of column 1) > #It does the same thing as the Maple command ` nops(A) ' > Mynops:=proc(A) local a: > > if not type(A,set) then > print(A, `is not a set `): > RETURN(FAIL): > fi: > > add(1, a in A): > end: > #CheckMult(A,B): Empirically checks the trivial but EXTREMELY important fact > CheckMult:=proc(A,B) > evalb(Mynops(CP(A,B))=Mynops(A)*Mynops(B)): > end: > CheckMult({1,2},{3,4}); > true ; > ; > #CheckAdd(A,B): Empirically checks the trivial but EXTREMELY important fact ; > CheckAdd:=proc(A,B): > if not type(A,set) then > print(A, `is not a set `): > RETURN(FAIL): > fi: > > if not type(B,set) then > print(B, `is not a set `): > RETURN(FAIL): > fi: > > if A intersect B<>{} then > print(A,B, `have the following common elements `, A intersect B): > RETURN(FAIL): > fi: > > #This checks that if A and B are disjoint finite sets, then > #|A U B|= |A| + |B| > evalb(Mynops(A union B)=Mynops(A) + Mynops(B)): > end: > ; > CheckAdd({1,2,3},{3,4,5}) {1, 2, 3}, {3, 4, 5}, `have the following common elements `, {3} FAIL ; > > #Words(A,n): inputs a finite set, the "alphabet" and outputs the set of n-letter words in A. ; > Words:=proc(A,n) local W1,w,a: > option remember: > if n=0 then > RETURN({[]}): > else > W1:=Words(A,n-1): > RETURN({seq(seq([op(w),a], a in A),w in W1)}): > fi: > end: > member([d,o,r,o,n],Words({d,o,n,r},5)); true ; > ; > #Q2 ; > ; > #When n = 106, I found the seq A262530 ; > ; > #Q3 ; > ; > #given a UNIVERSAL set U, and subsets A1, A2, defining the COMPLEMENT of a subset A of U by Comp(U,A), > #|Comp(U,A1) intersect Comp(U,A2)|= |U|- |A1|-|A2|+ |A1 intersect A2| ; > ; > #1.Check it by hand for U={1,..., 15}, A1= subset of U that are multiples of 3, A2=subsets of U that are multiples of 5 ; > #Comp({1,...,15},[3,6,9,12,15]) = {1,2,4,5,7,8,10,11,13,14} ; > #Comp({1,...,15},[5,10,15]) = {1,2,3,4,6,7,8,9,11,12,13,14} ; > # Comp(U,A1) intersect Comp(U,A2) = {1,2,4,7,8,11,13,14} nops = 8 ; > #|A1 intersect A2| = |{15}| = 1 ; > #|U|=15 |A1|=5 |A2|=3 ; > #left = 8; right = 15-5-3+1 = 8 -> EQUAL! ; > ; > #2.Write a Maple procedure PIE2(U,A1,A2) that inputs three sets U, A1, A2. After checking that U, A1,A2, are indeed sets (return FAIL if they are not) AND that A1 or A2 are NOT subsets of U (return FAIL if they are not), computes it in two ways. The first way by directly finding |Comp(U,A1) intersect Comp(U,A2)| and the second way by using the above formula, namely |U|- |A1|-|A2|+ |A1 intersect A2|.For example the output of PIE2({1,2,3,4,5},{1,2,3},{2,3,5})should be [1,1] ; > PIE2 := proc(U,A1,A2) local i,S1, S2, n1, n2: > option remenber: > > if (not type(U, set)) or (not type(A1, set)) or (not type(A2, set)) then > RETURN(FAIL): > fi: > if (not A1 subset U) or (not A2 subset U) then > RETUEN(FAIL): > fi: > > #|Comp(U,A1) intersect Comp(U,A2)| > n1 := nops((U minus A1) intersect (U minus A2)): > > n2 := nops(U) - nops(A1) - nops(A2) + nops(A1 intersect A2): > > RETURN([n1, n2]): > > end: > PIE2({1,2,3,4,5},{1,2,3},{2,3,5}) [1, 1] ; > ; Warning, premature end of input, use + to avoid this message. ;