
// Make newform 8030.2.a.p in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8030_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_8030_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_8030_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 := [-3, -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_8030_a();"
function MakeCharacter_8030_a()
    N := 8030;
    order := 1;
    char_gens := [1607, 2191, 881];
    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_8030_a_Hecke(Kf)
    return MakeCharacter_8030_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, -1], [-1, 0], [2, 1], [1, 0], [5, -1], [-5, -1], [2, -2], [-1, -3], [0, 3], [-2, 4], [-1, -3], [12, 0], [8, 0], [2, 0], [6, -2], [-2, 0], [-2, 0], [-4, 1], [-2, -1], [1, 0], [6, -4], [8, -5], [-11, 5], [6, -1], [12, -3], [-4, 4], [13, -3], [0, 0], [8, -2], [14, -4], [1, -7], [-10, 7], [-5, 7], [-6, 8], [-1, 1], [-8, 4], [-20, -2], [-8, 8], [6, -4], [-6, 6], [16, -3], [-10, 4], [5, 1], [-12, 11], [14, -4], [-6, 4], [13, -5], [14, 0], [8, 0], [10, -12], [11, -3], [-2, 4], [4, -5], [14, -12], [-18, 3], [-4, 3], [14, 5], [12, -1], [15, -9], [-8, 6], [2, 4], [20, 5], [-20, 0], [10, -14], [-7, 7], [8, 10], [30, 1], [-18, 0], [14, 4], [-6, -11], [0, -2], [5, 11], [20, -4], [22, 6], [0, 8], [-3, 15], [15, -3], [-10, 15], [-6, -7], [-28, 5], [-20, 8], [-23, 11], [-18, -6], [26, -4], [6, -10], [6, 12], [2, -4], [-18, 0], [11, -7], [18, 6], [-6, 4], [6, -19], [-4, -11], [9, 9], [10, 4], [-3, 11], [-18, 12], [-8, 22], [8, -13], [8, -8], [-18, 4], [-24, 13], [-14, -7], [11, -5], [10, -6], [0, 17], [-12, 2], [-26, -8], [23, 3], [-10, 16], [11, -15], [-20, -2], [17, 9], [12, -2], [-38, 1], [-8, 8], [-36, 2], [-32, -5], [-12, -6], [-11, 7], [-6, 4], [5, 7], [6, -12], [-14, -6], [-18, 4], [8, -8], [-8, 20], [-26, 15], [-32, 0], [0, 6], [-6, -11], [16, -2], [-36, 7], [13, 17], [-31, 9], [-40, 0], [10, 12], [27, 5], [4, -6], [-26, -2], [6, 10], [-20, 4], [48, 2], [16, -4], [-28, -4], [-32, -6], [38, 4], [-18, 16], [14, 0], [16, -8], [8, -22], [-10, 24], [-2, -1], [-16, 16], [11, -15], [16, 16], [-48, -2], [-20, 18], [30, 2], [-36, 12], [18, 12], [14, -8], [2, 20], [-14, -4], [0, -16], [-38, -8], [38, -8], [-2, -13], [-46, 0], [-6, 18], [0, -21], [20, -2], [22, 5], [24, -10], [23, -1], [-20, 17], [-6, -8], [30, -8], [48, -3], [-14, -17], [28, 14], [-31, 11], [14, 0], [-17, -19], [18, -32], [14, 12], [34, -10], [2, 5], [-9, 17], [-18, 4], [-33, -1], [13, 5], [2, 10], [-40, 4], [-18, 16], [7, 23], [-34, -14], [-48, 6], [56, 0], [48, -6], [-4, 24], [3, -31], [12, -28], [-47, 5], [-11, -5], [-14, 7], [23, -17], [-12, -16], [31, 7], [7, -13], [-36, 10], [-36, 8], [-18, 18], [9, 19], [10, 26], [14, -31], [54, -8], [-10, -6], [-2, -20], [-24, 32], [-18, -18], [0, -18], [59, -7], [-16, -17], [13, 11], [-15, -7], [6, -14], [-13, 5], [24, 16], [26, -32], [4, 12], [-9, 1], [-6, -16], [-43, -7], [-10, 36], [-6, -14], [-63, -1], [2, -2], [34, -6], [48, -6], [-38, 6], [44, 0], [-20, 9], [-66, 1], [-18, -1], [-20, 6], [9, 13], [-32, -12], [6, 6], [-30, -18], [-18, -13], [24, -40], [-9, -5], [-30, 20], [-12, 4], [-42, -8], [19, -29], [-36, -2], [-6, -16], [-40, -2], [44, -2], [22, 0], [56, -3], [-6, -3], [8, -24], [18, -16], [-24, -2], [11, -13], [12, -12], [-54, -8], [4, 0], [-30, 21], [2, 21], [44, 14], [-4, -4], [12, -31], [-22, 30], [32, 2], [24, -8], [-42, -4], [13, -17], [-34, -5], [4, -4], [-29, 19], [54, 3], [-2, 3], [-7, 7], [4, -12], [-18, 6], [0, 3], [24, 15], [30, 9], [51, -5], [-6, -12], [39, 17], [4, -38], [-75, 3], [-18, -16], [-16, -6], [18, -19], [-55, 23], [23, -11], [-25, -5], [-40, -4], [-6, 0], [-4, -2], [34, -24], [12, 0], [-62, 12], [32, -30], [28, -9], [-6, 12], [22, 0], [21, 7], [46, 7], [-8, -8], [-36, -2], [50, -28], [-56, -4], [-32, 25], [-10, -32], [24, 22], [0, 32], [-12, 30], [2, -18], [16, -20], [10, -24], [-30, 33], [34, 10], [-82, 0], [10, 18], [-18, -14], [-48, 1], [4, 16], [81, -3], [10, 8], [66, 8], [-26, -11], [-54, 0], [-2, 10], [-72, 12], [42, -14], [14, -7], [-12, 43], [-38, 4], [-20, 0], [42, 6], [6, -3], [44, -2], [2, 28], [-37, -25], [40, -16], [-30, 21], [12, -35], [-42, 24], [56, 12], [2, -22], [-48, 34], [22, -12], [-64, -3], [-48, -14], [54, 3], [52, -20], [-56, 20], [-37, -17], [-10, 0], [2, -15], [-31, 5], [41, 21], [55, -25], [26, -14], [-60, 16], [0, 40], [56, 3], [49, -11], [44, -30], [-66, 14], [-4, 34], [26, -49], [10, -44], [-72, -6], [-28, -6], [7, 23], [16, 19], [82, -7], [52, -20], [-46, 24], [86, -4], [36, -30], [-23, -23], [-51, -13], [-21, -21], [43, -25], [-8, 34], [14, 2], [26, -44], [42, -38], [32, 8], [51, -3], [14, -48], [60, 18], [20, 32], [-59, -11], [-29, 41], [-29, 9], [-52, 23], [6, -30], [8, 2], [50, -18], [41, -15], [-63, 31], [-11, 37], [36, -4], [-57, 11], [72, 12], [12, -32], [-32, -26], [-1, 23], [-4, -16], [-30, 18], [-28, 0], [-34, 12], [72, -22], [-8, 16], [42, 8], [-30, 22], [-16, -4], [-28, -12], [20, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8030_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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