
// Make newform 3.16.a.a in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3_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_3_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_3_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_3_a();"
function MakeCharacter_3_a()
    N := 3;
    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_3_a_Hecke(Kf)
    return MakeCharacter_3_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 := [[-234], [-2187], [280710], [-1373344], [34031052], [384022262], [1259207586], [-2499071020], [11284833672], [-48413458530], [130547265752], [-200223317554], [679141724202], [279482194892], [1520672832576], [2646053822502], [7399371294540], [-42659617819498], [-56408026065964], [-133149677299848], [105603350884922], [-55665674361880], [378077412997332], [219315065897610], [703322682162626], [560433556971702], [-2240485294244848], [-1236114081911844], [913052074727990], [-1993156301199438], [4907765948834216], [-3510360429243948], [-7537992423256854], [12847522506173660], [-18839651220736650], [-28642300378866448], [-42883367531479594], [15506703058873412], [81463700588285736], [48772298477287422], [-77636069817467580], [-12620533145836498], [-62650507810030848], [-22147419233842558], [-163105516098059274], [73428308258387600], [-1229525300353948], [-818320231607552728], [-579062267695069884], [900390530092063070], [-397436897373808518], [1067696755340726160], [-298002623370987598], [801025711780277052], [-1359521014622352894], [2743884683101578312], [-1711951597422706770], [-1670005248268070248], [-650406932144269234], [1435570276692581802], [2602176052251597932], [-1148503865216723658], [-5533682997603309244], [2574100671178139352], [-5900301218129018038], [5158061453045345886], [4148405829357694652], [3830022022387327346], [12930032087157635676], [4285560761990117750], [-12395743504543668798], [13277011825847865240], [-4374164365182893464], [22567150240613873822], [-24974733201454013380], [5615752438257656832], [21128843260217541510], [-24710537120192798074], [1371137678718784002], [-45572341835454053110], [-5381557878041017020], [-23256202934598956098], [9184317904813429152], [-41099107004192311918], [-119914204545687660640], [-88527320477249549508], [-133462612204095142350], [-10290698301353089894], [162617374780404070302], [7898718006425775512], [194988615899925992436], [-129233240116065272880], [-16597090075131378304], [-56023054851334989348], [221529216131428722500], [193755134395327560552], [-273406913620570079010], [307240154012207595402], [131068446638536468172], [-252108830827549914298], [305686923099185296676], [-609048350007916597794], [89097251829779941812], [50217993573669282330], [161396236484952331052], [865106428933656814466], [288925747120403564796], [-844009799020367616558], [87670213986564743400], [317658985510243789802], [41600399560775629256], [1142576551015298295662], [-1259832925721867575014], [-1253537892449770722820], [1474334523120921727952], [1463020085859931207602], [428205499152326029892], [319295226370130336376], [2052593431866097834302], [-654660769257863184060], [-10523433764359822498], [1384746793596397098722], [-2386168878453254888634], [722891103200437398732], [-3355003355207421192748], [-2236959604163190491298], [951303187511232817790], [-2884922648723381116320], [-591621950956797787984], [2122762146878419095782], [1370933221848933896660], [3088595282093903848392], [-3918403161164035899448], [5364236806009767379406], [2537455047186732983802], [2553397903120192720130], [1211868493116367848822], [5530513694632562786996], [9387931475406862234926], [6049978290978272031690], [-3972235193772510583348], [-1221390824688642669498], [-7212740369334820221328], [-14230510781512138036884], [3265303033630072516070], [-6614933544447443960040], [11800269413846230542302], [2984810531233803136506], [2596723285422823803740], [2377316688245571815712], [-6080942684977936889434], [402741511960143518802], [-12584719534700950567468], [-3445524292200086120904], [22361060032688462001356], [22619266849498247536752], [-2655002839004773843120], [12805649527572551902770], [-6630768429868616023654], [-3640557292994659319298], [3776811048998295063876], [-25752352812527735848998], [27912649175639352677936], [28433702802875761191852], [-23957769558809750231934], [20168820320980124428632], [6463901026074455997752], [23649134578494473428526]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3_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_3_16_a_a(:prec:=1)
    chi := MakeCharacter_3_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_3_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_3_16_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,16,sign)));
    Vf := Kernel([<2,R![234, 1]>],Snew);
    return Vf;
end function;