// Try to compute sfct using the derivative.

pIntegral := function(f)
// formal int. in char p
// NOTE: it disregards terms in x^(k*p-1)
    P:=Parent(f);
    p:=Characteristic(P);

    res:=0;
    for mon in Terms(f) do
        d:=Degree(mon);
        if ((d mod p) ne (p-1)) then
            res+:=(GF(p)!(1/(d+1)))*P.1*mon;
        end if;
    end for;

    return res;

end function;

f:=&+[ Random(GF(7))*x^i : i in [0..Random([5..30])] ]; 

    
nAAuxFct := function(f,i)
// return the terms in x^(np-i) of f
// f has only one variable!!!
    P:=Parent(f);
    p:=Characteristic(P);
    d:=Degree(f);

    if ((i mod p) eq 0) then
        min:=i div p;
    else
        min:=(i div p) + 1;
    end if;

    max:=(d+i) div p;

    // print [min,max];
    
    res:=0;
    for k in [min..max] do
        res+:=Coefficient(f,k*p-i)*(P.1)^(k*p-i);
    end for;

    return res;

end function;
       


nAuxFct := function(f)
// finds only the p-powers of the SpcFct
// f has only one variable!!!
    P:=Parent(f);
    p:=Characteristic(P);

    term:=Terms(f)[1];
    g:=f-term;

    if g eq P!0 then
        return 0;
    end if;

    d:=Degree(term);
    tmp:=&+[(-Binomial(p,i) div p)*term^i*nAAuxFct(g^(p-i),d*i) : i in [1..(p-1)] ];

    return tmp + $$(g);

end function;


nSpcFct := function(f)
    // for now assumes f is a polynomial in one variable
    P:=Parent(f);
    p:=Characteristic(P);
    tmp:=pIntegral(-Derivative(f)*f^(p-1));

    return tmp + nAuxFct(f);

end function;








/*

load "sec_coord.m";

p:=11;P<x>:=PolynomialRing(GF(p));
f:=&+[ Random(GF(p))*x^i : i in [0..Random([5..30])] ];f;
time a:=SpcFct(f);
time b:=nSpcFct(f);
a eq b;

*/
