
// Make newform 1682.4.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1682_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_1682_4_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_1682_4_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_1682_a();"
function MakeCharacter_1682_a()
    N := 1682;
    order := 1;
    char_gens := [843];
    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_1682_a_Hecke(Kf)
    return MakeCharacter_1682_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 := [[2], [-7], [5], [-2], [-37], [27], [-24], [88], [-28], [0], [143], [360], [-386], [-381], [103], [-431], [288], [840], [-180], [706], [-716], [-931], [1188], [642], [-486], [-240], [542], [374], [449], [-898], [-1280], [1292], [-1852], [-1532], [-1357], [-2134], [-2386], [-3937], [-2762], [-3822], [1430], [-525], [-224], [490], [106], [-2034], [-5717], [3438], [5754], [-2074], [5063], [-1624], [-4463], [2229], [-4187], [7611], [-5136], [-4015], [2150], [-1965], [-2452], [-142], [-9097], [4592], [1225], [-9852], [4183], [4480], [-5002], [-6427], [9734], [-5379], [9640], [7543], [-3804], [9174], [4536], [-9853], [-11429], [-2662], [5126], [16200], [12000], [3736], [3172], [1540], [-8358], [798], [-15330], [-6020], [3275], [-13463], [-12358], [-407], [14084], [-13767], [21381], [10243], [-10568], [15720], [-18106], [-2346], [691], [15542], [12124], [-15808], [-6516], [14751], [18681], [22526], [9329], [19525], [25332], [21091], [6242], [-15392], [-14870], [17016], [24122], [20217], [3942], [-6551], [-14958], [-27584], [24244], [7431], [-28429], [-29890], [-13072], [-13456], [-16915], [-10164], [-5816], [-30496], [-25654], [15348], [-22062], [-14942], [-13744], [-19376], [38174], [-15533], [-13624], [37475], [-11600], [10783], [20026], [-37259], [19681], [-12422], [-6111], [33998], [5214], [27507], [-49012], [-38047], [23214], [13890], [20830], [32305], [35759], [21847], [9151], [26980], [-25659], [-48693], [-10898], [41216], [-13679], [-4668], [6310], [-21535], [23298], [-1646], [-43952], [12606], [11352], [35204], [52581], [7538], [2833], [-49589], [-31238], [35769], [-13080], [-1961], [-66876], [-596], [69992], [-38090], [-54035], [59221], [6835], [-63576], [-74828], [41418], [62848], [-67198], [14913], [-17292], [41886], [-22892], [-18046], [-1686], [-37468], [-25521], [31049], [-30134], [-33021], [14009], [-20854], [-23310], [29597], [-3909], [74264], [69869], [56632], [-46356], [-5592], [-61314], [-59912], [15574], [47186], [43633], [27504], [-99906], [-28920], [76888], [6223], [41334], [49168], [-61104], [70468], [-49704], [23942], [25640], [80320], [82022], [-81026], [-36889], [82069], [11922], [-73402], [-31668], [-18702], [14334], [-36556], [87828], [-97070], [-88163], [96460], [-31857], [-44603], [-31280], [-105670], [-32484], [71458], [-123455], [-6114], [-36919], [62560], [13659], [91330], [-122044], [19048], [-116425], [-33864], [-24572], [-99412], [-39119], [103381], [-24694], [-121947], [-33488], [-4896], [125267], [70116], [-7893], [-68860], [69796], [9494], [-7294], [23264], [152098], [-1996], [-112404], [80588], [33922], [-79798], [-108999], [94743], [147196], [-13179], [112821], [-131882], [60003], [-112365], [-79719], [-31422], [-23140], [-132260], [70206], [87976], [29043], [106156], [32569], [-23509], [40999], [-63870], [-172505], [-18768], [-130830], [180834], [-100388], [35586], [-14117], [-32660], [179982], [182649], [149288], [-181951], [-88463], [-30730], [-96911], [76467], [-165558], [45914], [30788], [-166375], [-48663], [28348], [-25157], [-118420], [-49700], [195601], [-53896], [129958], [-84618], [104230], [23300], [143100], [38778], [-116216], [-128264], [6978], [196735], [-107784], [70836], [-71839], [-57210], [126984], [214294], [66378], [50108], [15328], [-161472], [199659], [60162], [25552], [-196538], [4691], [-51567], [177241], [224250], [93671], [-235430], [-43696], [-814], [204166], [-94593], [101198], [109222], [178650], [35656], [31081], [-4068], [-18471], [110740], [-59841], [-196483], [202512], [-55623], [-95229], [94176], [155266], [176166], [-164344], [-60041], [-189781], [-230116], [74682], [111601], [-139767], [90505], [-19781], [-90019], [184996], [-254620], [158138], [209050], [28206], [151950], [34812], [-25899], [221243], [-25304], [-24727], [107852], [-74093], [-46260], [-28722], [34041], [32810], [27628], [157730], [-128078], [38505], [-145378], [110124], [-244491], [-287558], [-31050], [-82939], [140245], [-153353], [-183627], [-53760], [199720], [10080], [-162195]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1682_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1682_4_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_1682_4_a_b(:prec:=1)
    chi := MakeCharacter_1682_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_1682_4_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_1682_4_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1682_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![7, 1]>],Snew);
    return Vf;
end function;