
// Make newform 5568.2.a.bj in Magma, downloaded from the LMFDB on 01 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5568_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_5568_2_a_bj();".
// 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_5568_2_a_bj();".
// 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_5568_a();"
function MakeCharacter_5568_a()
    N := 5568;
    order := 1;
    char_gens := [4351, 2437, 3713, 4033];
    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_5568_a_Hecke(Kf)
    return MakeCharacter_5568_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], [1], [4], [3], [-1], [3], [-5], [4], [6], [1], [-2], [-6], [6], [-12], [-7], [12], [-10], [-10], [-13], [2], [14], [8], [6], [5], [0], [-3], [8], [-8], [5], [15], [-6], [-9], [-10], [-11], [12], [-4], [10], [-14], [-16], [-12], [-18], [11], [0], [-22], [-12], [-15], [22], [21], [2], [-20], [30], [8], [-25], [11], [-20], [8], [-7], [-2], [17], [6], [-20], [-9], [-14], [21], [29], [3], [24], [28], [22], [14], [36], [-24], [-20], [-2], [-24], [16], [31], [22], [16], [2], [24], [-30], [0], [-28], [13], [9], [15], [17], [-14], [-11], [-20], [24], [-32], [-4], [-11], [5], [-22], [4], [-27], [-12], [13], [14], [-19], [-11], [28], [28], [-42], [-8], [21], [28], [2], [35], [-22], [8], [-35], [21], [-37], [34], [31], [13], [3], [-31], [-43], [36], [27], [-42], [34], [8], [10], [-28], [22], [13], [-16], [-8], [0], [-22], [6], [0], [-26], [-29], [-17], [-6], [52], [36], [28], [5], [-28], [-56], [2], [2], [-6], [-7], [-24], [27], [-54], [33], [-3], [-42], [-1], [36], [-57], [-32], [4], [-32], [18], [4], [-1], [8], [-37], [7], [-14], [-5], [18], [-20], [20], [-18], [27], [57], [-16], [29], [32], [3], [-13], [38], [-50], [54], [-30], [-16], [-20], [18], [23], [15], [-50], [31], [53], [46], [-8], [-1], [-6], [36], [-18], [31], [-44], [60], [-5], [-38], [10], [-36], [-26], [34], [26], [30], [56], [69], [-69], [1], [-49], [37], [24], [-21], [-4], [-49], [-5], [14], [-6], [0], [-6], [40], [-52], [-50], [-8], [36], [2], [18], [1], [31], [-34], [-54], [-22], [-31], [63], [12], [-17], [-12], [12], [-30], [-8], [52], [-31], [53], [-28], [-38], [-23], [-10], [30], [-52], [38], [-14], [-22], [37], [70], [21], [-29], [-16], [-38], [-20], [-11], [-38], [-80], [-34], [-25], [-23], [-6], [-80], [4], [-50], [-12], [9], [-46], [36], [-16], [-7], [-6], [29], [48], [-43], [26], [-71], [41], [4], [10], [64], [36], [-48], [-10], [-54], [-48], [-40], [78], [-56], [28], [10], [40], [20], [-52], [-50], [-77], [-14], [-8], [-19], [8], [59], [24], [5], [60], [-47], [-24], [58], [-57], [-3], [34], [41], [-70], [-58], [-88], [66], [-52], [4], [-17], [-64], [-40], [82], [-56], [45], [-86], [-30], [-3], [45], [10], [-31], [82], [36], [-83], [83], [48], [72], [24], [48], [35], [27], [32], [1], [-66], [4], [-74], [-54], [27], [-32], [67], [-6], [65], [-70], [-43], [-6], [-38], [-64], [59], [61], [-8], [40], [-55], [-46], [-5], [35], [-2], [31], [-19], [74], [-74], [-38], [98], [94], [-72], [-51], [4], [-34], [26], [55], [86], [51], [54], [76], [39], [86], [17], [-1], [-52], [6], [-79], [-55], [58], [-30], [12], [-48], [72], [-56], [-19], [5], [26], [64], [21], [-6], [14], [8], [8], [41], [9], [-19], [17], [8], [-34], [102], [48], [20], [47], [32], [-6], [-1], [43], [96]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5568_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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