
// Make newform 20.5.d.c in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_20_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_20_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_20_5_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_20_5_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 := [256, 0, 96, 0, 31, 0, 6, 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, 2, 0, 0, 0, 0, 0, 0], [0, 80, 0, 1, 0, 10, 0, -1], [6, 0, 4, 0, 0, 0, 0, 0], [0, 80, 0, 31, 0, 6, 0, 1], [28, 0, 1, 0, 2, 0, -1, 0], [384, -176, 120, -57, 48, -26, 8, -7], [-60, 0, -23, 0, -6, 0, -1, 0]];
    Rf_basisdens := [1, 1, 32, 1, 8, 2, 32, 2];
    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_20_d();"
function MakeCharacter_20_d()
    N := 20;
    order := 2;
    char_gens := [11, 17];
    v := [1, 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_20_d_Hecke();"
function MakeCharacter_20_d_Hecke(Kf)
    N := 20;
    order := 2;
    char_gens := [11, 17];
    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, 1, 0, 0, 0, 0, 0, 0], [0, 0, -1, 0, 0, 0, 0, 0], [-5, 1, 0, -1, 0, 0, -1, -1], [0, 6, 1, 0, -2, 0, 0, 0], [0, 0, 0, 0, 0, -4, 0, 0], [0, -18, 0, -1, -4, 0, -2, -1], [0, 16, 0, 2, 4, 0, 4, 2], [0, 0, 0, 24, 0, 4, 0, -8], [0, 66, 13, 0, -22, 0, 0, 0], [338, 0, 0, -4, 0, 0, 0, -4], [0, 0, 0, -24, 0, 16, 0, 8], [0, 162, 0, -21, 24, 0, -42, -21], [182, 0, 0, -79, 0, 0, 0, -79], [0, 348, -77, 0, -116, 0, 0, 0], [0, 102, -7, 0, -34, 0, 0, 0], [0, -502, 0, -59, -124, 0, -118, -59], [0, 0, 0, -216, 0, 28, 0, 72], [822, 0, 0, 131, 0, 0, 0, 131], [0, -300, 139, 0, 100, 0, 0, 0], [0, 0, 0, 216, 0, -120, 0, -72], [0, -108, 0, 144, 36, 0, 288, 144], [0, 0, 0, -24, 0, 96, 0, 8], [0, -204, -693, 0, 68, 0, 0, 0], [3458, 0, 0, -340, 0, 0, 0, -340], [0, -1224, 0, 22, -236, 0, 44, 22], [5042, 0, 0, -268, 0, 0, 0, -268], [0, -606, 1213, 0, 202, 0, 0, 0], [0, 1272, 407, 0, -424, 0, 0, 0], [1238, 0, 0, -81, 0, 0, 0, -81], [0, 2248, 0, -84, 416, 0, -168, -84], [0, 834, -203, 0, -278, 0, 0, 0], [0, 0, 0, 648, 0, 52, 0, -216], [0, 188, 0, 66, 64, 0, 132, 66], [0, 0, 0, -288, 0, 380, 0, 96], [-26602, 0, 0, -133, 0, 0, 0, -133], [0, 0, 0, 288, 0, -536, 0, -96], [0, -2826, 0, -127, -616, 0, -254, -127], [0, 2472, -1145, 0, -824, 0, 0, 0], [0, -3894, -1995, 0, 1298, 0, 0, 0], [0, -3842, 0, 61, -744, 0, 122, 61], [0, 0, 0, -2160, 0, 468, 0, 720], [-1998, 0, 0, -436, 0, 0, 0, -436], [0, 0, 0, 2160, 0, 224, 0, -720], [0, -1800, 0, -910, -724, 0, -1820, -910], [0, 3602, 0, 1309, 1244, 0, 2618, 1309], [0, 0, 0, 504, 0, 72, 0, -168], [0, 0, 0, -2136, 0, 1188, 0, 712], [0, -3990, -3923, 0, 1330, 0, 0, 0], [0, -6792, 1591, 0, 2264, 0, 0, 0], [-42702, 0, 0, -1420, 0, 0, 0, -1420], [0, -6652, 0, 616, -1084, 0, 1232, 616], [0, 0, 0, 1512, 0, 640, 0, -504], [-13738, 0, 0, 705, 0, 0, 0, 705], [0, 0, 0, 3024, 0, -468, 0, -1008], [0, 6800, 0, -1240, 864, 0, -2480, -1240], [0, 9966, -1111, 0, -3322, 0, 0, 0], [-39562, 0, 0, 515, 0, 0, 0, 515], [0, 0, 0, -1320, 0, -2368, 0, 440], [0, -19638, 0, -521, -4136, 0, -1042, -521], [-48298, 0, 0, -715, 0, 0, 0, -715], [0, -10776, 6295, 0, 3592, 0, 0, 0], [0, 8626, 0, 927, 2096, 0, 1854, 927], [0, 18924, 6259, 0, -6308, 0, 0, 0], [0, 0, 0, 2592, 0, 1672, 0, -864], [0, 10980, 0, 2950, 3376, 0, 5900, 2950], [0, 13654, 0, -1817, 2004, 0, -3634, -1817], [0, 0, 0, -2664, 0, 3436, 0, 888], [0, -23400, 0, 1760, -3976, 0, 3520, 1760], [0, 12108, -7021, 0, -4036, 0, 0, 0], [-77422, 0, 0, 5988, 0, 0, 0, 5988], [0, 6656, 0, -1188, 856, 0, -2376, -1188], [0, 0, 0, -864, 0, -5736, 0, 288], [0, 3858, 6997, 0, -1286, 0, 0, 0], [0, 17370, 0, -1945, 2696, 0, -3890, -1945], [0, 0, 0, -2256, 0, -340, 0, 752], [0, 13602, -6107, 0, -4534, 0, 0, 0], [-39082, 0, 0, -7861, 0, 0, 0, -7861], [0, -20466, 0, -3957, -5676, 0, -7914, -3957], [39842, 0, 0, -892, 0, 0, 0, -892], [68918, 0, 0, 2265, 0, 0, 0, 2265], [0, 0, 0, 16416, 0, -2300, 0, -5472], [-3178, 0, 0, 10419, 0, 0, 0, 10419], [0, 0, 0, -13608, 0, -1056, 0, 4536], [0, 8208, 0, 2886, 2796, 0, 5772, 2886], [0, 0, 0, 4416, 0, 5192, 0, -1472], [0, -2256, -4161, 0, 752, 0, 0, 0], [178838, 0, 0, 593, 0, 0, 0, 593], [0, -30708, 0, 3814, -4616, 0, 7628, 3814], [-141358, 0, 0, 1940, 0, 0, 0, 1940], [0, 35574, 11257, 0, -11858, 0, 0, 0], [0, -13776, 23503, 0, 4592, 0, 0, 0], [0, 0, 0, -11232, 0, 3872, 0, 3744], [0, 21966, -24887, 0, -7322, 0, 0, 0], [0, 0, 0, -6264, 0, 1132, 0, 2088], [0, 0, 0, -5376, 0, -5388, 0, 1792], [0, 15618, 17437, 0, -5206, 0, 0, 0], [-168622, 0, 0, 380, 0, 0, 0, 380], [-159838, 0, 0, 7760, 0, 0, 0, 7760], [0, 56832, -2561, 0, -18944, 0, 0, 0], [101202, 0, 0, -13780, 0, 0, 0, -13780], [0, -22080, 21871, 0, 7360, 0, 0, 0], [0, -29906, 0, 5813, -3656, 0, 11626, 5813], [0, -14712, -29209, 0, 4904, 0, 0, 0], [199478, 0, 0, 11081, 0, 0, 0, 11081], [0, 0, 0, -6144, 0, -5300, 0, 2048], [0, -75600, 0, 2460, -14136, 0, 4920, 2460], [0, -42324, -2157, 0, 14108, 0, 0, 0], [0, 65896, 0, 1892, 13936, 0, 3784, 1892], [0, 0, 0, 7992, 0, 7096, 0, -2664], [-79338, 0, 0, 6893, 0, 0, 0, 6893], [0, 34854, 1849, 0, -11618, 0, 0, 0], [0, 10098, 0, -10659, -2244, 0, -21318, -10659], [0, 56452, 0, 684, 11564, 0, 1368, 684], [0, 0, 0, 14184, 0, 9964, 0, -4728], [0, 0, 0, -14592, 0, -6968, 0, 4864], [182102, 0, 0, 8393, 0, 0, 0, 8393], [0, -67044, -28589, 0, 22348, 0, 0, 0], [0, 26886, -26223, 0, -8962, 0, 0, 0], [0, 2174, 0, 4403, 2196, 0, 8806, 4403], [0, 0, 0, 5184, 0, 2708, 0, -1728], [481782, 0, 0, 2663, 0, 0, 0, 2663], [0, 5688, 0, 10846, 5476, 0, 21692, 10846], [0, -63230, 0, -5975, -15036, 0, -11950, -5975], [0, -83292, -14661, 0, 27764, 0, 0, 0], [0, 0, 0, 8664, 0, 356, 0, -2888], [-462058, 0, 0, -3697, 0, 0, 0, -3697], [177138, 0, 0, 25964, 0, 0, 0, 25964], [0, 0, 0, 7560, 0, 8352, 0, -2520], [0, 26142, -12455, 0, -8714, 0, 0, 0], [0, 62406, 0, -4963, 10496, 0, -9926, -4963], [0, 0, 0, 912, 0, -220, 0, -304], [0, 16662, 20417, 0, -5554, 0, 0, 0], [0, 0, 0, 21552, 0, 14256, 0, -7184], [0, 59130, 0, -19605, 3984, 0, -39210, -19605], [195842, 0, 0, 1316, 0, 0, 0, 1316], [-730942, 0, 0, -2832, 0, 0, 0, -2832], [0, -7198, 0, -5711, -3724, 0, -11422, -5711], [0, -27228, 24379, 0, 9076, 0, 0, 0], [0, -68458, 0, 539, -13476, 0, 1078, 539], [-394462, 0, 0, -18640, 0, 0, 0, -18640], [0, 0, 0, -5040, 0, -12804, 0, 1680], [520502, 0, 0, -18193, 0, 0, 0, -18193], [0, 50298, -56795, 0, -16766, 0, 0, 0], [0, 187860, 37323, 0, -62620, 0, 0, 0], [531638, 0, 0, -525, 0, 0, 0, -525], [0, 0, 0, 1728, 0, -16072, 0, -576], [0, -98262, 0, -14579, -25484, 0, -29158, -14579], [0, -63548, 0, -23366, -22056, 0, -46732, -23366], [0, 0, 0, -31416, 0, 17788, 0, 10472], [0, 54174, -88463, 0, -18058, 0, 0, 0], [0, 55278, 0, 3521, 12464, 0, 7042, 3521], [797942, 0, 0, -6163, 0, 0, 0, -6163], [0, -5808, 29263, 0, 1936, 0, 0, 0], [0, -62826, -5919, 0, 20942, 0, 0, 0], [0, -116472, 33463, 0, 38824, 0, 0, 0], [0, 0, 0, -216, 0, -7072, 0, 72], [0, 0, 0, 47160, 0, -17928, 0, -15720], [-660202, 0, 0, 14321, 0, 0, 0, 14321], [0, 81972, 0, 6624, 19044, 0, 13248, 6624], [-451438, 0, 0, -50020, 0, 0, 0, -50020], [0, -143340, -65189, 0, 47780, 0, 0, 0], [0, 65044, 0, -26182, 2536, 0, -52364, -26182], [0, -198534, -1355, 0, 66178, 0, 0, 0], [0, 0, 0, 13608, 0, 32076, 0, -4536], [0, -264512, 0, -2034, -53716, 0, -4068, -2034], [0, -130866, -51815, 0, 43622, 0, 0, 0], [0, 0, 0, -64416, 0, 16096, 0, 21472], [0, 50994, 0, 1513, 10804, 0, 3026, 1513]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_20_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_20_5_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_20_5_d_c(:prec:=8)
    chi := MakeCharacter_20_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_20_5_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_20_5_d_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_20_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,5,sign)));
    Vf := Kernel([<3,R![8640, 0, -240, 0, 1]>],Snew);
    return Vf;
end function;