
// Make newform 240.3.bg.a in Magma, downloaded from the LMFDB on 19 April 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_3_bg_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_240_3_bg_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 := [9, 0, 0, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
    Rf_basisdens := [1, 1, 3, 3];
    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 := 3;
    raw_aps := [[0, 0, 0, 0], [0, 1, 0, 0], [-1, -1, -3, 2], [-1, 0, 1, 2], [-4, -3, 0, 3], [-8, 2, -8, 0], [-10, 0, 10, 6], [0, 6, -6, 6], [-14, 2, -14, 0], [0, -7, -18, -7], [4, -6, 0, 6], [16, 0, -16, 18], [-14, 6, 0, -6], [2, 20, 2, 0], [-32, 0, 32, 10], [14, -12, 14, 0], [0, -31, 36, -31], [50, -18, 0, 18], [50, 0, -50, 4], [68, 0, 0, 0], [19, 48, 19, 0], [0, -10, 0, -10], [4, -14, 4, 0], [0, -36, 6, -36], [-5, 0, 5, 16], [34, -57, 0, 57], [77, 10, 77, 0], [-32, 0, 32, 30], [0, -6, -54, -6], [56, 34, 56, 0], [-25, 0, 25, 114], [-40, -27, 0, 27], [-76, 0, 76, 74], [0, -56, -54, -56], [0, 7, -102, 7], [-2, -60, 0, 60], [34, 0, -34, 14], [-160, 24, -160, 0], [-26, 0, 26, -58], [56, 74, 56, 0], [0, 75, 0, 75], [-154, -54, 0, 54], [-4, -18, 0, 18], [-89, -136, -89, 0], [-82, 0, 82, 12], [0, -12, 222, -12], [-206, 24, 0, -24], [-67, -82, -67, 0], [-116, 0, 116, 112], [0, -4, 234, -4], [-22, -150, -22, 0], [0, -4, -336, -4], [-16, 48, 0, -48], [368, -15, 0, 15], [-28, 0, 28, -50], [310, -26, 310, 0], [0, 99, 246, 99], [-14, -48, 0, 48], [-92, 0, 92, -158], [358, -6, 0, 6], [38, -32, 38, 0], [-70, 136, -70, 0], [-22, 0, 22, -120], [116, 192, 0, -192], [25, -104, 25, 0], [-268, 0, 268, -130], [-122, 150, 0, -150], [253, 0, -253, 32], [-80, 0, 80, -196], [0, 178, -138, 178], [122, -118, 122, 0], [0, 54, -84, 54], [47, 0, -47, 158], [-122, 170, -122, 0], [0, 0, -210, 0], [-80, 74, -80, 0], [0, 221, -6, 221], [82, 0, -82, -30], [-50, 192, 0, -192], [0, 176, 84, 176], [0, 101, -336, 101], [-22, -96, 0, 96], [-496, -126, 0, 126], [-95, -204, -95, 0], [0, 140, 90, 140], [88, -272, 88, 0], [0, -18, -342, -18], [31, 0, -31, 208], [-98, -255, 0, 255], [-103, 190, -103, 0], [-452, 0, 452, -30], [0, 100, 60, 100], [275, 0, -275, -126], [260, 63, 0, -63], [0, 226, -186, 226], [208, -162, 208, 0], [0, -119, -486, -119], [-134, 186, 0, -186], [362, 180, 362, 0], [446, 6, 0, -6], [554, 0, -554, -88], [170, 0, -170, 36], [-236, 356, -236, 0], [0, -76, 786, -76], [586, -138, 0, 138], [-395, 0, 395, -84], [-452, 0, 452, 130], [488, 30, 488, 0], [0, -82, -168, -82], [374, 228, 0, -228], [-325, 0, 325, 154], [-314, -246, -314, 0], [74, 0, -74, -386], [0, -136, 486, -136], [-32, 30, 0, -30], [106, -24, 0, 24], [152, -500, 152, 0], [-20, 0, 20, -246], [-40, 106, -40, 0], [0, 25, 1020, 25], [-118, -210, 0, 210], [199, 28, 199, 0], [248, 0, -248, 242], [-500, 234, -500, 0], [-254, 42, 0, -42], [70, -333, 0, 333], [0, -296, 6, -296], [0, -252, 12, -252], [131, 0, -131, -94], [-332, 50, -332, 0], [0, -66, -354, -66], [-188, -190, -188, 0], [316, 222, 0, -222], [-596, 0, 596, 294], [-602, 114, 0, -114], [0, -388, 258, -388], [-220, 486, -220, 0], [-862, 0, 862, 40], [152, 0, -152, 130], [0, 0, 150, 0], [-602, -300, 0, 300], [238, -111, 0, 111], [-391, -74, -391, 0], [340, 0, -340, 564], [0, 150, 330, 150], [0, 250, -540, 250], [310, -314, 310, 0], [62, 0, -62, 290], [0, -260, -150, -260], [742, 270, 742, 0], [286, 0, -286, 38], [298, -96, 0, 96], [452, -240, 452, 0], [178, 0, -178, 170], [188, 0, -188, 620], [488, -270, 0, 270], [0, 150, 780, 150], [0, 14, 186, 14], [133, 0, -133, 432], [-674, 351, 0, -351], [-164, 0, 164, -824], [-388, -198, -388, 0], [-385, 0, 385, -626], [464, 219, 0, -219], [650, 0, -650, 46], [676, 442, 676, 0], [838, -120, 0, 120], [-716, 0, 716, -326]];
    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_3_bg_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_240_3_bg_a(:prec:=4)
    chi := MakeCharacter_240_bg();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    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_3_bg_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_240_3_bg_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_240_bg();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<7,R![100, -40, 8, 4, 1]>],Snew);
    return Vf;
end function;