
// Make newform 1216.2.a.h in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1216_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_1216_2_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_1216_2_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_1216_a();"
function MakeCharacter_1216_a()
    N := 1216;
    order := 1;
    char_gens := [191, 837, 705];
    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_1216_a_Hecke(Kf)
    return MakeCharacter_1216_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 := 2;
    raw_aps := [[0], [0], [-3], [-5], [5], [4], [-3], [-1], [0], [0], [10], [-8], [0], [5], [5], [6], [10], [5], [10], [10], [-11], [-10], [0], [-10], [-12], [2], [10], [-10], [-6], [-4], [0], [-5], [13], [5], [11], [-10], [-2], [20], [-20], [6], [0], [18], [15], [-14], [22], [-5], [-10], [-20], [0], [-25], [11], [15], [20], [-5], [12], [25], [26], [0], [3], [30], [-15], [-16], [30], [5], [6], [18], [10], [8], [15], [-5], [-6], [15], [-20], [-24], [10], [-10], [9], [-13], [2], [-6], [-20], [10], [20], [4], [20], [15], [36], [7], [-13], [5], [15], [40], [30], [-20], [-5], [0], [20], [-22], [10], [17], [20], [-13], [-20], [-24], [40], [7], [35], [-26], [10], [10], [-40], [-21], [37], [-20], [-5], [-20], [5], [15], [-29], [50], [0], [-26], [-28], [0], [-45], [-30], [10], [-45], [-35], [6], [25], [-50], [-20], [13], [37], [-5], [-26], [-20], [-2], [-45], [0], [-45], [-5], [30], [-4], [30], [14], [38], [5], [-20], [38], [-15], [25], [10], [30], [-30], [-40], [54], [37], [-42], [20], [-14], [-40], [20], [-2], [20], [40], [23], [30], [39], [20], [-22], [20], [14], [-20], [-5], [20], [-37], [-40], [1], [-35], [-60], [26], [18], [-5], [55], [2], [-50], [-4], [20], [-64], [45], [-10], [20], [-60], [-24], [45], [-34], [-42], [25], [20], [0], [-2], [-40], [60], [-57], [45], [60], [-15], [-50], [-58], [-10], [55], [40], [0], [40], [40], [-12], [-30], [29], [70], [-40], [-60], [25], [-30], [29], [14], [50], [-10], [-15], [-31], [-70], [-30], [30], [55], [-5], [41], [-39], [-60], [30], [-30], [15], [-40], [50], [64], [-15], [45], [-50], [40], [-55], [2], [27], [60], [-6], [1], [60], [47], [40], [-12], [-38], [20], [10], [-55], [-26], [-73], [50], [30], [3], [-50], [9], [58], [-20], [-39], [15], [62], [40], [-55], [-70], [10], [-35], [-30], [-40], [-15], [38], [5], [15], [-29], [-48], [35], [-16], [50], [-75], [-16], [70], [-14], [14], [-80], [29], [10], [-5], [9], [-22], [35], [-70], [55], [-2], [-20], [20], [75], [14], [55], [-50], [-68], [80], [0], [30], [55], [-10], [31], [-71], [-40], [53], [-30], [50], [-6], [-10], [80], [20], [40], [-9], [5], [78], [0], [45], [65], [-45], [-26], [-64], [-65], [20], [54], [-63], [-44], [-30], [-46], [60], [30], [-60], [0], [27], [40], [52], [-15], [30], [90], [4], [80], [0], [13], [0], [3], [-42], [20], [-50], [-45], [-84], [-38], [-70], [10], [-60], [-40], [45], [-64], [-65], [-53], [60], [-40], [99], [-69], [28], [78], [19], [45], [-37], [20], [-80], [-20], [42], [-45], [20], [-34], [-14], [20], [-25], [30], [-66], [10], [60], [-20], [30], [-86], [-9], [0], [-22], [34], [5], [-97], [48], [0], [95], [-86], [-63], [30], [55], [58], [-38], [-60], [-30], [-22], [90], [6], [-8], [5], [20], [76], [68], [-80], [-1], [5], [25]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1216_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1216_2_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_1216_2_a_h(:prec:=1)
    chi := MakeCharacter_1216_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 2));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_1216_2_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_1216_2_a_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1216_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![0, 1]>,<5,R![3, 1]>,<7,R![5, 1]>],Snew);
    return Vf;
end function;