
// Make newform 768.6.d.c in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_768_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_768_d_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_768_6_d_c();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_768_6_d_c();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function ConvertToHeckeField(input: pass_field := false, Kf := [])
    if not pass_field then
        poly := [1, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_768_d();"
function MakeCharacter_768_d()
    N := 768;
    order := 2;
    char_gens := [511, 517, 257];
    v := [2, 1, 2];
    // chi(gens[i]) = zeta^v[i]
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    F := CyclotomicField(order);
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),[F|F.1^e:e in v]);
    return MinimalBaseRingCharacter(chi);
end function;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_768_d_Hecke();"
function MakeCharacter_768_d_Hecke(Kf)
    N := 768;
    order := 2;
    char_gens := [511, 517, 257];
    char_values := [[1, 0], [-1, 0], [1, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
end function;


function ExtendMultiplicatively(weight, aps, character)
    prec := NextPrime(NthPrime(#aps)) - 1; // we will able to figure out a_0 ... a_prec
    primes := PrimesUpTo(prec);
    prime_powers := primes;
    assert #primes eq #aps;
    log_prec := Floor(Log(prec)/Log(2)); // prec < 2^(log_prec+1)
    F := Universe(aps);
    FXY<X, Y> := PolynomialRing(F, 2);
    // 1/(1 - a_p T + p^(weight - 1) * char(p) T^2) = 1 + a_p T + a_{p^2} T^2 + ...
    R<T> := PowerSeriesRing(FXY : Precision := log_prec + 1);
    recursion := Coefficients(1/(1 - X*T + Y*T^2));
    coeffs := [F!0: i in [1..(prec+1)]];
    coeffs[1] := 1; //a_1
    for i := 1 to #primes do
        p := primes[i];
        coeffs[p] := aps[i];
        b := p^(weight - 1) * F!character(p);
        r := 2;
        p_power := p * p;
        //deals with powers of p
        while p_power le prec do
            Append(~prime_powers, p_power);
            coeffs[p_power] := Evaluate(recursion[r + 1], [aps[i], b]);
            p_power *:= p;
            r +:= 1;
        end while;    
    end for;
    Sort(~prime_powers);
    for pp in prime_powers do
        for k := 1 to Floor(prec/pp) do
            if GCD(k, pp) eq 1 then
                coeffs[pp*k] := coeffs[pp]*coeffs[k];
            end if;
        end for;
    end for;
    return coeffs;
end function;


function qexpCoeffs()
    // To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
    weight := 6;
    raw_aps := [[0, 0], [0, -9], [0, 66], [-176, 0], [0, 60], [0, -658], [-414, 0], [0, 956], [-600, 0], [0, 5574], [-3592, 0], [0, 8458], [-19194, 0], [0, -13316], [-19680, 0], [0, 31266], [0, -26340], [0, -31090], [0, -16804], [-6120, 0], [25558, 0], [74408, 0], [0, -6468], [32742, 0], [166082, 0], [0, 22002], [79264, 0], [0, -227988], [0, -8530], [-195438, 0], [173000, 0], [0, 151260], [128454, 0], [0, -154196], [0, -29454], [203872, 0], [0, 136142], [0, -171124], [676200, 0], [0, 133158], [0, -693396], [0, -377174], [-265344, 0], [295298, 0], [0, -201294], [-652448, 0], [0, -1147060], [701960, 0], [0, 1236108], [0, -105830], [438678, 0], [28464, 0], [892562, 0], [0, 110124], [140802, 0], [938760, 0], [0, -1114506], [567704, 0], [0, 1213258], [-687738, 0], [0, 830908], [0, 1312626], [0, 1690220], [1502040, 0], [-810842, 0], [0, 903558], [0, -1121972], [-2752174, 0], [0, -1917492], [0, 1836590], [-622014, 0], [-3740616, 0], [16232, 0], [0, -293606], [0, -3180116], [-2979840, 0], [0, -3459774], [0, -3904162], [5441154, 0], [-1969946, 0], [0, 139020], [0, -4327430], [-2799360, 0], [-5902414, 0], [446512, 0], [0, -3495252], [-1206126, 0], [-233546, 0], [0, -1744890], [-2917864, 0], [0, -5310756], [2344656, 0], [-9815312, 0], [0, 5945196], [0, 6478316], [-4717944, 0], [0, -1907706], [-8019738, 0], [0, -1911620], [0, -11990050], [0, 4458092], [0, 9026118], [0, 6848988], [5463222, 0], [0, 10232380], [15943682, 0], [0, 9477132], [2453490, 0], [9299784, 0], [11461702, 0], [11278376, 0], [0, -93782], [14964150, 0], [0, 5068876], [-15591872, 0], [10970130, 0], [0, -2837044], [6056856, 0], [0, -1088922], [0, 7418028], [0, -767654], [1422626, 0], [0, 6162306], [0, -15062148], [0, -5876356], [0, 3600774], [0, -9225158], [-26392320, 0], [9794848, 0], [0, 4075838], [0, -16570852], [-14414136, 0], [16794440, 0], [0, -13294262], [2147862, 0], [-13105918, 0], [0, 23715378], [0, -8400484], [0, 5410230], [26077734, 0], [0, -19002116], [0, 31017282], [15628960, 0], [0, -15842052], [0, 2061758], [-30390024, 0], [0, 29773786], [-8641002, 0], [0, 33566284], [39019104, 0], [0, -18138178], [30531186, 0], [0, -43553284], [13415160, 0], [0, -3108164], [1190352, 0], [47199616, 0], [1335954, 0], [-14723786, 0], [0, -26919642], [0, -3731604], [-21873546, 0], [-17602496, 0], [0, -16731684], [55538178, 0], [38678424, 0], [9584984, 0], [0, 10365034]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_768_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_768_6_d_c();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
function MakeNewformModFrm_768_6_d_c(:prec:=2)
    chi := MakeCharacter_768_d();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 1;
    while true do
        trunc_vec := Vector(Kf, [0] cat [f_vec[i]: i in [1..prec]]);
        B := Basis(S, prec + 1);
        S_basismat := Matrix([AbsEltseq(g): g in B]);
        if Rank(S_basismat) eq Min(NumberOfRows(S_basismat), NumberOfColumns(S_basismat)) then
            S_basismat := ChangeRing(S_basismat,Kf);
            f_lincom := Solution(S_basismat,trunc_vec);
            f := &+[f_lincom[i]*Basis(S)[i] : i in [1..#Basis(S)]];
            return f;
        end if;
        error if prec eq maxprec, "Unable to distinguish newform within newspace";
        prec := Min(Ceiling(1.25 * prec), maxprec);
    end while;
end function;


// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_768_6_d_c();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function MakeNewformModSym_768_6_d_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_768_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<5,R![4356, 0, 1]>,<7,R![176, 1]>],Snew);
    return Vf;
end function;