
// Make newform 384.4.a.f in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_384_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_384_4_a_f();".
// 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_384_4_a_f();".
// 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_384_a();"
function MakeCharacter_384_a()
    N := 384;
    order := 1;
    char_gens := [127, 133, 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_384_a_Hecke(Kf)
    return MakeCharacter_384_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], [3], [-4], [-10], [4], [-26], [14], [-8], [-148], [-72], [-18], [-262], [-378], [432], [-148], [-360], [428], [442], [692], [-540], [-1018], [-386], [-108], [-382], [298], [-1800], [818], [-716], [1134], [1858], [1402], [-1188], [734], [-404], [684], [3502], [3258], [856], [-1552], [1548], [-3420], [3502], [4792], [-98], [-1184], [1466], [2692], [4554], [-5580], [902], [1962], [-2016], [-1826], [3060], [-558], [-5544], [-4856], [-7030], [-738], [-1854], [-1404], [2056], [2340], [-1616], [3286], [-9936], [2116], [-7874], [10476], [-7030], [-5310], [-7628], [-13778], [-2566], [-7120], [14040], [4972], [-8766], [-12042], [8002], [-364], [1502], [-3532], [8946], [1602], [-4356], [2110], [13698], [12028], [-10286], [-11516], [13932], [-10682], [11660], [-12404], [2308], [1984], [3982], [-18368], [2870], [-14176], [12884], [-15588], [-4730], [-4724], [16838], [9932], [15714], [20340], [-16434], [8234], [6506], [-20542], [-11116], [3574], [7110], [1728], [-14724], [23076], [27252], [-18910], [-350], [-13468], [-12852], [1080], [-22720], [-22346], [-35244], [-25074], [23742], [4932], [26248], [-26146], [-4202], [-31018], [-7570], [-31392], [14608], [-30204], [-306], [34920], [-14720], [14030], [-24764], [-1810], [-4500], [29458], [15030], [27368], [-39352], [-32246], [1714], [-1288], [12888], [44920], [-12384], [8190], [-4698], [-10646], [25852], [3172], [53150], [9802], [-46660], [-41994], [11848], [7390], [47682], [-28054], [57604], [-39204], [8442], [6440], [20330], [31366], [16146], [50436], [-58324], [55970], [-14222], [-11474], [28548], [57870], [63410], [42408], [40932], [29710], [-72692], [17686], [-73120], [18226], [-56340], [65708], [-56380], [-37076], [59170], [7478], [23390], [-54230], [36720], [69116], [34946], [16982], [-98], [-26604], [-86688], [-70334], [64876], [-64886], [-10648], [-38242], [-20876], [-1934], [60700], [59292], [-31670], [58374], [7718], [-82368], [2268], [-71018], [-59094], [-76626], [-24390], [93172], [-82430], [7186], [-51476], [-30014], [56380], [12430], [-54272], [-101806], [-99310], [52264], [44060], [65834], [-92740], [112716], [-99540], [-96228], [-71612], [30718], [-61030], [-23782], [13820], [-73738], [16812], [-13516], [36216], [-60354], [40658], [9644], [16282], [102856], [56732], [-113354], [82332], [-61344], [33786], [50138], [82580], [1990], [96346], [-36162], [-107892], [11812], [-54198], [-99592], [46508], [40474], [67636], [72826], [-126442], [-118494], [117530], [-26284], [47826], [-117522], [9468], [137452], [51554], [-30780], [-50086], [-126280], [-89032], [88462], [143316], [2854], [59522], [121684], [-120636], [-83358], [76572], [-54142], [-41220], [63154], [52312], [-121788], [-52336], [54710], [-72252], [-70074], [-30996], [153728], [-77922], [2452], [-109378], [-62820], [48410], [29912], [39528], [-83034], [47816], [-124448], [132426], [-73228], [68180], [-183762], [-179798], [44380], [-39322], [-103032], [-42658], [88182], [-40582], [175896], [97072], [2416], [-40388], [-4258], [-86724], [144026], [-65412], [42716], [169668], [-130598], [14602], [-126234], [-119662], [88802], [-21506], [-26856], [-109934], [-60768], [-3172], [132622], [206820], [-157356], [-40208], [135440], [9890], [22608], [147906], [-32174], [163498], [116784], [-77508], [-129834], [-126864], [52154], [-118782], [-160488], [122756], [74708], [-182242], [-181480], [154558], [-195382], [-132404], [-148960], [-175824], [4248], [217198], [-146674], [78764], [26712], [-66590], [142398], [134758], [103036], [30294], [200654], [133074], [-89776], [-112032], [135358], [69182], [-9332], [-54796], [133522], [22932], [-125996], [-20008], [82984], [-65582], [63514], [-52114], [78192], [-109476], [136242], [-21762], [-151902], [176742], [238968], [227286], [211870], [225846], [13672], [-24628], [37638], [-9180], [-70268], [-118664], [140246], [259160], [9752], [-15634], [-219690], [-20484], [240268], [54738], [-104504], [74124], [-20250], [4104], [211972], [-11502], [48888], [295020]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_384_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_384_4_a_f();".
// 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_384_4_a_f(:prec:=1)
    chi := MakeCharacter_384_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_384_4_a_f();".
// 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_384_4_a_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_384_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![4, 1]>,<7,R![10, 1]>],Snew);
    return Vf;
end function;