
// Make newform 240.5.bg.b in Magma, downloaded from the LMFDB on 26 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_240_bg();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_240_bg_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_240_5_bg_b();".
// 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_240_5_bg_b();".
// 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 := [9, 0, 0, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 3, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
    Rf_basisdens := [1, 1, 3, 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_240_bg();"
function MakeCharacter_240_bg()
    N := 240;
    order := 4;
    char_gens := [31, 181, 161, 97];
    v := [4, 4, 4, 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_240_bg_Hecke();"
function MakeCharacter_240_bg_Hecke(Kf)
    N := 240;
    order := 4;
    char_gens := [31, 181, 161, 97];
    char_values := [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 1, 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, 0, 0], [0, 0, 0, 1], [21, -2, -7, -1], [7, 14, 7, 0], [-116, 7, 0, -7], [-84, 0, 84, -38], [98, 46, 98, 0], [0, -58, -154, -58], [-242, 0, 242, 42], [0, 77, -274, 77], [140, 74, 0, -74], [564, 210, 564, 0], [98, 202, 0, -202], [-54, 0, 54, -140], [2268, -102, 2268, 0], [-1070, 0, 1070, -84], [0, 165, 1540, 165], [-1134, 418, 0, -418], [562, 924, 562, 0], [-4516, 112, 0, -112], [5131, 0, -5131, 472], [0, 266, -2712, 266], [84, 0, -84, -1142], [0, 1164, -1358, 1164], [-10101, 160, -10101, 0], [4270, 821, 0, -821], [7301, 0, -7301, -1330], [-8960, -1274, -8960, 0], [0, -630, 7190, -630], [-6764, 0, 6764, 2282], [7303, -1162, 7303, 0], [-896, -113, 0, 113], [528, -14, 528, 0], [0, 632, -31514, 632], [0, -973, -9294, -973], [-730, -1316, 0, 1316], [13734, -1170, 13734, 0], [-7544, 0, 7544, 3920], [17738, -5362, 17738, 0], [-14392, 0, 14392, -4966], [0, -497, 2744, -497], [8918, -5458, 0, 5458], [-29764, 1386, 0, -1386], [-20457, 0, 20457, -896], [-18054, -4172, -18054, 0], [0, 6596, -14742, 6596], [18274, 4592, 0, -4592], [9093, 0, -9093, -10318], [1820, 736, 1820, 0], [0, -900, -18690, -900], [-29994, 0, 29994, 1442], [0, 3948, -19176, 3948], [30352, 11640, 0, -11640], [5376, -8749, 0, 8749], [-26936, -4170, -26936, 0], [-26782, 0, 26782, -16338], [0, -9993, 20006, -9993], [-11718, -3072, 0, 3072], [144, -17710, 144, 0], [-53010, -6034, 0, 6034], [31654, 0, -31654, -2152], [-6930, 0, 6930, -13104], [-29862, -4872, -29862, 0], [-3556, -9368, 0, 9368], [-30639, 0, 30639, 6592], [48900, 7294, 48900, 0], [11558, -5110, 0, 5110], [-111747, 8456, -111747, 0], [-13064, -9380, -13064, 0], [0, 10650, 44170, 10650], [62454, 0, -62454, -16990], [0, 4662, -139684, 4662], [-35609, -21110, -35609, 0], [-8006, 0, 8006, -15750], [0, 4032, 40466, 4032], [-79212, 0, 79212, 15482], [0, 777, -198174, 777], [42910, -20246, 42910, 0], [25398, -2408, 0, 2408], [0, -24512, 5964, -24512], [0, -6295, 55440, -6295], [-109774, -11872, 0, 11872], [-77576, 18102, 0, -18102], [50729, 0, -50729, 19780], [0, -4804, -300482, -4804], [1464, 0, -1464, 26768], [0, -12978, 87566, -12978], [37623, -29904, 37623, 0], [-376054, -2877, 0, 2877], [-62879, 0, 62879, -32550], [91924, 26202, 91924, 0], [0, 132, 78316, 132], [-93429, -4410, -93429, 0], [-145828, 26453, 0, -26453], [0, 5362, 370426, 5362], [24780, 0, -24780, 26814], [0, -50283, -4774, -50283], [146370, 47606, 0, -47606], [-60942, 0, 60942, -3188], [-220274, -17542, 0, 17542], [60306, 13720, 60306, 0], [56670, 98924, 56670, 0], [-177828, 0, 177828, -41724], [0, 25004, -73658, 25004], [359434, -35798, 0, 35798], [-54131, -8220, -54131, 0], [-280980, -27174, -280980, 0], [-53788, 0, 53788, -49562], [0, 29302, 73696, 29302], [-67690, 67596, 0, -67596], [6027, 68934, 6027, 0], [32586, 0, -32586, -53438], [299142, 6902, 299142, 0], [0, -25272, 242634, -25272], [-9592, 13510, 0, -13510], [-444854, 7728, 0, -7728], [-97104, 0, 97104, 58340], [387464, 6922, 387464, 0], [-418032, 0, 418032, 1834], [0, -16779, -415252, -16779], [407498, -47598, 0, 47598], [124639, 0, -124639, 50540], [86576, 21058, 86576, 0], [90372, 0, -90372, 90146], [207522, 42158, 0, -42158], [-601462, 11417, 0, -11417], [0, -54096, -562238, -54096], [0, -85740, 152740, -85740], [-550389, 29590, -550389, 0], [-2072, 0, 2072, 97714], [0, 18886, -92942, 18886], [-102664, 0, 102664, 37730], [-575196, 17262, 0, -17262], [-134256, -6090, -134256, 0], [-649810, -9634, 0, 9634], [0, -8964, 406238, -8964], [-128604, 0, 128604, 29462], [-1078, 174064, -1078, 0], [252672, -78958, 252672, 0], [0, -138712, 77674, -138712], [244006, -63604, 0, 63604], [-670510, -14189, 0, 14189], [573433, 0, -573433, 28602], [241716, -158060, 241716, 0], [0, 91654, -594538, 91654], [0, -48822, 128324, -48822], [165970, 0, -165970, -42274], [-32046, 179770, -32046, 0], [0, 31612, -513674, 31612], [237490, 0, -237490, -135226], [42834, 63910, 42834, 0], [1223474, 19424, 0, -19424], [-48724, 0, 48724, -66920], [440118, 18178, 440118, 0], [102884, -65268, 102884, 0], [480848, 154350, 0, -154350], [0, 44898, 646684, 44898], [0, 93422, -709954, 93422], [-350147, -117584, -350147, 0], [274890, -189979, 0, 189979], [225812, 249144, 225812, 0], [-119976, 0, 119976, 73570], [-218593, -144606, -218593, 0], [-1499008, -45407, 0, 45407], [-10770, 200774, -10770, 0], [115976, 0, -115976, 116170], [-32098, 142968, 0, -142968], [523768, 135586, 523768, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_240_bg_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_240_5_bg_b();".
// 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_240_5_bg_b(:prec:=4)
    chi := MakeCharacter_240_bg();
    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_240_5_bg_b();".
// 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_240_5_bg_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_240_bg();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,5,sign)));
    Vf := Kernel([<7,R![26977636, 145432, 392, -28, 1]>],Snew);
    return Vf;
end function;