Help3:=proc():print(` Box(L), CAT(L1,L2) , PreP(a,L), App(L,a), UC(n) , NEX(w) `): print(`REV(L), UCG(n), NEXG(w), PREG(w) , RBW(n)`): end: #Box(L): Inputs a list L (where k=nops(L)) of non-negative integers L outputs #the LIST of all LISTS [a1,a2,..., ak] in LEX ORDER such that ai is in {0,1,.., L[i]) #In particular the n-dim UNIT cube would be Box([1$n]); Box:=proc(L) local k,i,L1,B1,j: option remember: if not type(L,list) then print(L, `should be a list `): RETURN(FAIL): fi: k:=nops(L): if k=0 then RETURN([[]]): fi: if not ({seq(type(L[i],integer),i=1..nops(L))}={true} and min(op(L))>=0) then print(`bad input`): RETURN(FAIL): fi: L1:=[op(1..k-1,L)]: B1:=Box(L1): [ seq(seq([op(B1[j]),i],i=0..L[k]),j=1.. nops(B1))]: end: #CAT(L1,L2): joining two lists in order CAT:=proc(L1,L2): [op(L1),op(L2)]:end: #PreP(a,L): Given a list L of lists, outputs the list where #sticks a to the beginnin of each of them #For example #PreP(0,[[1,2,3],[1,3,4]])= [[0,1,2,3],[0,1,3,4]] PreP:=proc(a,L) local i: [ seq([a, op(L[i])],i=1..nops(L))]:end: #App(L,a): Given a list L of lists, outputs the list where #sticks a to the end of each of them #For example #App([[1,2,3],[1,3,4]],0)= [[1,2,3,0],[1,3,4,0]] App:=proc(L,a) local i: [ seq([op(L[i]),a],i=1..nops(L))]:end: #UC(n): inputs a non-neg. integer n, and outputs the n-dim unit cube [0,1]^n #AS A LIST OF 0-1 vectors in Lex-order (same as Box([1$n]) UC:=proc(n) local B: option remember: if n=0 then RETURN([[]]): fi: B:=UC(n-1): CAT(PreP(0,B),PreP(1,B)): end: #NEX(w): Inputs a 0-1 vector w (of length n=nops(w)) outputs the vector right after it in lex order #or returns FAIL NEX:=proc(w) local n,w1: n:=nops(w): if n=0 then print(`You have reached the top`): RETURN(FAIL): fi: w1:=[op(1..n-1,w)]: if w[n]=0 then RETURN([op(w1),1]): fi: w1:=NEX(w1): if w1=FAIL then RETURN(FAIL): else RETURN([op(w1),0]): fi: end: #NEXr(w): Inputs a 0-1 vector w (of length n=nops(w)) outputs the vector right after it in lex order #or returns FAIL NEXr:=proc(w) local n,w1: option remember: n:=nops(w): if n=0 then print(`You have reached the top`): RETURN(FAIL): fi: w1:=[op(1..n-1,w)]: if w[n]=0 then RETURN([op(w1),1]): fi: w1:=NEXr(w1): if w1=FAIL then RETURN(FAIL): else RETURN([op(w1),0]): fi: end: #REV(L): REV:=proc(L) local i: [seq(L[-i],i=1..nops(L))]:end: #UCG(n): inputs a non-neg. integer n, and outputs the n-dim unit cube [0,1]^n #USING THE GRAY-CODE WHERE ONLY ONE BIT CHANGES AT A TIME #AS A LIST OF 0-1 vectors in Lex-order (same as Box([1$n]) UCG:=proc(n) local B: option remember: if n=0 then RETURN([[]]): fi: B:=UCG(n-1): CAT(PreP(0,B),PreP(1,REV(B))): end: #NEXG(w): inputs a word in {0,1}^n (n=nops(w)) and outputs the NEXT one in the the Gray Code given by UCG(n). #Try: NEXG([1,0,1]); NEXG:=proc(w) local n,w1, w1Next,w1Prev: n:=nops(w): if n=1 then if w[1]=0 then RETURN([1]): else RETURN(FAIL): fi: fi: w1:=[op(2..n,w)]: if w[1]=0 then w1Next:=NEXG(w1): if w1Next=FAIL then RETURN([1,op(w1)]): else RETURN([0,op(w1Next)]): fi: elif w[1]=1 then #Since w[1]=1, we are going BACKWARDS w1Prev:=PREG(w1): if w1Prev=FAIL then #IF THERE IS NO WAY TO GO WE RETURN FAIL RETURN(FAIL): else RETURN([1,op(w1Prev)]): fi: else print(`Something is wrong`): RETURN(FAIL): fi: end: #PREG(w): inputs a word in {0,1}^n (n=nops(w)) and outputs the PREVIOUS one in the the Gray Code given by UCG(n). #Try: NEXG([1,0,1]); PREG:=proc(w) local n,w1, w1Prev,w1Next: n:=nops(w): if n=1 then if w[1]=0 then RETURN(FAIL): else RETURN([0]): fi: fi: w1:=[op(2..n,w)]: if w[1]=0 then w1Prev:=PREG(w1): if w1Prev=FAIL then RETURN(FAIL): else RETURN([0,op(w1Prev)]): fi: elif w[1]=1 then #Since w[1]=1, we are going FORWARD w1Next:=NEXG(w1): if w1Next=FAIL then #WE CHANGE THE first bit RETURN([0,op(w1)]): else RETURN([1,op(w1Next)]): fi: else print(`Something is wrong`): RETURN(FAIL): fi: end: #RBW(n): A random binary word in {0,1} of length n RBW:=proc(n) local ra,i: ra:=rand(0..1): [seq(ra(),i=1..n)]: end: