

Help18:=proc(): print(` IsCond3G(C,G), Cond3G(n,G) `):end:

with(combinat):

#IsCond3G(C,G): inputs a vote-count profile C outputs true iff 1->2->3->1, according to the generalized Condorcet i beats j iff the difference
#i beats j by a margin of G[i][j], 1<=i<j<=n. Try IsCond3G(C,[[0,1,1],[0,0,1]]);
IsCond3G:=proc(C,G) local i,P,T:


P:=permute(3):

for i from 1 to nops(C) do
 T[P[i]]:=C[i]:
od:
#1->2->3->1
if 
(T[[1,2,3]]+T[[1,3,2]]+T[[3,1,2]])- (T[[2,1,3]]+T[[2,3,1]]+T[[3,2,1]])>=G[1][2]
and
(T[[2,1,3]]+T[[1,2,3]]+T[[1,3,2]])- (T[[2,3,1]]+T[[3,2,1]]+T[[3,3,1]])>=G[1][3]
and
(T[[1,2,3]]+T[[2,1,3]]+T[[2,3,1]])-(T[[1,3,2]]+T[[3,1,2]]+T[[3,2,1]])>=G[2][3] then
 true:
 else
 false:
fi:
end:


#Cond3G(n,G): The set of generalized Condorcet scenarios with handicap G
Cond3G:=proc(n,G) local A,S,a:
A:=Comps(n,6):
S:={}:

for a in A do
 if IsCond3G(a,G) then
  S:=S union {a}:
 fi:
od:
S:
end:




#From C17.txt
Help17:=proc(): print(`Comps(n,k), GP1(L,n,d), GP(L,n) , IsCond3(C) `):end:




#Comps(n,k): The set of all compositions of n into exactly k parts (with non-negative entrties)
Comps:=proc(n,k) local S,a,S1,s:
option remember:
if k=0 then
 if n=0 then
   RETURN({[]}):
 else
  RETURN({}):
 fi:
fi:
S:={}:

for a from 0 to n do
 S1:=Comps(n-a,k-1):
 S:=S union {seq([op(s),a],s in S1)}:
 od:
 S:
end:



#GP1(L,n,d): Inputs a list of numbers L and a variable n and a non-neg. d outputs the polynomial of degree d
#P(n), such that P(i)=L[i], for all i from 1 to nops(L). OR FAIL
#nops(L)>=d+2
GP1:=proc(L,n,d) local a,i,P,eq,var:
if nops(L)<d+2 then
 ERROR(`List too short`):
 fi:
#P=a[0]+a[1]*n+...+a[d]*n^d
P:=add(a[i]*n^i,i=0..d):
var:={seq(a[i],i=0..d)}:
eq:={seq(L[i]=subs(n=i,P),i=1..d+2)}:
var:=solve(eq,var):
 if var=NULL then
   RETURN(FAIL):
 fi:
P:=subs(var,P):
if {seq(L[i]-subs(n=i,P),i=d+3..nops(L))}<>{0} then
  RETURN(FAIL):
 fi:
P:
end:



#GP(L,n): Inputs a list of numbers L and a variable n and outputs the polynomial of degree <=nops(L)-2
#P(n), such that P(i)=L[i], for all i from 1 to nops(L). OR FAIL
#nops(L)>=d+2
GP:=proc(L,n) local d,P:
for d from 0 to nops(L)-2 do
 P:=GP1(L,n,d):
 if P<>FAIL then
    RETURN(P):
 fi:
od:
FAIL:
end:


#IsCond3(C): inputs a vote-count profile outputs true iff 1->2->3->1,
IsCond3:=proc(C) local i,v,P,T:
P:=permute(3):
v:=add(C):
for i from 1 to nops(C) do
 T[P[i]]:=C[i]:
od:

#1->2->3->1
if T[[1,2,3]]+T[[1,3,2]]+T[[3,1,2]]>v/2 and 
T[[1,2,3]]+T[[2,1,3]]+T[[2,3,1]]>v/2 and
T[[2,3,1]]+T[[3,2,1]]+T[[3,1,2]]>v/2 then
 true:
 else
 false:
fi:
end:
