
// Make newform 507.4.a.g in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_507_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_507_4_a_g();".
// 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_507_4_a_g();".
// 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 := [-3, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_507_a();"
function MakeCharacter_507_a()
    N := 507;
    order := 1;
    char_gens := [170, 340];
    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;

function MakeCharacter_507_a_Hecke(Kf)
    return MakeCharacter_507_a();
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 := 4;
    raw_aps := [[0, 0], [-3, 0], [0, 3], [0, 6], [0, -30], [0, 0], [117, 0], [0, 14], [-18, 0], [-99, 0], [0, 112], [0, -65], [0, -21], [82, 0], [0, 42], [-261, 0], [0, -456], [-719, 0], [0, -406], [0, 270], [0, -395], [-440, 0], [0, 690], [0, -876], [0, 668], [-1575, 0], [-794, 0], [450, 0], [0, 344], [-1701, 0], [1664, 0], [-1476, 0], [0, 585], [1124, 0], [0, -1887], [0, 946], [1259, 0], [0, 1704], [0, 1812], [-4266, 0], [-3006, 0], [1873, 0], [-2736, 0], [0, 1503], [0, 2148], [-1198, 0], [2392, 0], [0, -1176], [0, -1242], [0, -2004], [1854, 0], [0, -2574], [0, -241], [-4104, 0], [-1989, 0], [738, 0], [-2106, 0], [0, -396], [-3665, 0], [0, 993], [1826, 0], [0, -291], [0, -1126], [3798, 0], [1378, 0], [0, 4101], [0, -5816], [-9001, 0], [3294, 0], [0, 6088], [0, -1431], [0, 3126], [-9946, 0], [-7301, 0], [0, -1976], [0, 3336], [-9153, 0], [0, 1168], [0, -4797], [0, 6015], [4176, 0], [0, 8355], [0, -3798], [6605, 0], [8542, 0], [-14328, 0], [0, 1740], [0, -1667], [0, -2079], [0, -1546], [-13878, 0], [0, -636], [0, 9886], [-450, 0], [0, 7632], [5346, 0], [0, -3387], [-9657, 0], [21626, 0], [0, 3101], [16946, 0], [0, -2229], [-21672, 0], [1386, 0], [-1162, 0], [0, -4645], [0, -15948], [0, 159], [22356, 0], [-18083, 0], [5480, 0], [0, 10241], [0, -5697], [0, -2376], [0, -7312], [3789, 0], [0, -9764], [27792, 0], [-594, 0], [17748, 0], [0, -9117], [-20933, 0], [3402, 0], [0, 14424], [0, 8006], [21906, 0], [0, -7539], [14220, 0], [5282, 0], [0, 6593], [0, 11856], [0, -12024], [4834, 0], [9046, 0], [0, -6948], [0, 21676], [0, 9072], [0, -2168], [7830, 0], [-6165, 0], [0, -17228], [0, -17184], [8920, 0], [0, 10416], [21023, 0], [0, -12744], [0, 15363], [12771, 0], [17134, 0], [0, -4578], [0, -5695], [-29169, 0], [-928, 0], [-14400, 0], [-19684, 0], [24480, 0], [38608, 0], [0, -5895], [28495, 0], [0, 6192], [0, -19608], [-23814, 0], [0, 29762], [-12312, 0], [0, 12435], [0, 18828], [-22330, 0], [-24931, 0], [0, -22140], [-35991, 0], [0, 10038], [0, -6759], [59040, 0], [0, 4027], [-33092, 0], [-20043, 0], [0, -5350], [0, 37395], [3076, 0], [28378, 0], [0, -3418], [-1908, 0], [-12563, 0], [0, 29136], [0, -31902], [-35829, 0], [41254, 0], [0, 19348], [0, -28811], [0, 15774], [6293, 0], [0, 23430], [48062, 0], [0, -18636], [-38214, 0], [-42282, 0], [0, 11883], [4853, 0], [0, -11571], [-63720, 0], [0, -27963], [46676, 0], [0, 13557], [56285, 0], [0, 40398], [-28845, 0], [0, -43634], [-7740, 0], [0, 41841], [-6508, 0], [-11203, 0], [-3510, 0], [24310, 0], [0, 390], [0, -15504], [0, -40913], [-40580, 0], [18981, 0], [0, 28884], [0, 49488], [16805, 0], [0, -37396], [0, 44349], [0, -50620], [20106, 0], [66655, 0], [6777, 0], [-4608, 0], [9200, 0], [0, 17520], [-79883, 0], [62252, 0], [0, -31944], [34173, 0], [-65212, 0], [0, -25440], [0, -20964], [0, -48345], [-24660, 0], [82926, 0], [0, -3798], [54458, 0], [88390, 0], [0, 63729], [0, -23847], [64548, 0], [0, 10932], [0, 32616], [0, 33518], [-35442, 0], [0, 31024], [0, -1368], [0, -11388], [48995, 0], [-39222, 0], [0, 23274], [53218, 0], [0, 15270], [80730, 0], [0, -14799], [-94528, 0], [-36828, 0], [0, 45287], [-14119, 0], [0, 16593], [-66590, 0], [0, -25236], [0, -44217], [0, -16746], [-82179, 0], [-139633, 0], [0, -56372], [0, -22047], [69436, 0], [-55718, 0], [0, -20638], [0, 55776], [0, -73227], [0, 22745], [100710, 0], [-53874, 0], [0, -23664], [-44028, 0], [0, -38565], [0, -5642], [-151722, 0], [45331, 0], [0, 50757], [0, -7766], [72855, 0], [23202, 0], [-34074, 0], [0, -41799], [0, 68988], [22130, 0], [-123381, 0], [29888, 0], [69003, 0], [105192, 0], [0, -65846], [108745, 0], [0, -13899], [52810, 0], [-41472, 0], [-90250, 0], [0, 67199], [23454, 0], [-134263, 0], [0, 28524], [-35551, 0], [130194, 0], [0, -77640], [-15849, 0], [-115370, 0], [0, -20022], [-10547, 0], [0, 4590], [0, -40794], [0, -21796], [141237, 0], [13534, 0], [0, 29400], [124011, 0], [0, 101894], [0, 62436], [21835, 0], [0, -59086], [0, -15390], [194670, 0], [-179343, 0], [0, 29033], [72945, 0], [100150, 0], [0, 40278], [0, 56458], [0, -42186], [0, 48385], [0, -2820], [0, 72321], [198532, 0], [0, -52960], [12951, 0], [0, 36039], [-182978, 0], [0, 66096], [-172746, 0], [-10313, 0], [0, -44688], [0, -2658], [92466, 0], [0, 16538], [0, 3452], [0, -47577], [93950, 0], [158471, 0], [175923, 0], [0, -61806], [0, -57168], [65187, 0], [0, 7062], [0, -62735], [13221, 0], [69660, 0], [0, 119676], [49420, 0], [21242, 0], [0, 131184], [0, 47428], [146467, 0], [76536, 0], [1234, 0], [0, -56250], [122571, 0], [54010, 0], [200321, 0], [0, -114222], [-203670, 0], [0, -12080], [-13743, 0], [-50969, 0], [0, 82536], [0, -124437], [0, 111278], [0, 52380], [0, -82284], [0, 100638], [0, -97362], [-168166, 0], [0, -83868], [-112536, 0], [0, 158235], [0, -97923], [0, 5862], [-62594, 0], [0, -57372], [-86354, 0], [0, 28222], [118134, 0], [-82280, 0], [0, 102321], [0, 50092], [-193302, 0], [0, -25256], [0, -55065], [0, 18165], [224650, 0], [0, -159636], [0, -50631], [0, 109242], [0, -54696], [-89675, 0], [10971, 0], [-256230, 0], [-107180, 0], [199621, 0], [-166761, 0], [0, 145416], [208906, 0], [0, -138072], [-158616, 0], [-238419, 0], [0, -102163], [0, 122394], [169650, 0], [0, -10420], [0, 2847], [-99072, 0], [0, 63255], [0, -49222], [-294228, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_507_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_507_4_a_g();".
// 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_507_4_a_g(:prec:=2)
    chi := MakeCharacter_507_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_507_4_a_g();".
// 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_507_4_a_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_507_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![0, 1]>,<5,R![-27, 0, 1]>],Snew);
    return Vf;
end function;