
#Yi Jin's project 

print(`Maple package FSF implementing an efficient recursive algorithm`):
print(`that expresses an arbitary monomial symmetric function as`):
print(`a polynomial in elementary symmetric functions e[1],e[2],...`):
print(`Version 1.0, May 7, 2005. Written by Yi Jin `):
print():
print(`To convert a monomial symmetric function corresponding to `):
print(`partition lambda (lambda is a list of positive integers)`):
print(`type: recursive_mtoe(lambda)`):
print(`For example, try recursive_mtoe([4,3,2,1])`):
print():
print(`When lambda = [a,b], for every fixed integer b, there is a`):
print(`recursion for m[a,b] , a>b involving only elementary symmetric`):
print(`functions and previous monomial symm functions with same b but`):
print(`smaller a. The recursion is derived through generating functions.`):
print(`It is implemented in procedure m2toe(a,b).`):
print(`For example, try m2toe(6,4).`):
print():

# The function m1toe(n) expresses m_{n,1}, n>=2 as a polynomial
# in e_{1},e_{2},....e_{n+1}. It demonstrates how m_{n,1} can be
# represented recursively using elementary symmetric functions.
# It is equivalent to m2toe(n,1), and serves as a checker for
# m2toe. It is not used by the master procedure. 
m1toe:=proc(n)
   local r,m,k;
   for k from 2 to n do
      m[k]:=expand(add((-1)^(r+1-k)*m[r]*e[k-r],r=2..(k-1))
                   +(-1)^(-k)*(2*e[2]*e[k-1]-(k-1)*e[1]*e[k]-(k+1)*e[k+1])):
   od:

   return(m[n]):
end:
   
# The function m2toe(n,k) expresses m_{n,k}, n>=k as a polynomial
# in e_{1},e_{2},....e_{n+k}.
m2toe:=proc(n,k)
   option remember:
   local i,j,m;

   if n=k then
      return(expand((qtoe(k)^2-qtoe(2*k))/2));
   fi:

   for j from 1 to n-k do
     m[k+j]:= -(-1)^(k+j)*expand(
      add((-1)^(k+i)*m[k+i]*e[j-i],i=1..(j-1))
      +qtoe(k)*add((-1)^i*qtoe(i)*e[k+j-i],i=1..k)
      -(-1)^k*add((-1)^i*qtoe(i)*e[2*k+j-i],i=1..2*k)
      +(k+j)*e[k+j]*qtoe(k)
      -(-1)^k*(2*k+j)*e[2*k+j]
      ):
   od:

   return(m[n]):
end:

# The function qtoe(n) expresses q_{n} as a polynomial
# in e_{1},e_{2},....e_{n}.
qtoe:=proc(n)
   option remember:
   local i:

   return((-1)^(n-1)*expand(n*e[n]+add((-1)^i*qtoe(i)*e[n-i],i=1..(n-1)))):

end:

# Here comes the master recursive procedure. It inputs the partition
# corresponding to a monomial symmetric function and outputs its
# elementary symmetric function expansion.
recursive_mtoe:=proc(li)
   option remember:
   local a,a1,sa,na,nsa,n,rst,p:

   a:=sort(li):
   n:=nops(a):

   if n=0 then 
      return(0):
   elif n=1 then
      return(qtoe(a[1])):
   else
      a1:=a[1]:
      sa:=[op(2..n,a)]:
      rst:=expand(recursive_mtoe([a1])*recursive_mtoe(sa)):
      p:=n-1:
      while p>0 do
         if (p>1) and (sa[p-1]=sa[p]) then
            p:=p-1:
         else
            na:=sa[p]+a1:     
            nsa:=sa: nsa[p]:=na:
            rst:=expand(rst-nops(select(x->x=na,nsa))*recursive_mtoe(nsa)):
            p:=p-1:
         fi:
      od:
      return(rst/nops(select(x->x=a1,a))):
   fi:

end:
