
// Make newform 4.21.b.a in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_4_b_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_4_21_b_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_4_21_b_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_4_b();"
function MakeCharacter_4_b()
    N := 4;
    order := 2;
    char_gens := [3];
    v := [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_4_b_Hecke();"
function MakeCharacter_4_b_Hecke(Kf)
    N := 4;
    order := 2;
    char_gens := [3];
    char_values := [[-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], [0], [-19306574], [0], [0], [190840318802], [750325121602], [0], [0], [203154876160402], [0], [-9492206529013198], [16082418088944802], [0], [0], [263609364120076402], [0], [342453856112605202], [0], [0], [5395059597962887202], [0], [0], [10944684939688527202], [-72063723240789129598], [-90603730003423137998], [0], [0], [425499704429379886802], [-161005614064246907198], [0], [0], [3586127681861140512802], [0], [10125961829865855062002], [0], [-15397568439659177573998], [0], [0], [-47839205693410263818798], [0], [-37446452600007179840398], [0], [-60318766893634662719998], [25379688277227929716402], [0], [0], [0], [0], [-699659920163653652283598], [35384427860012243261602], [0], [633359480350766841508802], [0], [796585930726884555392002], [0], [3387758029266057972875602], [0], [2223004843211587876873202], [5983530849957537176981602], [0], [-6501542376456241846785998], [0], [0], [-12580963163021104930759198], [14952916030126950114179602], [0], [-25694988530736769869835198], [0], [34807340013595934462778002], [-48624864441258409200361598], [0], [0], [44045565890354508469369202], [0], [0], [-56061645460889110884638798], [191845742859109242762008402], [116324937800881824270248002], [-258470588014702981022359198], [0], [-269938764496079818435035598], [0], [446176310105213333021649602], [0], [0], [599634101754681816877376002], [-645459747035333403800075998], [-836823823227452059492346798], [0], [0], [0], [0], [0], [0], [0], [-561410932939642220960681198], [-2377778036132379720130693598], [0], [-3692133768776967437544025198], [0], [5697200630142769828666370002], [0], [3608526966331896997409669602], [0], [5503689643675192973806291202], [0], [9856211521493484715062056002], [0], [-6988425408020546547813347998], [0], [-12604941923585927586630037198], [2220531145784578762471037602], [0], [0], [-23409633719618294973775763198], [0], [0], [-8906733274553336566336999598], [0], [-47429206162319352849142798], [-37414861940078059825864348798], [29071675810130988773764017202], [0], [0], [-45336459936854628205530645998], [53053988608741119237753754802], [0], [0], [8228450383524641479139091602], [0], [0], [0], [114696973637222916486513222002], [-113528188633641097234862720798], [-128669864677929540642551174398], [126469792558589295971801505202], [0], [-31078304544713928339985087598], [-221896867989878706070677647198], [0], [-196845014760091587516508147598], [0], [0], [211195642359229840265763344402], [0], [308098443333128844337328348402], [-393620005856433546031225731998], [0], [0], [-318019809484768427406864490798], [214543698796412759512397393602], [0], [0], [0], [0], [0], [-169277113849725360702193897598], [706885702129927735364789200802], [1019716612235613262141642062802], [0], [-919776498831102510826620397598], [0], [0], [-1329845128220374862438556164798], [0], [0], [-1506554041828809337781158395598]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4_21_b_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_4_21_b_a(:prec:=1)
    chi := MakeCharacter_4_b();
    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_4_21_b_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_4_21_b_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,21,sign)));
    Vf := Kernel([<3,R![0, 1]>],Snew);
    return Vf;
end function;