
// Make newform 21.33.h.a in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_h();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_h_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_21_33_h_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_21_33_h_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
        poly := [1, -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_h();"
function MakeCharacter_21_h()
    N := 21;
    order := 6;
    char_gens := [8, 10];
    v := [3, 2];
    // 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_21_h_Hecke();"
function MakeCharacter_21_h_Hecke(Kf)
    N := 21;
    order := 6;
    char_gens := [8, 10];
    char_values := [[-1, 0], [-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 := 33;
    raw_aps := [[0, 0], [0, -43046721], [0, 0], [36372171914815, -7592384673471], [0, 0], [122181402203975039, 0], [0, 0], [7821153231347348161, -7821153231347348161], [0, 0], [0, 0], [0, 1413723680265064241474113], [21556186719627646822930561, -21556186719627646822930561], [0, 0], [272889898556260470508993727, 0], [0, 0], [0, 0], [0, 0], [39541057150634567761997420926, -39541057150634567761997420926], [0, -5313292745304228970622516159], [0, 0], [0, -767515778351719658853870420479], [-2026961502021606539826450603839, 2026961502021606539826450603839], [0, 0], [0, 0], [121183196748580924499037281651714, 0], [0, 0], [-86060596338540190184078876971199, 86060596338540190184078876971199], [0, 0], [0, -445368363765905775085343556554879], [0, 0], [7155992185412426137645799475297599, 0], [0, 0], [0, 0], [-37900102398560026780523241051088321, 0], [0, 0], [0, -72485696297097733908845982925101314], [0, -270605925771741293054475559469638274], [355608894508514511242900472635127166, -355608894508514511242900472635127166], [0, 0], [0, 0], [0, 0], [-1427214112901964123341449112250582913, 0], [0, 0], [0, 2279427394021683286123502611739887873], [0, 0], [0, 8450925486438017053980485093088300286], [30341656661683649914459439473885871234, 0], [27539860251416513901853053932222149634, 0], [0, 0], [-42980961036758539149363789752572498559, 42980961036758539149363789752572498559], [0, 0], [0, 0], [0, 191462061732589875600926288328700850686], [0, 0], [0, 0], [0, 0], [0, 0], [-733880973405166089649946350919470947842, 733880973405166089649946350919470947842], [0, -1932655809402730222636726327731188131199], [0, 0], [0, -3086320798993426730820381729743306387519], [0, 0], [-143223266809830168500750961124796004673, 0], [0, 0], [-66697128768271112822077977523982759807, 66697128768271112822077977523982759807], [0, 0], [-26470156971149794612214881388867262661439, 26470156971149794612214881388867262661439], [-18506504249918389923555259898689652145793, 0], [0, 0], [93214500052073465694642081139197188496002, 0], [0, 0], [0, 0], [0, -144272186611762090897672126107895722861887], [3173806275674532317541990028268660021761, -3173806275674532317541990028268660021761], [-275275037010229168463753308542096676925761, 0], [0, 0], [0, 0], [738940142330219443052718017064577694552833, -738940142330219443052718017064577694552833], [0, 0], [0, -924279705212912512989222693885130496440319], [0, 0], [-1858574650740445416970811869417611698119681, 0], [0, 0], [2593821189201001474988616356233395208130687, 0], [551726852935627816740342545462940744100606, -551726852935627816740342545462940744100606], [0, 0], [0, 0], [7058194178528162401559072945476961386260481, -7058194178528162401559072945476961386260481], [0, 0], [6174769879282140326336370551955514411462079, 0], [0, 0], [0, 0], [0, -18283187608519085678764021893768566211695807], [0, 0], [23876163789972403166385631675067592730113601, -23876163789972403166385631675067592730113601], [0, 0], [0, 0], [0, 0], [57963697994324710187395453327979574259417153, -57963697994324710187395453327979574259417153], [96862484695695138757449840623736311324355841, -96862484695695138757449840623736311324355841], [20540337126753145726294287264869601061837442, 0], [0, 0], [0, 0], [0, 0], [0, 211509617623634054870710789124969194412594881], [0, -166895561852139837573895303777246026344926847], [0, 0], [0, 0], [0, 0], [246882923333614677685809681248556752985619199, 0], [659943275201501468120021703883189019440069441, -659943275201501468120021703883189019440069441], [0, -775209309066209112547287102322329990803114882], [0, 0], [0, -719355282710641125767049969647602865849863487], [-1158477195440059608338922730766350977406403326, 0], [0, 0], [-474213436276258610538884144353442564908443841, 0], [0, 0], [0, 0], [0, 0], [0, 2412737663216466918792452714471664185035374721], [3410605741007626267032647709514648543519751039, 0], [0, 0], [0, 0], [-4134427383327996016497461696638702253498464319, 4134427383327996016497461696638702253498464319], [0, 0], [-3975687364055008797304608424216330015393064834, 3975687364055008797304608424216330015393064834], [0, 0], [4213692804492345751381362982957243417274335679, 0], [7424626627812812981408458209324265519083031681, -7424626627812812981408458209324265519083031681], [0, -9670558059732723056211759854096755072918134719], [0, 0], [6839227723373875977848567960999706031494381121, -6839227723373875977848567960999706031494381121], [-23239145123849019008285074807944900167371594366, 0], [0, 0], [-29741334557240022863970142299624204686744627713, 0], [0, 0], [0, 17113042164745494185396277490146074488541981566], [0, 0], [0, 0], [64195177574369404052165643881335159514511434114, 0], [0, 0], [0, -15353552616819981376433645566629644846268916994], [0, 0], [0, 72273226308285650407030128338004281111661402241], [0, 0], [-21805768172205989671824966763813591068622943233, 0], [0, 0], [173105941948937586463823417604699427688623086718, -173105941948937586463823417604699427688623086718], [0, 0], [-184679267578569017933390530289127372351391888514, 184679267578569017933390530289127372351391888514], [0, 0], [-166493135138811078398621756837298252648553176001, 0], [0, 0], [0, -410946524026955885398179477103820512464284080319], [0, 0], [-447245952776205426584703664560570534373425175487, 447245952776205426584703664560570534373425175487], [0, 0], [572662496282119242710500826644245304322369870207, 0], [0, 0], [0, 0], [0, 0], [1169092298208241529130377940138216785187592157759, 0], [0, 0], [0, 0], [0, 0], [0, 1350238492898573786342874912471846443465051356993], [0, 1885483660985521805588175302303763953941618780801]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_h_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_21_33_h_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_21_33_h_a(:prec:=2)
    chi := MakeCharacter_21_h();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 33));
    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_33_h_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_21_33_h_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_h();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,33,sign)));
    Vf := Kernel([<2,R![0, 1]>],Snew);
    return Vf;
end function;