
// Make newform 87.6.a.a in Magma, downloaded from the LMFDB on 03 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_87_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_87_6_a_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_87_6_a_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 := [160, 14, -31, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-26, -17, 1, 1], [20, 29, -1, -1], [-70, 23, 5, -1]];
    Rf_basisdens := [1, 6, 6, 6];
    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_87_a();"
function MakeCharacter_87_a()
    N := 87;
    order := 1;
    char_gens := [59, 31];
    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_87_a_Hecke(Kf)
    return MakeCharacter_87_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 := 6;
    raw_aps := [[-1, -1, 0, 0], [9, 0, 0, 0], [-36, 0, -5, 3], [0, 7, 3, -18], [-223, 6, -15, 11], [-244, 45, 82, 13], [-729, -58, -142, 114], [466, 178, 225, -25], [-1303, -291, 17, 52], [841, 0, 0, 0], [-2899, -375, -275, 308], [-1040, 478, -377, -349], [-5003, -75, 618, -165], [-3917, -727, -124, -537], [2260, 739, -979, 614], [-12427, -1825, 1667, -224], [-15669, -2729, -3796, 3871], [-747, -5107, -2225, -310], [-6219, -4364, -1521, -2469], [5943, 4047, 3619, 1936], [37325, 3017, -2113, 634], [14541, -4229, -961, 4864], [-37544, -3158, -7708, -4374], [-6844, -349, 13348, -5243], [8455, 913, 15487, -12334], [21534, 19667, -314, -8545], [-83769, 7437, -834, -6653], [-45404, 17184, -499, -7805], [45228, 5887, -11266, 6459], [-23395, 11512, -2282, 14136], [-125024, -1626, -9594, 20176], [-174237, 15336, 13717, 2069], [-87116, 19498, 534, 4424], [101721, 5370, 25717, -16905], [102053, -12405, -15034, 9343], [48429, 28455, 30410, 20033], [-67713, -31625, -35398, 3593], [50691, 7591, 25828, -31743], [78900, -42568, 17790, -23646], [96099, -48191, -15332, 5503], [265353, 27601, -58467, 1472], [-195850, 34695, 28372, -37651], [393343, 15865, 5928, -40913], [-194942, 101892, 51590, -1834], [204970, -32898, 39005, 42759], [-97245, -7490, -33207, -44689], [-137895, -24183, -40980, 41303], [480231, -62894, -299, 74887], [288505, -43231, 58186, 27195], [128240, 46120, 48013, -68125], [-278346, 700, -47922, -90554], [-26839, 34987, -84565, 97600], [75531, -102836, -19370, 100194], [-1106945, 61884, 84377, -15979], [-16822, 91994, -98374, -24156], [-262943, 7507, -53608, 73469], [225552, -30211, 140512, -46169], [207928, 32942, 5508, 186258], [-279548, -69897, 84944, -44423], [-857280, -183754, -229118, 131272], [1259236, 6072, 47858, -98174], [404144, 187527, 114652, -188255], [1831759, 41215, -110761, 23100], [976464, 189687, 153065, -173738], [-552597, -65190, -86780, 18602], [-525656, -181173, 117922, 21291], [817326, -88054, -7961, 183141], [-20163, 208003, -24073, 31324], [847468, -275642, -49293, 246983], [416696, 192870, 19642, 256016], [-727797, 20337, 182513, -170124], [1769731, -337399, -152090, -133675], [908684, 101652, -68926, -24822], [-1963930, 71936, -81014, -229618], [-491915, 220299, -203151, -155790], [-922685, -297267, 180817, 155112], [1810884, 309195, 259018, 1107], [-778774, -166056, 253680, -129316], [-2819139, -248929, -350777, 264432], [-2312910, -117748, 4506, 266578], [-1976108, -620896, -508417, 271321], [-1944102, -340212, 29548, 237268], [-328752, 537300, -11452, -373160], [-1591829, 181261, -110527, 38880], [1065094, 341519, 494175, 427238], [1464701, 596832, 755247, -325433], [-962807, -176196, 714016, -647358], [-719131, -940128, -469042, 166662], [-1017216, -830410, -721420, 200830], [-2119129, 453594, 109429, 6831], [-1488316, -328200, 22420, -154228], [2563782, -429478, -457642, 116804], [773043, -78759, 264374, -82329], [4002262, 558202, 454808, -79966], [-522199, -606874, -631871, 234091], [-2384366, -157651, -465743, 295984], [3007331, -13, -479288, -671], [-6411604, 180924, 307654, -181054], [-4717471, -153022, -499779, 582851], [-194288, 345004, 627275, -642087], [-1788613, 230012, -114497, 941283], [-2986661, 281307, -747658, 41383], [-2219091, -1007340, -1022143, -92127], [-1775441, -356808, -721928, -723166], [-320056, 125884, -534290, 532758], [1004656, -188440, -993212, 843572], [655977, 470561, 847416, 132461], [-1664017, 1516013, 432203, 197434], [-3457183, -410774, -717257, -506895], [-9913799, -147589, 90309, -92538], [-4837282, -592520, -569926, -571606], [-8567190, 1043671, 19454, -344365], [-4154739, 388441, -1008480, 971121], [359854, 517542, -340385, 639905], [9086122, 1097315, 224839, -710326], [-6015133, 259564, -818008, 1244806], [5042625, 789684, 909459, 684927], [790891, 200759, 177425, 770690], [157756, 920371, 875376, 665173], [-566009, 1640618, 2409065, 112211], [3593490, 919431, 622288, -41851], [-6000623, -627944, 317242, -1037006], [13678910, 292983, -184014, 2811], [-4594439, 715719, 22314, 219501], [11093003, -2190146, -101039, -901837], [2033288, 6974, -260173, 1011669], [5470750, -969180, -1088836, -102740], [-2713954, 2023390, 1626906, -597788], [1259877, 276713, -371501, -189222], [1926780, -809708, -1115266, -246690], [-1014633, -3067253, -1371773, 1195948], [6620378, -892903, -145787, 531320], [261890, -1575418, 1494420, -105318], [3118349, 1698131, 1246106, -435287], [4458663, 2257813, 1476891, -1957542], [-4494377, -2782521, -1726179, 546518], [38548, 2571358, 262690, 417668], [-14479874, 1313322, 234930, -898576], [13151864, -155998, 1025164, -254058], [3587934, -544177, 429862, -1561869], [-5000617, 2300822, 2578917, 798459], [-7154036, -3452254, -3426470, 585112], [2528354, 125958, -2083408, 1294510], [-2269536, 1328256, -419920, -369368], [-1406559, -2453253, 613580, -668705], [-786638, -2850331, -2687717, 217074], [-12262269, 790881, -1968756, 31005], [6664569, -745649, 2681131, -2952176], [6968287, -2730509, -1425568, 898025], [-1289159, 1780605, 2179657, 154372], [-23788806, 1635880, 1946910, -120122], [3122898, 219853, 368286, -682003], [-13240126, 689190, 2027862, 1342248], [-247905, -1774990, 462957, 943263], [-2864004, 2694498, 1845092, -1920310], [-9733081, 1165308, 1101857, -1303351], [6214760, 1075409, 2409967, -1949416], [8747808, -2598086, -3982590, 136308], [-3463379, 5057710, -727330, -1050800], [-19918882, -729222, 2608584, 717014], [-5304997, 3803100, 2500321, -256075], [-7549884, 263728, -1371508, 432784], [-614031, 4167367, 1341585, -1818838], [-5039280, -2076408, 2871210, -3650878], [-5435723, 501741, -2452603, -1799084], [-4552080, -2101464, -1557816, 435248], [5295044, -740861, -3134727, 1305300], [3416827, 2063017, 3161924, -3081323]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_87_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_87_6_a_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_87_6_a_a(:prec:=4)
    chi := MakeCharacter_87_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    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_87_6_a_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_87_6_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_87_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<2,R![520, 72, -84, 3, 1]>],Snew);
    return Vf;
end function;