
// Make newform 320.6.a.b in Magma, downloaded from the LMFDB on 19 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_6_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_320_6_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_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 := 6;
    raw_aps := [[0], [-24], [-25], [-172], [-132], [946], [-222], [-500], [3564], [-2190], [2312], [11242], [1242], [-20624], [6588], [21066], [-7980], [-16622], [-1808], [-24528], [20474], [-46240], [51576], [-110310], [-78382], [-141942], [-436], [-232968], [174850], [182994], [-122452], [241908], [277098], [193540], [-140550], [433952], [555922], [66616], [-205692], [-433854], [489180], [-719462], [-185928], [-591406], [-449478], [157160], [-253052], [1073444], [626832], [116650], [-743046], [978720], [-1132798], [-905652], [1939938], [-805476], [858690], [-383608], [-2010758], [202602], [221536], [322506], [-1443008], [171312], [-1026886], [-752958], [-1994132], [-987022], [-2206008], [-2741870], [-2389566], [-279480], [-2476372], [-2745254], [1189060], [3257604], [-1983510], [-4974158], [-1342638], [-1094230], [954060], [1593898], [-2646648], [3723554], [-2583400], [-7562064], [4307730], [-2243542], [-1656702], [-2891596], [6526992], [-5962320], [2991908], [1204188], [-9205460], [-3359556], [2537010], [-9315798], [5028016], [-134222], [-605648], [7062402], [10302936], [1047690], [-14076452], [1626818], [-6960888], [-11363886], [14868840], [-1236118], [-12449812], [8734906], [12549498], [14665780], [-196288], [-11159598], [2251576], [8053188], [416466], [-13172100], [1694938], [-8916046], [14289402], [5333136], [-698252], [-17987982], [13946410], [6222720], [-7767292], [-24208334], [-12684980], [19763244], [-9017608], [1125562], [22574682], [-632350], [12586746], [-21579248], [30976002], [4249290], [-3423332], [-31057062], [-31190356], [8284872], [18168850], [-10274280], [-6285974], [15405018], [-14352620], [13327764], [-32484638], [15460002], [1694776], [-2872572], [-39542168], [11317752], [8513480], [-7545870], [-18450022], [-6750462], [-6456768], [-39664806], [-34301452], [5771148], [7087458], [45936204], [-45029848], [23736442], [-20803630], [47170986], [58006020], [44057458], [29775552], [32666714], [16809200], [31740570], [-44280932], [13812138], [-4063876], [-17612510], [18505148], [34573428], [32116186], [-78559302], [-56224956], [69313050], [77282962], [-5765864], [-13567750], [-12750648], [-14670526], [-86842464], [60425428], [-67415502], [-32094048], [-78047526], [43790882], [-44919614], [39396738], [22529964], [15206850], [45711272], [-39985718], [18008930], [-61223100], [-21847278], [-57242560], [-32822664], [-65439990], [-100054772], [-46070782], [50630778], [-487156], [37927512], [-75921480], [-93084838], [-13443652], [-30135918], [50331348], [-10996734], [-37417142], [-107350840], [60253410], [9760484], [126980592], [44478490], [3296874], [154005120], [-47968492], [-105115572], [-58565774], [22026220], [69668792], [96206682], [113191936], [-8992932], [140632610], [-43385334], [-29062140], [134895552], [-84445224], [174073228], [-50019316], [12733090], [170967474], [31145880], [-152990932], [-160698972], [-91016540], [-178513356], [-128414078], [-27099198], [-188967612], [-43429990], [144121986], [-22231380], [-72649382], [-41472248], [-193952598], [43730138], [-10294156], [39612912], [134688010], [12219826], [-10560942], [21820540], [-149348670], [-48254118], [7946416], [61705866], [5860258], [-45570608], [-69243286], [-10822240], [115304498], [206870684], [-5654568], [78581650], [-7220518], [32315748], [-93226476], [234278432], [198442308], [199291018], [-119444408], [-155549448], [174236834], [-58143798], [249951080], [276052530], [86493378], [126981552], [1043274], [40540188], [-23465534], [51989010], [6145352], [-138877254], [-117001020], [260341792], [-312560086], [141457122], [288597440], [218494536], [1927948], [-343319182], [-240646968], [-59454110], [-265664520], [47871946], [164699124], [154321530], [-230320158], [53692936], [-185517372], [-187903510], [-358379700], [-192807768], [336590114], [232162530], [-175376972], [-95475142], [127175538], [-48271516], [-200656086], [140781122], [33310780], [-227578064], [103387308], [159487866], [-42469502], [-220654638], [-210685360], [419416776], [284373628], [-58157688], [-236570990], [52044834], [95174522], [153941708], [43798906], [386024538], [40809930], [-118296208], [270568146], [-403796100], [23048458], [426227512], [-96787848], [168042762], [14122708], [439439498], [-285115902], [544559684], [-37345670], [253481034], [-135383280], [-439724052], [241482498], [-508975716], [-383867558], [-326776278], [529470828], [-449038860], [-150936128], [427275674], [454506162], [-360160516], [-3350518], [-56978892], [102703540], [-329445036], [333209370], [-215425408], [-316548158], [-314517300], [302602152], [-392708446], [-520846590], [-246499462], [-119078862], [105882954], [-299532412], [-540502302], [-612758660], [-217197156], [-609701368], [26842], [-186861104], [213454188], [-198574510], [-513480054], [588023460], [50081872], [-317558208], [669884474], [-220796080], [429133770], [481194748], [-509703942], [-30801230], [668919474], [420184268], [-449099142], [304147530], [412525712], [61509682], [-72801678], [560869336], [-677667060], [-352472206], [4318842], [-260597664], [-136978412], [377314778], [-175363422], [-661523520], [-804413692], [177131778], [484989084], [-526748430], [231029242], [621906588], [700217460], [-389023126], [-345550878], [344869896], [169167210], [-89934212], [505108440]];
    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_6_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_320_6_a_b(:prec:=1)
    chi := MakeCharacter_320_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    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_6_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_320_6_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_320_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<3,R![24, 1]>],Snew);
    return Vf;
end function;