
// Make newform 1008.3.cg.h in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1008_cg();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1008_cg_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_1008_3_cg_h();".
// 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_1008_3_cg_h();".
// 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 := [4, 0, 2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 0, 1, 0], [0, 4, 0, 1], [0, 2, 0, -1]];
    Rf_basisdens := [1, 2, 1, 1];
    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_1008_cg();"
function MakeCharacter_1008_cg()
    N := 1008;
    order := 6;
    char_gens := [127, 757, 785, 577];
    v := [6, 6, 6, 5];
    // 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_1008_cg_Hecke();"
function MakeCharacter_1008_cg_Hecke(Kf)
    N := 1008;
    order := 6;
    char_gens := [127, 757, 785, 577];
    char_values := [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [0, -1, 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 := 3;
    raw_aps := [[0, 0, 0, 0], [0, 0, 0, 0], [-4, -2, 0, 1], [0, -5, 0, -1], [-6, -6, 0, 0], [1, 2, 4, 0], [8, -8, 1, -1], [14, 7, 0, -1], [0, -12, -6, 3], [0, 0, -4, 8], [-17, 17, 3, -3], [0, -11, -4, 2], [-26, -52, -2, 0], [-7, 0, -1, 2], [-44, -22, 0, -1], [-60, -60, -3, -3], [-4, 4, 7, -7], [-24, -12, 0, 4], [-55, -55, 7, 7], [78, 0, -7, 14], [-11, 11, -20, 20], [0, -5, -22, 11], [-10, -20, -19, 0], [24, 12, 0, 0], [12, 24, -2, 0], [-6, 6, -24, 24], [-14, -7, 0, -19], [0, 144, -6, 3], [-145, -145, 4, 4], [-42, 0, 7, -14], [-125, 0, -5, 10], [-36, -18, 0, -6], [84, 84, -2, -2], [67, 134, -5, 0], [0, -132, 12, -6], [-170, -170, 14, 14], [108, -108, 0, 0], [0, -50, -8, 4], [-14, -28, 31, 0], [216, 108, 0, -9], [-42, -42, -15, -15], [59, 118, 22, 0], [0, 126, -14, 7], [-109, -109, 14, 14], [72, 0, -23, 46], [36, -36, -14, 14], [74, 0, 6, -12], [132, 264, 0, 0], [-30, 30, -24, 24], [74, 37, 0, 6], [0, 246, -2, 1], [282, 0, -10, 20], [-96, 96, 52, -52], [-20, -40, 37, 0], [-12, -6, 0, -3], [-96, -96, 22, 22], [130, -130, 32, -32], [120, 60, 0, 4], [205, 205, 8, 8], [96, 0, 27, -54], [-19, 19, -33, 33], [-20, -40, -56, 0], [19, 38, 117, 0], [58, -58, 62, -62], [-310, -155, 0, -28], [0, 60, -8, 4], [0, 185, -18, 9], [289, 0, -18, 36], [144, 144, -21, -21], [36, 72, -58, 0], [50, -50, 109, -109], [0, 156, 32, -16], [211, -211, -11, 11], [0, -325, 84, -42], [209, 0, 49, -98], [-208, -104, 0, -65], [-606, -606, -5, -5], [-218, -109, 0, 54], [0, 420, 34, -17], [-229, 229, -8, 8], [58, 116, 82, 0], [-85, 0, -14, 28], [-54, -54, -53, -53], [229, 458, 90, 0], [-120, -60, 0, 60], [0, -144, -6, 3], [-366, 0, -41, 82], [0, -227, -24, 12], [40, 80, 148, 0], [-233, 0, -75, 150], [-156, -78, 0, 27], [188, -188, -11, 11], [7, 7, 67, 67], [-84, 0, -38, 76], [0, 329, -10, 5], [26, 52, -4, 0], [-116, -58, 0, 185], [328, -328, -40, 40], [470, 235, 0, -47], [0, 119, -24, 12], [-26, 0, 22, -44], [-678, -678, -24, -24], [78, -78, -129, 129], [0, -378, 96, -48], [125, 125, 99, 99], [299, -299, 52, -52], [-252, -504, 0, 0], [-884, -442, 0, 14], [-636, -636, 28, 28], [47, 94, -24, 0], [-110, -55, 0, 13], [562, 562, -18, -18], [-294, 0, 110, -220], [-187, 187, 31, -31], [598, 0, 64, -128], [0, 0, -27, -27], [-451, -902, -15, 0], [630, -630, 18, -18], [0, -558, -168, 84], [-96, 0, 81, -162], [71, -71, 8, -8], [229, 0, -22, 44], [-744, -372, 0, -30], [-444, -444, 91, 91], [38, 19, 0, -213], [348, 0, 41, -82], [0, 730, 112, -56], [-76, -38, 0, 214], [-67, -134, -105, 0], [718, 359, 0, -66], [269, 269, 9, 9], [-114, 0, -94, 188], [0, 485, 22, -11], [526, 0, -80, 160], [208, 104, 0, 11], [-479, -958, -94, 0], [290, -290, 184, -184], [384, -384, -182, 182], [372, 744, -9, 0], [-102, -102, -15, -15], [-288, -576, 210, 0], [0, 210, 22, -11], [-502, -502, -74, -74], [-798, 0, -75, 150], [431, -431, -160, 160], [64, 128, 22, 0], [-265, -530, -62, 0], [728, -728, 70, -70], [144, 72, 0, 154], [0, 690, -80, 40], [0, 326, -76, 38], [-36, -72, -87, 0], [-187, 0, -159, 318], [700, 350, 0, -106], [-113, -113, -41, -41], [-480, 0, 83, -166], [0, 737, -46, 23], [932, 466, 0, 19], [-565, -1130, 152, 0], [-656, 656, 35, -35], [0, 162, -304, 152], [-528, 0, -73, 146], [343, 0, -89, 178], [656, 328, 0, -89], [-570, -570, 120, 120], [716, -716, -83, 83], [-281, -281, -41, -41], [133, -133, 252, -252], [767, 0, -8, 16], [1568, 784, 0, 4], [132, 132, -26, -26], [792, 1584, 96, 0], [0, -168, 92, -46], [-959, -959, 90, 90], [197, -197, 133, -133], [-592, -1184, 8, 0], [619, 0, -3, 6], [690, 690, -73, -73], [708, 1416, 14, 0], [194, 97, 0, -324], [0, 427, 42, -21], [644, 1288, -58, 0], [-1273, 0, 84, -168], [112, 56, 0, 167], [-1326, -1326, 72, 72], [-284, 284, 62, -62], [158, 158, 138, 138], [-499, 499, 11, -11], [0, -1261, 92, -46], [58, -58, 32, -32], [914, 457, 0, -10], [1398, 0, 49, -98], [0, 230, 176, -88], [584, 292, 0, -311], [996, 996, -107, -107], [464, -464, -206, 206], [-145, -145, -64, -64], [0, 710, -112, 56], [-246, -492, 126, 0], [-1840, -920, 0, -137], [-318, -318, -34, -34], [199, 398, 7, 0], [408, 204, 0, 160], [84, -84, -166, 166], [106, 212, -26, 0], [-230, 230, -190, 190], [-264, -132, 0, 2], [0, -78, 316, -158], [624, 0, 104, -208], [36, -36, -232, 232], [0, 407, 460, -230], [-96, -192, 90, 0], [1070, 0, 116, -232], [1592, 796, 0, 235], [8, -8, -5, 5], [146, 73, 0, -98], [190, 190, -4, -4], [540, -540, 36, -36], [0, 1380, -46, 23], [738, 0, 207, -414], [0, -575, -148, 74], [-389, -778, 333, 0], [0, -6, 434, -217], [0, -2014, 128, -64], [-850, -1700, -190, 0], [-1070, 0, 10, -20], [-1792, -896, 0, 196], [-414, -414, -34, -34], [-1114, -557, 0, 41], [0, 510, 166, -83], [71, 71, 32, 32], [-660, 660, 276, -276], [545, 0, -61, 122], [162, 162, 118, 118], [293, 586, -187, 0], [-366, 366, 141, -141], [142, 71, 0, -8], [0, 966, -96, 48], [1104, 0, 110, -220], [-70, -140, 167, 0], [690, 690, -7, -7], [322, 161, 0, -77], [953, -953, 167, -167], [0, -50, 528, -264], [-792, -1584, 30, 0], [-1496, -748, 0, 119], [-319, -638, -211, 0], [-938, 938, 143, -143], [1862, 1862, -44, -44], [-552, 0, -52, 104], [805, 0, 214, -428], [-72, -36, 0, -9], [834, 834, 192, 192], [215, 430, 290, 0], [-1048, 1048, -62, 62], [0, -2448, -52, 26], [-1019, -1019, -236, -236], [55, -55, 297, -297], [-390, -780, -81, 0], [216, 108, 0, -12], [1301, 1301, 167, 167], [180, 0, -154, 308], [-83, 83, -208, 208], [-455, -910, -20, 0], [-246, 246, 42, -42], [-854, -427, 0, 483], [-258, 0, -190, 380], [-654, -1308, -285, 0], [-1034, 0, 172, -344], [318, 318, 18, 18], [1654, 827, 0, -74], [-1994, -1994, 12, 12], [-407, 407, 378, -378], [0, 269, -710, 355], [-60, -120, -102, 0], [2592, 1296, 0, -82], [0, -2910, 98, -49], [803, 803, -110, -110], [0, 901, 280, -140], [-1904, -952, 0, -355], [-356, 356, -400, 400], [365, 365, -305, -305], [-1618, -3236, 14, 0], [1681, 3362, -26, 0], [-744, -372, 0, -298], [0, 2022, -152, 76], [2639, 2639, -16, -16], [990, 0, -121, 242], [1351, -1351, -49, 49], [566, 1132, 119, 0], [696, 696, -40, -40], [-256, 256, -266, 266], [0, -1470, -338, 169], [-32, -64, -698, 0], [2819, 0, -46, 92], [-1186, 1186, -26, 26], [-2842, -1421, 0, -113], [-584, -1168, 283, 0], [1948, 974, 0, 431], [713, 1426, 227, 0], [-744, -372, 0, 362], [0, -1494, -88, 44], [974, 974, -180, -180]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1008_cg_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1008_3_cg_h();".
// 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_1008_3_cg_h(:prec:=4)
    chi := MakeCharacter_1008_cg();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(1999) - 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_1008_3_cg_h();".
// 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_1008_3_cg_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1008_cg();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![144, -144, 36, 12, 1]>,<11,R![36, 6, 1]>,<13,R![145161, 0, 774, 0, 1]>],Snew);
    return Vf;
end function;