
// Make newform 847.6.a.a in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_847_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_847_6_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_847_6_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_847_a();"
function MakeCharacter_847_a()
    N := 847;
    order := 1;
    char_gens := [122, 365];
    v := [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_847_a_Hecke(Kf)
    return MakeCharacter_847_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 := [[2], [-6], [-74], [49], [0], [-364], [-148], [1320], [-436], [-2970], [8842], [138], [-532], [20676], [-11722], [5274], [-27670], [-19512], [64088], [-3708], [24296], [2200], [-74424], [34170], [151718], [-116852], [103694], [97092], [-52930], [-80526], [-221048], [-37572], [-290602], [-367360], [462730], [7648], [-161482], [179464], [-316848], [175116], [-69780], [-78638], [-927208], [-877474], [744602], [1079310], [-728772], [38374], [-323268], [813690], [-1108014], [1313520], [1056068], [1689142], [641938], [1101496], [-2151470], [1083268], [2223722], [153018], [-715324], [-347424], [-2640428], [-947778], [-248686], [2609038], [152332], [-206558], [-2018068], [580440], [572034], [-4565440], [924358], [-4920214], [3975400], [-982846], [-744090], [5730238], [4267562], [5817720], [-4388230], [-1484558], [206448], [-5678666], [4436660], [6173284], [-4931050], [4150302], [4403448], [6827284], [6882438], [-5024300], [-4516012], [-5557372], [-3497440], [-4612804], [7416090], [9759702], [-1660844], [-6972502], [-3085028], [3290522], [-5457544], [-7285710], [-10712852], [4220238], [-6241802], [-493664], [-8288900], [-80612], [-914168], [-9973274], [-4606362], [2517110], [-2021528], [15587022], [-5887546], [5589578], [17622694], [9755660], [-9795218], [4727266], [19398852], [-6220036], [6041402], [-3970682], [5159390], [-12673350], [13278258], [-3840504], [-11939420], [-25363144], [14390332], [-18543082], [-6431232], [-14824880], [19632474], [9486392], [33232598], [27579150], [7761068], [16057778], [1046664], [-8917988], [-25382090], [-31063550], [-22433684], [-4764488], [468030], [-12048696], [-3617178], [-22702498], [24192604], [40612492], [-19823472], [-22120908], [-44294920], [11316630], [-33757808], [33003648], [19259868], [-11850274], [14419592], [-13749418], [-9188022], [-34373226], [-43217948], [-25479308], [-42605810], [-7268286], [4971460], [-20287778], [-41814512], [13453906], [-22934050], [-4239970], [-69090212], [-35956918], [36434356], [-62479420], [9957848], [-56690312], [-15531066], [-26624748], [-62368296], [59672250], [-59542878], [-54466026], [32789290], [5315848], [-28516786], [37066916], [-32284968], [-28206378], [-86149668], [21107514], [12343258], [-40104206], [-22086288], [13987516], [-24448350], [44962848], [24067578], [-13204360], [66652670], [-71202242], [70471950], [-71231004], [-7606650], [8089642], [88264962], [-107470398], [-55743336], [-6525782], [92428860], [-1654678], [-53673728], [-131245752], [-6011532], [-81437466], [138147998], [-108017700], [-79041090], [-35259856], [-80203788], [-122750210], [-84406746], [-86070240], [66813212], [-105551332], [31621474], [2748500], [62861048], [146473198], [58385614], [110109572], [89292150], [11238306], [59459080], [162296722], [32750224], [96306088], [111716634], [88429290], [-75416404], [-12639840], [30216578], [90745922], [-104305260], [-157973944], [-111685198], [44611648], [83733828], [100041410], [170192616], [-122326300], [-47082998], [208098792], [-82421682], [53618492], [-98648224], [-67106748], [49132600], [-133265244], [187761498], [232325950], [-50003070], [-181169898], [39659716], [-141969474], [-37533178], [-166475712], [-140546426], [85503280], [-99847328], [-123292306], [210492588], [8129930], [-63514162], [261084408], [-230235664], [8813612], [8542492], [200699548], [-171234808], [-36725628], [-201807666], [-80644398], [237487510], [-198862300], [-18174118], [110857118], [112845546], [169362648], [211235946], [5523160], [42611362], [38679634], [-230340120], [104729632], [-226044024], [26459862], [126904880], [50259904], [62349152], [124006318], [123595188], [-251563410], [228804000], [37083426], [86871096], [296905990], [125353818], [366773564], [130147512], [59830000], [-221513530], [-307633672], [130272814], [-6087230], [-311155972], [-244471502], [-198439472], [98949944], [79558406], [25668002], [94743360], [55744454], [-81812568], [154283246], [360917258], [-201122142], [-137802560], [45359096], [-152437092], [-59468682], [-276301310], [-17727244], [-219341318], [177293932], [280031174], [13730838], [7101780], [-111904168], [58126934], [77218580], [447778402], [182908808], [347820648], [-69289182], [450602128], [-226250322], [-181094898], [-365774004], [303310730], [-469022484], [-337456410], [122643268], [-317102378], [-325042576], [-158136638], [-545037972], [51419448], [-114113620], [398207958], [-210257546], [441252252], [-486587384], [143437278], [-139612848], [58868610], [283533896], [153122450], [-105497512], [510171898], [-486718210], [402529888], [624934876], [-91774980], [-456918588], [-232059218], [377255454], [41975512], [519988942], [373811480], [-535132506], [-212071248], [85290038], [107638036], [-329068642], [-427744210], [578996554], [48585680], [-209434082], [-197312208], [-247789174], [111881860], [158526690], [-97087688], [358069198], [92705680], [-146307086], [-130237728], [467526258], [492852380], [367896288], [-355057602], [349886318], [440246234], [353282450], [61233456], [-184641658], [332738884], [-319609652], [-724590638], [90525102], [115425760], [714174378], [-452210542], [-583653024], [-90390010], [668994512], [-543592572], [-255989020], [477545194], [-89084862], [334580664], [-673368670], [110900602], [-345634780]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_847_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_847_6_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_847_6_a_a(:prec:=1)
    chi := MakeCharacter_847_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_847_6_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_847_6_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_847_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<2,R![-2, 1]>],Snew);
    return Vf;
end function;