
// Make newform 400.6.a.w in Magma, downloaded from the LMFDB on 19 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_w();".
// 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_w();".
// 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 := [-60, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-1, 2]];
    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], [10, -1], [0, 0], [100, -2], [98, 25], [180, 16], [-745, -68], [1590, -35], [780, -6], [-1960, 40], [548, -550], [-1010, 192], [13877, -200], [-1500, 1064], [12880, -772], [13490, 376], [-5980, -980], [-12198, -1000], [-20030, 793], [43648, 500], [35145, 556], [-32740, 2510], [46290, 429], [-36405, 5220], [63070, 5472], [36702, 7200], [118160, 4524], [79950, -777], [-114970, -6720], [-3295, 5416], [179340, -1162], [155748, 2400], [26115, -17808], [30730, 12105], [243800, -18200], [76548, 5050], [550, 22152], [49370, 20409], [186120, 4068], [-113280, 11256], [-426490, -11615], [293402, -9000], [259348, -28450], [-56825, 10236], [552570, -9528], [72600, 6100], [-225002, 44175], [-475120, -39156], [28940, 24688], [-33260, -25760], [-891990, 8496], [-105520, 70480], [-226923, -8500], [216498, 40875], [-984750, 68352], [1417220, -14366], [154360, 56360], [-1631652, -31550], [-1114240, -47488], [-263298, 22800], [-1091210, 10429], [-1773150, -61464], [-1216350, -69577], [-1806852, 52950], [-924170, -73584], [57580, -77368], [1506698, -110275], [573765, -68408], [-994970, -182497], [-541650, 128600], [-553110, 85776], [910020, 78270], [2417920, 108868], [-115530, 300256], [-351990, -67865], [316740, 278754], [-461730, 257520], [3163020, -13328], [-1903023, 71100], [-49795, 61080], [-3925010, 103865], [3209652, 78800], [-3048252, -188850], [1248735, -213404], [584880, 225880], [580350, 140289], [-327875, -221500], [-3476025, 192952], [-476748, 322200], [4877320, 162484], [-1356580, 206368], [2465460, 37710], [4487660, 179958], [2559148, 260600], [-5984700, 71800], [4037360, -335276], [-7769870, 22880], [2125277, -189200], [153030, 316619], [-685448, 142600], [-9033870, -211897], [5688900, -103248], [3338820, -213516], [9993585, 270960], [6486748, -368400], [5906485, -282888], [77310, -835767], [5935225, -32464], [3076900, -450850], [-2038873, -232500], [-1702200, -580552], [4556130, -556784], [9038130, -77568], [-11451860, -57360], [-8868852, 656050], [7563402, -64800], [6625900, -729536], [15564880, 323228], [2107090, 781976], [11264270, 146395], [-5325648, -497200], [-10060330, 341456], [1768560, 861312], [-17723010, -366621], [5516598, 689425], [-6475948, -804200], [3014580, -185920], [18923340, 339090], [-5836360, 342888], [-582140, -1019904], [-214620, -734120], [994300, -432886], [5432448, -1092800], [-17179800, 550552], [-9180123, 364200], [4237985, 631860], [-4326480, -1297944], [-8756340, 283208], [18266470, -488128], [13502930, -282320], [4467548, -624400], [-12810198, 1405200], [-3275320, -1585356], [-4120610, -162487], [-1844660, 1516840], [-4864520, 965480], [-15306010, 43376], [6261875, -47648], [5153370, -137255], [-6522880, -1556216], [-10862140, 1033112], [-8701298, -2183200], [-24710110, 483279], [5749560, -206892], [10136500, 1027648], [2580848, -1099100], [-23007060, -1487810], [9181790, -1760960], [8985665, -1731508], [-17506948, 931600], [39447780, -498372], [-5921785, -275024], [-32208280, 6168], [-17289702, -366975], [-580765, 1630612], [45153540, 679554], [22003348, -975450], [-17818780, -2741128], [-32415895, -1672020], [-3902720, 382616], [21897090, -2005785], [30902202, -51400], [-7810452, 1750950], [9378785, 449396], [-25004520, -841520], [21767475, -1380900], [-8984302, -174925], [-42136698, -753000], [-25512880, -336216], [-32714790, -318040], [-8956840, 453708], [-43247402, -287575], [42329250, -1762064], [1907845, -1014628], [16062360, 719724], [573580, 1857080], [-4089370, 2147432], [31167330, -317581], [20287265, -2592360], [14343248, 4144000], [17759265, 413776], [35704270, 287309], [5640398, 1088375], [-21451898, 3954200], [47006610, 1043283], [-22944650, -410464], [34588227, 1619600], [23497430, -4213984], [65792805, 527232], [38009180, -839606], [53808090, -2285160], [-14011552, 1819100], [-2842360, 745592], [-22119525, -1053400], [30815820, 1660320], [-19150890, -2499888], [-33633740, 3602010], [8982940, 2139704], [26808270, 5441520], [34070548, 1585500], [-25703330, -1616928], [-16257598, -854600], [-54112640, -207776], [45035350, 2798123], [-31714860, 3838890], [-16991023, -6366000], [-73332060, 2069438], [-12079023, 3090300], [28610420, 2002618], [66936270, -238944], [-43013898, -2693800], [42533800, -5108700], [-45721995, 310380], [-18351420, -2843206], [96511490, -2619137], [28061390, 1991640], [-113793315, -753204], [62666080, 1518080], [14839080, -7265572], [-89358102, -1278225], [99653390, -1955224], [-111098730, 2690395], [-40177052, -442950], [-45356573, -1704100], [-18517060, -1262796], [-16723140, -6627342], [83040145, 4106020], [-41957600, -5790664], [5048250, 6508875], [-14851752, 4998300], [-94272870, -976281], [-39289102, -5782825], [-38990100, 6763214], [-36634700, 1244800], [11294565, 6105576], [423920, -7044080], [-12427180, 8016018], [49658948, 3284800], [7141210, -1750665], [-46144160, -4007896], [86492520, 739672], [17401627, -2032000], [-7373100, 470298], [10010455, 10089580], [44040280, -5764384], [117893790, 795915], [-124405248, 1918400], [67850390, 6789513], [-66559410, 1107792], [-3018125, -6138648], [70123620, -580966], [4845770, -4842407], [26915360, 3186360], [-34099300, 12424136], [63712695, 4614972], [-51238050, -7612175], [-126884570, 2136680], [-23452498, 2154400], [-48571570, -2745981], [6556310, -8226704], [148315052, 5424600], [30506730, -3750547], [-207031235, 453276], [-67700080, 6204920], [133203710, -1732288], [61481840, -5829396], [-126656490, 1336683], [-125994080, -6975080], [46082302, -3375200], [76017198, -8088375], [172450280, 93244], [106256748, 223150], [71994680, 5788528], [91195252, 7157200], [-50686130, 5767693], [210445848, 1915200], [130972770, 4205056], [32075760, -14059488], [115054360, 378360], [-123398430, -6487680], [124500602, -6958400], [-170372500, 1093648], [47716305, 270516], [-19484502, 6004275], [95226460, 3246696], [-142224450, 4642800], [-176411252, 947250], [79837320, -3000144], [-105847140, -7241640], [32059610, -10861467], [25489175, -17941264], [171489770, 2827672], [80843400, -10004600], [-133461390, -13730901], [254426298, 4938475], [195092155, -4732668], [-244213260, -2407512], [13550990, 12765240], [124872780, -11071470], [-149544910, -11725024], [141924420, -131166], [10472610, -3278640], [81678477, 10925700], [80933540, -11954196], [37590560, 5308108], [14920470, -7377280], [-55140450, 7181925], [156695448, -6528000], [76731905, 12541616], [89361315, 13159440], [-61022252, 2669400], [-33445685, 13786392], [145453852, 13660400], [-74411700, 17313614], [-118880010, -14190624], [107184402, -8184400], [121779910, 13955535], [97689060, 2272424], [293717700, 554598], [-230298220, 4224616], [60876652, -20268200], [-315689410, 4930792], [274192180, 5345430], [-403489750, 4165939], [-81039902, -2761275], [-65028580, -18287132], [-34085440, -11422440], [-265247330, 839456], [-319399573, -791100], [258262660, 9120958], [-225520800, -14759864], [-344903705, -6207928], [-10294170, -5644920], [-150651952, -2233900], [-140868190, 11096296], [25911780, -14856720], [-67511548, -4428000], [-97279770, 9842453], [208715448, -14521800], [-151253150, 13825952], [81135998, -8314525], [177715310, 9155312], [-41283298, 404800], [-46829760, -10468996], [386975770, 6552520], [-330391575, 979236], [-186707000, 1476500], [142592898, -20793675], [121202130, -1469568], [318215080, -8312956], [-19601610, 24033592], [102484327, -19454500], [-236363220, 18909378], [-55415380, -21133880], [46955570, 10009893], [-288936255, 11227156], [27127560, -1880688], [-83088440, -302576], [20794277, 3664800], [260217348, 23304000], [495847730, -264895], [138914200, 3207764], [-47431500, -1011000], [-87198352, 7516400], [-155613150, 3906952], [-969490, 31153885], [489087348, 394050], [446496925, -912264], [-193855995, -21151620], [-125235570, -19934768], [160772402, -678200], [-8914665, -10241304], [37265880, 29331728], [-72802175, -32475448], [-337096180, 151820], [46789420, 21301334], [-398437352, -255000], [228091360, -4341888], [173128540, -15454196], [97458660, -9030042], [-293962205, -1701080], [-46714900, 24755536], [-146148550, -7603925], [397537250, 7903773], [219469148, 31723450], [-141600245, 29556216], [-14452160, -32052660], [63828465, 7245840], [-112716102, -19707575], [648987402, -5413800], [-88134600, -26737600], [249324015, -7577724], [-289706280, -5018332], [-139924265, 41386112], [-48102280, 22495720], [-12932952, 34822000], [-153839330, -17462928], [-378907798, 4859200], [515377660, -1840976], [121353390, 29874015], [214920785, -1751104], [-467399760, 984192], [38220550, 21926489], [-186682252, -5533000], [525836725, 4486452], [258964702, 7021400], [-79257740, -22605490], [211062060, -16885642], [-105635205, 31828572], [499945460, -13122926], [-597163720, 13086280], [325557330, -27198368], [-229724460, 14767038], [263571530, 6222655], [288772915, -21106824], [-104642800, -14776448], [-548103580, -772916], [120184935, 2013060], [466652698, -25185075], [-452442700, -29882450]];
    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_w();".
// 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_w(: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_w();".
// 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_w( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_400_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<3,R![-141, -20, 1]>],Snew);
    return Vf;
end function;