
// Make newform 1216.2.a.v in Magma, downloaded from the LMFDB on 20 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_v();".
// 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_v();".
// 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, -10, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-8, 1, 1]];
    Rf_basisdens := [1, 1, 2];
    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_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, 0], [0, 1, 0], [0, 0, 1], [-2, 1, -1], [-2, 0, -1], [-2, 1, 0], [0, 1, -1], [-1, 0, 0], [0, 3, -2], [2, 1, -2], [0, 0, 0], [2, 0, 0], [2, 2, 0], [6, -2, -1], [2, -2, 3], [-2, 1, -4], [-8, 1, 0], [0, -2, 1], [4, 1, -2], [4, -2, -2], [0, 3, -1], [-8, -2, 0], [-4, 2, -4], [6, -2, -2], [-2, 0, 2], [-6, -4, 0], [4, -2, 6], [0, -1, 0], [-2, 3, -4], [2, -4, 0], [-4, -2, -2], [-6, 2, 1], [8, 5, -1], [-2, 2, -5], [12, 0, 3], [4, -2, 6], [-14, 0, 0], [-4, 4, 0], [12, 0, -2], [-6, 4, 0], [0, -8, 6], [10, 0, 4], [-2, 5, 3], [-2, 4, -6], [2, 2, 0], [6, 5, -5], [-8, -3, 4], [4, 2, 2], [12, -3, 2], [-4, 6, -1], [4, 6, -5], [6, -3, -1], [-14, 2, -4], [-6, 4, 1], [-2, -2, 6], [-14, -2, -1], [-2, 4, -2], [-8, 3, -2], [-4, -4, 3], [18, -6, 8], [-2, 0, -1], [6, 3, -4], [4, 0, 4], [10, 1, 1], [-14, -3, -2], [10, -5, 6], [-4, -5, 2], [-2, 6, -2], [-6, 0, 1], [-4, 2, -5], [18, -7, 10], [-2, -9, 3], [8, -4, 0], [6, -9, -4], [-16, 1, -4], [16, -2, 4], [-8, -2, 1], [12, -8, 7], [14, -2, -2], [6, 10, -2], [-4, -4, 0], [-10, -3, 0], [-16, 6, -4], [-6, -4, -4], [12, -8, 6], [10, 0, 9], [14, 4, 2], [20, -1, 5], [-4, 12, -9], [-22, 2, -1], [18, 0, 1], [0, 4, 4], [-4, 10, -6], [-12, 0, 8], [2, -2, -3], [-8, 11, -6], [22, 0, 2], [-18, 10, -14], [16, 3, -8], [16, -2, -7], [12, 0, 0], [20, -4, -5], [0, -4, -2], [22, -2, -2], [-12, 8, 0], [12, -3, 1], [10, -6, 13], [10, -8, 4], [8, -8, 8], [-6, 10, -12], [0, 10, 4], [-4, 2, -9], [-16, 2, 5], [20, -6, 12], [-10, 2, 5], [14, -4, 2], [6, 4, 7], [-26, 5, -5], [28, -4, 7], [16, -5, 4], [-18, -5, 0], [26, -10, 8], [10, 3, 2], [-12, 0, 0], [10, 4, -3], [-14, 6, -4], [2, -4, -4], [-10, 1, 7], [-6, -7, 9], [-6, 6, 0], [6, 10, -5], [0, 8, -4], [16, 8, -8], [-40, 2, 1], [0, 1, 3], [-12, -5, -7], [10, -1, -6], [12, -3, 10], [-10, 5, 4], [12, -9, -7], [-20, 7, 10], [4, -12, -1], [14, 3, -5], [-4, -3, -10], [6, -5, 16], [12, 8, -10], [18, 4, 4], [-22, 6, -4], [6, -12, 3], [12, 2, -2], [-46, 3, -6], [16, -10, 17], [14, -8, 11], [-36, -6, 2], [-28, 7, -2], [20, -8, -2], [-40, 1, -2], [26, -7, -10], [-16, -5, 7], [10, -3, 10], [-20, 8, -8], [-2, -12, 6], [0, -8, 0], [20, 0, 0], [-26, -2, 2], [-32, 2, 0], [-12, 4, -10], [4, 4, 7], [14, -10, -2], [4, -12, -1], [-24, 7, 12], [14, -7, 0], [0, 8, 0], [26, -5, -2], [24, -16, 20], [20, 2, 3], [-12, -8, -8], [20, -2, 3], [24, -12, 8], [0, 4, 5], [-14, -5, -3], [36, 3, -6], [-30, 8, 0], [-14, -10, 8], [6, -11, -1], [4, -22, 11], [-6, -1, -10], [-16, 0, -10], [2, -4, 4], [0, 11, -6], [-14, 8, 8], [-26, -2, -9], [-20, 4, -8], [-14, -1, 10], [36, -4, 4], [-2, 6, -6], [-32, -5, 3], [10, -18, 8], [-38, 7, 2], [26, 11, -3], [14, 8, -2], [20, 8, 6], [22, -7, 0], [2, -18, 12], [-12, 2, -4], [4, 0, 3], [-6, 5, -19], [36, -3, 6], [0, -9, 7], [-32, 5, 12], [2, -3, 18], [-30, 6, 0], [-18, 6, -15], [4, -17, 14], [44, -12, 14], [10, 4, 0], [-40, -1, 6], [22, 12, -10], [24, -14, 12], [-16, -10, 9], [2, 12, 8], [12, -2, -14], [10, 24, -12], [-22, 17, -11], [24, 3, 0], [8, 18, -7], [10, -8, 8], [40, -6, 16], [-12, 18, -10], [-34, -4, -1], [-8, -2, -3], [20, 17, -2], [-16, -8, 8], [42, 8, 0], [34, -16, 21], [30, -5, 15], [-8, -2, -3], [-16, -12, 9], [-52, 10, -16], [-24, 4, 0], [-16, -12, 6], [2, -16, -3], [0, -7, 2], [42, -11, 10], [-6, 14, -8], [-6, -22, 3], [-14, -26, 11], [44, -8, 8], [-4, 8, 4], [2, -11, 17], [-38, 0, -12], [-4, 5, -7], [-24, -1, 2], [-2, 4, -10], [-48, 0, 5], [12, -14, 20], [12, -10, -1], [16, -20, 10], [26, -4, 4], [-22, 12, -16], [12, -12, 2], [16, 4, -10], [-48, -4, 1], [18, 9, 6], [-28, -18, 7], [-4, -11, 18], [-10, -13, 0], [28, -11, 21], [8, 3, -4], [0, 22, -3], [-18, 17, -8], [0, -1, 20], [48, -5, -1], [22, -9, 11], [38, -8, 14], [0, -11, 10], [-18, -4, -1], [22, 1, -4], [2, 10, -8], [30, 6, -1], [4, 2, -14], [-8, -1, -14], [34, -7, 13], [50, 1, 2], [54, -6, 7], [-2, -6, -11], [20, -9, 9], [6, -12, -2], [-50, 7, -5], [-2, 4, -14], [34, -16, 16], [6, -2, -13], [6, 16, -14], [8, 12, -18], [-30, 0, -12], [26, -14, 8], [16, 2, -4], [-8, -2, -7], [-12, -17, 14], [2, 8, -23], [-16, -2, -3], [-38, 11, 6], [-26, 9, -9], [-20, 1, 2], [-22, 18, -11], [62, -8, 2], [-44, 13, -22], [-30, 11, -30], [22, 2, 13], [-14, 18, -8], [-50, 10, -3], [-14, 12, -12], [-50, -12, 2], [16, -20, 22], [-32, -16, 0], [-18, 22, -14], [-14, -8, -7], [24, 2, 16], [20, -3, -19], [16, -10, 1], [32, -13, 4], [0, -3, 11], [18, 25, -10], [-40, 6, 16], [-46, 3, 2], [30, -12, -14], [12, -11, 10], [-32, 23, -24], [28, -4, 2], [-12, 12, 15], [48, 8, -11], [34, 3, -6], [-8, 7, 6], [-42, 8, -17], [14, -8, 15], [26, -14, 13], [-26, -17, 8], [-6, -2, -16], [20, -15, 25], [8, -19, 2], [22, -25, 20], [-24, 5, 7], [-38, 1, -2], [-32, -2, -8], [-38, -11, -6], [36, -8, 12], [-14, -8, 20], [0, -13, -8], [0, 10, 12], [20, 8, -1], [8, 24, -6], [14, -16, -18], [24, -4, 9], [-4, -16, 10], [6, 4, 6], [-10, -2, 22], [56, -1, -2], [-12, 0, 24], [8, 5, 3], [-4, 10, -22], [-12, 24, -9], [-22, 4, -8], [4, -6, -22], [-44, 5, -14], [10, -16, -11], [-14, 14, 8], [-38, 4, 12], [12, -26, 18], [-2, -4, 22], [28, -6, -12], [-16, 11, -16], [-22, -3, -3], [34, 9, -14], [6, 6, -3], [12, -16, 15], [44, 7, -2], [8, 0, 16], [28, -6, 19], [-20, 11, 5], [38, -6, -18], [34, 0, -12], [-12, -10, -13], [10, -1, -11], [-52, 10, -5], [-8, -19, 20], [8, 2, 12], [24, 25, -6], [-14, -14, 8], [18, 0, 5], [-12, -4, -10], [-14, 8, 8], [-38, 11, -22], [28, 14, 0], [-6, -12, 13], [28, 8, 6], [-6, -16, 8], [-32, 14, 4], [-38, 14, -20], [48, -4, 10], [-62, 2, 8], [-10, 0, -6], [36, 6, 7], [16, -22, 12], [26, 10, -4], [6, -8, -10], [42, -15, -7], [-12, 14, -17], [10, 6, -8], [0, 15, -4], [18, 10, -19], [38, 18, -14], [48, -2, -3], [-24, 5, -24], [-30, 4, -15], [-22, -3, -22], [2, -14, -12], [0, 2, 12], [-8, 8, 16], [2, -33, 6], [-52, 4, -10], [-34, -20, 6], [-14, 23, -26], [-6, -9, 17], [32, -5, 0], [-50, -12, 6], [-26, 15, -32], [0, 13, 0], [52, -11, 9], [-2, -6, -5], [-6, 33, -23]];
    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_v();".
// 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_v(:prec:=3)
    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_v();".
// 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_v( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1216_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![8, -10, -1, 1]>,<5,R![-8, -10, 1, 1]>,<7,R![-16, -5, 4, 1]>],Snew);
    return Vf;
end function;