
// Make newform 4592.2.a.l in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4592_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_4592_2_a_l();".
// 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_4592_2_a_l();".
// 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_4592_a();"
function MakeCharacter_4592_a()
    N := 4592;
    order := 1;
    char_gens := [575, 3445, 3937, 785];
    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_4592_a_Hecke(Kf)
    return MakeCharacter_4592_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], [3], [-1], [-1], [2], [0], [-3], [8], [4], [-5], [3], [10], [-1], [5], [-6], [-9], [10], [13], [2], [-9], [4], [11], [14], [-1], [7], [-10], [-13], [-3], [-2], [9], [-2], [8], [12], [-14], [3], [-19], [4], [4], [0], [-1], [16], [-14], [11], [12], [-12], [24], [12], [-21], [-25], [20], [10], [12], [-10], [0], [-15], [16], [-10], [-20], [26], [2], [-18], [14], [28], [-18], [-22], [-18], [32], [-19], [-12], [14], [-24], [-24], [17], [-18], [-37], [22], [-2], [20], [-25], [-10], [-28], [-19], [16], [0], [-34], [25], [-33], [-18], [-21], [-16], [-6], [10], [-12], [-37], [-10], [0], [-36], [-38], [-20], [10], [-2], [33], [-4], [-25], [44], [46], [21], [-43], [44], [21], [43], [-16], [30], [24], [-30], [-30], [7], [3], [3], [-44], [-38], [30], [6], [-40], [-13], [16], [3], [8], [28], [13], [-19], [12], [-24], [-5], [6], [-14], [-38], [52], [21], [54], [0], [-4], [51], [-30], [11], [-14], [-21], [-38], [8], [32], [-32], [0], [26], [-20], [-19], [-2], [-3], [-50], [7], [18], [4], [30], [47], [-13], [54], [3], [-40], [4], [-19], [-36], [30], [-49], [60], [19], [-4], [49], [40], [-58], [28], [-22], [46], [-42], [-26], [-22], [-54], [-17], [-58], [-32], [-4], [10], [-64], [-30], [4], [-43], [-33], [4], [40], [-18], [-63], [-62], [54], [28], [-8], [18], [-49], [60], [-69], [-52], [30], [-32], [38], [42], [40], [22], [24], [48], [23], [18], [-3], [72], [31], [35], [-32], [32], [28], [23], [62], [2], [8], [60], [-44], [31], [-2], [-9], [-56], [-46], [-1], [10], [-9], [-14], [16], [1], [52], [-4], [28], [29], [0], [-67], [-33], [-30], [30], [34], [-40], [-56], [18], [-17], [61], [-4], [-28], [-43], [-12], [-2], [-66], [28], [32], [-34], [79], [-42], [40], [-2], [76], [-19], [-52], [4], [-7], [-76], [-52], [-2], [38], [36], [24], [9], [63], [-42], [-55], [-3], [40], [-82], [38], [-35], [33], [-4], [52], [42], [-26], [74], [78], [-42], [29], [-28], [-36], [38], [-68], [-16], [25], [37], [-12], [14], [53], [59], [36], [19], [31], [51], [-16], [-38], [-42], [-40], [-35], [72], [38], [10], [-14], [-65], [-9], [-50], [4], [1], [-31], [30], [45], [-72], [49], [59], [-47], [28], [-33], [-64], [49], [36], [19], [51], [-14], [-9], [-88], [75], [-10], [60], [-14], [-22], [1], [19], [-47], [-32], [3], [14], [15], [80], [66], [40], [-26], [-1], [72], [81], [-53], [38], [70], [86], [23], [-54], [71], [32], [30], [-32], [17], [-4], [-30], [46], [6], [21], [-38], [-61], [26], [47], [-35], [-81], [37], [-10], [11], [42], [44], [-8], [2], [8], [102], [68], [-60], [-14], [75], [33], [-85], [-46], [88], [6], [-3], [50], [54], [-68], [3], [-90], [34], [-4], [96], [-80], [-82], [55], [-24], [52], [21], [15], [-58], [-78], [-58], [-56], [28], [-10], [-24], [72], [52], [-4]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4592_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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