
// Make newform 663.4.a.a in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_663_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_663_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_663_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_663_a();"
function MakeCharacter_663_a()
    N := 663;
    order := 1;
    char_gens := [443, 613, 547];
    v := [1, 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_663_a_Hecke(Kf)
    return MakeCharacter_663_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 := [[4], [3], [-10], [-10], [18], [-13], [-17], [-74], [-132], [210], [-230], [-46], [-114], [36], [446], [-754], [-50], [-226], [582], [-370], [826], [272], [162], [-186], [-790], [1518], [1400], [-836], [986], [78], [2464], [-2628], [-714], [-356], [1446], [-526], [-262], [-2634], [2002], [1142], [-2580], [1874], [-412], [1082], [-1194], [-2860], [-5972], [1998], [-474], [-6390], [-6310], [-870], [6058], [436], [5402], [6364], [-3918], [2938], [-6226], [1070], [1152], [-8314], [2866], [-7428], [-4786], [-4402], [5470], [-3634], [-11588], [2434], [-8258], [-7594], [-4704], [4178], [7450], [-7398], [10234], [-1406], [8550], [6730], [-3988], [14450], [774], [11970], [12884], [-12928], [3814], [-5390], [-6634], [4370], [-15164], [10162], [-17174], [-3828], [-10894], [2336], [4382], [-14634], [20696], [18034], [-10836], [14566], [-13380], [-1582], [9680], [-24574], [10722], [-8930], [-20284], [7790], [13008], [-26478], [-18290], [10614], [-20062], [-12962], [-21238], [-20480], [-14558], [888], [-31142], [-15666], [-14098], [666], [-4254], [-9230], [1994], [2260], [1136], [17434], [25234], [-33546], [-4636], [-11010], [27686], [-21934], [27630], [16886], [3282], [9830], [-582], [-32650], [30084], [16842], [22430], [13074], [47938], [-38], [18256], [14994], [-43478], [-36670], [22772], [36736], [4912], [-3312], [-17508], [-27898], [13354], [39742], [2474], [20882], [47942], [56208], [23718], [-48974], [-36760], [-6778], [-20870], [-9522], [52482], [42610], [39036], [-13534], [37888], [-1086], [6230], [26934], [13848], [51210], [-52358], [-50016], [-42810], [-22010], [59134], [-45638], [-7846], [38942], [43922], [10626], [-21802], [51462], [52576], [-21514], [-46192], [2202], [-66182], [23706], [-30218], [65628], [-32202], [26472], [-34622], [12654], [19182], [-43482], [26350], [-46736], [-4482], [956], [-590], [18222], [69920], [-25690], [48534], [-56142], [14556], [-60350], [-89994], [-19682], [17630], [80390], [62054], [38010], [-23404], [19222], [23362], [-60120], [19168], [-56846], [-27222], [-58612], [-25282], [-97626], [41440], [-16678], [-19038], [-36250], [96680], [36196], [68134], [57876], [88300], [-21126], [84006], [-102220], [-27922], [117994], [-38930], [-12024], [-3998], [67902], [41302], [34950], [-78966], [-41394], [9618], [-2950], [87666], [63658], [29012], [-16024], [-69846], [-133682], [-78794], [-91960], [-58962], [127350], [-7186], [105982], [39894], [25334], [22330], [-78008], [-21074], [-31314], [22506], [44882], [14010], [-82828], [-49292], [54198], [-90344], [88786], [83878], [12312], [77938], [4630], [-111030], [-38646], [57462], [-9060], [98070], [116322], [40778], [-2310], [23660], [-83862], [24900], [-71190], [149322], [-91362], [-170612], [169532], [-117116], [-95702], [652], [85518], [60222], [-110558], [-34192], [82894], [-57378], [-99440], [-78786], [-62714], [9298], [153270], [89394], [-191042], [52136], [-108006], [180710], [93906], [-77914], [-61934], [70414], [-41958], [-31236], [8010], [62658], [41570], [-42624], [-135178], [-116886], [57686], [3666], [61782], [-135710], [-57032], [-33854], [4998], [201102], [49760], [160510], [195720], [-40286], [-7966], [87682], [-20302], [24142], [-126966], [168910], [33868], [-174570], [157110], [-62394], [67558], [-199582], [159066], [75682], [-156858], [91348], [17414], [-95276], [-181082], [630], [-17102], [-65242], [-103840], [158424], [-123318], [-183378], [186700], [-132290], [-185930], [134636], [145954], [146386], [154174], [12454], [19942], [-215026], [19182], [-51970], [-113262], [130286], [-22666], [-50286], [-27636], [252650], [-64138], [24022], [-162656], [25422], [-100550], [120526], [-202906], [-50612], [-24138], [169178], [-235986], [-69142], [-158466], [-134850], [94320], [-283054], [73054], [184866], [195442], [162754], [-96274], [249488], [139048], [154], [-45450], [47718], [58064], [118230], [176504], [-308602], [-147542], [-268770], [-14016], [-106782], [-87066], [-242992], [-286858], [294138], [215336]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_663_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_663_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_663_4_a_a(:prec:=1)
    chi := MakeCharacter_663_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_663_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_663_4_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_663_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-4, 1]>],Snew);
    return Vf;
end function;