
// Make newform 21.8.a.b in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_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_21_8_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_21_8_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
        poly := [-266, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_21_a();"
function MakeCharacter_21_a()
    N := 21;
    order := 1;
    char_gens := [8, 10];
    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_21_a_Hecke(Kf)
    return MakeCharacter_21_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 := [[-4, -1], [-27, 0], [-190, 20], [343, 0], [-2312, -308], [3710, 288], [-14570, 556], [-31396, -936], [41660, -1060], [-219042, 2088], [-9544, -10152], [-353266, -3024], [-32298, 39540], [242132, 11952], [-795544, 16088], [1044286, -31136], [-523820, -53384], [33830, -38664], [-2258860, 36936], [46356, -38172], [478706, -290808], [1134584, 53784], [-3772188, 159984], [649718, 485012], [8194298, -122184], [1219978, -341588], [2350256, -777384], [62744, 894428], [5432726, 761184], [2392434, 242208], [-2654776, 1218312], [-5867212, -2101168], [-15069854, -535496], [2842364, 542448], [30167966, -1112224], [-17378776, -1369872], [-3840490, 2156472], [-39234220, 4369464], [7723632, -1871016], [-19387390, 783260], [2146072, 3408724], [7939190, 364608], [-60759556, 3855236], [87753650, 3845232], [13555950, 547008], [-87970984, -5868720], [-112843204, 2293776], [49906304, -7515072], [95535052, -7096712], [-144672970, -92880], [11007650, 2673464], [42797316, 9270300], [-22333510, 18091944], [-276303468, -12809784], [283541390, -11805676], [391928332, 6258412], [41779210, 878068], [92631944, 18787464], [119814686, -1253664], [-76953966, -20153832], [-174845884, -4556952], [-697642806, 1516236], [311399900, 14587416], [-174900176, 16392184], [191678906, -11614176], [28794566, -13541584], [-670649452, 6296976], [28394690, -40047696], [1076257264, -22410620], [-396275530, 4778424], [-661980266, -4225268], [-510129484, -47569036], [556939856, 43566048], [988216190, -27482400], [732447620, -34270704], [1063315616, -25425712], [-457687274, 50752936], [102175814, -88705800], [210288746, -42310552], [824208242, 108240840], [94203564, 103586856], [-963513010, -58269024], [811576540, -131288348], [1118846834, 57419568], [1776214712, 35972784], [478772864, 127868708], [275639394, -75154848], [-2229126022, 65669184], [-837659214, -150894612], [-388749352, -171762120], [-648065860, -105810376], [404593144, 87821464], [404667464, 182499840], [68437056, -125688012], [-2457563476, 1677024], [2023362936, -123853968], [-845112934, -33905932], [-3152753810, -59777444], [-4244416324, -33543072], [3748248374, 65644704], [2284849940, 20367576], [-2120867786, -177736496], [-3296576132, -101143352], [990507458, 363122024], [458588492, 289135368], [-1119043726, 111909168], [-3925342428, 236996328], [-1685262202, -152253652], [5778812884, -133249004], [4009159994, 340498800], [-6724627120, -111323664], [-1666715794, -209307888], [81613506, 97584024], [-4551924772, 79618464], [-8272014112, 217890504], [-3673534118, -117454328], [418301660, -341146440], [3492346624, 176152072], [-4132998370, -522651688], [-2853726384, 358637964], [-1021188178, -58555080], [1393783442, -101988720], [2418904298, -13744228], [1824479872, -406470956], [-9186859900, -165855600], [3457049598, -155083896], [6009467390, -790229952], [-3639126016, 744638528], [-2795134720, -436497480], [-6158724034, -58847040], [-11197338028, 441156024], [6765867348, 532773204], [-4432971232, 630496800], [-9967229074, 47006208], [-3590509210, 390598196], [-395135230, -454389264], [-9770902214, -385504388], [23815176116, -169587648], [-1713320910, 1018759356], [-17589454262, -162311600], [-6705926836, -936198000], [3476555246, -1419911536], [19371701984, 387463176], [-8843196360, 79929132], [-18964025986, 261098784], [15266627376, 315260664], [-4781608930, 1198131480], [-23653980122, 284002228], [2490422612, 1449792648], [-17998116028, 747396572], [13487253062, 1230091920], [-4083748746, -308294340], [-10948225300, -188707968], [2424211040, -1839263368], [10329471332, -206796240], [-12956448132, 806091876], [26229609416, 42144192], [-21929981746, 102947300], [11367049274, 74596320], [15650309434, -1777398092], [-42226574128, 312932012], [17358730314, -451319232], [367416128, 1110683304], [19890369260, 294625424], [-13151877398, 1020403816], [-20775401384, 1226009152], [30898154432, -929386656], [494084798, -2962313640]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_21_8_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_21_8_a_b(:prec:=2)
    chi := MakeCharacter_21_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(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_21_8_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_21_8_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<2,R![-246, 9, 1]>],Snew);
    return Vf;
end function;