
// Make newform 91.8.a.a in Magma, downloaded from the LMFDB on 04 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_91_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_91_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_91_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_91_a();"
function MakeCharacter_91_a()
    N := 91;
    order := 1;
    char_gens := [66, 15];
    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_91_a_Hecke(Kf)
    return MakeCharacter_91_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]];
    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 := [[22], [21], [140], [-343], [5051], [-2197], [27384], [-32690], [-23085], [-14068], [-203007], [544041], [-352079], [340412], [406329], [-1909680], [-2867214], [-216419], [2538043], [-2071872], [185913], [-954631], [5649672], [-4673830], [-13686645], [-8718857], [4209254], [7897772], [-5570694], [15953849], [26290959], [-40418560], [45012594], [-4221756], [62286729], [-68693320], [91955689], [48067300], [31248560], [-21119798], [29312014], [141828211], [-26521312], [-69059476], [17161181], [118181196], [39659818], [206177083], [-28809480], [70458248], [122216067], [213571962], [-136379698], [40292357], [33978616], [-81530704], [275400405], [531520759], [76939510], [591776656], [-30249275], [-836752994], [-563858876], [1389234], [-449942122], [-500789379], [-610962589], [350592601], [-103915808], [-274408106], [141846173], [-1618832564], [-67547424], [-53762176], [-1861969128], [1056393975], [-852713172], [2387999964], [268331316], [1410009062], [676421697], [-2383293589], [-967411938], [1681282666], [-3468470754], [-831727140], [3447214374], [2785308646], [2570924860], [1476490528], [-3531731700], [2184781144], [-3554494142], [-1408324496], [-745092935], [-2403845724], [-2330672582], [5596059294], [5275706317], [-5091382498], [982087780], [-4938990199], [-5929493899], [-3426926131], [-2358158806], [2419778830], [-4707797038], [3089897118], [-3005623239], [-6454467306], [-3254116054], [4939227367], [8530480086], [11605393114], [-209335558], [-6380800569], [3226565384], [2706567808], [3689515236], [-7165405854], [-7044552284], [-2720241977], [59283483], [-9300314397], [-9396473800], [14998175208], [6035012691], [7211834910], [-5394393718], [-10097677884], [-19180146072], [6371786640], [-9598201409], [4006411670], [-7159606503], [18069412193], [-7430249162], [-7027362608], [9940130627], [8695579974], [-4020869020], [-23990493974], [11959560519], [27733603788], [-5492382070], [-28171910137], [7200576656], [12496165430], [-8085319165], [11931662436], [26111751835], [12044633202], [-9629263642], [-17870738858], [-27775069402], [10585031172], [546661529], [13279878157], [-42971003964], [29176040950], [-39081932168], [-4093366562], [30039874342], [-32653547073], [41798573226], [13331571624], [-2426125579], [32715206209], [-51734792240], [-7571865770], [18839878740], [24683661198], [-45745593585], [-7880754136], [14567189210], [-62115696356], [-19475957555], [-11837344606], [-43912722588], [34853837095], [31401335666], [37231133583], [-3051505210], [-38049763241], [70206480348], [31609361938], [21299465296], [84399969976], [17437711516], [-14660030539], [-31464719468], [87578859279], [-94809324322], [-107766944502], [-94593646990], [-13049014230], [103386707288], [21886017042], [63162718626], [-25774684824], [73280988715], [29221893176], [113038122876], [-79631515166], [-8451438758], [-1025733926], [118663394892], [127188350934], [136232758818], [-43581410579], [9210790821], [83863338153], [-24287537783], [-43970298400], [34813603615], [85293650785], [17339843903], [-58978599820], [74589625874], [-27515079513], [-152024281114], [-50935366643], [-116461080728], [22178374478], [-180208853573], [111354145856], [-61317280108], [-58329816023], [46847520524], [86021350017], [-69813133904], [-72207923564], [167410268888], [-39289672789], [-217245291165], [-62491839627], [-170031454313], [-75485784107], [176815062636], [39431081788], [26537409441], [-165765123825], [-233328773496], [111186393382], [-161305474659], [11169393914], [280249356961], [-140115778754], [-117983731421], [-28784620695], [153901897991], [58185368850], [-73784364286], [-35920951346], [109454658635], [-96470714024], [219491131934], [76892644290], [272989487337], [-7499298205], [-79133701048], [-192037221122], [304227654122], [385997317517], [16522705465], [-42821310952], [-115326492909], [-76711788979], [-231103384811], [25865226692], [-196389401411], [19814172809], [-286936062119], [-23762027008], [234020537506], [-182138396440], [117392679860], [136388000193], [63693727840], [504493359580], [-181733418734], [-88585218942], [-184923638942], [-150437003066], [304247378204], [-534496384029], [-224964813839], [377402856126], [183427703627], [198466989336], [-219016867450], [439826537969], [-77329661142], [414252173286], [248040252910], [-319806608590], [20783907088], [-559686221465], [-214833167465], [208581299906], [164557438290], [168108728313], [561081035176], [424069570808], [-425336322564], [18194872572], [-199589632686], [668081610966], [339426887822], [-117809634368], [-385803957294], [79031687998], [222068873349], [-8631785886], [50332004914], [-154613456788], [103470953460], [-137580573714], [-472820359634], [335969562273], [-94708371961], [-299276984264], [505070485185], [207181235270], [530902178874], [-336741235418], [-2522486105], [-182735943712], [-165504471656], [778466996442], [-38063107445], [-910298551116], [414254305346], [-244743556764], [-769494810899], [814960651358], [30064209254], [-192531820593], [449021047454], [500030513374], [-527910935198], [70022177773], [889603315650], [-580139792111], [-697052176001], [26608105064], [-850164951482], [777668334892], [-552660298029], [120152712757], [-105993835850], [613256095008], [819653739851], [155383991994], [-55322249450], [626172256192], [-716854274073], [622716303090], [-832914764933], [-48672854424], [-266303202534], [724055032778], [-528648867897], [61528044607], [-10923612931], [-768363073169], [127682305156], [-1089690279506], [-1099796764407], [508470348558], [1061563962627], [-318995276214], [91735539324], [-111428367568], [239132019568], [349747546148], [-849972760747], [87755558933], [-7140391104], [-570477186972], [-866106669092], [922630624260], [-1375326064040], [-1737578686616], [-79293287892], [1194606989632], [-1609599637642], [-699010245773], [-463250547388], [735807253104], [-1433824654274], [885288402558], [-995116052115], [-1097884923436], [1285825649578], [-1628192891573], [-82162831541], [-144535781964], [599392638462], [334018960482], [-879233713456], [-1066047158210], [-1266583760924], [-1220061752385], [-760077519264], [1804539036244], [28480275261], [-2006736191444], [-1940980326964], [-551414627820], [-2210035246342], [227607582532], [-421374797534], [-995551857572], [722205361635], [1968765231930], [2271200305410], [2214580738496], [452489718345], [-1134371461566], [-738209699622], [278398133818], [-483439134764], [-1836439249652], [404015411702], [1793403451056], [119874419414], [-397441803372], [-1606041255714], [881880515334]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_91_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_91_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_91_8_a_a(:prec:=1)
    chi := MakeCharacter_91_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_91_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_91_8_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_91_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<2,R![-22, 1]>],Snew);
    return Vf;
end function;