
// Make newform 21.4.e.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_e_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_e_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_4_e_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_e();"
function MakeCharacter_21_e()
    N := 21;
    order := 3;
    char_gens := [8, 10];
    v := [3, 2];
    // 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_e_Hecke();"
function MakeCharacter_21_e_Hecke(Kf)
    N := 21;
    order := 3;
    char_gens := [8, 10];
    char_values := [[1, 0], [-1, 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 := 4;
    raw_aps := [[3, -3], [0, -3], [3, -3], [-14, 21], [0, 15], [-64, 0], [0, -84], [16, -16], [84, -84], [-297, 0], [0, 253], [316, -316], [360, 0], [26, 0], [30, -30], [0, -363], [0, 15], [118, -118], [0, 370], [-342, 0], [0, -362], [-467, 467], [477, 0], [-906, 906], [503, 0], [0, 1086], [-1736, 1736], [1353, -1353], [0, 370], [-648, 0], [377, 0], [651, -651], [0, 1770], [-1558, 0], [-2454, 2454], [0, -1259], [0, 196], [1252, -1252], [-2646, 0], [786, -786], [0, -2892], [1352, 0], [-3912, 3912], [0, -1493], [-4086, 0], [0, 3556], [1250, 0], [425, 0], [0, -3855], [2188, -2188], [-852, 852], [5508, 0], [0, -791], [5265, 0], [6870, -6870], [0, 222], [0, -7851], [-5183, 5183], [0, 4960], [-774, 0], [0, -3698], [-6273, 0], [-1684, 0], [0, 1320], [8503, -8503], [2577, -2577], [484, -484], [-8359, 0], [0, 1860], [-1918, 0], [0, 3048], [30, -30], [0, 11311], [-1208, 1208], [7640, 0], [-12750, 12750], [0, -3126], [5932, -5932], [-1608, 1608], [0, 4465], [-1584, 0], [-1330, 0], [0, -9588], [494, 0], [16009, -16009], [-7773, 7773], [864, 0], [-2519, 2519], [-342, 0], [-4336, 0], [-18636, 18636], [0, -15078], [0, -6221], [-7371, 0], [-4274, 4274], [-2520, 0], [14277, -14277], [0, 6306], [-8072, 8072], [22858, -22858], [-24724, 0], [0, 9843], [0, 13371], [5232, -5232], [0, 14398], [0, -19871], [-16137, 0], [21324, -21324], [0, 8646], [11195, 0], [8971, -8971], [0, 12772], [12762, 0], [0, -12842], [21365, 0], [0, -8274], [27998, 0], [0, 17466], [-2157, 2157], [19944, 0], [0, -27506], [-19123, 0], [-13857, 13857], [0, 22245], [640, -640], [-15561, 0], [-5534, 5534], [21846, -21846], [-11089, 0], [-11762, 11762], [0, 22726], [6678, 0], [19987, -19987], [314, 0], [11496, -11496], [2765, 0], [0, 14046], [0, 18514], [-27495, 0], [0, 7944], [-28942, 0], [-8187, 8187], [0, 280], [25317, 0], [0, -15320], [34092, 0], [-7378, 0], [0, 15594], [30538, -30538], [822, -822], [41824, -41824], [-46098, 0], [21008, 0], [-24036, 24036], [0, -13292], [-9306, 0], [-16496, 16496], [-14154, 14154], [-3781, 0], [0, 25863], [42384, -42384], [10530, 0], [-38341, 0], [-1923, 1923], [0, -57090], [0, -5484], [0, 22465], [0, -29366]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_21_4_e_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_4_e_a(:prec:=2)
    chi := MakeCharacter_21_e();
    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_e_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_4_e_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![9, -3, 1]>],Snew);
    return Vf;
end function;