
// Make newform 4014.2.a.p in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4014_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_4014_2_a_p();".
// 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_4014_2_a_p();".
// 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, -5, 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_4014_a();"
function MakeCharacter_4014_a()
    N := 4014;
    order := 1;
    char_gens := [893, 2233];
    v := [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_4014_a_Hecke(Kf)
    return MakeCharacter_4014_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 := [[1, 0, 0], [0, 0, 0], [0, 0, 1], [3, -1, 0], [-2, 1, 0], [4, 0, -1], [1, 2, -1], [2, 1, -1], [0, -1, 2], [3, 1, 0], [-2, -3, 1], [0, -1, 4], [-3, -5, 1], [1, 3, -1], [4, -2, -1], [0, 1, -3], [3, 0, -3], [-1, -5, 2], [4, 0, 3], [5, 2, 0], [-1, 6, -1], [8, 5, -2], [-6, 3, 3], [1, 1, -6], [0, 5, -1], [3, -5, -2], [14, 0, 0], [-2, 1, -5], [2, -3, -3], [3, -3, 0], [3, 5, -5], [-3, 0, -2], [-1, 7, 3], [13, -2, -1], [-2, 1, 1], [-10, 5, 1], [8, -7, -2], [0, -4, 8], [-15, 1, -3], [3, -6, -1], [5, -7, 4], [-4, 2, 1], [-8, -5, 5], [1, 3, 4], [-3, 0, -1], [-9, 3, 0], [1, -2, 2], [1, 0, 0], [0, 5, -4], [-12, 7, 5], [10, 4, -5], [-7, 9, 1], [-17, 4, 2], [-1, -4, -5], [4, 4, 1], [7, 3, -2], [-3, -3, 1], [-6, -6, 6], [13, 4, -4], [0, 9, 0], [4, 0, -4], [2, -4, 7], [7, 0, 0], [2, 6, 5], [5, -3, -3], [-18, -3, 6], [-9, 7, -4], [-20, 0, -3], [-19, -9, 4], [-17, 1, -4], [1, 1, 2], [-14, 2, -6], [18, -7, 2], [-3, -7, -1], [8, 2, -5], [10, 1, -1], [30, -3, -2], [11, 11, -5], [10, 2, -1], [-7, 2, 7], [-2, -14, 7], [-8, -4, -4], [7, -10, 5], [3, -7, -8], [-19, 3, 4], [-1, -8, 1], [-19, -4, 0], [-15, 12, 6], [-5, 8, -3], [-4, -6, 3], [-12, -8, 1], [12, -3, -3], [15, -4, -5], [7, 10, -10], [12, 13, -1], [-21, 6, -5], [-18, 0, 0], [-15, -3, -3], [2, 4, 5], [14, 2, 4], [16, 6, -9], [-20, 4, 1], [-19, -6, 2], [-6, -3, 3], [16, 1, 2], [19, -9, -1], [-1, 11, -8], [-16, 2, 1], [15, 9, -9], [-11, -9, 4], [-14, 13, 2], [-6, 0, -6], [0, -3, -4], [15, -1, 7], [-2, -10, -8], [6, -2, 7], [4, -5, 5], [-2, -1, -1], [-13, -7, -7], [-5, -5, 3], [-4, 0, -9], [0, 17, -1], [22, -4, -9], [-10, -10, 3], [5, -4, 7], [0, -15, -2], [-17, -4, -1], [-29, 7, 6], [-11, -5, -4], [9, -1, -12], [18, 9, -12], [3, -10, 11], [-21, -2, 0], [-11, 4, 2], [-9, 13, 9], [-18, -11, -2], [5, 15, -2], [-3, -7, 0], [-9, 0, 15], [8, -15, 7], [-4, 0, 5], [12, 1, 7], [-11, -16, -3], [15, -2, 9], [-9, 2, 4], [7, 0, -7], [6, -2, -10], [-2, 13, 0], [11, -3, 6], [-7, -17, 1], [-28, 3, 0], [-3, -18, 6], [17, 18, -3], [-9, -16, 14], [-16, -6, 10], [11, -4, 11], [-14, 15, -4], [-8, -10, -7], [-18, 10, 11], [-3, -3, 12], [8, -7, 12], [-23, -8, -3], [-2, -6, 9], [10, 12, -14], [18, 1, -11], [-4, 2, -18], [-21, -11, -6], [-7, 18, -1], [20, -5, 11], [11, -21, -1], [8, -1, -13], [-23, 17, 5], [27, 3, 7], [2, 10, -1], [-13, 10, 1], [-36, -6, 4], [-2, -11, -13], [39, 5, -4], [18, 7, 2], [6, 2, -5], [23, 7, -9], [14, -7, 10], [-1, 6, 17], [-44, 4, 0], [17, -1, 14], [-12, -15, 10], [-5, 2, -14], [-21, 8, -3], [-18, -2, 11], [33, -6, 10], [14, -2, 21], [38, 2, 8], [-19, -14, 10], [20, -4, 8], [-1, 9, -16], [17, 2, 4], [-16, 6, -17], [-1, -6, 1], [14, 9, -8], [-12, 19, -5], [-22, -6, 7], [2, 21, -1], [-35, -4, -1], [-41, -6, 1], [-28, 4, -2], [-20, 9, 11], [-4, 16, -2], [-17, 28, 4], [1, 16, -1], [-26, -13, 8], [-5, 5, 11], [1, -9, 8], [9, -2, 12], [-8, 10, 6], [-8, -23, 6], [3, 12, 0], [19, -21, 0], [-8, 4, -6], [-15, -21, 14], [-10, 11, 1], [-4, 8, -8], [-39, 4, -12], [-1, 11, -2], [-30, -14, 9], [13, -9, 10], [3, -7, 22], [-28, -4, 11], [-10, -1, 18], [43, 1, 11], [-11, 3, -10], [-37, 9, -12], [39, 2, 3], [24, -7, -3], [-11, 6, -5], [-1, -21, 6], [34, -15, 1], [22, -6, -3], [-8, -2, 13], [2, 9, -17], [16, -14, -2], [62, -4, 1], [29, 15, -4], [28, 6, 11], [3, 21, -3], [-7, -12, 14], [-11, 6, -13], [-13, 5, -11], [1, 12, 4], [25, 7, -4], [-18, 0, -9], [27, -8, 3], [-27, -10, 11], [-4, 17, -3], [34, 13, -10], [-26, 19, -2], [-9, -15, -9], [10, 12, -18], [4, -1, -4], [2, 9, -5], [-23, -9, 1], [32, -4, -11], [33, 18, -3], [12, -10, -14], [-29, -5, 11], [-45, 1, -2], [6, -23, 0], [-9, -8, -8], [-12, -9, 15], [40, 18, -15], [27, -23, -11], [45, 1, -2], [-43, -5, 2], [3, 2, 24], [30, -7, 7], [-9, 11, 5], [6, 4, 5], [6, 8, 21], [-16, 19, 9], [5, 27, 0], [8, 17, 12], [7, -5, -18], [-2, -26, 20], [15, 3, -18], [6, -16, -8], [1, -15, 12], [3, 23, 8], [-21, 4, 14], [18, -30, 0], [22, -4, 19], [-16, -21, 5], [-18, 18, -5], [-42, -9, 13], [-24, 10, 16], [6, -11, -11], [10, -3, -17], [20, -4, 9], [15, -8, -10], [-3, 36, 3], [-57, -19, 6], [-23, 3, -11], [31, -4, -6], [45, 11, -1], [-9, 5, 3], [-27, -7, 5], [43, -3, 8], [-13, 6, 24], [1, -12, -1], [-2, 27, -7], [22, 1, -6], [25, -23, -4], [-23, 10, 11], [44, 20, 0], [22, 24, 1], [-3, 21, -6], [15, -21, -9], [4, -1, 21], [-8, -19, -9], [45, -2, -11], [17, 3, -15], [12, 3, -9], [40, -4, -6], [18, 2, -11], [36, 27, -18], [-62, 5, 7], [-5, -8, 7], [25, 27, -9], [-27, -20, -4], [16, -9, 13], [48, 3, -3], [26, -33, -7], [3, -14, 0], [27, 3, -18], [7, 18, 0], [58, 9, -1], [-40, -23, 15], [6, 24, -30], [48, 7, -7], [28, 7, -2], [-35, -15, 18], [2, -8, 21], [29, 15, -10], [-3, -12, 12], [7, -8, -23], [25, 8, -1], [-24, 19, 7], [-3, 27, 6], [16, 12, 3], [28, 6, 10], [-36, -8, 9], [10, -3, -1], [25, 3, 1], [-23, 24, 7], [-52, -5, 9], [2, 24, -3], [12, -4, 11], [21, -22, -3], [20, -10, -1], [45, -9, 12], [-10, -4, -8], [43, -8, 3], [54, 9, 0], [-6, 21, -9], [19, 1, 10], [-51, 3, -7], [-11, 23, -9], [-5, -6, -21], [-10, -18, 10], [-25, -2, -25], [29, -21, 0], [37, 7, -7], [9, 7, -16], [44, -2, 3], [40, 1, 18], [19, 6, 1], [4, -3, 12], [-50, 31, 13], [22, -9, -2], [-21, 12, -9], [-16, -37, 21], [36, -13, -20], [-11, -2, 0], [-22, 3, -16], [-35, -11, 8], [21, 18, 9], [40, -9, 8], [17, 17, 2], [3, -10, -9], [80, 2, 1], [62, -13, 4], [36, -13, -13], [6, -19, 13], [5, 1, 16], [-49, -7, -2], [66, -1, -11], [16, -31, -12], [-34, 10, 13], [-46, -38, 20], [0, -20, 4], [12, -5, 8], [40, 22, -16], [-28, 7, -25], [16, -8, -9], [17, 21, -14], [-78, 14, 5], [-9, 24, 2], [35, -9, 3], [11, -18, 35], [15, -24, 7], [-30, -26, 9], [11, 5, -14], [7, -25, 7], [-17, -9, 16], [-17, 13, 6], [-39, 20, 10], [45, 12, -3], [78, 1, 10], [-48, 24, 6], [26, 4, 21], [-6, 14, -13], [-1, 29, -26], [12, -12, 0], [-2, -39, 11], [72, 9, -3], [-14, -8, 27], [-6, -4, -32], [-11, 31, 3], [56, -12, -13], [28, -33, -13], [10, 45, -11], [50, 12, 10]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4014_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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