
// Make newform 4312.2.a.d in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4312_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_4312_2_a_d();".
// 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_4312_2_a_d();".
// 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_4312_a();"
function MakeCharacter_4312_a()
    N := 4312;
    order := 1;
    char_gens := [1079, 2157, 3433, 3137];
    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_4312_a_Hecke(Kf)
    return MakeCharacter_4312_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], [2], [0], [-1], [3], [-2], [4], [-4], [-7], [-8], [12], [-8], [8], [-10], [14], [-9], [-5], [-3], [-6], [4], [-17], [-6], [-2], [7], [-3], [6], [-6], [10], [-3], [-13], [-16], [-13], [0], [10], [19], [24], [-5], [-3], [-13], [-1], [-10], [10], [0], [-3], [-4], [-4], [-24], [-10], [-16], [-4], [5], [-8], [16], [27], [21], [-12], [-13], [-11], [-24], [-16], [18], [-14], [-18], [-31], [24], [17], [-4], [-6], [14], [-18], [-21], [-28], [-7], [-3], [-6], [10], [22], [-9], [18], [8], [-8], [15], [-26], [21], [-4], [-14], [-2], [-15], [14], [12], [25], [-32], [6], [28], [-35], [-42], [-6], [2], [-41], [-14], [46], [28], [0], [38], [21], [-41], [30], [4], [22], [16], [-26], [33], [-12], [50], [-11], [19], [-42], [-16], [32], [2], [-26], [42], [33], [29], [31], [52], [6], [-10], [31], [34], [20], [-16], [-2], [42], [34], [-32], [28], [-6], [48], [-36], [9], [-10], [36], [26], [6], [-14], [-48], [-23], [-4], [17], [13], [7], [33], [-28], [34], [-20], [7], [42], [-33], [-20], [46], [0], [13], [18], [6], [-6], [-26], [36], [34], [22], [12], [-9], [-26], [-36], [-19], [-10], [-42], [4], [34], [46], [-10], [40], [-8], [-42], [-24], [53], [35], [-66], [-51], [14], [6], [17], [-66], [-32], [-9], [-56], [20], [-56], [13], [17], [-27], [-10], [40], [-9], [0], [-64], [-48], [38], [37], [36], [-18], [40], [48], [5], [-15], [27], [-10], [0], [-50], [-27], [55], [-73], [-36], [46], [-67], [-39], [2], [19], [42], [64], [-70], [37], [-50], [19], [-3], [62], [21], [43], [30], [-4], [20], [-16], [-56], [-48], [40], [16], [-61], [0], [11], [-55], [-28], [16], [-42], [19], [-6], [2], [32], [0], [78], [-64], [72], [-30], [-11], [-15], [-19], [4], [-11], [-48], [66], [-14], [76], [-43], [0], [66], [-70], [41], [-41], [32], [-36], [-63], [2], [27], [-15], [-34], [30], [-9], [-14], [24], [-72], [-42], [7], [38], [46], [-19], [37], [-48], [-16], [54], [44], [14], [69], [-75], [-9], [39], [41], [51], [-68], [34], [38], [47], [20], [-80], [-56], [11], [70], [85], [-80], [-62], [-42], [-22], [-13], [27], [-38], [2], [-43], [-20], [-44], [3], [-45], [-42], [48], [37], [16], [56], [-37], [-62], [52], [47], [-92], [-48], [-42], [15], [-76], [-64], [-12], [22], [55], [-56], [26], [92], [10], [-24], [21], [54], [-72], [48], [2], [-60], [-84], [-13], [48], [24], [0], [15], [65], [30], [72], [10], [-19], [20], [-3], [-26], [67], [-24], [-35], [12], [38], [-82], [-68], [-50], [-46], [1], [-18], [32], [-30], [56], [-4], [-16], [-86], [-43], [-50], [-33], [-33], [-62], [-4], [-35], [1], [-65], [-33], [-9], [-1], [-93], [-99], [-49], [-28], [32], [-20], [60], [9], [30], [-42], [65], [10], [30], [90], [85], [-44], [-3], [73], [12], [-61], [-102], [66], [93], [-12], [55], [-30], [-97], [31]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4312_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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