
// Make newform 539.4.a.e in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_539_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_539_4_a_e();".
// 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_539_4_a_e();".
// 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_539_a();"
function MakeCharacter_539_a()
    N := 539;
    order := 1;
    char_gens := [199, 442];
    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_539_a_Hecke(Kf)
    return MakeCharacter_539_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 := [[1, 1], [1, 4], [-1, -8], [0, 0], [-11, 0], [-40, 20], [62, -12], [-36, -60], [-49, -36], [72, -56], [17, -28], [27, -8], [-268, 4], [-30, -16], [136, 120], [-246, -56], [-317, 132], [-420, -184], [377, -20], [-339, 76], [200, 468], [158, 656], [-234, -120], [921, 328], [-1097, -144], [-566, 420], [880, -488], [174, 380], [490, 320], [-855, 664], [-604, -412], [222, 796], [905, 408], [1466, -828], [530, -1708], [-1870, -408], [-1409, -616], [-2060, -384], [-1608, -652], [-2058, -144], [-2871, 900], [-1303, 1216], [2321, -348], [-572, 1104], [-4322, 464], [-408, -240], [1500, -928], [1973, 1132], [-1422, 1844], [-3495, 912], [2304, 1208], [114, -2424], [-1456, -1416], [-409, -396], [1226, -1160], [3194, 1724], [-3010, 1168], [-3032, -892], [166, 232], [198, 2952], [4964, -136], [3276, -3236], [1588, 52], [1956, -3192], [-3263, -2264], [109, -72], [3607, -2844], [5726, -3444], [-632, -3020], [-5106, 932], [2589, 4808], [-7808, -2132], [-8215, 836], [-2726, -1492], [-1829, 572], [4585, -1012], [-2847, 3440], [-9262, -2856], [-2950, -1912], [-2514, 3872], [12100, -1624], [-170, 7688], [2658, 1300], [6755, 2880], [2000, 5672], [10885, -452], [8315, -5000], [-13380, 1624], [-6660, 1112], [-2243, 3276], [5019, -5852], [-10784, -452], [-5641, -7324], [-10472, 1648], [-3580, 9520], [-8974, -688], [-2487, -2288], [5859, 7776], [-9356, 1788], [-2348, -6732], [-7984, 1752], [-7910, 2372], [9332, 328], [-2472, 4504], [-7180, -5648], [17607, -1224], [-484, -6120], [-3740, -352], [11632, 896], [6994, 6796], [-9650, -7056], [4592, -4688], [-3246, -11248], [4393, 9844], [8791, 7356], [6375, 7952], [-8189, -11940], [6031, -2564], [13183, 3952], [2338, -7424], [-2797, -288], [11006, -7040], [-5538, -4436], [-21688, 4536], [-8993, 10860], [5126, 14332], [15215, -2256], [15393, 9948], [4029, -2036], [-10308, 968], [-22750, 2536], [-9020, 11664], [3937, 7388], [5666, -17360], [-21432, 7484], [-10616, -12828], [24126, 6608], [-5656, -5816], [9571, -10080], [14772, -5124], [-23842, 5568], [-8618, -6480], [1887, -17372], [-1864, 20724], [-1897, -1752], [37781, 236], [-10238, -18896], [-13064, 17520], [-19425, 5836], [2352, -6928], [-11760, 18180], [-11645, -4520], [48940, 88], [-25394, 9296], [29652, 4640], [18348, 8488], [4066, -15712], [8310, 8584], [29524, 2916], [-28626, -7624], [-2955, 17428], [10102, 17804], [-27176, -10968], [12865, -4452], [12117, -1400], [-1815, -17956], [-4304, 19504], [29374, 1484], [24350, 1784], [16617, 11744], [32102, 16616], [-4126, -10448], [23132, -14028], [1988, -33204], [42691, -11468], [-13001, 13792], [-54874, -2428], [1979, -14120], [-28364, 17216], [-53212, -3232], [-4000, 3216], [-19910, 12200], [17277, 1096], [-39006, -1880], [-12627, 4972], [50382, 8984], [660, -9412], [-22464, 3432], [3470, 7972], [-37490, -4072], [5743, -12952], [-16658, -2660], [-46011, 1484], [-15990, 9200], [32280, -6148], [2133, -9160], [-18970, 13836], [-17177, 3088], [28290, -6888], [1006, -25124], [-47376, -17060], [37050, -9632], [-8442, 8432], [46400, 4312], [-24983, -3156], [8663, 15944], [30327, 6580], [-32988, 24552], [-32568, -22072], [10544, 40136], [-10848, -27328], [-23691, -10376], [71343, -10164], [-20884, 9144], [-13710, 35412], [27481, -1904], [-18544, 29636], [71196, 5432], [15672, -16552], [41751, 18048], [-7784, 17172], [-27552, 18740], [47673, 29936], [-11515, -36204], [27864, -34636], [8978, 26184], [-8502, 7440], [12288, 22680], [22280, 18508], [97192, -1108], [47425, -19400], [-34696, 22988], [-6674, -36592], [-38008, -13560], [39263, -7908], [-39562, -15928], [3903, -30280], [-8160, 20188], [69295, -2212], [55499, -10468], [-35513, -27724], [29632, -21200], [13908, -17832], [46161, -37592], [52424, 6784], [7332, -40104], [15460, 2600], [10300, 22848], [38178, -728], [-33446, 31536], [10432, 17376], [26098, 16156], [-4311, 41644], [13330, 23912], [43674, 15916], [-8828, -12872], [-27314, 4336], [-27450, -34220], [-10737, 32760], [-19546, -47252], [22876, 32232], [12276, 8364], [-38846, 49084], [71970, 13172], [-37134, -3976], [-32224, 21016], [46395, 18456], [66603, -29424], [-29206, -33488], [29538, -46696], [28519, 45600], [-45273, -17708], [-21418, 18584], [30116, 12756], [12748, -30564], [-15227, -64396], [22263, -43732], [13186, 68808], [78784, 8484], [-76080, 34996], [-74524, -11580], [27149, -18956], [-84704, 2852], [-64118, -2648], [5132, 728], [-53235, -30244], [24714, -34768], [80094, 1060], [-86485, -18284], [-3154, -12148], [-90595, -7064], [78700, -48040], [-80436, -47792], [72006, 7300], [-125478, -18692], [-13460, -75220], [40703, 1828], [-1543, -36200], [51990, 39124], [64894, 35344], [23790, -10796], [7692, 3948], [96580, 19480], [34180, 61536], [63275, 4732], [-7097, -4488], [16023, 4852], [30579, 63528], [-13620, -92488], [-151104, -4748], [43104, -49096], [33454, 8384], [-44994, -60960], [73481, 29420], [-111312, 7616], [-31322, -66428], [26005, -13172], [53314, 37152], [-25522, 24552], [97904, 26676], [-23920, 19140], [34969, -63296], [-91994, -7964], [-78019, 14788], [43822, 38264], [-20195, -81184], [32907, -46764], [82369, 50516], [133428, 27164], [-44972, -36144], [49654, 21824], [27534, -17000], [-70046, 60412], [118640, -29272], [-360, -26616], [16755, 35692], [26185, 11888], [332, 108400], [14177, -8], [114166, 32092], [-50947, 63288], [-117345, -14496], [44976, -5836], [37499, -1764], [-56317, 63208], [106378, 34312], [-180763, -18080], [25643, -50108], [29196, -51772], [50553, -55664], [-6416, -40820], [-11949, -92432], [-14682, 27952], [41700, -85184], [77338, -38540], [-61134, -78536], [27795, 11660], [-147806, -19544], [-75886, 60120], [-70039, -31532], [-70834, -13536], [14970, -39844], [-119685, -22092], [-64094, -75692], [-91107, 61548], [-18347, 93520], [-87204, 14100], [102374, -20352], [29412, -15372], [-9975, 40612], [-34728, -24440], [-161498, 13108], [-43208, -105436], [19586, 58840], [12637, -14672], [91444, 6728], [67970, -40980], [29938, 7616], [-44490, -34760], [-18966, -33356], [-35734, 65704], [131967, -1624], [12602, 105172], [64298, -93732], [-64216, -86400], [141225, -5020], [10632, -84472], [83527, 40880], [-175420, 38428], [-151175, -57532], [-49027, -17440], [-17191, -119584], [-6535, 89300], [37193, 12900], [-43905, -127052], [-112148, 13148], [191170, 21420], [121050, -62024], [-66668, -3912], [101750, 55296], [11146, -107244], [207193, 45840], [-163680, -18080], [91170, -100168], [-202900, 5816], [-17958, 78276], [154878, 23600], [-85304, 91564], [86932, 37952], [-41279, -85372], [-15282, 97736], [176350, 35284], [152452, -16400], [-145954, 46044], [121082, -49260], [-129225, -2608], [41456, -40872], [4665, -135524], [118186, 4048], [54114, -84096], [-142657, 30128], [36704, -73672], [183192, -8808], [-103210, 103396], [-171466, -78608], [36357, 78592], [-123033, -69732], [121042, -114668], [173765, -86460], [85040, -20416]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_539_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_539_4_a_e();".
// 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_539_4_a_e(:prec:=2)
    chi := MakeCharacter_539_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_539_4_a_e();".
// 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_539_4_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_539_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-2, -2, 1]>,<3,R![-47, -2, 1]>],Snew);
    return Vf;
end function;