load 'sec_coord.m';

// old way
// This function does not quite work, as it does
// not compute the Teich. lift of the coef.
// But it's OK to get times.
SpcFct2 := function(f)
    P:=Parent(f);
    p:=Characteristic(P);
    r:=Rank(P);
    PP:=PolynomialRing(Integers(),r);
    PPP:=PolynomialRing(ResidueClassRing(p^2),r);
    f1:=(PPP!PP!f)^p;
    f2:=PPP!PP!(f^p);
    res:=(PP!(f2-f1)) div p;
    return P!res;
end function;



Coef := function(p,i)
    return Binomial(p,i) div p;
end function;


// new fct, which computes sfct by breaking the pol.
// in two pieces of half number of terms
SpcFct3 := function(f)

    v:=Terms(f);
    n:=#v;
    
    if n le 1 then
        return 0;
    end if;

    P:=Parent(f);
    p:=Characteristic(P);
    term:=Terms(f)[1];

    g1:=&+[ v[i] : i in [1..(n div 2)] ];
    g2:=&+[ v[i] : i in [((n div 2)+1)..n] ];

    delete v;
    
    tmp:= &+[ -Coef(p,i)*g1^i*g2^(p-i) : i in [1..(p-1)] ];

        
    return $$(g1) + $$(g2) + tmp;
    
end function;



/*

p:=19;
P<x,y>:=PolynomialRing(GF(p),2);

f := &+[ Random([1..(p-1)])*x^(Random([0..7]))*y^(Random([0..7]))
 : i in [1..12] ]; f;

time a:=SpcFct(f)

time b:=SpcFct2(f);

time c:=SpcFct3(f);

*/
