#C5.txt: Continuing the polynomial anstaz Help:=proc(): print(` PreMag(a,b,n), IsMagic(A,k) , MagRec(a,b,n) `): end: ###stuff from C4.txt Help4:=proc(): print(`GuessPol1(L,n,d) , GuessPol(L,n), Comp(n,k) `): end: #GuessPol1(L,n,d): inputs a list of pairs [[input,output], ...] a variable name n #and a pos. integer d, outputs a polynomial of degree d f such that f(input)=output GuessPol1:=proc(L,n,d) local a,i,f,var,eq: if nops(L)FAIL then f:=GuessPol1(L,n,d): if f<>FAIL then RETURN(f): fi: fi: od: print(`Too bad, it did not work out, generate more date, or bad news, it is not a polynomial`): FAIL: end: #Comp(n,k): inputs non-neg. integers n and k and outputs the SET of # compositions of n into k parts (vectors of non-neg. integers that add-up to n) #of length k Comp:=proc(n,k) local S,a1,S1, s1: option remember: if k<0 or n<0 then RETURN({}): fi: if n=0 then RETURN({[0\$k]}): fi: if k=0 then if n=0 then RETURN({[]}): else RETURN({}): fi: fi: S:={}: for a1 from 0 to n do S1:=Comp(n-a1,k-1): S:=S union {seq([a1,op(s1)], s1 in S1)}: od: S: end: #end of C4.txt #PreMag(a,b,n):inputs non-neg. integers n,a,b, and outputs the set of a by b pre-magic #rectangle with row sums n PreMag:=proc(a,b,n) local S,J,s,j: option remember: if a=0 then RETURN({[]}): fi: J:=Comp(n,b): S:=PreMag(a-1,b,n): {seq(seq([op(s),j], s in S), j in J)}: end: #IsMagic(A,k): inputs a matrix A and outputs true iff #all the columns of A add-up to k IsMagic:=proc(A,k) local i,j: evalb({seq(add(A[i][j],i=1..nops(A)),j=1..nops(A[1]) ) }= {k}): end: #MagRec(a,b,n): the set of a by b matrices with non-neg. entries such that #every row sum is n and every column sum a*n/b MagRec:=proc(a,b,n) local A,G,c,a1: if not type(a*n/b,integer) then RETURN({}): fi: c:=a*n/b: A:=PreMag(a,b,n): G:={}: for a1 in A do if IsMagic(a1,c) then G:=G union {a1}: fi: od: G: end: