
// Make newform 3024.2.a.bk in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3024_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_3024_2_a_bk();".
// 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_3024_2_a_bk();".
// 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 := [-7, 0, 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_3024_a();"
function MakeCharacter_3024_a()
    N := 3024;
    order := 1;
    char_gens := [1135, 757, 785, 2593];
    v := [1, 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_3024_a_Hecke(Kf)
    return MakeCharacter_3024_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], [0, 1], [1, 0], [2, 1], [0, -2], [0, 0], [1, 0], [4, 1], [-6, 0], [3, 2], [3, 2], [-6, 1], [2, -2], [6, -2], [0, 0], [2, 2], [4, 4], [-4, -2], [0, -5], [6, 2], [6, -2], [10, 0], [6, -3], [8, -4], [-4, 0], [-1, -2], [8, 2], [-1, -6], [-4, -6], [12, -2], [-10, -2], [-2, 4], [0, 4], [0, 6], [10, 4], [4, -2], [-4, 2], [12, -2], [-4, -7], [-16, -2], [14, 4], [24, -1], [-10, 4], [4, -2], [9, -6], [-6, 0], [19, -2], [-8, 2], [-16, 4], [-8, 2], [-16, -2], [18, 2], [-8, 4], [18, -5], [-4, 5], [24, -1], [16, 4], [-9, 2], [-6, 6], [-8, -4], [-16, -4], [-3, -8], [-10, 4], [8, -2], [-26, -2], [0, -4], [-17, -4], [-10, -1], [10, 4], [10, 3], [0, 6], [21, -2], [-5, -6], [-2, 6], [-8, -2], [28, 0], [10, 0], [-2, -8], [-2, -6], [20, -2], [-1, -2], [4, 11], [-8, 6], [0, 0], [22, -5], [0, -10], [1, 0], [0, -15], [-2, 12], [-10, -6], [-24, 0], [-18, 2], [6, -9], [-30, 2], [10, -4], [32, 4], [6, -3], [-17, -4], [15, -2], [4, 4], [-8, -10], [2, -8], [-20, -10], [-22, 0], [-10, 4], [-12, -6], [-22, 1], [12, -9], [20, 6], [-24, -4], [-35, -2], [14, -2], [-11, -4], [10, -14], [8, 2], [-7, 0], [18, -2], [-22, -2], [-6, 9], [-4, 6], [-6, -4], [-8, -5], [-2, -17], [-8, 8], [-34, -4], [-9, 6], [6, 14], [12, 4], [-34, 6], [-34, 0], [4, 3], [22, 2], [18, -4], [-12, 0], [-24, -2], [-16, -9], [4, 0], [-36, 3], [-18, 4], [9, 4], [-2, 14], [-10, 8], [22, -3], [14, 6], [-42, 0], [-16, -12], [2, 5], [-27, 8], [48, -2], [26, 8], [-6, 3], [20, 12], [-22, 4], [-14, 12], [24, 6], [-20, -12], [44, 4], [16, -12], [-28, -7], [22, 5], [-14, -8], [26, -8], [14, 8], [-12, 4], [-26, 10], [-16, -2], [-38, 0], [-5, 8], [-12, 9], [-32, -2], [50, -2], [-16, -10], [13, -16], [5, -6], [-10, 17], [0, -8], [8, -14], [-3, 18], [-20, -4], [-6, 4], [-48, 6], [-15, -2], [-4, -4], [24, -7], [28, 1], [-10, 20], [4, -16], [9, -4], [4, -12], [8, 18], [-20, -10], [-10, -12], [-20, 16], [26, -7], [-30, -9], [3, -20], [6, 12], [42, 7], [28, -2], [10, -12], [39, 2], [-18, -18], [20, -16], [-38, 0], [8, 4], [36, 12], [-22, 1], [2, -10], [56, -4], [-51, 4], [-28, -7], [-2, 6], [-10, 2], [8, 8], [40, 8], [4, 22], [26, -7], [-24, -3], [48, -4], [62, 0], [24, 16], [18, -16], [-14, -2], [-20, -10], [-25, 2], [-10, -1], [-44, 9], [-17, -10], [-2, -1], [43, 2], [31, 4], [8, 22], [-16, 10], [-19, 12], [38, -8], [-10, 12], [34, 8], [-28, 2], [-18, -4], [-22, 19], [45, -4], [19, -6], [41, -6], [-42, 3], [6, 6], [27, 2], [10, -16], [36, 10], [-8, 13], [30, -16], [-12, -12], [-20, -22], [34, 8], [-8, 19], [-30, -15], [-19, 10], [19, 12], [32, 0], [10, -14], [-22, 6], [66, 3], [8, 10], [38, -6], [10, 21], [-27, 4], [38, 12], [4, -16], [-10, -16], [-58, 0], [-22, -14], [32, 8], [12, 10], [4, -10], [10, 22], [-15, -10], [-54, -7], [-18, -16], [2, 16], [26, -4], [22, -2], [38, -12], [4, 14], [-14, 2], [23, -4], [-32, 15], [-63, 4], [26, -2], [25, -6], [14, 1], [4, 22], [22, 10], [2, 4], [16, -14], [-17, 6], [-40, 13], [-21, 10], [-20, 16], [-10, -20], [-21, -16], [30, 0], [-14, -12], [6, -16], [18, -15], [54, 8], [7, -12], [70, -1], [18, 2], [-36, 15], [-15, -2], [48, -6], [2, 10], [-24, 16], [40, 2], [-4, 5], [24, 8], [-50, -4], [60, 9], [-52, 4], [26, 6], [-12, 8], [-17, 12], [28, 9], [-46, -6], [-16, -8], [28, 0], [18, 20], [27, 20], [8, -10], [16, -22], [49, -10], [-28, -2], [-5, -10], [18, -28], [-48, 10], [24, -8], [41, -10], [-50, 7], [14, -14], [45, -10], [-73, 6], [-30, 0], [-12, 0], [-8, -22], [-18, -10], [-20, 14], [-30, 2], [4, -26], [-22, 16], [-52, -8], [-29, -8], [6, -24], [-52, 4], [-45, 18], [27, 10], [-10, -17], [-26, -10], [-4, 8], [66, 6], [8, -18], [-30, -24], [-2, 11], [4, 26], [10, 21], [8, -12], [23, 8], [-56, 1], [-22, 24], [-39, 12], [18, 3], [49, -12], [0, -14], [18, -14], [1, 6], [11, 6], [58, -14], [28, -2], [-54, 12], [26, 5], [-44, -18], [-36, -3], [24, 18], [-32, 10], [-54, 6], [-9, 12], [14, 14], [14, 0], [-40, -4], [-72, 4], [-44, 20], [-42, 4], [-32, 5], [6, -15], [24, -16], [-40, 18], [11, 20], [13, 6], [50, 7], [-6, 34], [-14, 8], [-14, 18], [-30, 0], [2, 8], [62, 1], [24, -11], [-37, -6], [15, 6], [-26, -20], [11, 12], [-36, 12], [-12, 4], [-42, 18], [-64, 2], [52, -8], [6, -8], [40, -19], [-56, -7], [-24, 4], [60, 0], [72, 6], [44, 8], [20, 4], [32, -1], [-6, 10], [-58, -4], [64, 8], [-28, -14], [0, 12], [-15, 0], [-22, 8]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3024_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3024_2_a_bk();".
// 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_3024_2_a_bk(:prec:=2)
    chi := MakeCharacter_3024_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_3024_2_a_bk();".
// 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_3024_2_a_bk( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3024_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-7, 0, 1]>,<11,R![-3, -4, 1]>,<17,R![0, 1]>],Snew);
    return Vf;
end function;