
// Make newform 138.8.a.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_138_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_138_8_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_138_8_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_138_a();"
function MakeCharacter_138_a()
    N := 138;
    order := 1;
    char_gens := [47, 97];
    v := [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_138_a_Hecke(Kf)
    return MakeCharacter_138_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 := 8;
    raw_aps := [[8], [27], [-230], [106], [-4326], [-5426], [-3300], [-4140], [12167], [-150186], [-307192], [-55200], [270130], [36264], [494224], [646646], [-387948], [-2060876], [-17664], [-3580320], [484550], [-2167314], [381182], [-628620], [13964418], [-5655606], [-1644478], [2076158], [20740136], [-1488164], [-20163996], [9077992], [8085576], [-25347372], [52462938], [29671308], [92828272], [16920804], [22206056], [104445246], [99881456], [-84520912], [-15869160], [12282302], [-5537982], [59133158], [-109893700], [-116685564], [265411790], [-261307056], [65219050], [-195421984], [-191667486], [69603954], [-282130542], [-386040308], [160166682], [-536017052], [-387882478], [64176436], [-404594028], [-67512018], [-10556660], [1002121096], [-302654618], [-215289386], [284305804], [163181630], [-138238616], [248820554], [-265446266], [-853319184], [687321382], [-129619136], [1119856740], [-1840613828], [-1897102086], [-102854418], [-1009833092], [-2187258010], [-108610154], [-1759258916], [1434287748], [2266003054], [398793536], [-932547132], [1464432458], [-1186633918], [-3046666950], [-1667512360], [434566026], [-2317927224], [-883549864], [1723256368], [273182620], [1241493444], [-909231850], [-32038136], [878115608], [-4624234534], [2564623636], [-4262842038], [5629409222], [3893607724], [2675667424], [-1684674286], [8086125712], [8973796482], [-1149054608], [-9044683190], [5120280932], [-5890489492], [-2239811804], [782323260], [8448438898], [-6398667764], [7312414592], [-4683120640], [4203247514], [-147561342], [-4861345444], [3662609042], [11535317682], [3615968808], [-3407284684], [-5093583778], [-13437170324], [-4033647672], [-2745137766], [17128048188], [-10577445860], [-9753597668], [-1364134538], [4553222056], [2915666070], [3506713366], [-8566898370], [-12482242696], [-1566002766], [14209822822], [11180497108], [-19565365446], [15737168088], [8312803386], [-26365334982], [-19439479908], [-2569497762], [-12315242926], [5772910844], [9181395896], [-24260518670], [2305045348], [21973985148], [11402536328], [-5038960628], [-11968104020], [29147644510], [-2776733166], [25546732994], [28026101874], [-40443733864], [11332379988], [10569367624], [13728827630], [7485885744], [54249824596], [58612618304], [-12539657270], [52524051122], [33215749042], [-53049363534], [-53775785222], [-37302958724], [11020750982], [-47768400620], [-66923629536], [38789877620], [-47425731474], [50073376898], [41154427984], [-53389963440], [-17114541774], [-20215272702], [-78772158370], [-63432871420], [685315330], [48982557710], [41308211584], [-25928537530], [9239548896], [-15624208738], [-84669496872], [-19501064952], [-47251693594], [-75583861170], [66615184328], [-7164915062], [36607855412], [-34873213064], [-6721097832], [-46464961622], [44966551996], [-29890533382], [-23215558034], [40894465170], [128073150806], [-58257697414], [-107300838980], [108708713498], [1717761140], [-61215322610], [148304296014], [-147523550330], [-151582248726], [-155984662320], [78461343854], [-26281384276], [26895610238], [-26004818344], [149151053622], [-9327702786], [-199513917794], [-11851872546], [164024888402], [-168072020900], [130467993606], [68650164796], [143287850016], [21058258150], [166104377748], [-115541775518], [-48980166900], [122719826990], [-43965330666], [193230830828], [-34223361764], [-113985173110], [97773084962], [244778421120], [212909936560], [124934422638], [214736567788], [-154095496488], [-170676418098], [-155351257638], [114186580936], [-234714675076], [287555591302], [-156999448932], [-171286652016], [-263352736584], [-738789960], [-146079185036], [87659413294], [-54639475802], [-86810731116], [-140776117760], [20259259944], [93706680002], [-172404798474], [305642121058], [250895943134], [32904270858], [-299723695348], [-88457790542], [334109179804], [13472739330], [-103982319520], [332576502652], [-252015965966], [144789504402], [324229742168], [418890087878], [-299702188442], [44440968394], [333167390440], [392561575140], [6891405774], [382492215874], [186948845250], [-217481949312], [16070862946], [-209013243040], [436481221380], [301263439348], [-91739456472], [-347463795886], [-383478285822], [-18190353600], [-269574389154], [260683315282], [230428772278], [-330606057858], [351671246262], [-22227645902], [-351625611830], [102809735762], [178818414378], [-221645609364], [-83682088884], [65267447478], [-514486949262], [-11472750798], [60436135524], [-339743009308], [-638732321930], [-506315412868], [-388652072952], [-190594456140], [-600430677994], [30471087408], [-141444464450], [43040028240], [-93622778116], [-223991238780], [-381921677714], [-83122091244], [-668555495848], [186028332934], [885529502354], [568319391560], [-353268395138], [215460756342], [331828886716], [-282227471292], [-63337436542], [133815733120], [729132727444], [-276868167816], [-282558376598], [259902365858], [-207237771814], [480144253576], [214673492904], [824254506556], [-53709274704], [-355698281280], [-923187634548], [-252381452458], [530539476634], [-417409294714], [589952124564], [658029248270], [230420952842], [-282473891658], [798932881356], [912519552766], [8040793276], [550512296468], [1234990060198], [-594482361908], [-332850827014], [412550481802], [658238113054], [-554670227124], [-22769758962], [-962150601204], [997297468770], [-401031858146], [-59534648392], [-90882385148], [810428852358], [337872411696], [146047553830], [-337836922924], [124700433478], [1045510494886], [1153465149438], [-303445576162], [704188219608], [-80953055540], [-355740429672], [-775331424730], [-341425775294], [-1366500332738], [-1188997685392], [-1213502755492], [-473232389470], [1532779633160], [1389097724926], [-626575923678], [-539269620720], [-766139652580], [69950313546], [226679025764], [1632237051536], [-1575430058860], [1573148408274], [644705197792], [-1955335123644], [-1218512254346], [471464158866], [-1160921360884], [-331966308580], [-1362279123052], [-1416861755382], [656419996998], [1378357012648], [-917252489368], [-1791527087106], [-1132503050906], [686002600062], [-1823747013610], [436280642108], [1200775662830], [-1225917167204], [504411291000], [-1901262419978], [-757555092532], [-1249777801976], [1591120952994], [-2003292227922], [-190186637010], [724027115312], [-12384992858], [1707920653126], [919465269896], [-1742047057192], [-2211349640536], [-1039708038984], [-1680037844518], [-1443694148408], [1045363772232], [2302461987172], [1780964473858], [-2768669030574], [-25922996978], [503346387354], [1512892649700], [635588382552]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_138_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_138_8_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_138_8_a_a(:prec:=1)
    chi := MakeCharacter_138_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 8));
    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_138_8_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_138_8_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_138_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<5,R![230, 1]>],Snew);
    return Vf;
end function;