###################################################################### ## Cramer.txt Save this file as Cramer.txt to use it, # # stay in the # ## same directory, get into Maple (by typing: maple ) # ## and then type: read `Cramer.txt` # ## Then follow the instructions given there # ## # ## Written by Doron Zeilberger, Rutgers University , # ## DoronZeil at gmail dot com # ###################################################################### print(`Written: Aug. 18, 2024: tested for Maple 2020 `): with(combinat): with(combinat): print(): print(`This is Cramer.txt, A Maple package`): print(`implementing Doron Zeilberger's combinatorial proof of Cramer's Rule as described in his article`): print(`A combinatorial proof of Cramer's Rule`): print(): print(`The most current version is available from:`): print(` http://sites.math.rutgers.edu/~zeilberg/tokhniot/Cramer.txt .`): print(`Please report all bugs to: DoronZeil at gmail dot com .`): print(): print(`-----------------------------`): print(`For general help, and a list of the MAIN functions,`): print(` type "ezra();". For specific help type "ezra(procedure_name);" `): print(): print(`-----------------------------`): print(`For a list of the supporting procedures type ezra1()`): print(`For specific help type "ezra(procedure_name);" `): print(`For a list of the supporting functions type: ezra1();`): print(): print(`-----------------------------`): print(`-----------------------------`): ezra1:=proc() if args=NULL then print(`The SUPPORTING procedures are: FindMate, Fn, inv, w, W, WtFn `): print(` `): else ezra(args): fi: end: ezra:=proc() if args=NULL then print(`The main procedures are:`): print(`CR1, CR2, GBguys, iMate`): elif nargs=1 and args[1]=CR1 then print(`CR1(a,b,n): The solution to Ax=b, where A is a generic n by n matrix a[i,j] and b is a column vector [b[1],..., b[n]]^T spelled out. Done via Maple's solve. Try:`): print(`CR1(a,b,3);`): elif nargs=1 and args[1]=CR2 then print(`CR2(a,b,n): Like CR1(a,b,n) but via Cramer's Rule. Try:`): print(`CR2(a,b,3);`): elif nargs=1 and args[1]=FindMate then print(`FindMate(i,p,a,b): emprically looks for the i-Mate for an i-bad guy. Try`): print(`FindMate(2,[1,[1,3,2]],a,b);`): elif nargs=1 and args[1]=Fn then print(`Fn(n): The set of pairs [j,pi] where 1<=j<=n and pi is in S_n. Try:`): print(`Fn(3);`): elif nargs=1 and args[1]=GBguys then print(`GBguys(i,n): For the weight W_i on Fn(n) returns the good guys followed by the bad guys in pairs. Try:`): print(`GBguys(1,3):`): elif nargs=1 and args[1]=hofkhi then print(`hofkhi(pi):the inverse of the permutation pi. Try`): print(`hofkhi([1,3,2,4]);`): elif nargs=1 and args[1]=iMate then print(`The mapping T_i([j,pi]) at the bottom of p.2 of the paper`): print(`iMate(i,p): The i-Mate of the bad guy p=[j,pi]. Try`): print(`iMate(1,[1,[2,1]]);`): elif nargs=1 and args[1]=inv then print(`inv(pi): the number of inversion of the permutation pi. Try:`): print(`inv([4,1,2,3]);`): elif nargs=1 and args[1]=w then print(`w(j,pi,a,b): w_j(pi) in the paper. Try`): print(`w(1,[3,1,2],a,b);`): elif nargs=1 and args[1]=W then print(`W(i,p,a,b): If p=[j,pi] is a member of Fn(n), the weight W_i(p) defined by a[i,j]*w(j,pi,a,b). Try:`): print(`W(2,[1,[3,1,2]],a,b);`): elif nargs=1 and args[1]=WtFn then print(`WtFn(n,i,a,b): The weight-enumerator of Fn according to the Weight W_i. Try:`): print(`WtFn(2,1,a,b);`): else print(`There is no such thing as`, args): fi: end: with(combinat): inv:=proc(pi) local n,i,j,co: n:=nops(pi): co:=0: for i from 1 to n do for j from i+1 to n do if pi[i]>pi[j] then co:=co+1: fi: od: od: co: end: #CR1(a,b,n): The solution to Ax=b, where A is a generic n by n matrix a[i,j] and b is a column vector [b[1],..., b[n]]^T spelled out CR1:=proc(a,b,n) local x,i,j,var,eq: var:={seq(x[i],i=1..n)}: eq:={seq(add(a[i,j]*x[j],j=1..n)-b[i],i=1..n)}: normal(subs(solve(eq,var),[seq(x[i],i=1..n)])): end: #w(j,pi,a,b): w_j(pi) in the paper w:=proc(j,pi,a,b) local n,gu,k,i: n:=nops(pi): if not (j>=0 and j<=n) then RETURN(FAIL): fi: gu:=(-1)^inv(pi)*mul(a[pi[k],k],k=1..n): if j=0 then RETURN(gu): else gu:=subs({seq(a[i,j]=b[i],i=1..n)},gu): RETURN(gu): fi: end: #CR2(a,b,n): Same as CR1(a,b,n) but via weight-enumerators CR2:=proc(a,b,n) local gu,X0,pi,k: gu:=permute(n): X0:=add(w(0,pi,a,b),pi in gu): normal([seq(add(w(k,pi,a,b),pi in gu)/X0,k=1..n)]): end: #Fn(n): The set of pairs [j,pi] where 1<=j<=n and pi is in S_n. Try: #Fn(4); Fn:=proc(n) local j,gu,pi: gu:=permute(n): {seq(seq([j,pi],pi in gu),j=1..n)}: end: #W(i,p,a,b): If p=[j,pi] is a member of Fn(n), the weight W_i(p) defined by a[i,j]*w(j,pi,a,b). Try: #W(2,[1,[3,1,2]],a,b) W:=proc(i,p,a,b) local pi,j: j:=p[1]: pi:=p[2]: a[i,j]*w(j,pi,a,b): end: #WtFn(n,i,a,b): The weight-enumerator of Fn according to the Weight W_i WtFn:=proc(n,i,a,b) local gu,gu1: gu:=Fn(n): factor(add(W(i,gu1,a,b),gu1 in gu)): end: #FindMate(i,p,a,b): inputs a pair p=[j,pi] where pi is in S_n and i from 1 to n, finds a pair with the opposite weight #or returns FAIL. Try: #FindMate(2,[1,[1,3,2]],a,b); FindMate:=proc(i,p,a,b) local n,gu,gu1,w1: n:=nops(p[2]): gu:=Fn(n): w1:=W(i,p,a,b): for gu1 in gu do if expand(w1+W(i,gu1,a,b))=0 then RETURN(gu1): fi: od: FAIL: end: #GBguys(i,n): For the weight W_i on Fn(n) returns the good guys followed by the bad guys in pairs. Try: #GBguys(1,3): GBguys:=proc(i,n) local gu,gu1,gu2,G,B,a,b: gu:=Fn(n): G:={}: B:={}: while gu<>{} do gu1:=gu[1]: gu2:=FindMate(i,gu1,a,b): if gu2=FAIL then G:=G union {gu1}: gu:=gu minus {gu1}: else B:=B union {{gu1,gu2}}: gu:=gu minus {gu1,gu2}: fi: od: [G,B]: end: #hofkhi(pi) hofkhi:=proc(pi) local n,T,i: n:=nops(pi): for i from 1 to n do T[pi[i]]:=i: od: [seq(T[i],i=1..n)]: end: #iMate(i,p): The i-Mate of the bad guy p=[j,pi]. Try #iMate(1,[1,[2,1]]); iMate:=proc(i,p) local j,pi,a,sig,j1,n,i1: j:=p[1]: pi:=p[2]: n:=nops(pi): a:=pi[j]: if a=i then print(p, `is an `, i, `- good guy!`): RETURN(FAIL): fi: j1:=hofkhi(pi)[i]: for i1 from 1 to n do sig[i1]:=pi[i1]: od: sig[j]:=i: sig[j1]:=a: sig:=[seq(sig[i1],i1=1..n)]: [j1,sig]: end: