
// Make newform 784.4.f.g in Magma, downloaded from the LMFDB on 14 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_784_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_784_f_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_784_4_f_g();".
// 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_784_4_f_g();".
// 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 := [576, -672, 808, -20, 29, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0], [-285776, 11424, -13736, -8463, -493, 17], [428592, -19488, 23432, -1629, 841, -29], [-52824, 117976, -5834, -667, 23, -197], [-64104, 159176, 986, 5713, -197, 203], [-404688, 982432, -17788, 34331, -399, 1191]];
    Rf_basisdens := [1, 45520, 45520, 68280, 68280, 45520];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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_784_f();"
function MakeCharacter_784_f()
    N := 784;
    order := 2;
    char_gens := [687, 197, 689];
    v := [1, 2, 1];
    // 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_784_f_Hecke();"
function MakeCharacter_784_f_Hecke(Kf)
    N := 784;
    order := 2;
    char_gens := [687, 197, 689];
    char_values := [[-1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0], [-1, 0, 0, 0, 0, 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 := 4;
    raw_aps := [[0, 0, 0, 0, 0, 0], [-2, 0, 1, 0, 0, 0], [0, 0, 0, -1, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, -1, -11, 1], [0, 0, 0, 1, 23, 0], [0, 0, 0, 5, 0, -8], [48, 1, 2, 0, 0, 0], [0, 0, 0, 6, 0, -1], [-50, -1, 23, 0, 0, 0], [76, 1, 24, 0, 0, 0], [75, -2, -26, 0, 0, 0], [0, 0, 0, 9, -141, -8], [0, 0, 0, -16, 20, 22], [32, 1, 22, 0, 0, 0], [231, 6, 54, 0, 0, 0], [106, 2, -1, 0, 0, 0], [0, 0, 0, 6, 189, -8], [0, 0, 0, 42, -44, -25], [0, 0, 0, -14, -322, -26], [0, 0, 0, 42, 95, 8], [0, 0, 0, -14, -372, 47], [-20, -22, -70, 0, 0, 0], [0, 0, 0, -28, 291, 32], [0, 0, 0, 1, -45, 48], [0, 0, 0, -35, -216, 80], [722, 48, 119, 0, 0, 0], [0, 0, 0, -13, -503, -51], [-113, 19, -149, 0, 0, 0], [-374, -25, -217, 0, 0, 0], [0, 0, 0, 32, 420, -118], [602, 4, -233, 0, 0, 0], [1607, -11, -83, 0, 0, 0], [-768, 80, -20, 0, 0, 0], [-301, 73, -71, 0, 0, 0], [0, 0, 0, 81, 15, 77], [0, 0, 0, -2, 219, 56], [0, 0, 0, 172, -34, -217], [1626, -45, 93, 0, 0, 0], [0, 0, 0, -12, 1815, 16], [0, 0, 0, -107, 371, 45], [0, 0, 0, -52, 544, 56], [0, 0, 0, -167, -697, 29], [-1465, -79, 257, 0, 0, 0], [518, -23, 49, 0, 0, 0], [-2040, 31, 218, 0, 0, 0], [0, 0, 0, -128, -628, -50], [-1244, 42, -266, 0, 0, 0], [2924, -35, 124, 0, 0, 0], [0, 0, 0, 43, -668, -40], [1855, 101, -235, 0, 0, 0], [0, 0, 0, -109, 2011, -96], [0, 0, 0, 82, -297, -272], [-1082, -31, -13, 0, 0, 0], [0, 0, 0, -3, -2682, 104], [0, 0, 0, -130, 544, 119], [0, 0, 0, 120, 249, -8], [-1770, 134, 265, 0, 0, 0], [1879, -120, -72, 0, 0, 0], [-3166, -23, -455, 0, 0, 0], [-4192, 23, 114, 0, 0, 0], [0, 0, 0, 132, 2856, 144], [6192, 52, -352, 0, 0, 0], [-210, 204, -171, 0, 0, 0], [0, 0, 0, -200, -433, 136], [-877, -134, -614, 0, 0, 0], [0, 0, 0, 310, 88, 435], [-3814, -153, -801, 0, 0, 0], [0, 0, 0, 310, -3688, -289], [0, 0, 0, -1, -495, 440], [0, 0, 0, 133, -1248, 528], [0, 0, 0, 181, 1115, -147], [1090, -114, -845, 0, 0, 0], [763, 286, 142, 0, 0, 0], [0, 0, 0, -173, 4195, 76], [-2528, 101, -1054, 0, 0, 0], [431, 313, -695, 0, 0, 0], [0, 0, 0, 29, 226, 616], [439, -259, 629, 0, 0, 0], [0, 0, 0, 217, -548, 280], [472, 248, -868, 0, 0, 0], [1078, 351, -945, 0, 0, 0], [0, 0, 0, 269, -4253, -43], [0, 0, 0, 415, -1475, 272], [992, 77, -1230, 0, 0, 0], [0, 0, 0, -695, 1871, 841], [1834, 227, 251, 0, 0, 0], [2807, 336, -936, 0, 0, 0], [0, 0, 0, -25, 4905, 344], [0, 0, 0, 206, -1630, -570], [-2278, 160, 667, 0, 0, 0], [1586, 34, -497, 0, 0, 0], [0, 0, 0, -14, 3076, -465], [0, 0, 0, 207, -1185, 308], [0, 0, 0, -264, -3542, -641], [9428, -86, 718, 0, 0, 0], [0, 0, 0, 1019, 780, -464], [0, 0, 0, 484, 5133, -464], [2028, -517, 592, 0, 0, 0], [-7125, 367, 703, 0, 0, 0], [0, 0, 0, 939, -3093, -316], [3039, 297, 369, 0, 0, 0], [13226, 118, 1243, 0, 0, 0], [-3401, 50, -1222, 0, 0, 0], [0, 0, 0, -1003, 4323, 1149], [0, 0, 0, 18, 6935, -128], [-4224, 36, 1608, 0, 0, 0], [0, 0, 0, 151, -2028, 352], [0, 0, 0, 1058, -3668, -1249], [0, 0, 0, 997, -5161, -880], [9284, -13, -924, 0, 0, 0], [5087, 75, 291, 0, 0, 0], [-8942, 29, 725, 0, 0, 0], [3702, 584, 841, 0, 0, 0], [0, 0, 0, 680, 9532, -62], [4543, -574, 722, 0, 0, 0], [-11724, -254, -2158, 0, 0, 0], [17142, 96, -591, 0, 0, 0], [16067, -284, 292, 0, 0, 0], [0, 0, 0, 552, -1164, -770], [0, 0, 0, -234, 3107, -472], [-4806, 259, -1109, 0, 0, 0], [0, 0, 0, -101, -2844, 1912], [0, 0, 0, -669, -7167, -455], [6102, -322, -179, 0, 0, 0], [22, 188, -2020, 0, 0, 0], [10203, 560, 872, 0, 0, 0], [-1746, -606, -915, 0, 0, 0], [-16496, -524, -12, 0, 0, 0], [0, 0, 0, 148, 743, 1232], [0, 0, 0, 629, -2421, -883], [0, 0, 0, -280, -11252, 514], [0, 0, 0, -995, -6261, 1557], [12550, -23, 625, 0, 0, 0], [0, 0, 0, 625, 9066, -2104], [0, 0, 0, -611, -3641, 216], [0, 0, 0, -1173, 10818, 1424], [-20222, 118, -1605, 0, 0, 0], [0, 0, 0, 885, -6573, -1384], [19663, -118, -1870, 0, 0, 0], [-3380, 238, 3582, 0, 0, 0], [-541, 235, 3379, 0, 0, 0], [0, 0, 0, 910, -1384, -1289], [0, 0, 0, -950, -922, 1994], [0, 0, 0, 529, 2980, -648], [17532, -702, 1398, 0, 0, 0], [0, 0, 0, 165, 3755, 176], [0, 0, 0, -1548, -1059, -488], [-3544, 305, -342, 0, 0, 0], [0, 0, 0, 1836, -4770, -93], [-15141, 713, -1399, 0, 0, 0], [0, 0, 0, 724, 10572, -2952], [0, 0, 0, -626, 6466, 2106], [-24476, 215, -856, 0, 0, 0], [0, 0, 0, 44, 13062, -965], [0, 0, 0, 648, 4596, -1830], [0, 0, 0, 537, 2207, 2693], [0, 0, 0, -830, -9963, 2360], [0, 0, 0, -316, -7372, 1712], [0, 0, 0, 1822, -3381, -944], [0, 0, 0, 8, 4738, -2641], [4450, 479, 3455, 0, 0, 0], [0, 0, 0, 1326, 2842, 1454], [-37622, -508, 335, 0, 0, 0], [22983, 225, 177, 0, 0, 0], [14224, -409, -1390, 0, 0, 0], [0, 0, 0, 258, 4300, 2207], [0, 0, 0, 2700, -8027, -3000]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_784_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_784_4_f_g();".
// 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_784_4_f_g(:prec:=6)
    chi := MakeCharacter_784_f();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_784_4_f_g();".
// 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_784_4_f_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_784_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![-313, -55, 7, 1]>],Snew);
    return Vf;
end function;