/**************************************************************************
   X.m -- compute the group that Cornut called "X" on August 9, 2001. 
          This is the intersection of the ell-old and ell-new subvarieties
          of J_0(ell*N).
 **************************************************************************/

function OddPart(n)
// Returns the odd part of n.
   return n eq 0 select 0 else Parent(n)!(n / 2^Valuation(n,2));
end function;

intrinsic XOldNew(ell::RngIntElt, 
                    N::RngIntElt, 
                 sign::RngIntElt) -> GrpAb
{}
   require ell ge 2 and IsPrime(ell) : "Argument 1 must be prime.";
   requirege N, 1;
   require sign in {-1, 0, 1 } : "Argument 3 must be in {-1,0,1}.";

   return XOldNew(ell,N,sign,2);
end intrinsic;

intrinsic XOldNew(ell::RngIntElt, 
                    N::RngIntElt, 
                 sign::RngIntElt,
                    k::RngIntElt) -> GrpAb
{}
   require ell ge 2 and IsPrime(ell) : "Argument 1 must be prime.";
   requirege N, 1;
   require sign in {-1, 0, 1 } : "Argument 3 must be in {-1,0,1}.";
   requirege k, 2;

   SN := CuspidalSubspace(ModularSymbols(N, k, sign));
   if Dimension(SN) eq 0 then
      return AbelianGroup([]);
   end if;
   SNell := CuspidalSubspace(ModularSymbols(N*ell,k,sign));
   A := SNell!!SN;
   B := CuspidalSubspace(Complement(A));
   if Dimension(B) eq 0 then
      return AbelianGroup([]);
   end if;
   return IntersectionGroup(A,B);
end intrinsic;

intrinsic XExperiment(N::RngIntElt, 
               ellstart::RngIntElt, 
                 ellend::RngIntElt) 
{}
   fname := Sprintf("XExperiment_N%o_ell%o-%o.out",N,ellstart,ellend);
   file := Open(fname,"w");
   fprintf file, "// XExperiment\n";
   fprintf file, "N := %o; ellstart := %o; ellend := %o;\n\n", 
                 N, ellstart, ellend;
   Flush(file);
   for ell in [l : l in [ellstart..ellend] |IsPrime(l)] do
      t := Cputime();
      X := XOldNew(ell,N,+1,2);
      invs := [OddPart(a) : a in Invariants(X)];
      fprintf file, "X%o[%o] := %o;       // (time = %os)\n", 
                                  N, ell, invs, Cputime(t);
      Flush(file);
   end for;
end intrinsic;
