
// Make newform 320.4.a.a in Magma, downloaded from the LMFDB on 23 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_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_320_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_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], [-10], [5], [-18], [16], [6], [-6], [124], [42], [-142], [-188], [-202], [54], [-66], [38], [-738], [-564], [262], [554], [140], [882], [-1160], [-642], [-854], [-478], [1794], [642], [850], [-666], [-1446], [-1154], [368], [-670], [572], [-1730], [1324], [-2946], [-2098], [-866], [1678], [1620], [-2510], [-372], [2938], [-2234], [-3048], [-4896], [-5302], [3778], [3034], [-3478], [1560], [-3218], [-688], [2170], [2274], [-7146], [2604], [5150], [5270], [-3434], [9878], [-8054], [5492], [-422], [-6194], [-7688], [-1438], [-8838], [7810], [5906], [8904], [-7370], [734], [-10300], [2682], [-6114], [7174], [10498], [-1810], [3396], [14974], [-13540], [15426], [-10472], [-722], [-11898], [-790], [3418], [-7534], [14314], [-7016], [15190], [12624], [2492], [11714], [5618], [13770], [-6986], [-11958], [4194], [5382], [-15418], [-5778], [-6024], [554], [2386], [-846], [22304], [5510], [-8234], [1046], [-3862], [-13964], [-14884], [17838], [7814], [774], [23422], [13508], [6222], [-15566], [-2234], [-13282], [27416], [-25626], [-11702], [-28008], [-7682], [14270], [-29324], [29258], [19076], [22670], [-23206], [-1854], [-6474], [20354], [1886], [-9462], [-24512], [-36242], [-17718], [-6726], [-41722], [16720], [33286], [38978], [1916], [-2374], [-32722], [5390], [43238], [-11010], [74], [17460], [17072], [-14826], [3354], [15434], [9306], [12202], [17478], [-10920], [10834], [-36862], [5380], [-31266], [-28334], [14886], [29444], [52682], [-59916], [-3022], [13776], [-50854], [-45064], [-24238], [34778], [-10474], [-2026], [15328], [43790], [-46254], [45898], [-29910], [16886], [-41402], [16814], [57332], [-43094], [24702], [56232], [56294], [-71934], [75178], [1070], [-66386], [40034], [-61134], [52562], [49052], [-2194], [-21870], [-27588], [48046], [-46304], [-64938], [53898], [55400], [-6182], [54990], [41186], [12530], [63888], [19494], [39278], [56114], [-55362], [-30378], [-10178], [-92864], [54226], [-39998], [-96782], [84942], [-18126], [22464], [67462], [8280], [20086], [55756], [-43972], [-80282], [11494], [-44418], [-10330], [-6098], [-44060], [-10172], [57262], [42128], [22682], [56290], [-76990], [52104], [22414], [30984], [92540], [33554], [-39114], [113298], [78774], [73150], [-30962], [66636], [-25310], [24874], [122798], [89578], [23770], [-132366], [42266], [-38058], [-31350], [-100564], [49746], [-72486], [-3602], [-16122], [-91014], [-69238], [-45974], [25168], [-134390], [-7510], [-48918], [59266], [26710], [103296], [-148374], [-78268], [-14682], [-98750], [11618], [26020], [-129894], [-130602], [-41136], [-34282], [33430], [5138], [-75102], [97344], [-60050], [-42186], [25396], [-155802], [108900], [-19846], [-68222], [-2754], [-101352], [-26298], [-6928], [-118046], [-58694], [-95390], [76296], [-27914], [177258], [40298], [-77150], [-26074], [-149554], [135450], [163164], [21044], [-143078], [147766], [124056], [181362], [-109210], [33506], [64050], [-130754], [61516], [-71650], [758], [-134034], [46966], [-145986], [117800], [-44754], [-196864], [62514], [1942], [144090], [-76218], [22686], [42070], [-127142], [-306], [-115284], [43446], [-166004], [159646], [162762], [147876], [79718], [-20160], [-71206], [-21130], [-174622], [-59734], [-119230], [-225288], [-106392], [167858], [200890], [-45010], [-198630], [-126570], [167324], [-22718], [-102534], [146342], [-185870], [55878], [112928], [-208604], [40082], [131998], [-30068], [-43466], [55500], [210420], [214418], [-22746], [126962], [-38422], [34882], [209222], [252370], [-179396], [144586], [46748], [91174], [122446], [-74666], [169718], [122990], [277276], [167162], [64220], [63298], [-99464], [48138], [-32208], [58926], [-183850], [-248326], [-232586], [203010], [-41730], [71276], [-120466], [237042], [164062], [35236], [-122702], [171782], [-145034], [-12400], [-50046], [-253958], [-93984], [-177938], [-164510], [69802], [163430], [-209514], [222886], [77628], [-70502], [-21714], [246470], [-105266], [-129512], [289784]];
    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_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_320_4_a_a(: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_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_320_4_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_320_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![10, 1]>,<7,R![18, 1]>],Snew);
    return Vf;
end function;