
// Make newform 21.5.f.a in Magma, downloaded from the LMFDB on 12 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_f_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_5_f_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_5_f_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_f();"
function MakeCharacter_21_f()
    N := 21;
    order := 6;
    char_gens := [8, 10];
    v := [6, 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_f_Hecke();"
function MakeCharacter_21_f_Hecke(Kf)
    N := 21;
    order := 6;
    char_gens := [8, 10];
    char_values := [[1, 0], [0, 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 := 5;
    raw_aps := [[0, -2], [3, 3], [12, -6], [56, -35], [-194, 194], [-95, 190], [-140, -140], [302, -151], [0, 112], [1040, 0], [-673, -673], [0, 1075], [754, -1508], [-1087, 0], [2500, -1250], [2200, -2200], [-3088, -3088], [808, -404], [-2375, 2375], [-8938, 0], [5269, 5269], [0, -8147], [3854, -7708], [15752, -7876], [-2020, 4040], [4038, 4038], [-10542, 5271], [0, 10732], [-7289, 7289], [-4330, 0], [18899, 0], [25284, -12642], [-20876, 20876], [16819, -33638], [0, -4412], [-21362, 21362], [-16828, -16828], [0, 12946], [-20342, 40684], [-6656, 3328], [-5210, 5210], [-6597, 13194], [0, -13646], [-1397, 1397], [25244, 0], [13972, 13972], [-13366, 0], [-14596, 29192], [25254, 25254], [-42726, 21363], [0, -17198], [-66970, 0], [16296, 16296], [55920, -111840], [-85836, 42918], [40792, -40792], [41374, 41374], [-4408, 2204], [101389, -101389], [31916, 0], [10733, 10733], [-55388, 110776], [85587, -171174], [19190, 19190], [-31046, 15523], [0, 10588], [0, 127327], [211553, 0], [25084, -25084], [-2212, 4424], [4218, 4218], [0, 234508], [-58869, -58869], [0, 97837], [74105, 0], [-298632, 149316], [-85310, 85310], [-81034, 40517], [0, 59704], [-20341, -20341], [-5594, 11188], [258923, 0], [95050, -95050], [83421, -166842], [22456, -11228], [0, 118876], [-323110, 0], [0, -133157], [24392, -48784], [-412345, 0], [299732, -149866], [129192, 129192], [-82337, 82337], [812, 0], [0, -409649], [87486, -174972], [349596, -174798], [-175496, -175496], [-151834, 75917], [0, -35303], [153398, 0], [96070, -96070], [-46810, -46810], [0, -537770], [-180371, 180371], [319891, 319891], [-177220, 354440], [-370012, 185006], [-323972, 323972], [-217657, 435314], [121746, -60873], [-509270, 509270], [-141154, 0], [-294003, -294003], [135542, 0], [-59108, 59108], [300565, -601130], [34138, 34138], [0, -675182], [74684, 0], [-461353, -461353], [155765, 0], [433144, -216572], [-203576, 203576], [-672170, 336085], [372680, 0], [0, -112394], [122636, -61318], [382301, -764602], [27054, -13527], [-881411, 881411], [-224686, 0], [0, 228859], [95150, 0], [-1238712, 619356], [-161567, 323134], [674782, 674782], [149360, 149360], [140536, -281072], [-459230, 459230], [480072, -960144], [0, 705598], [386386, -386386], [-842158, 0], [81631, 81631], [-366568, 733136], [-679441, 1358882], [-391872, -391872], [157824, -78912], [0, 161194], [0, -126830], [-315768, 631536], [155933, 0], [786276, -393138], [-338441, 338441], [658004, 0], [0, -1311809], [619444, -309722], [819675, -1639350], [-22972, -22972], [0, 346090], [806780, 0], [660791, 0], [-926344, 463172], [-681446, 681446], [-894408, -894408], [-78305, 78305], [-909075, -909075]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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