
// Make newform 4011.2.a.e in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4011_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_4011_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_4011_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, -4, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-3, 0, 1]];
    Rf_basisdens := [1, 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_4011_a();"
function MakeCharacter_4011_a()
    N := 4011;
    order := 1;
    char_gens := [2675, 2866, 2311];
    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_4011_a_Hecke(Kf)
    return MakeCharacter_4011_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, 0], [-1, 0, 0], [1, 0, 1], [-1, 0, 0], [-2, 0, -2], [3, 1, -1], [-2, 3, 1], [5, -1, 1], [-3, 2, 0], [3, 0, 0], [-3, 3, -1], [0, 2, 1], [5, -3, 1], [4, 2, 0], [-2, -5, 1], [0, 5, 0], [1, -1, 4], [8, -1, 2], [-6, 0, -1], [-2, 4, -4], [9, 2, 1], [5, -1, -1], [-6, 3, 1], [2, 4, -1], [-1, -9, 4], [4, -4, 2], [1, 2, 0], [-7, -3, 3], [3, -6, 6], [3, -6, 0], [11, -1, 7], [-5, 0, -5], [4, 4, -5], [1, 1, -4], [-10, 4, -7], [4, -1, -5], [-3, 6, -2], [1, -1, -5], [5, 7, -4], [-17, -5, 3], [12, 0, 1], [-10, -1, -2], [1, 0, 0], [9, 4, -4], [-3, -7, 2], [6, -10, 0], [3, 5, -7], [6, -5, 0], [15, -3, 7], [7, -10, 5], [15, 8, -4], [-5, 5, -2], [2, -4, 6], [-14, 2, -2], [1, -7, -4], [6, 3, 6], [-9, 9, -7], [-4, -1, -2], [6, 1, -3], [-3, -9, -5], [8, 0, 0], [16, -5, 3], [11, -3, 11], [9, 3, 7], [-16, 3, 3], [-7, -8, 9], [7, -3, -6], [9, -7, -6], [-8, -3, 5], [5, -11, -3], [-21, 8, 4], [21, -1, 8], [4, -8, -5], [15, -5, -3], [-20, -3, 0], [-15, 3, -5], [10, 5, 5], [0, -6, -5], [-6, 3, -2], [17, -7, 2], [-7, 1, -3], [26, -5, -2], [5, -14, 17], [14, -6, 4], [-4, -8, -4], [-5, -9, -9], [6, -2, 6], [0, 9, -13], [-11, -5, -11], [-20, 6, -1], [-23, 1, -11], [14, -4, -2], [-12, 8, -2], [-4, -11, 6], [-12, 0, -12], [11, -9, 6], [-7, 5, 8], [23, -3, 5], [7, -3, 13], [21, 7, -5], [8, 8, 6], [23, 4, -2], [-27, 4, -15], [6, -20, 2], [14, 9, -5], [-5, 5, 4], [5, 2, 5], [28, 2, -2], [-8, -15, 1], [-2, 0, -8], [21, -8, 8], [21, 5, -8], [-17, 2, -16], [9, 18, -9], [1, -11, -3], [17, -7, 7], [-12, 2, -20], [13, 9, 3], [-4, -16, -1], [-1, -11, 14], [-6, -13, 11], [5, -6, 10], [-7, -6, -12], [0, -2, 2], [-1, 3, 9], [1, -1, -12], [7, -6, 6], [-2, -1, 2], [-14, 6, -12], [1, 25, -12], [-11, 14, -22], [-10, 20, -11], [-7, -8, 3], [-6, -1, -4], [-17, -5, 10], [26, 14, -3], [-15, -3, -6], [12, 5, 4], [-5, -7, -12], [-7, 5, -5], [-5, 1, -3], [-22, 9, -13], [-2, 0, -6], [9, 17, -9], [4, -16, -7], [-10, 0, -5], [23, -8, 16], [11, -19, 20], [-2, -18, 11], [15, 9, -4], [-1, 7, 5], [1, -6, -16], [5, 12, 8], [-20, -5, 7], [-18, 5, -8], [2, 15, -10], [14, -3, -14], [-6, -22, 0], [13, 3, -5], [-44, -1, -7], [27, 3, 11], [25, -6, -1], [-21, 13, -7], [-12, 6, 1], [0, 2, -14], [-8, -5, -2], [9, 5, 7], [7, 2, 0], [19, -4, -14], [47, -5, 0], [8, 15, -1], [-11, 26, 2], [1, 7, -15], [-15, 0, -26], [-3, 27, 2], [-17, -11, -11], [43, -7, 16], [2, -3, 8], [28, 13, 0], [18, -15, 16], [-5, -2, 2], [-22, 20, 5], [2, -1, -9], [-19, 11, -9], [-10, -26, 9], [-2, 8, -8], [12, -8, -3], [6, 10, 21], [22, 0, 11], [-26, 8, 7], [22, 18, -5], [5, -22, -7], [-10, 10, -13], [-6, -26, 9], [-25, -13, 6], [31, 1, 17], [-11, 5, 7], [5, 14, -5], [-3, -1, 11], [-11, -33, 16], [-12, -16, 13], [2, -7, 6], [3, -6, 18], [23, -15, -7], [31, -2, 20], [19, 25, -12], [11, 23, -3], [19, 29, -9], [16, -2, 22], [29, 3, -7], [-44, 2, -7], [11, -15, -11], [-19, -8, 14], [-17, -1, 3], [-22, 3, -10], [13, 7, -9], [15, 8, -6], [25, -14, 0], [-5, -20, -4], [25, -7, 6], [33, 3, 15], [-17, 7, -15], [31, 3, 22], [0, 18, -6], [29, -19, 7], [-15, 12, -1], [11, -12, 9], [46, 12, -7], [-1, -1, -15], [31, 9, 6], [5, -9, 2], [15, -15, -1], [-15, 9, 5], [48, 12, -4], [-37, 5, 2], [-38, 6, -8], [-15, 16, -27], [43, 5, -3], [-18, -19, 15], [-21, 7, -17], [3, -15, 12], [32, 3, 16], [18, -13, 27], [31, 7, 8], [-21, 0, -8], [-3, 24, -8], [-14, -20, 19], [-18, 6, 8], [35, -9, 0], [-37, -7, -6], [36, 9, -4], [-31, -5, -8], [1, -13, 0], [3, -1, 21], [29, 13, -10], [-5, 19, 14], [48, -6, 6], [6, 26, -16], [2, -27, -8], [-55, 7, 1], [-13, -9, 2], [10, -20, 11], [-6, 24, 14], [-53, -7, -3], [-10, 23, -13], [-6, 26, 6], [49, 11, -3], [25, -6, 21], [12, -28, 23], [-38, 13, -29], [-35, -7, 0], [16, -7, 22], [11, -25, 31], [48, -12, 1], [0, -6, 4], [-30, 17, -4], [23, 38, -22], [-1, 1, 9], [5, 5, -11], [5, -9, 11], [-20, -26, -3], [-33, -14, 2], [-20, -28, 5], [49, -9, -9], [-21, -15, 10], [14, 10, -12], [-31, -21, -5], [-30, -4, -20], [-38, -11, -7], [32, 11, 10], [-35, -21, 3], [-22, -5, -25], [0, 0, -4], [-11, 14, -14], [-15, -6, 7], [8, -12, -13], [-3, 35, -15], [-49, -15, 11], [-28, 4, -20], [-37, -5, 9], [31, 27, -8], [18, 3, 12], [15, 38, -19], [-28, -12, 0], [1, 3, 6], [-19, 8, 3], [6, 24, -11], [1, 27, -23], [4, -17, -5], [26, 10, -17], [5, -19, 15], [-18, -5, -3], [-18, 4, 16], [46, -22, 0], [39, 14, -14], [-6, -9, -17], [-5, -40, 20], [15, -14, 8], [-20, 32, 1], [72, 5, 3], [-18, 2, 16], [42, 3, -9], [-4, 19, -15], [-14, -39, 24], [-61, 1, -3], [-29, -15, 1], [-21, -1, -19], [-7, 22, 6], [-40, 8, -10], [59, -3, -3], [-28, 1, -25], [7, 41, -15], [-3, -34, 9], [43, -9, 13], [18, -12, 12], [-7, -3, 23], [-5, 25, -22], [-23, 26, -8], [8, 8, 0], [-8, -10, 14], [-31, 5, 6], [43, -7, -9], [-10, -5, -4], [18, -13, 14], [-59, -2, 1], [53, -5, 21], [-5, 19, -25], [-24, -35, 11], [5, -3, -12], [23, -33, 2], [-28, -6, 5], [-3, -31, -12], [-17, 30, -11], [36, -14, -8], [35, 5, -3], [-7, -23, -3], [26, -4, 11], [-24, -6, 1], [-2, 26, 1], [9, 25, -1], [11, 9, 27], [-62, 14, 1], [-27, -11, 9], [1, 35, 1], [-66, -12, -2], [-22, 30, -10], [-42, 0, -1], [-11, -19, 6], [-7, 19, -33], [-51, 5, -21], [-50, 18, 0], [8, 23, -25], [24, 7, -1], [7, -33, 1], [20, 2, 14], [34, 12, -13], [50, 9, 9], [45, 0, -16], [-24, 24, -25], [-22, 18, -6], [-39, -22, -8], [-20, -8, -9], [-29, 18, 1], [-3, 25, 24], [12, 36, 7], [-17, -1, -25], [7, 24, 30], [20, -12, 12], [-31, -9, 14], [-20, -4, 9], [-29, 0, -16], [2, -20, 16], [-2, -24, -3], [-31, 13, -18], [-9, -13, -9], [-11, -27, 18], [77, 4, -4], [-15, -3, 15], [0, -31, 23], [-31, -23, 11], [-16, -30, 15], [41, 3, 25], [52, 1, 11], [7, -16, -6], [45, 10, -16], [-38, -23, 9], [10, 8, -18], [40, -5, 27], [-6, -25, -19], [11, -3, 0], [-2, 18, -19], [23, -7, -12], [25, 0, 27], [-21, 23, 7], [-8, -14, 18], [20, -14, -5], [33, -3, 5], [-28, -2, -19], [-8, 37, 20], [-40, 9, -14], [29, 18, 12], [18, -8, 37], [-62, -20, 2], [-35, 1, -37], [19, -2, 15], [-29, 26, 18], [-12, -12, -14], [-12, -30, -9], [13, -3, 26], [-24, 36, -33]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4011_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4011_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_4011_2_a_e(:prec:=3)
    chi := MakeCharacter_4011_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_4011_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_4011_2_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4011_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-1, -4, 0, 1]>],Snew);
    return Vf;
end function;