
// Make newform 3024.2.a.r in Magma, downloaded from the LMFDB on 18 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_r();".
// 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_r();".
// 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_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], [1], [-1], [0], [-6], [-7], [8], [2], [4], [2], [9], [11], [7], [11], [10], [-9], [-8], [0], [12], [-14], [-1], [9], [14], [4], [6], [6], [-6], [-5], [6], [1], [-4], [-2], [-12], [8], [5], [0], [-7], [-21], [-14], [26], [20], [22], [-9], [-12], [16], [-20], [-6], [8], [8], [22], [2], [-14], [1], [6], [6], [-3], [-2], [-23], [-6], [16], [-9], [26], [15], [-4], [6], [-29], [-1], [-18], [20], [-27], [30], [-14], [23], [-15], [15], [-4], [-14], [-12], [-24], [-31], [-2], [24], [-2], [36], [-16], [26], [34], [-9], [1], [12], [-11], [8], [6], [-31], [31], [9], [-3], [-2], [1], [23], [-4], [-44], [18], [7], [0], [-44], [-3], [16], [10], [-10], [-26], [-6], [12], [37], [32], [-22], [-24], [-38], [-16], [-22], [-2], [42], [-6], [34], [38], [-29], [-45], [52], [4], [20], [20], [40], [19], [-33], [-32], [-25], [2], [30], [18], [-6], [-24], [13], [18], [-16], [-45], [-20], [-21], [-46], [-22], [17], [-22], [-19], [39], [37], [-32], [-43], [-49], [-36], [-15], [28], [-34], [0], [-11], [48], [-9], [51], [-32], [9], [-61], [-8], [48], [6], [-5], [0], [13], [52], [0], [-28], [-2], [-61], [5], [-14], [17], [48], [27], [38], [10], [6], [45], [-34], [-24], [13], [51], [40], [57], [10], [-37], [14], [1], [28], [-12], [-34], [46], [-57], [15], [-56], [0], [-2], [-58], [-27], [-18], [35], [45], [5], [36], [15], [10], [18], [-64], [27], [14], [46], [39], [37], [65], [-9], [38], [38], [42], [-50], [-2], [-37], [56], [26], [0], [44], [-12], [46], [3], [-40], [-68], [46], [10], [30], [43], [2], [41], [9], [-10], [53], [-38], [-62], [-54], [11], [0], [-25], [10], [-54], [18], [-8], [-18], [12], [-56], [-66], [-32], [20], [50], [33], [18], [70], [-4], [14], [15], [-54], [-56], [-34], [-53], [-61], [-15], [-3], [-17], [-21], [-36], [-2], [-36], [-51], [32], [-66], [-33], [-30], [25], [-32], [-11], [-1], [11], [42], [1], [9], [-44], [-34], [-12], [-48], [-54], [45], [2], [12], [22], [82], [-70], [-64], [70], [-46], [53], [-4], [34], [67], [-62], [40], [-48], [-74], [-91], [-69], [32], [10], [-70], [47], [56], [-72], [-12], [-66], [24], [80], [49], [13], [83], [-62], [-61], [-16], [32], [-69], [-12], [-39], [40], [-62], [-72], [-32], [35], [-23], [-62], [58], [1], [-42], [24], [-21], [-51], [-60], [-1], [84], [-24], [-85], [15], [-42], [-60], [48], [-31], [45], [-21], [67], [32], [-74], [-24], [-82], [26], [78], [-68], [-18], [38], [-50], [88], [47], [88], [-64], [-54], [-50], [24], [-20], [74], [-59], [-20], [-1], [-58], [-90], [-42], [100], [38], [26], [65], [20], [66], [24], [4], [69], [15], [-26], [-20], [17], [38], [48], [0], [36], [-18], [2], [65], [75], [54], [-74], [-52], [33], [-32], [66], [-64], [18], [-9], [78], [75], [4], [12], [-2], [-32]];
    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_r();".
// 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_r(:prec:=1)
    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_r();".
// 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_r( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3024_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-1, 1]>,<11,R![0, 1]>,<17,R![7, 1]>],Snew);
    return Vf;
end function;