
// Make newform 3.16.a.b in Magma, downloaded from the LMFDB on 17 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_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_3_16_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
        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 := [[-72], [2187], [-221490], [-2149000], [37169316], [-279974266], [2492912754], [-4669782244], [-18467933400], [-115953449418], [-56187023200], [614764926830], [549859792410], [-982884444028], [2076144322896], [-12048378188130], [23087905758324], [-8505809142442], [-12331010771476], [58989192692472], [-5609828808070], [159918683826800], [57675894342876], [-362287610413974], [-539786645144926], [-422221178347794], [-145688745672808], [-408695540692284], [1101336449315366], [-2130233234977134], [-6259615459994176], [10105943855780268], [-9937386711103302], [-19507824035757916], [15893535235551678], [31059679780457192], [-54137154349185802], [-55461201814975732], [-5835518490234600], [44095788247355046], [-51059364411094020], [31614318157549790], [94429139943986496], [41774350542859586], [-148382887404728178], [197555277031230008], [-448570375765990180], [159976555031971424], [145802883930618060], [681194299472247854], [-389035242122156838], [67622720071243536], [-900528136468490734], [1589287890480009012], [2297378233448929026], [-505657173596256072], [-2447306655748733754], [-1538067272763918544], [-789959230592268418], [-615302697885479766], [-1262791502664103276], [-3498638452354252242], [1288605101654371196], [8430299982091785096], [7883590525242514058], [-8715390014965547754], [12699475298841480932], [-6642495975233647438], [1878345205444740564], [-14854522384172753098], [-8233804259497859166], [18741679085116355544], [8631166293762742352], [-9959748640666138978], [10094912534452874420], [-27176435474292461952], [-9052846076114191026], [-17649549576643665274], [28113532973569144818], [64295485994942788394], [-23447367597680173044], [-72822684882457138066], [-47183114356672388400], [37909864608426436178], [-22683197539783117240], [-99488830803349432716], [130158716788780040898], [-101909565119822477830], [-108141624880502630778], [169113182134034913392], [-45841790233003252644], [131512559101569540000], [61367054721809486744], [242056655505032684868], [146048777730768465596], [-168041101448914553016], [73770901329620743254], [322023389229689994810], [-11906438434832304988], [159032862912629245430], [-425405003091702113140], [271579107027983105574], [-268438020460265638404], [-134893481895995762934], [-681152988763547508364], [-242927194834338988606], [-106241037801969867804], [-770415330917081775438], [-75979555563455251992], [778002901832621528426], [321866863362683685344], [668866839020097172526], [-847904041901539441062], [-351027265883470736956], [-1307182814190830167288], [-1779752332105531922430], [-51826889078644584148], [1802684617653769664184], [601117375413388032198], [-1344920855032227778020], [300300755667143304062], [-2482520705609071596190], [-1840000480224181963218], [2191435379742904116228], [-525386955561768485764], [-3782273206208863287402], [-4246469902884102806194], [-317360003841578766672], [1251124222854917396648], [4269932606402497031222], [1733018098259810146124], [-2637392592178860942888], [2270780863225341131216], [-1729593775012451254882], [-206739307377029676726], [3014896872258279403778], [8993573445851655191118], [-5928074382697292701924], [-3838366691486956153674], [-6179484613496049460710], [1878617849653764389252], [3932268132964473965214], [-7929267104294759032504], [-1970382647944955013516], [2439296401238354534870], [-13531760328063565580040], [-7300380742815491712322], [11647548526020459616746], [-10547266915019125915180], [12551434448806603131552], [-15729434024599653568090], [18041243903277819739794], [24186595800427801953212], [7053924937177774969416], [24849773221164628033316], [-2838476812290294806928], [-18776233023497018050840], [-2617287766783916273694], [12436616676467017433882], [17126622755410193418150], [1260351475004993827068], [-21988685539240808634486], [1211298094334384529464], [12682461590851990463460], [-3293667221387184381006], [37416643572958489895400], [-44030538212918474533408], [26206656072475624653998]];
    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_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_3_16_a_b(: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_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_3_16_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,16,sign)));
    Vf := Kernel([<2,R![72, 1]>],Snew);
    return Vf;
end function;