
// Make newform 8.21.d.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_8_d_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_8_21_d_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_8_21_d_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_8_d();"
function MakeCharacter_8_d()
    N := 8;
    order := 2;
    char_gens := [7, 5];
    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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_8_d_Hecke();"
function MakeCharacter_8_d_Hecke(Kf)
    N := 8;
    order := 2;
    char_gens := [7, 5];
    char_values := [[-1], [-1]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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 := 21;
    raw_aps := [[1024], [114226], [0], [0], [-42383023726], [0], [-3353535763774], [-1014654432526], [0], [0], [0], [0], [-25418071370591326], [2781113986388498], [0], [0], [-173912197184497198], [0], [-356137514166464974], [0], [-6016717170316692574], [0], [-31022856480301602574], [61202446863210984674], [-50009130514058267902], [0], [0], [362646817436936119826], [0], [152948156188402348226], [0], [2172940798248513783602], [2116403153546311797026], [2542390685813946237074], [0], [0], [0], [70679155446967099826], [0], [0], [-39223372648761065424526], [0], [0], [120383610792393698116226], [0], [0], [-282329070495580832698126], [0], [-221143024593650726842702], [0], [-785873597218067503579102], [0], [713387456315373240363074], [-1557659566675807116852526], [1504072781429979874730498], [0], [0], [0], [0], [2044201570035889992261602], [-768741734404861133171374], [0], [2648673660753493352909426], [0], [15361771727312529620841698], [0], [-30869067915369365901199726], [1119420523751313423925826], [-50399363804099416022118574], [0], [56278995596599716081204098], [0], [0], [0], [-38300763970462329736039726], [0], [0], [0], [-213089545138808110344990526], [127052872960919940563429474], [-316628626680511185268219726], [0], [0], [-240752313245869391105194174], [0], [130434004735451145461743826], [-595619891910556202636166526], [694600393489111328145621026], [0], [0], [-922043394124427772387481102], [0], [0], [1592017643284972790583958802], [1906410415724761290365670002], [0], [0], [-2578847374310616289494728926], [2215975393325985240020878226], [0], [-4193734846312413088332580174], [0], [2783556262732588270063491698], [-6379846809300183052622884126], [-7293818093842084499630007598], [-8179427373801150559795819774], [-7681620219008736577914884974], [3000070015661982055752489026], [0], [8693914607092565996378626274], [0], [0], [14724151424646361280371684898], [6025243609314221615043602], [0], [17717516320241974305527356802], [-23577429124348807443383415502], [0], [0], [19664596970449202819587161074], [0], [-28219027856515271390638408702], [0], [43426496426662293004708894226], [-38470608195480230913220176526], [0], [0], [0], [0], [0], [56880724049162662781047489202], [0], [0], [0], [-71600271564380232994720976926], [-43262309314102930835906483326], [0], [-182292238457966560495503884302], [0], [-124018793203811569402461487198], [-111744825141209354978463874798], [0], [0], [219923573660836889621248803026], [0], [0], [0], [-35015307561134012444840239774], [-425331847184040057801762737326], [0], [0], [559187941648255434910886673602], [450563832873710856514085511026], [0], [-739354968149790967985726043502], [0], [0], [-933454634145842574031780377598], [734116307275545394923391182626], [0], [-842381928210016083425331413902], [-907851930598533072717177804574], [0], [-767973221757635946827978518126], [-546005287302852786846817613374], [0], [0], [0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8_21_d_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_8_21_d_a(:prec:=1)
    chi := MakeCharacter_8_d();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 21));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 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_8_21_d_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_8_21_d_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,21,sign)));
    Vf := Kernel([<3,R![-114226, 1]>],Snew);
    return Vf;
end function;