
// Make newform 4029.2.a.c in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4029_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_4029_2_a_c();".
// 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_4029_2_a_c();".
// 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 := [-2, 0, 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_4029_a();"
function MakeCharacter_4029_a()
    N := 4029;
    order := 1;
    char_gens := [2687, 3556, 3163];
    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_4029_a_Hecke(Kf)
    return MakeCharacter_4029_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], [1, 0], [-1, 1], [1, 0], [0, -4], [0, 0], [1, 0], [-5, 2], [-3, 3], [2, 2], [4, -4], [-5, 4], [2, 4], [2, -6], [-3, -5], [-11, -1], [-5, 1], [5, -4], [-10, 0], [-6, -5], [2, -9], [-1, 0], [4, 1], [-12, 4], [16, 1], [0, 3], [2, -3], [-2, -1], [0, 6], [-12, 0], [16, 3], [-1, 1], [3, 11], [-6, 6], [-9, -5], [9, -6], [-2, 9], [-8, 3], [3, 5], [-2, 5], [-6, -3], [2, -1], [-21, 1], [7, 6], [12, -6], [1, -2], [-3, 12], [-7, 2], [-16, -2], [-4, 1], [6, 13], [6, -9], [-4, 1], [-3, 11], [-2, 16], [-6, -13], [-6, -6], [10, 9], [0, -12], [-8, 1], [12, 0], [-3, 19], [14, 11], [6, -14], [-16, 8], [-15, -9], [-18, 3], [0, -9], [13, -13], [-2, -3], [-3, 9], [-10, -4], [-8, -11], [10, 3], [13, 18], [18, -3], [16, 1], [-22, 10], [0, 3], [-10, 20], [8, 5], [-5, 12], [-27, -9], [-20, -4], [28, -5], [-31, -1], [-6, -6], [-9, 18], [12, 14], [6, 3], [12, -18], [25, 7], [20, 15], [8, -14], [6, -24], [-12, 0], [7, 13], [0, 8], [-2, -18], [-16, 9], [28, 11], [-14, -10], [-36, 1], [20, 13], [-2, 16], [6, -9], [3, -7], [-26, -7], [-32, 11], [15, 12], [22, 0], [10, -6], [2, -8], [21, 0], [24, 0], [-9, 11], [-12, -15], [-3, 1], [-25, -5], [-9, 9], [16, 21], [0, 12], [-23, 1], [-1, 19], [-17, -6], [-9, -1], [-25, -14], [4, 8], [5, 0], [-13, -20], [26, -16], [-21, -15], [-4, -20], [5, -12], [12, -11], [32, 6], [-12, -4], [-16, 9], [-35, 1], [2, -4], [-10, 9], [-12, -22], [-23, -6], [24, -3], [-10, -8], [15, 23], [27, -12], [-2, 8], [4, -18], [-8, -16], [-30, -9], [0, 24], [-42, 12], [23, 1], [-32, 7], [-22, -8], [-10, 12], [-36, 5], [36, -9], [1, 35], [18, -8], [18, -4], [-23, -8], [-20, -20], [-27, -1], [24, -9], [-17, -6], [34, 6], [0, -12], [29, 1], [27, -7], [11, 0], [14, -4], [20, -9], [22, 16], [21, -19], [9, -36], [-18, -7], [11, 8], [21, 24], [-4, 10], [-45, 7], [10, -32], [-45, 9], [26, 5], [-54, -2], [0, -6], [46, 13], [-10, -29], [-4, 2], [-7, -16], [-12, -9], [7, 14], [-29, 7], [-11, -7], [-15, -3], [8, 10], [-20, -20], [-9, -25], [38, 5], [12, -18], [6, -45], [-45, -6], [3, 0], [-21, 31], [0, -28], [-4, -16], [18, 30], [30, 6], [28, -8], [30, -12], [-36, 2], [-25, 24], [-3, -27], [-24, 14], [5, 22], [-10, 2], [6, 33], [-12, -20], [27, 1], [-11, -6], [22, 18], [19, 23], [16, -17], [-6, 5], [64, -7], [13, -19], [-44, -5], [8, 36], [0, -32], [-7, 10], [-53, 0], [1, -2], [15, -27], [-30, 24], [6, 8], [4, 24], [12, -27], [48, 7], [-28, -16], [27, -13], [-20, 21], [31, -8], [2, -1], [25, -1], [-4, 44], [-13, 12], [-42, -21], [-8, 25], [-13, 7], [-14, -10], [10, 4], [-3, 5], [9, 0], [39, -21], [33, -9], [-31, 26], [-11, 16], [18, 2], [52, -2], [18, -3], [54, -15], [16, -3], [-16, 4], [-24, -18], [-35, -14], [-24, 2], [30, 0], [24, -3], [-51, -9], [23, -8], [34, 12], [-4, 26], [2, -42], [-20, -30], [4, 32], [-42, -1], [28, 18], [-6, 33], [0, -50], [-11, -29], [-66, 0], [30, -21], [40, -10], [-40, -15], [33, -3], [-27, 18], [-6, -25], [7, 34], [12, 33], [15, 35], [-15, 1], [-6, -2], [-30, 0], [25, -6], [-60, 1], [-38, -28], [6, 40], [-23, -23], [24, 6], [-12, -24], [-18, -36], [-7, 16], [-6, -33], [62, -16], [32, -20], [24, -21], [-7, -56], [-18, 27], [14, -4], [18, -8], [0, 9], [-60, -9], [12, 18], [-8, -14], [-48, -15], [-18, -19], [60, 9], [-24, 12], [42, 2], [10, 28], [-10, -25], [-52, 14], [29, -18], [6, -28], [-22, -12], [-82, -6], [-7, 6], [22, 8], [51, 15], [54, 18], [7, 53], [-23, 38], [-12, -32], [-49, -8], [-4, -4], [50, -17], [-70, -8], [27, -6], [0, -39], [11, 42], [6, -12], [-5, 35], [47, -16], [56, -1], [36, -12], [16, -30], [36, 3], [-66, -19], [16, -38], [-28, -28], [7, -10], [76, 2], [14, 30], [3, -54], [80, 7], [3, -47], [-22, 10], [72, 2], [63, 5], [16, 21], [-66, 3], [2, -8], [1, 5], [-74, -12], [0, -3], [-64, 8], [22, 46], [-32, 3], [27, 13], [10, 5], [-52, 7], [36, -34], [-83, 6], [-20, 20], [-30, -36], [38, 34], [40, -24], [34, -16], [80, 6], [48, 1], [10, 2], [-10, 20], [-5, -43], [-24, -12], [84, 3], [-23, -22], [20, 8], [-4, -24], [-42, 2], [92, 0], [12, 27], [-36, -45], [23, 46], [28, -34], [45, 0], [-10, -15], [6, 26], [-78, 12], [72, 6], [62, 0], [36, -22], [0, -12], [46, 13], [-42, 20], [-8, -41], [7, -8], [-30, -15], [-1, 0], [0, -11], [-35, 30], [-46, 25], [-51, -31], [-50, -24], [-77, -2], [-54, -5], [52, -31], [-15, -36], [8, -7], [87, 3], [-42, 15], [-16, -27], [-76, 4], [-6, 40], [15, 0], [-3, 15], [-9, -1], [-21, 17], [-32, 30], [33, 9]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4029_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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