
// Make newform 320.4.a.e in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_320_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_320_4_a_e();".
// 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_320_4_a_e();".
// 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_320_a();"
function MakeCharacter_320_a()
    N := 320;
    order := 1;
    char_gens := [191, 261, 257];
    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_320_a_Hecke(Kf)
    return MakeCharacter_320_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 := [[0], [-4], [-5], [16], [-36], [42], [-110], [116], [16], [-198], [240], [258], [442], [292], [392], [-142], [348], [570], [-692], [168], [-134], [784], [-564], [1034], [-382], [674], [-992], [500], [-1046], [-558], [-328], [212], [1434], [-2196], [2418], [3672], [-358], [-2564], [-3056], [234], [-524], [1138], [1520], [-2142], [2306], [3288], [3876], [-5688], [2796], [-4446], [2522], [816], [-5422], [5900], [5250], [6240], [714], [2144], [4466], [-5302], [6932], [4034], [3836], [664], [2986], [-2726], [9212], [-3278], [-4956], [-4678], [1890], [-6472], [1960], [-8750], [380], [-9688], [-3870], [-1622], [9906], [-4214], [7012], [1602], [-3584], [-3470], [-3416], [-9708], [-10366], [-16742], [1258], [13528], [-6916], [1728], [16656], [1084], [-5804], [10512], [4314], [-1190], [3780], [11002], [-5908], [-14806], [684], [-2582], [2540], [22786], [-7884], [-21902], [15080], [-19702], [7320], [-24350], [19546], [-3476], [21880], [20994], [18204], [-2064], [-9942], [-24236], [-17614], [13058], [33186], [31716], [2084], [7418], [18242], [3024], [-26176], [17818], [22052], [15840], [21024], [38034], [37802], [15042], [-5950], [-23364], [-19846], [24762], [-16644], [-3182], [-7504], [-12604], [-12230], [9656], [-5806], [-39094], [18876], [32296], [9578], [-41710], [-2260], [-33696], [-7756], [-5312], [-23576], [-19038], [20570], [21386], [-38020], [20202], [29840], [12476], [-36974], [-16368], [-49552], [-24414], [-63214], [-5838], [8060], [13210], [10376], [4090], [-39184], [30666], [-27252], [59298], [-48192], [18154], [-17384], [27412], [-18814], [-6918], [-17928], [24850], [-20198], [-7172], [41722], [75600], [-27326], [-46812], [-40444], [-26214], [844], [48986], [-29870], [7898], [-14142], [63040], [-28502], [76256], [9554], [-33118], [-25076], [-67398], [-45568], [44060], [-10470], [30332], [-21166], [-6542], [-28208], [-26876], [28760], [15418], [22024], [-41870], [67552], [-77190], [22594], [32840], [-36030], [-72712], [-57604], [-105550], [72618], [101664], [64432], [-58020], [35722], [-44780], [-67600], [5722], [-64092], [-53784], [72370], [-76590], [82460], [-101528], [-1044], [49036], [-65088], [76714], [-62606], [-12024], [50776], [-63468], [-70260], [22328], [-28006], [-107070], [26800], [9242], [-90934], [97140], [-98350], [-38620], [52098], [-46678], [-57848], [-40436], [-33598], [128026], [-69886], [-110908], [89290], [-45590], [29972], [-125918], [-90326], [73596], [-38486], [32416], [-1134], [128592], [96036], [1050], [-66534], [6948], [-86040], [20200], [-145376], [53186], [89780], [-44928], [117714], [-93038], [12520], [17250], [94826], [-138084], [-75318], [-142564], [-33526], [21082], [11632], [146706], [-34116], [77356], [-63398], [-100982], [70096], [-17940], [-79188], [-113086], [-77804], [55626], [-22616], [-144926], [118808], [-129774], [-103966], [122396], [44048], [-36614], [-85036], [78672], [-147390], [145074], [60676], [-32886], [-22566], [10056], [81274], [171858], [117860], [12340], [-110760], [-115358], [86186], [171514], [-3104], [-3332], [-129124], [-169052], [10650], [-177438], [51130], [40680], [168914], [14250], [177698], [-167320], [-100390], [90692], [-88958], [123572], [-197792], [-16974], [-112556], [8794], [-207990], [60536], [162514], [65194], [-46464], [21724], [-202382], [-118832], [161602], [89562], [-57528], [5308], [-18292], [-5542], [-72822], [50912], [43850], [-7308], [193900], [187192], [3794], [-5512], [135002], [-210444], [-69456], [101346], [166098], [220394], [-155430], [-100070], [259776], [-51294], [-28412], [-44224], [193088], [-102510], [-20332], [-190792], [-222270], [114722], [236204], [176828], [-272360], [-112566], [71872], [-6598], [57916], [216530], [-158854], [-71070], [182856], [114698], [-11454], [49672], [261386], [186834], [5612], [138756], [143730], [-199374], [-281068], [132980], [250778], [203210], [111936], [-158800], [-25902], [140976], [-41126], [-224926], [-43896], [168028], [168538], [34122], [-93268], [144522], [-143668], [6312]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_320_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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