
// Make newform 21.5.f.b in Magma, downloaded from the LMFDB on 19 April 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_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_5_f_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 := [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, 5], [3, 3], [-2, 1], [-35, -21], [149, -149], [24, -48], [-154, -154], [-412, 206], [0, 560], [235, 0], [-743, -743], [0, -1970], [-1626, 3252], [2798, 0], [4768, -2384], [-901, 901], [797, 797], [-4120, 2060], [4156, -4156], [484, 0], [-548, -548], [0, -4325], [753, -1506], [2284, -1142], [-3147, 6294], [-2500, -2500], [16688, -8344], [0, -4465], [-3320, 3320], [-9734, 0], [-29149, 0], [31822, -15911], [-17824, 17824], [-16158, 32316], [0, -9214], [-11975, 11975], [-1148, -1148], [0, 20800], [24528, -49056], [-2792, 1396], [-19378, 19378], [4302, -8604], [0, 28760], [44089, -44089], [-46478, 0], [-22652, -22652], [-27688, 0], [28335, -56670], [42761, 42761], [27596, -13798], [0, -96382], [-44276, 0], [-32879, -32879], [-43137, 86274], [33892, -16946], [89078, -89078], [41129, 41129], [-48886, 24443], [46516, -46516], [50284, 0], [-21362, -21362], [-38679, 77358], [44028, -88056], [30362, 30362], [-107206, 53603], [0, 61583], [0, 91480], [9995, 0], [-62458, 62458], [-134736, 269472], [-47932, -47932], [0, -164044], [-2477, -2477], [0, -78626], [56234, 0], [253276, -126638], [4262, -4262], [168824, -84412], [0, 90812], [26167, 26167], [85980, -171960], [4256, 0], [147662, -147662], [-33948, 67896], [-118510, 59255], [0, 293981], [21220, 0], [0, 176467], [-128208, 256416], [142286, 0], [-450920, 225460], [-87514, -87514], [-206027, 206027], [-477113, 0], [0, -164810], [-117558, 235116], [544714, -272357], [242054, 242054], [-345832, 172916], [0, -305090], [-260974, 0], [-251263, 251263], [-119407, -119407], [0, -510862], [-374600, 374600], [-226277, -226277], [116997, -233994], [739600, -369800], [-461284, 461284], [-348585, 697170], [454610, -227305], [-404900, 404900], [258952, 0], [-73496, -73496], [-211021, 0], [161168, -161168], [247092, -494184], [184148, 184148], [0, 466049], [103258, 0], [115510, 115510], [160973, 0], [-248474, 124237], [145997, -145997], [716924, -358462], [626353, 0], [0, -310298], [-676820, 338410], [-436965, 873930], [-127492, 63746], [-107162, 107162], [90580, 0], [0, 124279], [-147904, 0], [-460424, 230212], [-622209, 1244418], [22508, 22508], [-262184, -262184], [-369267, 738534], [-305272, 305272], [372132, -744264], [0, -919921], [214018, -214018], [129841, 0], [-352250, -352250], [521466, -1042932], [-153888, 307776], [662636, 662636], [-306556, 153278], [0, 1056662], [0, 523456], [693324, -1386648], [872138, 0], [489616, -244808], [1557292, -1557292], [491572, 0], [0, 1033030], [-738164, 369082], [378801, -757602], [286547, 286547], [0, 1190822], [255670, 0], [417065, 0], [191374, -95687], [352370, -352370], [-320506, -320506], [-837791, 837791], [-584660, -584660]];
    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_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_5_f_b(: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_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_5_f_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,5,sign)));
    Vf := Kernel([<2,R![25, -5, 1]>],Snew);
    return Vf;
end function;