
// Make newform 21.4.c.b in Magma, downloaded from the LMFDB on 25 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_4_c_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_4_c_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 := [121, 0, 46, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, -35, 0, -1], [0, 57, 0, 1], [253, 35, 11, 1]];
    Rf_basisdens := [1, 22, 22, 44];
    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, 0, 0], [-1, 0, 0, 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 := 4;
    raw_aps := [[0, 0, -1, 0], [0, 0, 0, 1], [0, -1, 0, -2], [7, -7, 0, 0], [0, 0, 8, 0], [0, 23, 0, 0], [0, -6, 0, -12], [0, -15, 0, 0], [0, 0, -22, 0], [0, 0, 14, 0], [0, 104, 0, 0], [230, 0, 0, 0], [0, 14, 0, 28], [44, 0, 0, 0], [0, 34, 0, 68], [0, 0, 50, 0], [0, -13, 0, -26], [0, -29, 0, 0], [-64, 0, 0, 0], [0, 0, -112, 0], [0, -36, 0, 0], [-442, 0, 0, 0], [0, 49, 0, 98], [0, 48, 0, 96], [0, -446, 0, 0], [0, -125, 0, -250], [0, 20, 0, 0], [0, 0, 356, 0], [-1870, 0, 0, 0], [0, 0, -406, 0], [-1048, 0, 0, 0], [0, -253, 0, -506], [0, 0, -244, 0], [0, 569, 0, 0], [0, 0, 62, 0], [1448, 0, 0, 0], [0, -1387, 0, 0], [3128, 0, 0, 0], [0, -70, 0, -140], [0, 223, 0, 446], [0, 0, 848, 0], [0, -75, 0, 0], [0, 0, -64, 0], [-652, 0, 0, 0], [0, 0, -490, 0], [0, 1980, 0, 0], [-2224, 0, 0, 0], [0, -782, 0, 0], [0, 589, 0, 1178], [0, -1513, 0, 0], [0, 0, -232, 0], [0, 0, -1162, 0], [0, 1546, 0, 0], [0, -399, 0, -798], [0, 20, 0, 40], [0, 0, -2008, 0], [0, -573, 0, -1146], [0, -498, 0, 0], [-3214, 0, 0, 0], [0, 0, 2240, 0], [0, 1217, 0, 0], [0, -511, 0, -1022], [0, 765, 0, 0], [0, 848, 0, 1696], [0, -218, 0, 0], [0, 0, 1238, 0], [3632, 0, 0, 0], [-6256, 0, 0, 0], [0, 0, 260, 0], [0, -2567, 0, 0], [0, -524, 0, -1048], [0, 0, -778, 0], [0, -3830, 0, 0], [4598, 0, 0, 0], [5252, 0, 0, 0], [0, -190, 0, -380], [0, 0, -1714, 0], [0, 5445, 0, 0], [0, 0, -3214, 0], [0, -3046, 0, 0], [0, 917, 0, 1834], [-2854, 0, 0, 0], [0, 0, 2738, 0], [0, 5322, 0, 0], [0, -1226, 0, 0], [0, 0, -1660, 0], [0, 0, 644, 0], [1196, 0, 0, 0], [0, -539, 0, -1078], [926, 0, 0, 0], [0, -771, 0, -1542], [0, -1238, 0, -2476], [13712, 0, 0, 0], [0, 0, 1400, 0], [15476, 0, 0, 0], [0, 168, 0, 336], [0, 419, 0, 838], [0, -1938, 0, -3876], [0, -3417, 0, 0], [-16150, 0, 0, 0], [-18352, 0, 0, 0], [0, 0, 806, 0], [0, 1415, 0, 2830], [0, 0, 3590, 0], [4136, 0, 0, 0], [0, 7944, 0, 0], [0, 1813, 0, 3626], [0, -792, 0, -1584], [0, 0, -3016, 0], [0, 7576, 0, 0], [0, 650, 0, 0], [-1786, 0, 0, 0], [0, 0, -2002, 0], [0, -3389, 0, 0], [-13102, 0, 0, 0], [0, 0, -2386, 0], [0, 3929, 0, 0], [0, -762, 0, -1524], [0, 0, 6866, 0], [0, 0, -5572, 0], [0, -4411, 0, 0], [26882, 0, 0, 0], [0, 2029, 0, 4058], [0, 0, 176, 0], [0, 4801, 0, 0], [0, 0, 5306, 0], [-24382, 0, 0, 0], [0, 1098, 0, 2196], [0, -2420, 0, 0], [0, -9199, 0, 0], [-9220, 0, 0, 0], [0, 0, 3626, 0], [26648, 0, 0, 0], [-3274, 0, 0, 0], [0, -3634, 0, -7268], [0, -8398, 0, 0], [0, 2619, 0, 5238], [0, 9827, 0, 0], [0, -2443, 0, -4886], [0, 0, 10886, 0], [0, -12423, 0, 0], [0, 0, -4474, 0], [35006, 0, 0, 0], [0, 0, -364, 0], [0, -13917, 0, 0], [0, -98, 0, -196], [0, 8815, 0, 0], [0, 4642, 0, 9284], [0, 14531, 0, 0], [0, 0, -904, 0], [-30430, 0, 0, 0], [0, 3612, 0, 7224], [21632, 0, 0, 0], [0, 314, 0, 628], [26900, 0, 0, 0], [0, 0, -6286, 0], [-13126, 0, 0, 0], [0, -4082, 0, -8164], [0, -5136, 0, 0], [0, 533, 0, 1066], [0, 0, 12452, 0], [0, 0, 7868, 0], [34232, 0, 0, 0], [0, 1959, 0, 3918], [0, 0, -9652, 0], [0, -2246, 0, -4492], [-50422, 0, 0, 0], [0, 18829, 0, 0]];
    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_4_c_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_4_c_b(:prec:=4)
    chi := MakeCharacter_21_c();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_4_c_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_4_c_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![17, 0, 1]>],Snew);
    return Vf;
end function;