
// Make newform 400.6.a.q in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_400_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_400_6_a_q();".
// 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_400_6_a_q();".
// 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 := [-32, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-2, 4]];
    Rf_basisdens := [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_400_a();"
function MakeCharacter_400_a()
    N := 400;
    order := 1;
    char_gens := [351, 101, 177];
    v := [1, 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_400_a_Hecke(Kf)
    return MakeCharacter_400_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 := [[0, 0], [-6, -1], [0, 0], [26, -3], [-280, -6], [-694, -12], [-74, 84], [500, -36], [-1226, 123], [670, 312], [1124, -54], [2970, -216], [11538, -156], [8842, 339], [-1454, 885], [2706, 540], [-31292, 504], [7054, -1104], [-42706, -543], [-23604, 546], [33726, -1308], [32952, -2508], [54362, 711], [-27510, -1464], [-73834, 4620], [51366, -4440], [-65474, -1305], [79230, 405], [13934, -1080], [-137538, -4296], [-167886, 2877], [-52920, 102], [-154666, 4704], [-36588, 11304], [-74986, 10104], [396652, 2586], [-215038, -6456], [43186, 19707], [107730, -13383], [-10574, -7608], [-172868, 9312], [8966, -25104], [-203924, -2706], [-565210, -13116], [-148030, -5892], [11800, 7968], [181480, -9954], [-504106, -17625], [-915282, -4911], [629014, -32160], [468270, -6804], [1086888, 19548], [812570, -13452], [-109704, 27354], [1011150, -33240], [1166886, 6939], [-1102786, -14400], [-253764, 42270], [-48086, -11736], [-377006, -2676], [-1505206, -25413], [-176854, 3792], [792670, -60063], [-132188, -108426], [816502, -13464], [2324922, 46020], [-853008, -129810], [-747490, -58056], [2295662, 2421], [901326, 33936], [-1834034, 5304], [2354136, 66192], [-2343910, -36855], [943466, 169464], [-2587060, 113580], [1578390, -100041], [2722518, -122208], [-1311094, 17748], [1559442, -91296], [2117842, 154092], [-1631364, 40632], [-2799258, -49224], [-1048628, -148458], [4041766, -996], [-487384, 259968], [-1990014, -230025], [-1475958, 56004], [4414838, -42192], [-896658, 321480], [-1912938, -250161], [726230, -233667], [126056, -226188], [-2153246, 2049], [-5953176, 22890], [-9136364, -22692], [4722558, -148929], [-894194, 242208], [863530, -293760], [-980486, -125829], [4366558, 84600], [-3202402, 341001], [-5625422, -281496], [-5540582, 404919], [-3069550, 11772], [-4328272, 489222], [2655262, -61680], [-5765130, 285777], [-4761714, 168528], [1938432, 475308], [-7688846, 35964], [-3513054, -15771], [-409326, 123156], [2196078, 38604], [-2580980, -261564], [3056492, -510582], [-932694, 208788], [-2045310, 708627], [717378, 10785], [-4690214, 224700], [14536284, 207072], [-9311994, -490992], [9709926, -25932], [8290130, 204948], [-9175398, 26811], [2718400, 325674], [-10069186, -418392], [11308070, 542232], [-9238072, -671844], [-9914086, -334827], [-3190062, -867792], [-18651684, 240072], [-13676658, 518703], [14853060, 103266], [-7460294, -563904], [-12292902, 252864], [-305774, -1078488], [-9768126, -572004], [7263542, 369165], [18748362, 170460], [-1946550, -906432], [-2048408, 108234], [-3190538, 856920], [-14154746, 595443], [-13433538, -264435], [34042894, -5904], [-3500912, -147252], [26031538, 33180], [-5365962, -174216], [-18529676, -826560], [3257790, -371733], [6742850, -48816], [-2831670, -129300], [20219498, 257247], [29830666, 117021], [-18213690, -570567], [-31315564, -198534], [22033168, 415476], [-15899798, 723396], [-20110114, 16644], [-13538690, -337536], [13675294, 1424001], [-31621050, -705744], [-27708822, 208677], [-29794592, 333318], [44622742, -341820], [-46413786, 48435], [6065236, 1355586], [14705298, 1198260], [9592994, -1217640], [2903266, 953508], [3535468, 955452], [25578574, 1685856], [-52880300, 566934], [29966158, -515988], [5256256, -545880], [15365994, -1067880], [47578112, -964674], [-9362410, 1130592], [29515750, -641109], [-24987170, -1136256], [-5017942, 1994505], [10936680, -1265898], [7601746, 2763396], [-35188834, 713748], [-963346, -587925], [500630, -1067160], [-33732926, 1508712], [28088450, -131421], [-50469118, 177276], [-5363596, -215454], [-12953858, -3207840], [-20730606, -1405065], [-17785856, -3264198], [-50143570, -767856], [-8501642, 1784805], [15708862, -1813644], [16273770, 2295900], [-17621766, 2397732], [25316254, -2091408], [-32717986, -582009], [-798930, 368328], [-45239764, -1672362], [-34809758, 2635380], [-39146526, -653376], [-37231068, 2587584], [55835314, -1984848], [-34740256, 296688], [-47856158, -2736957], [3152826, 1349544], [-66528704, -1947114], [-26255194, -1612308], [2666966, -750504], [28499366, -2218797], [-69418506, 593769], [12448784, 2925756], [-15466734, 2011812], [-25675886, 1774293], [88005842, -1780512], [21804306, -3068415], [-3200182, 1518492], [-3302698, -3130896], [54710528, 388092], [-34876718, -942072], [32853246, -147405], [-28735962, -2101851], [-79019354, 2641824], [-6941474, -3675492], [82710544, -1166280], [-72482710, -841419], [15404080, -2438850], [4518266, -1320132], [35654292, -1860612], [-43699604, -2020626], [-94997630, 1492332], [-12964254, -907617], [-41313294, -5247243], [13901370, -2256780], [-44131902, 816612], [-59200420, -556860], [-29983740, 3623358], [-37009094, -1656153], [-59622568, 2044554], [77960590, 1367199], [-69598658, -2133168], [-69847394, -1875624], [57467800, -1174584], [105478498, -3211035], [-110839712, -592206], [63211220, 1184280], [-24965146, 2048487], [-7178118, -5237820], [-31241726, 3968736], [25349154, 5200881], [-11550526, -2286996], [-100906142, 155664], [-28667388, -1436892], [-35865130, -1477080], [-18660594, 3197829], [-123248070, -2448456], [33032934, -2665440], [8230462, 2455707], [-29115250, -3573327], [5459574, 734208], [92430034, 2706288], [45693334, 1661004], [2124356, -4907988], [115723198, 2823552], [-35161158, -5871792], [-67782654, 4949175], [133965914, 2244120], [-59986690, 2013936], [-150918986, -1731987], [-101206818, -832740], [67560832, 4798704], [12767086, 4691376], [52474382, 1268967], [-19922426, -7157727], [19486718, 1585272], [29829218, -5350212], [-80965608, 1000566], [181835934, 1512891], [63692244, 9386406], [-12735286, -2386467], [-36651338, 4804512], [-43128938, 5741793], [117776564, 3838314], [-33089258, 6151140], [80263746, 678660], [-119923968, 4593708], [1473066, -5151996], [74871502, 8154552], [-23283882, 5038509], [143783166, 5856540], [-38186280, -1321134], [-154763806, -2069136], [-171002082, -2906136], [-166393724, -1364838], [63057018, -5314656], [25117868, 11431548], [85420902, 368685], [12751446, -5956800], [204084018, 30936], [-137186648, 1681164], [216147362, -2162757], [-101533368, 4435410], [101444390, -7910412], [-118842810, 7416777], [331335598, -549192], [13527480, 5794824], [93277978, -659880], [22887190, -3944529], [24003942, 4188624], [261057282, 4258032], [-267323270, -1946577], [57692018, 2063241], [83416762, -5903208], [130686116, -2833500], [41559492, 145866], [28931342, -5084952], [-173717990, 1070436], [124870576, -3279150], [-284937818, -50544], [-152631762, -4611792], [-65260490, -6693993], [102739046, 7824696], [4111482, 14047716], [122090132, 3538788], [-158249710, -2063505], [-151137502, 440517], [11633586, -2057052], [223689614, -6460680], [30876530, -6443088], [-353207512, -1287612], [-91691702, -4903449], [122133896, -717270], [70378254, -4131003], [64701566, 6397944], [52548318, 12004608], [-121480094, 5559564], [70965066, 8455809], [73248634, 4504704], [-148230618, 4559808], [-3094394, 3931992], [-287738228, 1823778], [-16629886, 11661096], [-178097404, 10608972], [-407813354, -1106448], [348220990, 1147029], [308539428, -8125326], [-190397358, 5020956], [109865912, 2752542], [37620390, 15762408], [260713150, 1280160], [164363438, -4090101], [-269626810, -2737056], [-91200274, -5882844], [304810584, 638412], [41995136, -19943322], [132083510, 8501796], [118194638, 8602503], [214441138, 1301268], [260893546, 7367136], [238531178, -11057727], [245486244, -13021032], [-117929274, -5533395], [-85436962, -6137724], [-124612326, 2661900], [-169947178, 18780147], [-341179486, 1553076], [195951960, 17055102], [15601612, -26773212], [190727990, 487119], [-23687754, -6882792], [71210700, -17587398], [7976122, 21008964], [-245105388, -8730708], [-84582092, 13835538], [406669342, -8091744], [449329434, 3496308], [-263549258, 2350056], [422785166, 2921184], [-301854298, -16115832], [9152346, -18503883], [-446616354, -855264], [221588004, 2429628], [404913910, 4751667], [-553675348, -1809690], [447013194, -11100984], [-157596942, 632751], [-217644014, -14452947], [-198950774, 12531684], [-384382758, 4807512], [-170529660, 10209216], [429896086, 13192125], [-50861060, 781890], [-86995706, -9318432], [-461053736, -4569492], [-199743990, 8510688], [-330082376, -6543918], [119232582, 4975968], [95661918, -12394632], [61811094, -2893284], [-137650982, -10390215], [241720518, 11444448], [99851478, 18818928], [-201062964, 26473842], [-103924582, -17876340], [367226898, -327504], [-180196550, 4912551], [75351564, 9327120], [-234221122, 9382632], [-5312694, 7893912], [403987274, 6130107], [141447560, 12734550], [-204915106, -6685788], [-252749106, 13865736], [268348736, -8622720], [-209933438, -5548767], [-99356442, -2520372], [-36329826, 28458783], [194299182, -4489488], [82301042, 19598340], [645042562, -6632115], [-557922988, -13059840], [-322024650, -2087376], [251756754, 22056072], [257519778, 7212123], [-20701518, 15372636], [-7259120, -15332058], [155158408, 16041264]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_400_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_400_6_a_q();".
// 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_400_6_a_q(:prec:=2)
    chi := MakeCharacter_400_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(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_400_6_a_q();".
// 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_400_6_a_q( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_400_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<3,R![-480, 12, 1]>],Snew);
    return Vf;
end function;