
// Make newform 42.4.e.c in Magma, downloaded from the LMFDB on 01 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_42_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_42_e_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_42_4_e_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_42_4_e_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 := [112896, 336, 337, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [112896, -337, 337, -1], [673, 0, 0, 1]];
    Rf_basisdens := [1, 1, 113232, 337];
    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_42_e();"
function MakeCharacter_42_e()
    N := 42;
    order := 3;
    char_gens := [29, 31];
    v := [3, 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_42_e_Hecke();"
function MakeCharacter_42_e_Hecke( : Kf := false)
    N := 42;
    order := 3;
    char_gens := [29, 31];
    char_values := [[1, 0, 0, 0], [0, 0, -1, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := ISA(Type(Kf), Fld), 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]];
    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, -2, 0], [-3, 0, 3, 0], [0, 1, -3, 0], [2, 0, -3, -1], [-34, 1, 33, 1], [21, 0, 0, -1], [44, 4, -48, 4], [0, -3, -20, 0], [0, -4, -72, 0], [44, 0, 0, -11], [261, -2, -259, -2], [0, 9, -8, 0], [-210, 0, 0, -6], [-61, 0, 0, 15], [0, 12, -294, 0], [120, 3, -123, 3], [16, -25, 9, -25], [0, -4, -110, 0], [-349, 11, 338, 11], [226, 0, 0, 20], [443, 35, -478, 35], [0, 8, 259, 0], [-98, 0, 0, -25], [0, -42, -366, 0], [994, 0, 0, -35], [-362, 20, 342, 20], [0, 21, 1216, 0], [0, -45, -87, 0], [323, -49, -274, -49], [-922, 0, 0, 82], [-1461, 0, 0, 68], [0, 7, -93, 0], [1660, 50, -1710, 50], [-749, 0, 0, -101], [0, 6, -474, 0], [-496, 59, 437, 59], [646, -18, -628, -18], [0, 132, -1160, 0], [1274, 0, 0, -8], [0, 70, -282, 0], [-1266, -114, 1380, -114], [-1407, 0, 0, 143], [0, 78, 336, 0], [589, 222, -811, 222], [-1580, 0, 0, 170], [2872, 36, -2908, 36], [2732, 0, 0, 102], [-4480, 0, 0, -9], [-3078, -57, 3135, -57], [0, 211, -500, 0], [0, 62, -1944, 0], [2970, 0, 0, 54], [-110, 39, 71, 39], [84, 0, 0, -75], [0, -48, 2370, 0], [-5172, 102, 5070, 102], [-1790, -55, 1845, -55], [0, -247, -2009, 0], [2983, -255, -2728, -255], [2368, 0, 0, 38], [-2875, 17, 2858, 17], [-2016, 0, 0, 9], [3745, 0, 0, 175], [1066, 86, -1152, 86], [0, -38, -2897, 0], [0, -63, -3345, 0], [0, 147, -1532, 0], [-1083, 0, 0, 44], [3060, 168, -3228, 168], [5362, 0, 0, 232], [1626, -162, -1464, -162], [0, -218, 1878, 0], [-4877, -354, 5231, -354], [0, 157, 9280, 0], [-5171, 0, 0, 307], [0, 302, 1026, 0], [11866, 68, -11934, 68], [0, -319, 2788, 0], [0, -264, -7512, 0], [-509, 366, 143, 366], [1554, 0, 0, 174], [6477, 0, 0, 53], [-2358, -246, 2604, -246], [-3913, 0, 0, -201], [0, 41, -8681, 0], [0, -91, 4635, 0], [-10918, 0, 0, -38], [0, -518, 3865, 0], [-7756, 0, 0, 274], [877, 0, 0, -797], [0, 502, -6636, 0], [12560, -218, -12342, -218], [-1301, 282, 1019, 282], [2004, 0, 0, 633], [0, -101, 2146, 0], [-2310, 0, 0, -606], [0, 423, -285, 0], [13932, -426, -13506, -426], [0, 675, 4132, 0], [0, -583, -6458, 0], [5028, 0, 0, -364], [-16862, -301, 17163, -301], [-20662, -143, 20805, -143], [0, -690, 8580, 0], [-18339, -287, 18626, -287], [7793, -544, -7249, -544], [6832, 0, 0, 623], [0, 510, -8016, 0], [-4164, -1014, 5178, -1014], [18767, 0, 0, 348], [0, -802, 7429, 0], [3438, 538, -3976, 538], [394, 0, 0, -472], [-733, 1259, -526, 1259], [-3568, 0, 0, -645], [-30652, 82, 30570, 82], [-14245, 0, 0, 591], [-54, 420, -366, 420], [0, -399, 12405, 0], [13624, 0, 0, -688], [-11947, -211, 12158, -211], [9599, 0, 0, 798], [0, 1131, 3081, 0], [18768, 21, -18789, 21], [0, 627, 3088, 0], [3068, 0, 0, 493], [0, 744, 9442, 0], [0, 264, 3486, 0], [-2975, 0, 0, 496], [0, -823, 10474, 0], [14337, -191, -14146, -191], [6918, 0, 0, 1128], [0, 356, -22307, 0], [-9966, 0, 0, -724], [0, 80, -3204, 0], [16135, 0, 0, 910], [-7502, 1616, 5886, 1616], [2928, 562, -3490, 562], [25788, 0, 0, -813], [-5522, -958, 6480, -958], [5992, 0, 0, -674], [0, 1127, -4437, 0], [19664, 608, -20272, 608], [19434, 0, 0, 627], [9011, -703, -8308, -703], [-3332, 0, 0, -1876], [-30625, 0, 0, 567], [12000, -1374, -10626, -1374], [0, 486, 12826, 0], [0, 196, 19734, 0], [0, -458, 8320, 0], [11844, 0, 0, 1782], [-20347, 0, 0, 963], [0, 846, -13056, 0], [-6957, 637, 6320, 637], [38124, 0, 0, -450], [0, -1613, -16880, 0], [0, 384, 46038, 0], [13559, 0, 0, -204], [-11408, 977, 10431, 977], [0, 1822, 17520, 0], [11272, 0, 0, 962], [-22279, 0, 0, 804], [0, 891, 25581, 0], [-12942, -444, 13386, -444], [3600, 1788, -5388, 1788], [6301, 2394, -8695, 2394], [17929, 873, -18802, 873]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_42_e_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_42_4_e_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_42_4_e_c(:prec:=4)
    chi := MakeCharacter_42_e();
    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_42_4_e_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_42_4_e_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_42_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![108900, -1650, 355, 5, 1]>],Snew);
    return Vf;
end function;