
// Make newform 216.4.a.h in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_216_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_216_4_a_h();".
// 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_216_4_a_h();".
// 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 := [-8, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-3, 6]];
    Rf_basisdens := [1, 1];
    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_216_a();"
function MakeCharacter_216_a()
    N := 216;
    order := 1;
    char_gens := [55, 109, 137];
    v := [1, 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;

function MakeCharacter_216_a_Hecke(Kf)
    return MakeCharacter_216_a();
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], [0, 0], [4, 1], [12, 1], [1, 0], [16, -4], [-28, -4], [92, 2], [46, -4], [-168, 2], [188, -5], [174, -4], [-156, 10], [40, -14], [-114, -8], [76, -13], [340, -24], [-56, -32], [176, 34], [908, -12], [-287, 0], [-680, 32], [-391, 32], [120, 18], [-169, 8], [-828, -9], [-56, -52], [693, -56], [46, 76], [-732, -70], [-164, 23], [-1003, -104], [-1296, 2], [1416, -100], [2132, -31], [-1468, -113], [-764, 72], [1236, 120], [-390, 144], [3252, -19], [-1197, 56], [2016, 16], [-840, -196], [-499, -40], [1092, -245], [1564, -129], [3028, 34], [-2192, 8], [1740, 24], [-1682, -8], [-3128, -50], [-2878, -164], [1950, 248], [-1168, 336], [-3940, -56], [2158, 132], [560, 226], [68, 185], [-3008, -24], [284, -116], [-1420, -178], [-624, 134], [-8148, 4], [1110, 84], [-2427, -96], [36, -449], [1952, -134], [-5694, -56], [2655, 304], [1434, -156], [-460, 526], [-3462, -24], [-8564, -193], [7628, 276], [1856, 12], [10044, 108], [5668, 409], [-5152, 36], [-7104, 132], [9279, 128], [-612, 512], [3584, 360], [7346, 224], [5269, -264], [3780, 291], [6884, -224], [9652, -378], [-5569, -200], [-7868, -255], [3524, -3], [1209, 616], [-10114, 488], [3448, 248], [14571, 328], [2256, -926], [7986, 332], [-948, 771], [6380, -288], [-6884, 788], [2556, 264], [4652, 428], [14788, -15], [-14921, 272], [7144, -396], [-15204, 488], [-17074, 344], [-15959, -456], [2920, -1122], [-6594, 104], [-2645, 416], [-19528, 264], [-3514, 880], [-996, 38], [21724, -192], [7612, 345], [-7172, 978], [-8016, 276], [-13768, 328], [-1308, -123], [-12993, -48], [-12862, -572], [-3203, -112], [-872, 590], [9964, 224], [9872, -1212], [-15540, 875], [27548, -64], [20212, -272], [-7692, 1545], [5486, -1292], [2804, 1596], [4456, 1604], [17172, -449], [1970, -40], [22520, -168], [23849, -360], [-14304, -478], [-22592, -992], [-14900, 749], [-14200, -434], [-2588, -726], [-4608, 842], [19916, -489], [31424, 320], [34, 184], [3492, -268], [13286, -408], [11004, 468], [8040, 1088], [16828, 1696], [2850, 1952], [-8908, -1696], [7172, -1190], [-34576, 380], [-9116, -2094], [8230, -2208], [36916, 3], [42344, 468], [-14853, -888], [-24660, -1019], [24999, 880], [-17064, 1404], [-11596, 641], [13929, 992], [-6408, 826], [5934, -3096], [14428, 1673], [-44552, 252]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_216_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_216_4_a_h();".
// 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_216_4_a_h(:prec:=2)
    chi := MakeCharacter_216_a();
    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_216_4_a_h();".
// 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_216_4_a_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_216_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![-281, -8, 1]>],Snew);
    return Vf;
end function;