
// Make newform 300.6.a.a in Magma, downloaded from the LMFDB on 19 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_300_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_300_6_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_300_6_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_300_a();"
function MakeCharacter_300_a()
    N := 300;
    order := 1;
    char_gens := [151, 101, 277];
    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_300_a_Hecke(Kf)
    return MakeCharacter_300_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], [-9], [0], [-91], [-174], [785], [1794], [-925], [-2346], [-726], [-811], [7922], [-360], [-4951], [9906], [-8292], [7014], [-51433], [581], [-56520], [-42478], [-28912], [-104586], [-118080], [110273], [-193332], [-37684], [17292], [142133], [-68388], [185384], [329772], [143982], [-365644], [155178], [-227185], [-171277], [110675], [-440628], [-699834], [-309786], [628625], [192774], [708971], [-119154], [1018643], [-32245], [815765], [-1459080], [406241], [-901080], [-1365132], [-180247], [1877388], [-825108], [196944], [-20874], [770024], [1751693], [1726926], [2200607], [-2040858], [-433153], [-2206626], [-2176069], [-380136], [-3435988], [1577177], [2087442], [542426], [-4160538], [-1690176], [-846061], [-1259101], [-4374367], [-4778268], [2986176], [-238633], [-1668564], [3052421], [-3162228], [-2761690], [-2468322], [838931], [-481867], [4835496], [-1644492], [6599030], [253836], [-5039848], [-419958], [-7286226], [-7484197], [-339816], [280499], [2415216], [8097462], [-3523338], [4272407], [3805853], [-7463272], [-6262902], [13442550], [12504606], [6802715], [-375517], [3361644], [9312144], [10300716], [-4143277], [-6427288], [-2118238], [-15339924], [10412399], [2886257], [-14858676], [-9596764], [10852932], [12035154], [-5807112], [19109258], [-2155450], [8695434], [781896], [11351768], [-19940394], [-17536711], [17821998], [-8290627], [16921934], [9407180], [18158076], [-28146808], [7504361], [-31849908], [-23874973], [8436648], [25597103], [-12234252], [-28253844], [-20222125], [2926446], [-11997583], [28787748], [6765842], [16444314], [11744003], [-25938012], [-4811320], [-8801232], [-14782309], [-24721920], [-16391167], [34748094], [-44267188], [5789718], [6958943], [11328078], [33800207], [-10191210], [27858462], [12682776], [31743440], [18189498], [26126658], [6446532], [21614633], [-21610534], [-15136939], [-19690926], [-23678862], [43739627], [47449662], [-12005947], [54478739], [-11343552], [25767779], [70819950], [692645], [40346459], [15527561], [56296056], [40958177], [-36360006], [44634810], [22465110], [-16444639], [46683707], [-27525547], [31055154], [64008929], [-76042464], [-60813499], [2435814], [-21736998], [-78751506], [42707222], [34549430], [16541994], [-50871810], [-26088864], [-28232356], [86690618], [38704586], [-6617124], [27689034], [-53453941], [30995754], [-90345636], [-34229827], [27687827], [57935130], [-49008751], [32276484], [-26198094], [-69309850], [116864093], [-86354934], [64639128], [30612294], [26478197], [-103523521], [62279970], [130336256], [30167640], [34363619], [-117851382], [-64731078], [-49370557], [-47301990], [30205109], [5414060], [95364695], [-87198522], [-42047428], [132969576], [-97400350], [-92239596], [-122332878], [-162372948], [71128626], [42278453], [53882123], [153039743], [1509126], [-7813242], [-148665265], [-101620974], [64205405], [131007822], [35290502], [-37672158], [-58906452], [-13071427], [136293042], [-116854908], [166307957], [72593924], [26800140], [46065566], [-169277431], [-116439642], [-171453178], [-4318645], [152306634], [-54930712], [170761242], [35629848], [147128444], [23493978], [235596611], [-67423624], [113074106], [-222564919], [202103225], [38547233], [220176606], [140026235], [190438463], [145090032], [133875696], [121320872], [-60211254], [178187087], [104552951], [-191632542], [-118592959], [5517744], [186472616], [-83660736], [-48697614], [-45751308], [60680940], [-196665144], [120342647], [48822816], [-129291115], [218543400], [109087794], [-183347356], [-220549978], [-112433052], [-156036847], [-134855046], [329593076], [-169332043], [90871596], [-244113115], [-67802436], [209707958], [-143976774], [-77451780], [-217642260], [55801925], [383540166], [-84041527], [-223489614], [197017548], [-216085495], [-157478754], [-172121440], [328295747], [20995524], [-264571819], [-222361182], [-424053286], [-17893261], [305080283], [-256745310], [-350112648], [-217121011], [377294814], [416621129], [-398281410], [-124737157], [-43859916], [-456547897], [186146964], [247520117], [280747712], [-54032449], [322313400], [-136188702], [-17462053], [-192103914], [76023822], [-57562003], [-427356109], [-69908340], [-179981988], [5648195], [-343742326], [424776246], [-526733233], [-155647165], [-93515802], [412212552], [295371138], [-364322550], [161927118], [-440652613], [251037414], [-392913990], [-213334608], [278165180], [260200475], [43827048], [173666972], [164509295], [372241740], [569320061], [236457546], [-142458468], [441351512], [-586327255], [122844624], [43008378], [-2322193], [-410762940], [-486580075], [268860534], [-248573946], [-261441016], [-113761032], [-418866436], [-597101262], [460149293], [303346646], [66275588], [419596002], [-20201971], [-396992544], [491897460], [-621741307], [4018770], [271180913], [214673645], [-345818556], [37568117], [-240747828], [-240121702], [689528580], [234080747], [395351580], [-66699636], [276862664], [-123561823], [744968568], [479818787], [113814396], [-697790713], [39101676], [-652008054], [-757222045], [-219487795], [111300540], [-32193606], [-206922037], [-518944632], [-25336812], [810182856], [158692631], [338475006], [-470889078], [-287503234], [642157632], [-259260048], [705431868], [528119765], [-58499070]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_300_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_300_6_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_300_6_a_a(:prec:=1)
    chi := MakeCharacter_300_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_300_6_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_300_6_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_300_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<7,R![91, 1]>],Snew);
    return Vf;
end function;