
// Make newform 2.16.a.a in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2_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_2_16_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_2_16_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_2_a();"
function MakeCharacter_2_a()
    N := 2;
    order := 1;
    char_gens := [];
    v := [];
    // 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_2_a_Hecke(Kf)
    return MakeCharacter_2_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 := 16;
    raw_aps := [[-128], [6252], [90510], [56], [-95889948], [-59782138], [-1355814414], [3783593180], [-11608845528], [-28959105930], [253685353952], [817641294446], [-682333284198], [366945604292], [695741581776], [12993372468702], [9209035340340], [-42338641200298], [30029787950636], [115328696975352], [43787346432122], [79603813043120], [-3417068864868], [-377306179184790], [-166982186657374], [60680236307502], [587676402004952], [656968133121156], [-2090227783678810], [-1632955081904238], [6513337595398016], [-11263608694217748], [5103277652953146], [2766818784713060], [26846924611329150], [-36547941539093848], [29968513790601206], [-42949655950023988], [7100640246563736], [-56930880794733978], [-2656549234371780], [51764838979787102], [53675288757279552], [114449562301688642], [-207129734322721074], [-35983759623512200], [90269907274561052], [-322894137103599328], [784855840799205516], [-784091561182107730], [-717047533944033318], [693110354447654160], [965565808917411602], [-846640700229572748], [1401559966131752706], [1388044612099161912], [-2854722622192901370], [-1761582601077887248], [396897243417223166], [336756164587013802], [-4565289799382321068], [851075979146367342], [3969990685049876156], [3186658737303622152], [1598656477025729162], [-4922618687492902314], [-6428968905776435548], [14491233801832096946], [21149973039491539476], [-10478334124056258250], [13944183624623216802], [-10302717043689278760], [-11790047285366852464], [-33255145150314778978], [-6272369177076237580], [32791907326704162432], [26907402185918135310], [6983264957016571526], [12257125471824703602], [41328507748505589290], [-65831242648134829620], [-6815718740136590098], [-24344421555688828848], [37320935465926720082], [88815328885211578760], [-32446267771575487308], [140615166466843114050], [-110893306450658169094], [29612440593010015302], [-133020853136210791888], [-7073926250320974564], [13096370593308018720], [88223174902963614296], [-232523484978530205948], [191679747549886408700], [264371805300770023752], [135341978803894506390], [116387056808493897402], [237253140768304358372], [-201502494205089813898], [-212933722628900575924], [273615733273571378406], [-223410733057050565188], [-524953154866077681270], [-154330587745396275148], [719412993597562352066], [-431990037395095194204], [-419124315111945415758], [318808163037472852200], [-985640986046284507798], [1082967215047827402656], [1474879968902539772462], [-186047580614032187814], [544498671360295182980], [-134412385356022376248], [-215011191928405903998], [-1367048309326701260308], [114998262473166478776], [-1233911394764611979898], [1630818763318769448540], [-1387400997879304820098], [-1339365665163582571678], [1994084993241420893166], [-1251802004294577191868], [-1464310331335620951748], [-297987165417885862698], [1218811167980636150990], [-3018556524927520743120], [897709687558340647016], [-852856783886536991818], [2191956683366898194060], [1899287834890543771992], [-4401280949941167736048], [3326002274080363141406], [6663618118618587085002], [1777254644139518262530], [-4171454431854670481778], [4968355486347358187996], [4550556629628882583926], [11203979375955922171290], [1564306061231781619652], [5315337445585456827102], [2483800092476862910472], [-6014203217716988743884], [-4336525918553531989930], [7922236969592446631160], [4779756045324840685502], [-13850590188241015167894], [11338903675489670737940], [13450603597907554238112], [8712642599626390432166], [18428777183204026694802], [6386963409192241412732], [-12676116339862959720504], [1296697253709090336356], [22628014577804573066352], [-4370465969452095455320], [-24342274882313732208030], [16477551313221255640346], [-20938348648574896646298], [-33603580255297160054724], [34529196504570780833802], [1345370761206985481336], [1503340877222658441252], [-43613260394881822774734], [3219859061745904400232], [-48501045826223818529248], [53554291372107703319726]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2_16_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_2_16_a_a(:prec:=1)
    chi := MakeCharacter_2_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 16));
    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_2_16_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_2_16_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,16,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;