
// Make newform 27.10.a.b in Magma, downloaded from the LMFDB on 10 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_27_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_27_10_a_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_27_10_a_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 := [136, -118, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [-1, 3, 0], [-713, 6, 9]];
    Rf_basisdens := [1, 1, 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_27_a();"
function MakeCharacter_27_a()
    N := 27;
    order := 1;
    char_gens := [2];
    v := [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_27_a_Hecke(Kf)
    return MakeCharacter_27_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]];
    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 := 10;
    raw_aps := [[-1, -1, 0], [0, 0, 0], [661, 7, 3], [-1231, -233, -5], [5621, -124, -84], [38972, -2360, 208], [338016, -8472, -96], [-5074, 23682, 66], [975706, 44248, -2208], [1922930, 44306, 3762], [-2191741, -72215, -8483], [-3895342, 303456, 9624], [-7404506, 420274, 16434], [15128138, 1100902, -30674], [4130678, -60952, 44328], [26859879, 1534761, -42939], [81342220, -2420336, -85632], [123243320, -2098280, -32696], [-84204862, -235562, -227522], [134397696, 3010488, -188880], [-135542239, 2361348, 194220], [88483952, -4693196, -673892], [40541957, -3456100, 296820], [125968002, -20852538, 927582], [-146302513, -7785896, 606616], [-39268573, -26008411, -2489199], [-217416436, 10093168, -1131368], [977633865, -14505480, 680760], [802635266, 31771128, -1759488], [1583646502, 34854226, 599250], [2135002727, 12626697, -2374683], [-2162680319, 22898992, -5829312], [-517690486, -48795778, 7642854], [1259596532, -13448924, 9765364], [-2542302755, 85586635, 10650735], [753864311, 105324085, 6096937], [-3832198468, 180483376, -6733280], [-712987972, -150097164, -7874196], [-4590781286, 207563056, -3665616], [5204019641, 42254939, 6555255], [1808450391, -235325496, 18661512], [-1263111136, -405534984, 9466824], [-2043028828, 459084608, -5838696], [-5314415011, -100773380, 36123076], [142351215, -464336259, -32626767], [3942992711, -184796727, 10233381], [-16717017994, 182512786, -6351950], [-2666790880, -292572260, 22314820], [-28967781532, 404752232, -43249368], [-23579707570, -1153999424, -41149520], [30206573742, 1237004754, -10099734], [-25436262098, 166188496, -49403640], [-21457082698, 27939184, 81700480], [17515152816, -2561834184, -6148344], [85960626580, -1332954620, -15559236], [44959474370, -939578872, 50045376], [46707848874, 54034650, -138116214], [120370809377, 524393163, 80083863], [-71333616184, 1185744592, -36594752], [90097004696, 2062148600, -8435856], [15475368098, -4353833090, 124693822], [83413761118, 4197191782, 18499974], [79808782736, 4353956856, 21216096], [-171697645142, -4019212016, -5684040], [107930714933, 877544500, 37963132], [7656591683, -7073729275, -90433167], [36110815562, 3771060022, -254408306], [10767501002, 561021280, 17955568], [-20251019189, 7772592028, 425257620], [35556804614, -371243216, -269301608], [255443241050, 62811134, -180778482], [-258287916558, -5432923680, 90065328], [233927071319, -1626469883, -335480855], [23530362608, 7116458944, 394472680], [-71345752612, -5960376564, -209315508], [-103031973896, 12696523216, 55926696], [-144875998075, -5543032453, 520393839], [-229027404796, 13801595664, 366076344], [58189987780, 13298360980, -553749420], [-71794716649, -3280699664, 507236944], [-545765165540, 10136019880, 552759480], [93479997656, -31372604456, -197893352], [-299036408670, -10221348312, -1922277384], [-135743695747, -7770934752, -2224236912], [-414225239245, 13355475037, 1087887961], [808964078468, -7565700184, -160948488], [321876892578, 21980727294, 1158828510], [-749818205809, -8252969240, -280799576], [1295098240805, 9163322459, 363822543], [327775041893, -19434888737, 1399790443], [316973930397, 44207347620, -1403114916], [-278757880522, 25150674344, 1066136520], [-140796805336, -29709287700, 107183316], [968585786455, 2196638368, 1741318896], [1582295332178, 41518646878, -1217432810], [1336310238846, -9034629168, 2338922280], [-1494432834041, -41579007899, 1364011593], [-607489778820, -25049414724, 1563974772], [-1270887456112, 30768017112, -3113401824], [-76390809100, 76573788672, 3182898576], [93112115528, -57921199688, -2237138864], [-905894553147, 7752032115, -1974499593], [1000697998939, -97279074572, -2472946788], [-1061318854468, -35525634148, 3401816652], [-724425747388, 16221630340, -1356796868], [-250806841882, -103704692112, -1817502912], [675399754829, -19958097280, -6490386960], [-1513386401490, 27989154738, -4936301622], [-2095545200234, 9929670928, -4471517760], [765867197531, 3613187404, 2655224068], [2781270329384, -34800913676, -926602676], [-651464445490, -70576066008, 4754115864], [-1785478343366, -77571641354, 3383961702], [-3564018265732, -66293266820, 3726449908], [1259552294921, -42039900321, -7986356349], [-2969841025114, 169142053802, 6505080618], [1268886311300, 5104619740, -612151700], [525553606368, 112883874336, 6714771120], [42858021793, 100881855187, -6242481681], [-657377099077, -166728893200, -361922448], [780573283022, -4093810520, 2763641536], [-1920466333267, -142950076544, 7063452064], [-36110475010, -160743950866, 6883959438], [4236266261292, -141940086072, 1547039832], [449330213396, 81071232556, 12100727932], [3977091651231, -231694452459, 2219766921], [-714313861828, -125469230552, -1802295752], [3867592217844, 16491110856, 2565053688], [-2914228848319, 208742269423, -1185112685], [-9200647321702, -7078845824, -6652302296], [3960155859104, 26703054192, 9050051928], [4683223863100, -13967409104, -8825495688], [3843514995599, 153504583861, 13747703929], [519206000546, 299416510800, -10663945056], [-8143613123216, -141748036976, -2085314496], [-4605211190167, -96922012268, 2505161548], [92189393130, 308269516794, 20659056330], [1985209586336, -24569239808, 20510492608], [-1027828377071, -240668631845, -18742375593], [-4864273779618, 539474870250, 8441246898], [-5550549406162, -133239037614, -21223283838], [9458332681010, 452449914050, -4064821230], [585954045047, -228669429539, -26850827375], [-2524683586848, 236398526448, -21210132912], [5641806562418, 153232668120, 20341290936], [6889661849096, 210976622000, -6289869096], [1095049566758, -493356848552, -1158703592], [-5899523957152, -153486279184, 7617101448], [10859051522216, -317000392952, -7128513128], [2055452929164, 265077725400, -18295005816], [-15128080710838, 402913081600, 16753555648], [472700970516, -590360067444, 11082250596], [2035654195790, -436615023102, -16954691694], [-18974419996196, 71233425616, -32837879928], [-24731194126426, -33814507406, -23076049646], [-8927498868442, 529897536176, 31608252624], [1093342925531, -101513760327, 16435960749], [-7736656902052, -333568603348, 16477770876], [9783379161371, -44674721016, -37943613528], [-4835226176111, -1000976810513, 21190383555], [-4959674763373, 17316997496, -39809671416], [-15560731506972, -136266235476, 790412460], [7646433309425, 1352215860715, -20104056905], [-22515695157123, 564145849044, 32716363356], [-15668615218118, 378474157726, -27820984650], [-23356798711402, 29528062328, 35379825720], [6783198586913, 418369415751, 39209610123], [46694832000788, -137565525632, -1483169672]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_27_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_27_10_a_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_27_10_a_b(:prec:=3)
    chi := MakeCharacter_27_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 10));
    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_27_10_a_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_27_10_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_27_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,10,sign)));
    Vf := Kernel([<2,R![-3672, -1062, 3, 1]>],Snew);
    return Vf;
end function;