
// Make newform 4030.2.a.b in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4030_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_4030_2_a_b();".
// 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_4030_2_a_b();".
// 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_4030_a();"
function MakeCharacter_4030_a()
    N := 4030;
    order := 1;
    char_gens := [807, 1861, 2731];
    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_4030_a_Hecke(Kf)
    return MakeCharacter_4030_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, 2], [-1, 0], [1, -3], [-2, -2], [1, 0], [4, -1], [-4, 5], [-1, -3], [8, 1], [-1, 0], [-7, 5], [2, 6], [4, -2], [-8, 5], [10, 0], [5, -3], [1, -9], [2, 4], [-2, 8], [-2, 2], [14, -4], [0, 3], [9, -3], [-10, -1], [0, 4], [-4, -8], [-10, -1], [-5, -1], [4, 6], [4, -4], [-12, -2], [16, 0], [2, 11], [19, 3], [13, 1], [-16, 9], [6, 8], [-16, 4], [11, -3], [15, -7], [6, 4], [-2, -11], [14, -1], [-10, 13], [10, -12], [-6, -2], [0, -18], [12, -18], [-8, 10], [10, 0], [16, -7], [3, -9], [2, 7], [20, -12], [2, 15], [13, -21], [4, -5], [-4, -8], [16, -20], [-18, -1], [24, -16], [-2, 10], [21, -21], [2, -4], [0, 4], [-8, 4], [5, 19], [20, -4], [10, -1], [2, -22], [6, 4], [10, -1], [-27, 3], [11, -9], [-20, -6], [14, -12], [14, -28], [10, 5], [10, -4], [6, -10], [-18, 11], [16, -6], [17, -17], [11, -11], [24, -4], [-22, 9], [28, -16], [-4, 10], [-16, -8], [-17, -11], [28, 2], [22, -26], [1, 11], [-14, 12], [32, -6], [-10, -10], [-16, -9], [-14, 6], [-15, 9], [-31, 7], [-3, 13], [-20, 36], [18, -18], [-12, 36], [-15, 29], [-7, 25], [-5, 3], [-19, -5], [20, 6], [10, 14], [5, 9], [27, -29], [-18, 24], [16, -7], [-8, 16], [-4, -11], [-24, 8], [18, 16], [-24, 26], [23, -1], [1, -29], [-22, -14], [-4, 22], [-1, 11], [18, -6], [8, -34], [36, -12], [24, 8], [30, -22], [-6, -24], [8, -4], [34, -15], [38, -20], [39, -21], [12, -16], [10, -21], [-28, -7], [-34, 4], [2, 26], [-36, 24], [-18, 20], [-37, 9], [-43, -3], [16, 9], [-12, 32], [18, -24], [6, -6], [-11, 15], [-6, 32], [-38, 20], [-26, 12], [20, -42], [-36, 36], [-39, -1], [22, 0], [15, 17], [26, -47], [-22, 8], [2, 6], [-12, 0], [-20, 3], [-12, -26], [-14, 4], [39, 11], [-16, -14], [4, -12], [-48, -1], [26, -15], [2, 34], [-18, 32], [-6, -4], [-29, -7], [6, -15], [-22, 29], [24, 12], [-8, 16], [-10, 1], [-14, 4], [17, -37], [8, -37], [32, -16], [-3, -21], [8, -12], [-44, 1], [-11, -13], [-4, 33], [2, 24], [-34, 25], [-26, -18], [-16, 9], [32, -12], [34, 1], [44, 10], [-26, 13], [-31, -5], [-6, 0], [22, -48], [11, -1], [-20, 40], [26, 11], [12, 4], [-14, 22], [-15, -17], [-29, 23], [14, -4], [-18, 8], [-56, 14], [6, -28], [26, 4], [-4, -11], [16, -3], [44, -10], [16, -26], [-47, 21], [-46, 2], [-16, 22], [-32, 21], [52, -2], [2, 0], [21, -21], [28, 24], [46, 12], [8, -33], [30, 13], [38, 0], [-42, 12], [-10, -34], [-39, -17], [4, -27], [6, 32], [26, 6], [6, -16], [-6, 8], [8, 32], [-18, 26], [6, -30], [30, -30], [8, -16], [-34, 62], [32, -54], [43, 1], [-18, -5], [-30, 28], [8, 26], [10, -1], [-42, 16], [-62, -8], [32, 2], [2, 26], [18, 14], [-26, 22], [-2, -40], [5, 3], [-38, -24], [24, 31], [18, 6], [-49, -13], [-3, 27], [25, -11], [6, 0], [-16, 18], [60, 0], [-49, 13], [62, -30], [-20, 4], [-41, 7], [-36, -18], [50, -16], [-16, -21], [42, -10], [12, 1], [46, -40], [-18, 50], [-14, -14], [4, -3], [-4, -30], [36, -12], [22, -29], [0, -32], [32, -22], [42, -10], [-40, 38], [50, -27], [6, -8], [32, -4], [-38, -20], [50, -8], [-34, 16], [-16, -30], [32, -34], [25, -21], [-34, 50], [44, -16], [48, -34], [28, -31], [-18, 20], [-47, -9], [14, -7], [9, -47], [-30, 36], [1, 45], [20, 22], [53, -25], [2, -15], [2, 4], [20, 30], [14, 12], [36, 4], [-52, 1], [-37, 1], [-12, -18], [-32, 69], [-4, 34], [-12, -24], [-46, 2], [54, -40], [32, -28], [-2, 4], [-34, -20], [-37, 21], [-48, 0], [36, -30], [-20, 24], [10, -31], [8, -6], [-12, 21], [12, -24], [10, -31], [50, 2], [-18, 5], [57, -53], [2, -25], [-65, 43], [32, -36], [-44, -4], [-4, -50], [81, -15], [-28, -26], [-29, -23], [-78, 0], [-26, 72], [-76, 14], [26, -72], [-30, 28], [-34, 56], [-24, 40], [64, 5], [-65, 11], [-43, -5], [20, -42], [12, 50], [42, -23], [18, 13], [-26, 36], [-15, -9], [45, 17], [50, -46], [33, -5], [-45, 43], [-58, 2], [-8, -30], [34, -33], [-5, -57], [-19, 47], [4, 19], [-64, 28], [-8, 60], [-44, -5], [32, -34], [8, 34], [-19, -15], [-22, 24], [-7, 39], [-46, 56], [66, -42], [-17, 9], [22, -56], [-51, 7], [6, -20], [74, -18], [-22, 76], [66, -34], [26, -22], [-2, -16], [-40, 24], [20, 20], [44, -4], [-61, -25], [-86, -1], [-26, -8], [33, 17], [-71, -11], [62, -55], [-6, 2], [20, 24], [55, -61], [17, 3], [27, 5], [-70, 24], [-48, -10], [16, -48], [-51, 33], [-38, 5], [-64, -4], [-10, -5], [68, -28], [14, -37], [-32, 4], [12, -2], [64, 25], [-44, 9], [46, -20], [19, 33], [-24, -4], [-24, 20], [-47, 89], [4, -36], [-54, 40], [-8, 2], [-44, 63], [31, -41], [6, -24], [24, 6], [-49, -5], [34, -65], [-60, 8], [70, -23], [56, -28], [-14, 14]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4030_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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