
// Make newform 5712.2.a.s in Magma, downloaded from the LMFDB on 05 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_5712_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_5712_2_a_s();".
// 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_5712_2_a_s();".
// 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_5712_a();"
function MakeCharacter_5712_a()
    N := 5712;
    order := 1;
    char_gens := [2143, 1429, 3809, 3265, 2689];
    v := [1, 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_5712_a_Hecke( : Kf := Kf)
    return MakeCharacter_5712_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]];
    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], [1], [-1], [-5], [-1], [-1], [6], [-6], [6], [-4], [11], [0], [9], [-4], [-7], [-12], [6], [-13], [-4], [-13], [-15], [-13], [13], [-9], [4], [1], [0], [-14], [-13], [2], [6], [-16], [17], [-11], [4], [-14], [-22], [17], [-14], [-18], [0], [-21], [16], [8], [-4], [2], [24], [-4], [-7], [-1], [5], [22], [13], [3], [27], [18], [-9], [-18], [-18], [13], [12], [-28], [25], [-6], [14], [15], [34], [9], [5], [-31], [0], [0], [-4], [34], [32], [6], [20], [-30], [-26], [-18], [-6], [-4], [-28], [20], [-12], [19], [7], [10], [4], [-24], [-25], [23], [10], [-12], [16], [-24], [20], [20], [23], [-2], [-6], [-41], [-26], [36], [-10], [-21], [-30], [16], [2], [8], [40], [-1], [35], [20], [3], [-19], [12], [-10], [-12], [6], [-36], [-47], [-32], [-21], [27], [19], [48], [33], [-42], [12], [-18], [4], [-36], [-25], [-14], [32], [-5], [26], [-26], [-43], [30], [-23], [-1], [31], [-25], [-26], [0], [-30], [23], [1], [-10], [52], [-33], [-4], [-36], [-16], [16], [52], [3], [4], [2], [4], [-25], [12], [-32], [9], [-40], [52], [-3], [-34], [-19], [-4], [-11], [4], [62], [38], [-50], [16], [-18], [52], [-42], [-50], [54], [-21], [4], [39], [14], [-14], [7], [15], [-11], [15], [26], [3], [28], [-12], [37], [18], [-50], [-12], [30], [47], [38], [3], [38], [65], [-8], [-54], [-14], [52], [-70], [61], [-39], [39], [-38], [-2], [67], [-34], [69], [-20], [26], [-50], [17], [13], [28], [18], [12], [37], [16], [36], [-12], [-10], [-8], [-74], [-42], [5], [28], [68], [-42], [69], [-28], [-37], [-10], [-30], [21], [-64], [28], [31], [65], [-30], [-8], [-72], [23], [-24], [-6], [41], [-44], [73], [-32], [31], [26], [-28], [-58], [-26], [26], [-30], [-55], [-28], [27], [28], [-39], [-24], [-4], [-30], [-43], [64], [-40], [1], [-15], [-66], [-1], [43], [43], [-29], [-11], [-38], [28], [-32], [-21], [-42], [54], [4], [56], [1], [-17], [-81], [-40], [-50], [-28], [56], [-13], [-35], [-45], [-52], [52], [-54], [-88], [-16], [80], [-64], [18], [-33], [-76], [59], [16], [-84], [18], [-26], [16], [-27], [-10], [-48], [-90], [-38], [36], [-26], [79], [46], [54], [-23], [-78], [-74], [63], [74], [66], [-40], [-20], [-17], [7], [10], [60], [-33], [50], [27], [-15], [10], [11], [11], [73], [34], [-38], [55], [40], [2], [-9], [40], [92], [42], [-43], [-83], [-52], [69], [57], [24], [19], [21], [48], [2], [-48], [64], [42], [-27], [-32], [-62], [0], [-58], [-42], [9], [56], [75], [12], [-3], [7], [91], [95], [82], [-42], [-44], [6], [16], [-41], [20], [-90], [63], [-86], [29], [-102], [-18], [-35], [-50], [12], [36], [52], [-100], [-38], [-76], [14], [12], [-12], [51], [-69], [78], [-54], [-58], [75], [-62], [-98], [-26], [-60], [-55], [-80], [-12], [25], [-55], [-6], [-77], [46], [56], [8]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5712_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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