
// Make newform 21.34.c.a in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_c_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_34_c_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_34_c_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;
    Rf_num := [[1, 0], [-9, 18]];
    Rf_basisdens := [1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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_c();"
function MakeCharacter_21_c()
    N := 21;
    order := 2;
    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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_c_Hecke();"
function MakeCharacter_21_c_Hecke(Kf)
    N := 21;
    order := 2;
    char_gens := [8, 10];
    char_values := [[-1, 0], [-1, 0]];
    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 := 34;
    raw_aps := [[0, 0], [0, 4782969], [0, 0], [-794875726270, -5640229503943], [0, 0], [0, 304331380942850972], [0, 0], [0, -128209247178222747890], [0, 0], [0, 0], [0, 469815818712928115170390], [62015414955189042309371530, 0], [0, 0], [-599843481279770126435596040, 0], [0, 0], [0, 0], [0, 0], [0, 19037014717826077851908034660], [-1969714023707640842248910943760, 0], [0, 0], [0, 147965361767551410234239840568], [-38229893911174330582646171710084, 0], [0, 0], [0, 0], [0, 44800676428536808019786697263816], [0, 0], [0, 122012932953681408830805228542862], [0, 0], [199669564845898025905278034160254, 0], [0, 0], [-102645223005105014709647913957797740, 0], [0, 0], [0, 0], [0, 25466587888088250262973044702256570], [0, 0], [-1393059818729684233991960342111098748, 0], [0, -165863494939376002783108441867249964], [-5625623082464602729965178544083838200, 0], [0, 0], [0, 0], [0, 0], [0, -1691716224542080441517785130414456420], [0, 0], [-71249409572205598192671075083398146050, 0], [0, 0], [0, -8712635994651890271693633890481516430], [173724160584541719294030919955043313168, 0], [0, 53280047929687665536463454014117551018], [0, 0], [0, -16791042765976931747061906545805590740], [0, 0], [0, 0], [0, -248514208380614569504218718707628128880], [0, 0], [0, 0], [0, 0], [0, 0], [0, 962033267948284782306363103397711980230], [-38610416664554431583192704918008082878310, 0], [0, 0], [0, -2945401418749425031176770374434035465258], [0, 0], [0, 13970208906307946627461308705922403282814], [0, 0], [0, 19042216681691841437619626215418906006128], [0, 0], [595034863100175188462200730407275641840608, 0], [994934453570883275172609419210417661144610, 0], [0, 0], [0, -115426149551324527303933032637667104778740], [0, 0], [0, 0], [0, 253527052954585147244004203788269624686106], [5126778757890537705279450417709291035376550, 0], [-3663157332846828347816129728769706992299784, 0], [0, 0], [0, 0], [0, -658185224214263869508857170622184074556116], [0, 0], [0, 1361980942361181699160857398796144482552120], [0, 0], [7058053936108073498460163497087650615506538, 0], [0, 0], [0, -63970510985936613532214866834982409185192], [0, 2830286355156868664360801681911882297681410], [0, 0], [0, 0], [154444571557261002242887457521496377557208330, 0], [0, 0], [-46041828053792809164175015338757194077016940, 0], [0, 0], [0, 0], [376935203634205574468010999079724360023215700, 0], [0, 0], [-618940946586932343752601911071236709125295136, 0], [0, 0], [0, 0], [0, 0], [0, 81491449032196205030696083459558773991575882], [-548506410719312247993782163701686971738978478, 0], [-1948477792436142646454403322315393502739367720, 0], [0, 0], [0, 0], [0, 0], [5881608511951207106355890190932179499646140912, 0], [0, 106237034771239311236919592629905020754445624], [0, 0], [0, 0], [0, 0], [0, 837639275824422827446306814956943328259802200], [0, 362911556781550966208917493974825074581555914], [-8156764049619663369306019617181844183827451530, 0], [0, 0], [0, 724434038644938661113010018451879406381748890], [-19378327215408455230186996611328766730008410708, 0], [0, 0], [0, -395674373970184959936955453454247188624118402], [0, 0], [0, 0], [0, 0], [0, -2952170336930391676765776682335713188214734860], [80491038910457336351914107165816971695477094350, 0], [0, 0], [0, 0], [0, -2941573733642561387821921268660526594787070510], [0, 0], [216361137168149711559259510350745386822381604246, 0], [0, 0], [0, -21001061744915576249929367684448448507339508226], [0, 10151202593279494113768683154491846994705963308], [180333223332693560886579935490569569244900682224, 0], [0, 0], [-210818365640437931013534369896754595009466697548, 0], [324544198779797354057084558884716828566911929190, 0], [0, 0], [0, -19763586115601769489905150556561702249620613920], [0, 0], [0, 69568930859411988622642218157996759274743079154], [0, 0], [0, 0], [0, 28926549353321919493298917116371650188921425450], [0, 0], [658757425563662953369606208611145607192028612940, 0], [0, 0], [0, 117140293397827021551896414255872031327512535540], [0, 0], [0, 207381654871548395718926300513272372140433357212], [0, 0], [0, 150828568335601431545278734400930755965042165830], [0, 0], [-7040438289637322957128036327061710419331560748450, 0], [0, 0], [7850759567052174999424914429108312900273580731640, 0], [0, 0], [-11611572964761198900897864799433797632140384574680, 0], [0, 0], [8126827013694546721315615179574193498171079179956, 0], [0, 0], [0, 458897662433824702643620784922961924675638838496], [0, 0], [0, 0], [0, 0], [-35690572339009758055067538538298390009571747250540, 0], [0, 0], [0, 0], [0, 0], [-21337148372323240234917230410659725595616370118572, 0], [0, -1969488008721840411964953296534863343033988279684]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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