
// Make newform 4027.2.a.a in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4027_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_4027_2_a_a();".
// 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_4027_2_a_a();".
// 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;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_4027_a();"
function MakeCharacter_4027_a()
    N := 4027;
    order := 1;
    char_gens := [3];
    v := [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_4027_a_Hecke(Kf)
    return MakeCharacter_4027_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, 2], [-1, 0], [-3, -1], [-1, 0], [-1, -1], [3, -5], [-5, 0], [-5, -1], [-5, 1], [-7, 2], [-6, 8], [-6, 2], [-1, 6], [-6, 4], [5, -2], [-5, 6], [5, -10], [4, -6], [-9, 3], [-8, 5], [13, 0], [-2, -9], [-9, 3], [11, -4], [-4, 3], [-4, -7], [-5, 7], [3, -4], [-5, 2], [-14, 2], [-2, -11], [-11, 1], [17, 2], [0, 5], [-3, 6], [-1, -3], [-14, 5], [-12, 6], [10, 4], [2, -11], [2, -4], [-11, 12], [11, -10], [5, 6], [-20, 8], [-13, -8], [-4, 0], [-12, 12], [5, -21], [-5, 9], [14, -18], [3, 15], [8, 2], [-1, -13], [19, -1], [3, 2], [-14, 7], [-14, 4], [-19, 2], [7, 4], [5, -7], [20, -4], [5, -5], [-21, 14], [6, -18], [-26, -2], [17, -13], [13, -5], [17, 8], [6, 0], [17, -15], [13, -13], [10, -17], [17, -22], [13, 11], [-13, 26], [-11, 22], [-28, 7], [-16, -14], [-15, 2], [2, -24], [-37, 3], [-6, -7], [-1, -16], [16, -31], [-20, 14], [13, 0], [1, 20], [1, 16], [30, -18], [-1, 21], [-26, 16], [-17, 0], [12, -27], [4, 15], [40, -1], [17, -3], [-13, -8], [19, 11], [-9, 23], [-12, 0], [-7, -7], [-22, 19], [-26, 7], [8, 0], [22, -15], [13, -10], [25, -10], [28, -20], [1, -26], [-8, -15], [20, -15], [-38, -1], [39, -5], [-2, 30], [39, -10], [6, 14], [-9, 1], [-10, 0], [-28, 9], [-6, 11], [-8, -20], [-10, 2], [-33, -8], [-20, -9], [-24, -10], [24, 7], [-2, 0], [-40, -6], [-29, 8], [18, -36], [-26, 35], [23, -16], [-30, 15], [6, 6], [-1, 13], [-12, 37], [1, -18], [-7, 21], [-30, 15], [-21, 4], [-43, 14], [-23, -6], [-15, 3], [-27, -14], [-25, 6], [5, 26], [4, 16], [23, -44], [-5, 26], [-16, -7], [15, -38], [-29, 25], [3, -17], [-36, 24], [-10, -1], [3, 14], [-21, 39], [17, 15], [1, 6], [31, -25], [22, -14], [-34, 2], [2, -5], [15, -10], [-1, -12], [0, -21], [-32, 2], [2, 2], [2, -29], [9, -23], [-41, 0], [-51, 4], [39, 6], [-3, -22], [8, 15], [42, -22], [27, -19], [44, -14], [8, -16], [-3, 20], [16, 6], [26, -19], [-44, -10], [-1, 26], [-22, 6], [-10, 1], [-40, -11], [32, -8], [-17, 1], [-24, 45], [-14, -3], [-5, -30], [-17, 27], [33, -2], [-31, 32], [8, 24], [29, -1], [31, -34], [6, -33], [20, -3], [12, -15], [-24, 0], [-27, -5], [-4, 35], [23, -16], [24, -5], [38, -4], [29, -23], [14, 8], [-17, 39], [-7, 10], [-1, 20], [-37, -1], [-15, 10], [36, -10], [29, 8], [-12, 24], [-31, 56], [-33, -24], [16, -28], [19, -33], [29, -49], [43, 12], [-26, 45], [-5, -23], [-17, 33], [-23, -27], [-19, 25], [-48, 18], [-14, 19], [-31, 38], [-40, 4], [26, 2], [19, 28], [-42, -12], [16, -33], [-31, -23], [-49, 21], [17, -20], [-24, 48], [31, 25], [8, -10], [45, -12], [-1, -15], [22, 15], [49, -36], [-47, -14], [-46, -4], [-28, -23], [-29, 25], [-4, 22], [-6, 17], [14, 9], [-1, -28], [-9, -6], [-69, 4], [-4, 10], [-2, 16], [-48, 23], [-51, 30], [2, 28], [-11, -3], [10, -31], [-35, 48], [29, 0], [9, -38], [-36, 47], [42, -10], [20, -21], [-31, 3], [4, -29], [47, -14], [-15, 26], [50, -27], [9, -8], [-31, 43], [-37, 16], [21, -27], [-1, -12], [19, -3], [5, 35], [-64, 5], [27, -17], [-19, -36], [29, 14], [-59, 18], [-10, -42], [17, 16], [40, -34], [-11, 32], [8, -16], [-18, 21], [-36, 8], [37, 6], [-6, -44], [64, -34], [-68, -11], [9, -42], [43, -14], [-31, 60], [-48, 29], [42, -65], [-49, 9], [-55, 30], [-36, 64], [-44, 34], [29, -13], [16, 34], [-39, 7], [-53, 4], [-3, 20], [1, -5], [39, 28], [33, 16], [-77, 3], [-20, -6], [64, -26], [-17, 32], [-50, -15], [30, -25], [53, -19], [-7, 56], [18, -11], [-35, 29], [-30, 8], [-36, 17], [73, 4], [49, 24], [-6, -31], [10, 34], [-33, -9], [-35, -12], [-4, 46], [39, -27], [27, 16], [-37, -6], [-8, 20], [38, 30], [3, 42], [-39, 34], [58, 8], [45, -24], [-48, 18], [-55, -22], [44, -77], [-46, 34], [70, 1], [-1, 17], [16, -62], [-60, 33], [53, -45], [56, 3], [30, 5], [-2, 18], [75, -34], [-60, 48], [1, -35], [30, 14], [16, -23], [7, -1], [-75, 21], [32, -4], [-35, 4], [15, 48], [1, -56], [42, -36], [-21, -25], [-7, -30], [-33, 63], [-1, -33], [31, -7], [85, -12], [13, -45], [24, -63], [11, 7], [43, 22], [-33, 70], [-66, 46], [-36, 15], [-15, 27], [53, -24], [-33, 17], [4, 8], [-34, 12], [-33, 44], [14, 18], [-92, 10], [-6, -50], [-13, -26], [-16, 20], [45, -53], [60, -36], [-12, -10], [-63, 19], [44, 1], [36, -9], [10, -12], [25, -31], [67, 15], [48, -43], [-58, 41], [-45, 58], [17, -54], [12, -35], [-11, 6], [-16, -24], [-40, 15], [-41, -6], [-39, -20], [-20, 32], [1, 3], [15, -55], [42, -8], [34, -11], [-28, 67], [-39, 33], [-21, 7], [9, -27], [-69, 6], [-26, 30], [-75, -12], [18, -15], [-21, 30], [-15, 32], [-2, 19], [13, -35], [53, -62], [-47, 64]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4027_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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