
// Make newform 847.4.a.a in Magma, downloaded from the LMFDB on 04 May 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_4_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_4_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 := 4;
    raw_aps := [[-3], [4], [12], [-7], [0], [-38], [48], [70], [12], [-126], [-70], [-358], [216], [-344], [390], [438], [-552], [-830], [-196], [648], [16], [-1352], [-90], [1146], [-70], [1254], [-682], [384], [646], [-1314], [-344], [258], [-2730], [-1838], [510], [-2864], [-2968], [1604], [-180], [1626], [-3252], [1820], [-1212], [-2522], [3474], [-2842], [-5528], [4034], [-726], [-2788], [-2694], [-6480], [2320], [2088], [-4182], [-3696], [-6060], [8764], [-5186], [-3006], [3922], [5778], [610], [6882], [10334], [5934], [-3220], [6658], [6888], [6190], [-3990], [7656], [8426], [-11918], [3908], [-3246], [-8166], [-2824], [-10482], [-8156], [-11052], [5006], [9480], [-1942], [13660], [3828], [18270], [-10154], [17190], [4448], [11100], [15816], [-1924], [-13068], [17876], [3852], [15132], [-3054], [11770], [-10694], [-5636], [-15126], [-3246], [1050], [-6860], [-12634], [18144], [10896], [-20280], [-12332], [-21800], [-18542], [10098], [-124], [-14308], [-678], [17408], [-28686], [9858], [22824], [24212], [17458], [-14574], [-27588], [10424], [-3978], [18794], [33906], [-2446], [20410], [-14564], [7416], [-17980], [3170], [27492], [-2108], [32280], [-9578], [11952], [9030], [37762], [14334], [13988], [22284], [-12868], [2826], [17962], [-47148], [34904], [-44052], [36214], [17046], [41276], [-6456], [-11500], [27396], [-8840], [2874], [-7832], [-16926], [-17988], [29142], [-31160], [-33036], [12786], [24918], [-7648], [31750], [-53498], [-49092], [38568], [-57688], [37176], [60082], [-1546], [2358], [38428], [35466], [-4268], [-23546], [-23056], [1314], [-17278], [-21756], [26592], [-46668], [-41366], [48764], [30490], [-37428], [-50002], [-46080], [63596], [77232], [-57660], [67578], [2050], [47378], [-34776], [7476], [-29850], [37348], [-46600], [5200], [-6660], [84300], [-39562], [-16140], [4662], [-49444], [-39926], [-14328], [9272], [-45528], [12900], [-24370], [-71336], [-16836], [7248], [-64998], [-41654], [82996], [5430], [56072], [-20214], [-14546], [-98178], [53088], [-73892], [59928], [-83878], [40534], [-25040], [-67650], [19460], [-19284], [-38806], [-32898], [-32292], [42894], [84228], [63358], [32582], [78734], [70308], [-372], [-7018], [14280], [-96788], [97056], [-102854], [42144], [20544], [89498], [-65670], [60576], [-54358], [1474], [-57636], [-26408], [-53552], [-36252], [6514], [-87842], [-67242], [-58264], [25470], [67014], [-89372], [-26142], [-86848], [-125836], [-66202], [71872], [-55208], [15842], [86196], [106234], [-81794], [-34122], [-28392], [-57544], [-75960], [17926], [-434], [62868], [-104506], [5526], [140294], [27972], [85470], [1860], [-82542], [82638], [-94106], [-68298], [80462], [-117252], [132738], [-54398], [-26732], [40758], [9376], [2412], [51284], [52238], [-114996], [-21724], [117048], [-28658], [-106464], [134106], [-79806], [-51388], [-40536], [-61976], [-114408], [-45600], [-60730], [80802], [121822], [-104182], [-97470], [-18844], [-330], [-65410], [24788], [154604], [19392], [-60498], [17386], [-132366], [-18956], [115122], [-69500], [-97596], [50474], [708], [-59038], [197224], [-110050], [-80046], [94770], [191108], [-188070], [-52020], [-194812], [-105172], [-28944], [78528], [-50078], [145766], [144630], [31048], [-79994], [31932], [-156426], [89778], [-42474], [-4896], [-86186], [175812], [-56352], [21156], [113276], [56594], [-127422], [-61544], [-236882], [-95700], [-95452], [218976], [82242], [-187544], [14726], [-65460], [-22104], [-34076], [-35256], [-176816], [204144], [-204102], [192616], [-100770], [-235622], [91698], [-275596], [259868], [-226712], [83718], [-261718], [-23928], [99348], [222032], [114960], [-39494], [23128], [156798], [169724], [152958], [236770], [-36078], [-59960], [88158], [41178], [-53228], [112574], [-81378], [248996], [-40956], [-203096], [186954], [-97884], [-34220], [123226], [116256], [172392], [103898], [-175626], [197592], [-1038], [-85574], [-48456], [-29898], [-255274], [14856], [86484], [269514], [-213316], [90120]];
    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_4_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_4_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, 4));
    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_4_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_4_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_847_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![3, 1]>],Snew);
    return Vf;
end function;