
// Make newform 4012.2.a.e in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4012_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_4012_2_a_e();".
// 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_4012_2_a_e();".
// 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
        poly := [-1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-1, 2]];
    Rf_basisdens := [1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_4012_a();"
function MakeCharacter_4012_a()
    N := 4012;
    order := 1;
    char_gens := [2007, 3777, 3129];
    v := [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_4012_a_Hecke(Kf)
    return MakeCharacter_4012_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], [0, -1], [-1, 0], [0, 1], [0, -2], [-1, 1], [-1, 0], [0, 3], [0, -2], [1, 0], [6, 0], [-5, 3], [-3, 2], [-4, -2], [-2, 0], [5, -2], [1, 0], [7, 3], [-6, 4], [-2, -2], [0, 6], [-2, -3], [-1, -1], [2, -2], [-1, 3], [5, 3], [-5, -3], [8, -1], [-4, 2], [-5, 1], [8, -1], [0, 4], [-17, 0], [8, -4], [1, -7], [-7, -1], [-11, -5], [-12, 4], [-16, -3], [7, -5], [-7, -5], [-3, 8], [-4, 4], [11, 4], [2, 8], [-20, 1], [-22, -2], [2, -2], [-5, -7], [-3, 3], [-1, -1], [-2, -9], [-3, 4], [8, -7], [1, 0], [0, -3], [-5, -5], [4, -5], [-1, 0], [-3, -10], [-1, -7], [-1, -6], [-4, -7], [12, -3], [-15, 5], [-8, 2], [12, 7], [-2, -14], [7, 11], [-3, -3], [-3, 1], [-12, 3], [9, -7], [-4, -2], [0, -3], [-16, 0], [-4, 2], [-9, -1], [19, 9], [15, -7], [1, -5], [6, 0], [0, -4], [-19, 2], [0, -12], [8, 2], [-9, -10], [19, 3], [18, 4], [-18, -4], [-12, 10], [12, 12], [16, 1], [36, 1], [6, -15], [1, 3], [-31, -1], [22, 4], [20, 1], [4, 10], [-24, 4], [3, -8], [-16, 12], [5, 13], [-15, 3], [-11, 0], [2, 2], [7, 14], [-4, -1], [-18, -2], [-6, -1], [14, -2], [23, -10], [12, 5], [-14, -6], [14, 0], [-24, -1], [-16, -7], [-7, -10], [13, -13], [3, 8], [34, -6], [26, 0], [-33, 7], [4, 6], [16, 2], [13, 8], [-31, 5], [-8, 16], [6, -8], [19, 13], [16, -8], [-8, -6], [37, 0], [11, -6], [19, -1], [7, 17], [6, -2], [-29, -9], [-5, -1], [11, 15], [19, -7], [-1, -5], [-22, 6], [-3, 0], [-19, 3], [-45, -2], [-52, 0], [4, -2], [12, 2], [-27, 0], [37, -7], [-26, -5], [-18, -16], [-2, -3], [-22, -9], [11, -13], [38, 4], [-28, 4], [31, 5], [-12, -5], [-16, -18], [39, -1], [-32, 9], [-22, 6], [6, 16], [-35, 11], [13, 4], [-5, 1], [-8, -12], [16, -4], [-2, -8], [-30, 7], [-32, -12], [-44, 7], [15, -6], [36, -3], [18, 2], [-24, 4], [-31, 10], [38, -3], [34, -7], [11, -15], [25, 10], [12, 12], [12, -16], [14, -12], [-6, -2], [6, 16], [-28, 4], [34, 12], [5, -15], [9, -15], [48, -2], [-34, -3], [34, 4], [0, -2], [-14, -14], [-42, 10], [-5, 15], [12, -2], [-44, 7], [-2, 28], [-10, -20], [20, 13], [4, 4], [14, 20], [24, -10], [21, 1], [-55, 7], [-4, -26], [-27, 6], [-20, 3], [-26, 15], [-12, 5], [12, -20], [20, 12], [-16, 6], [-31, 11], [-26, -20], [-64, 2], [28, -4], [-17, 23], [2, -21], [33, -17], [15, -17], [5, -30], [4, 16], [10, 18], [-10, 1], [-6, 20], [13, 5], [31, 15], [-26, 0], [49, 9], [-66, 3], [25, 23], [-16, -12], [7, -21], [20, -12], [-2, -3], [8, -4], [-6, 23], [61, -4], [41, 12], [4, 16], [-33, 1], [-5, 1], [-24, 1], [-8, -13], [-3, 4], [34, 4], [69, 1], [-7, 32], [-13, -2], [20, -8], [-9, -2], [-45, 7], [2, 0], [20, -6], [-19, 7], [-18, 5], [-71, 2], [34, 16], [-19, 12], [-42, 2], [9, 16], [25, -17], [8, -9], [-39, -8], [-21, -15], [-48, 0], [13, -9], [-20, 17], [-13, 12], [-40, -14], [-62, -6], [-39, 10], [-12, 4], [0, -3], [6, -22], [49, -5], [-5, 3], [28, 14], [-48, 14], [38, 2], [-9, -5], [17, -2], [57, 5], [-9, -20], [-24, 24], [52, -4], [-46, 4], [-29, -1], [-53, 6], [2, 24], [-38, 2], [-19, -16], [11, 17], [-35, -5], [-29, 6], [-45, 7], [40, -4], [-49, 3], [38, 13], [32, 18], [2, -13], [-65, -3], [-19, 5], [19, 13], [14, 25], [-45, 0], [-23, 0], [1, -9], [-12, -27], [-25, -9], [8, 0], [56, -11], [-12, 10], [-56, -2], [-60, -8], [-44, 8], [21, -14], [-28, 7], [-11, -16], [2, 10], [67, 9], [26, -18], [52, -8], [-27, 7], [25, 15], [37, 13], [-29, 7], [-18, 23], [-16, 28], [18, -13], [27, -14], [27, 17], [36, 4], [40, -5], [12, 10], [-10, -24], [-18, 4], [-33, 3], [3, -9], [-27, 3], [21, -1], [6, 3], [-3, -13], [58, -14], [15, 21], [-11, -8], [39, 6], [40, -4], [-26, -20], [17, -19], [21, -7], [28, -5], [-17, -6], [-44, 13], [-17, -9], [-73, 0], [20, 23], [11, 17], [-12, -16], [19, -1], [13, -7], [-64, 5], [-26, 20], [52, 4], [-48, 6], [35, 15], [26, 0], [-12, 12], [39, -20], [-16, -10], [30, -28], [-15, -33], [-33, 15], [-15, 28], [3, 6], [-16, 10], [20, 32], [-23, 3], [34, 19], [-15, -7], [-28, 29], [32, -10], [70, 2], [42, 6], [-1, 9], [-30, -8], [-15, 5], [-33, -5], [72, -6], [-32, -12], [66, 15], [25, 18], [88, -3], [-51, -16], [4, 30], [-37, -25], [-46, -3], [-86, -4], [-15, -24], [28, -2], [15, -7], [-85, 0], [-56, -2], [-44, 8], [-69, -4], [46, 20], [40, -18], [-14, -13], [-15, 30], [-65, 2], [-6, -40], [-22, -24], [-4, 20], [-86, 1], [-58, 0], [-22, -28], [-28, 24], [20, 3], [39, 8], [-7, 8], [40, 0], [-30, 16], [48, 13], [-4, -20]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4012_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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