
// Make newform 342.5.d.a in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_342_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_342_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_342_5_d_a();".
// 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_342_5_d_a();".
// 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 := [77475204, 0, 4001228, 0, 68229, 0, 450, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [0, -770894, 0, -59427, 0, -450, 0, -1], [0, 2020778, 0, 59427, 0, 450, 0, 1], [0, 84215350, 0, 1017351, 0, 2484, 0, -1], [8802, 0, 225, 0, 1, 0, 0, 0], [-733602, 0, -33685, 0, -352, 0, -1, 0], [1115298, 0, 37093, 0, 352, 0, 1, 0], [0, 176424854, 0, 5973951, 0, 55236, 0, 151]];
    Rf_basisdens := [1, 1249884, 624942, 2499768, 71, 3408, 3408, 833256];
    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_342_d();"
function MakeCharacter_342_d()
    N := 342;
    order := 2;
    char_gens := [191, 325];
    v := [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_342_d_Hecke();"
function MakeCharacter_342_d_Hecke(Kf)
    N := 342;
    order := 2;
    char_gens := [191, 325];
    char_values := [[1, 0, 0, 0, 0, 0, 0, 0], [-1, 0, 0, 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 := 5;
    raw_aps := [[0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [-2, 0, 0, 0, 0, 0, 1, 0], [-20, 0, 0, 0, 0, 1, 0, 0], [2, 0, 0, 0, 2, 2, 3, 0], [0, -7, -6, -1, 0, 0, 0, -1], [-64, 0, 0, 0, -7, -1, 0, 0], [-1, -14, -57, -2, -3, -1, 3, 1], [48, 0, 0, 0, -14, 1, -7, 0], [0, 15, 24, -9, 0, 0, 0, 3], [0, 30, -222, 0, 0, 0, 0, 0], [0, 76, 246, -8, 0, 0, 0, -2], [0, 42, -234, -12, 0, 0, 0, 0], [-1078, 0, 0, 0, 48, 0, 15, 0], [-398, 0, 0, 0, 24, -18, 31, 0], [0, -183, -114, 3, 0, 0, 0, 15], [0, 159, -459, -6, 0, 0, 0, 6], [178, 0, 0, 0, 150, -20, 75, 0], [0, 275, 1017, -22, 0, 0, 0, 26], [0, 66, -912, 66, 0, 0, 0, 18], [2972, 0, 0, 0, -69, 39, 150, 0], [0, -218, -852, -50, 0, 0, 0, -2], [1362, 0, 0, 0, 64, 70, 158, 0], [0, 480, 2346, -6, 0, 0, 0, 18], [0, -194, 2946, 64, 0, 0, 0, 4], [-5754, 0, 0, 0, -20, -140, 20, 0], [0, 308, 486, 2, 0, 0, 0, -58], [0, -513, 2601, 30, 0, 0, 0, -6], [0, -435, 1104, 45, 0, 0, 0, 57], [0, -630, 1554, -36, 0, 0, 0, 36], [0, -456, -4632, 0, 0, 0, 0, -12], [-7490, 0, 0, 0, 216, 84, -251, 0], [1948, 0, 0, 0, 585, 93, 574, 0], [-7802, 0, 0, 0, -546, -110, 321, 0], [16106, 0, 0, 0, -502, -142, -93, 0], [0, 682, -1782, -200, 0, 0, 0, -32], [9658, 0, 0, 0, -582, 160, -480, 0], [4498, 0, 0, 0, 138, 376, -192, 0], [0, 690, 4896, -174, 0, 0, 0, 126], [0, 1464, -6114, 276, 0, 0, 0, 54], [0, -2256, -8706, 180, 0, 0, 0, -102], [0, -1244, -234, 88, 0, 0, 0, 250], [-848, 0, 0, 0, -480, 237, -482, 0], [0, -2126, 2400, -26, 0, 0, 0, 70], [-24714, 0, 0, 0, 154, -128, -448, 0], [-37420, 0, 0, 0, -78, 417, 240, 0], [0, -1445, 7923, 280, 0, 0, 0, -116], [0, -804, 2706, 90, 0, 0, 0, 270], [0, 2421, 1191, -546, 0, 0, 0, 138], [-4718, 0, 0, 0, 810, 772, -333, 0], [-58774, 0, 0, 0, -868, -346, 327, 0], [18160, 0, 0, 0, 1998, -255, -62, 0], [0, 1118, 9222, 68, 0, 0, 0, -220], [13994, 0, 0, 0, -1672, -310, -1413, 0], [0, 900, -11658, -18, 0, 0, 0, 198], [35498, 0, 0, 0, 920, -22, 795, 0], [0, 3636, 11898, 396, 0, 0, 0, 306], [59300, 0, 0, 0, 1098, -879, 729, 0], [38110, 0, 0, 0, -1548, 742, 837, 0], [0, 4452, -2352, -840, 0, 0, 0, -252], [16658, 0, 0, 0, 1428, -132, 1227, 0], [0, 4461, 11052, 705, 0, 0, 0, -39], [0, 5832, 4278, 240, 0, 0, 0, 282], [-60980, 0, 0, 0, -1482, -1185, 256, 0], [-8000, 0, 0, 0, 2127, 1657, 2841, 0], [0, 4119, 1032, -177, 0, 0, 0, -633], [0, -1087, -26493, -586, 0, 0, 0, -34], [0, 5960, -4962, 1130, 0, 0, 0, -214], [49318, 0, 0, 0, 2502, -756, 109, 0], [16478, 0, 0, 0, 3270, -510, 753, 0], [42852, 0, 0, 0, 2557, -479, -895, 0], [-94340, 0, 0, 0, 552, 3, -1676, 0], [54410, 0, 0, 0, -1620, 480, -2088, 0], [0, 1807, 20100, 859, 0, 0, 0, 283], [0, -11195, -2919, -356, 0, 0, 0, 520], [0, -2244, -4158, -294, 0, 0, 0, 78], [-95398, 0, 0, 0, -304, 1166, 2127, 0], [86558, 0, 0, 0, 756, -1290, -1611, 0], [0, 11988, 57048, 108, 0, 0, 0, -72], [0, 9154, -27420, -1082, 0, 0, 0, -1262], [190578, 0, 0, 0, -1868, -98, -1834, 0], [0, -9037, 53730, -1207, 0, 0, 0, -139], [0, -9840, -32676, 204, 0, 0, 0, 900], [0, 19680, 26118, -390, 0, 0, 0, 582], [0, 14868, -19116, 1332, 0, 0, 0, 636], [13042, 0, 0, 0, 4026, -438, -3221, 0], [0, 7038, -15768, 354, 0, 0, 0, -474], [56900, 0, 0, 0, 1719, -1389, 18, 0], [38438, 0, 0, 0, 122, 992, 3633, 0], [-26914, 0, 0, 0, -1272, -102, -4263, 0], [-70198, 0, 0, 0, 116, 4370, 915, 0], [-26706, 0, 0, 0, 3220, 1372, 3620, 0], [0, 1372, 25938, -194, 0, 0, 0, -950], [164826, 0, 0, 0, 1978, -3290, 3902, 0], [-3062, 0, 0, 0, 744, 2704, 1119, 0], [-135912, 0, 0, 0, -3380, 1009, -823, 0], [0, -3600, -36606, 1584, 0, 0, 0, 450], [0, 12402, 32850, 624, 0, 0, 0, 228], [0, -859, -3579, 200, 0, 0, 0, -388], [-69622, 0, 0, 0, -6516, -3780, -1125, 0], [0, 13220, 7530, 632, 0, 0, 0, -1474], [-181394, 0, 0, 0, -3096, 3192, -6215, 0], [0, -10296, -60798, -2424, 0, 0, 0, -1074], [0, -11418, 10962, 1764, 0, 0, 0, -1740], [105194, 0, 0, 0, -4212, -1536, -12888, 0], [-144824, 0, 0, 0, -549, 2509, 3708, 0], [6614, 0, 0, 0, -10894, 476, -3315, 0], [166134, 0, 0, 0, -11144, -3524, -6436, 0], [0, -15180, -73464, -2328, 0, 0, 0, -360], [0, -12966, -52440, 378, 0, 0, 0, -78], [0, -22022, -74772, -1358, 0, 0, 0, -1130], [74650, 0, 0, 0, -6852, 1300, 5619, 0], [-219446, 0, 0, 0, 4740, -3210, 2887, 0], [-125594, 0, 0, 0, -6198, -494, -9030, 0], [137686, 0, 0, 0, -7680, 3622, 3045, 0], [0, 22908, -27954, -2754, 0, 0, 0, 438], [203678, 0, 0, 0, -1248, 1674, 8601, 0], [72476, 0, 0, 0, -3310, 6671, 288, 0], [-59030, 0, 0, 0, 7836, -2130, 7279, 0], [0, 2733, 55725, 192, 0, 0, 0, 3084], [0, 3647, -68232, 3767, 0, 0, 0, 1991], [0, -10774, 52518, -3856, 0, 0, 0, -532], [0, -3687, 70992, -5007, 0, 0, 0, -51], [0, -12300, -63678, 3744, 0, 0, 0, 942], [-862, 0, 0, 0, 13518, -5808, 7983, 0], [-50310, 0, 0, 0, -2762, 1978, -16486, 0], [125654, 0, 0, 0, -4086, -7746, -1890, 0], [37184, 0, 0, 0, -10840, -91, -21354, 0], [-90340, 0, 0, 0, 3114, -2955, -972, 0], [153158, 0, 0, 0, -3456, -3510, -10422, 0], [162658, 0, 0, 0, 570, 184, -13737, 0], [0, 22374, 14082, -8076, 0, 0, 0, -840], [0, -59834, 21564, -1586, 0, 0, 0, 514], [128794, 0, 0, 0, 9642, -4760, 663, 0], [-92812, 0, 0, 0, 20405, 3569, 9042, 0], [335368, 0, 0, 0, 3285, 3721, -936, 0], [0, -38487, 157260, -4059, 0, 0, 0, -771], [0, -53137, 104313, 230, 0, 0, 0, 1346], [0, -57, -139824, 4935, 0, 0, 0, 1923], [434924, 0, 0, 0, -3985, 4505, -6390, 0], [0, -12927, -112647, -216, 0, 0, 0, -420], [283834, 0, 0, 0, 19668, 3798, 1639, 0], [173332, 0, 0, 0, 14232, 3877, 9492, 0], [0, 21951, -46125, 660, 0, 0, 0, -3012], [0, 5293, -236472, 3085, 0, 0, 0, 2749], [0, -9708, 39762, 1422, 0, 0, 0, 2994], [-278842, 0, 0, 0, -23184, -6054, 1818, 0], [0, 8664, 49056, -2076, 0, 0, 0, -336], [175042, 0, 0, 0, -9300, 1252, -3597, 0], [0, -1920, -2400, -1584, 0, 0, 0, -2076], [0, 25277, 47862, 815, 0, 0, 0, 1223], [159874, 0, 0, 0, -14460, 9474, 13075, 0], [-159062, 0, 0, 0, 14094, -4844, 20979, 0], [0, -15360, 35868, 504, 0, 0, 0, 348], [0, 44885, -258975, 3404, 0, 0, 0, 1196], [0, -54780, -12438, 5154, 0, 0, 0, -1014], [-44816, 0, 0, 0, -6900, -125, -29397, 0], [-296532, 0, 0, 0, -19373, -16631, 5705, 0], [292052, 0, 0, 0, -8343, 7623, -4698, 0], [0, 30999, 57450, 1389, 0, 0, 0, 7101], [-149742, 0, 0, 0, 16996, 1486, 23750, 0], [0, -35628, 62862, 7026, 0, 0, 0, 786], [-224078, 0, 0, 0, 2316, 6772, -3828, 0], [0, 2508, 98022, 3012, 0, 0, 0, -3942], [0, 39306, 198912, 5334, 0, 0, 0, -1842], [0, -26244, -216672, -4632, 0, 0, 0, 5052], [0, -105644, 77862, 958, 0, 0, 0, 3682], [318478, 0, 0, 0, -20652, 1438, 16125, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_342_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_342_5_d_a();".
// 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_342_5_d_a(:prec:=8)
    chi := MakeCharacter_342_d();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 5));
    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_342_5_d_a();".
// 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_342_5_d_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_342_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,5,sign)));
    Vf := Kernel([<5,R![964896, -6624, -2074, 9, 1]>],Snew);
    return Vf;
end function;