
// Make newform 3330.2.a.bc in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3330_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_3330_2_a_bc();".
// 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_3330_2_a_bc();".
// 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 := [-4, -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_3330_a();"
function MakeCharacter_3330_a()
    N := 3330;
    order := 1;
    char_gens := [371, 667, 631];
    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_3330_a_Hecke(Kf)
    return MakeCharacter_3330_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], [1, 0], [-1, 0], [1, 2], [-2, 3], [5, 0], [-2, 1], [4, 1], [-1, 3], [-1, -1], [1, 0], [-1, -3], [-5, -3], [8, 0], [5, -4], [6, -4], [-1, -5], [8, 2], [4, -2], [-4, -1], [4, 4], [10, -1], [2, -3], [11, -3], [-16, 2], [4, -8], [-2, 5], [3, -2], [3, 3], [12, 1], [-2, -2], [-6, 4], [1, 5], [-18, 4], [-18, 1], [5, -9], [-1, 8], [4, -9], [-3, 2], [4, 0], [10, 4], [1, -6], [-14, 0], [10, -3], [4, -8], [7, -1], [-7, -5], [-7, 7], [0, -2], [4, -6], [5, -5], [-2, 0], [0, -10], [2, -5], [9, -1], [4, 5], [4, -8], [20, 3], [6, -11], [-8, 7], [-9, 10], [10, -4], [-7, 11], [-30, 2], [1, -7], [-12, 0], [2, 1], [28, 0], [4, 4], [17, 5], [26, -2], [-17, 0], [-10, -8], [-8, 2], [20, 3], [-3, -7], [22, 2], [2, 5], [-6, 2], [8, 1], [-10, 16], [29, 0], [8, -15], [9, -9], [2, -14], [-24, 10], [-13, -7], [-3, 1], [10, -2], [-5, 1], [4, 1], [-14, 12], [-16, 11], [20, 5], [8, 4], [0, -9], [21, -1], [0, 12], [18, -15], [-9, -12], [18, -10], [13, -7], [-10, 3], [7, -5], [6, -8], [-21, 13], [-10, 2], [4, -12], [-7, -14], [-6, -8], [11, -1], [6, -8], [-23, -5], [23, -3], [-25, 9], [-29, -2], [24, 5], [8, 8], [8, 4], [-3, 14], [-16, -1], [-18, -7], [33, -1], [-13, 3], [6, -6], [-25, 8], [2, 0], [10, 16], [11, -13], [-11, -11], [17, -15], [-8, 12], [-38, 9], [-25, 3], [-30, 8], [-17, 8], [32, -2], [0, 10], [28, -1], [-28, 12], [-26, 1], [-12, 17], [13, -1], [-17, -2], [-18, -4], [-10, 7], [19, -4], [10, -5], [-33, 5], [29, 7], [-17, -7], [1, -6], [5, 5], [48, -1], [0, -8], [-24, 12], [-7, 5], [-16, 6], [-2, 4], [17, -13], [-24, 10], [48, -6], [3, 5], [33, -8], [7, 7], [-35, 15], [-14, 13], [-7, 17], [22, 10], [-2, -12], [15, -25], [-7, -5], [8, -13], [2, 9], [22, -12], [10, -18], [-26, 9], [15, 13], [24, -6], [40, -8], [4, -2], [36, -7], [-1, -17], [-37, 13], [-14, -17], [-9, -7], [-25, 14], [38, -10], [24, -16], [-18, -8], [-20, 0], [50, -7], [24, 9], [-30, 10], [-26, -2], [-20, 8], [-20, 22], [-18, -2], [-32, 11], [22, -8], [-2, -11], [9, -1], [17, -19], [27, 5], [54, 0], [14, 9], [-54, -3], [64, -1], [39, 5], [-21, -5], [-49, -7], [-14, -2], [-22, -7], [31, 4], [-1, 14], [22, 4], [20, 18], [-6, 7], [-25, 12], [-8, -14], [-22, 21], [55, 3], [-48, 2], [-24, 12], [-10, 1], [-32, 16], [36, -24], [-9, 17], [-50, 6], [25, -23], [-44, 8], [18, -21], [6, -6], [-14, -18], [25, -27], [18, -1], [2, 8], [-22, -16], [32, -8], [19, -15], [4, 3], [-20, 1], [37, -6], [-6, 10], [-31, 26], [48, -14], [44, -2], [19, -1], [-26, 17], [0, -11], [42, -2], [47, -9], [-62, -2], [-26, 12], [59, 1], [-62, 0], [-18, -16], [1, 12], [-51, -7], [50, -4], [-8, 4], [-24, 16], [-15, 25], [14, -17], [-21, 1], [8, 5], [-4, 6], [20, -34], [22, -12], [18, 13], [-15, 14], [-14, -18], [3, 1], [-2, 3], [53, -8], [-58, 15], [18, 8], [25, -22], [22, -8], [8, -30], [29, -27], [25, -15], [-8, -16], [-12, -16], [-6, 20], [-45, -5], [-23, 13], [-16, -6], [12, 19], [-3, 33], [-44, -10], [22, -4], [33, 4], [61, -5], [-34, 12], [26, -3], [-34, -9], [36, -12], [-36, 0], [-21, 7], [30, 16], [24, -8], [-32, -6], [-36, 13], [57, -9], [45, 7], [66, 8], [22, 20], [60, -11], [40, -8], [20, -13], [-45, -9], [-2, -2], [2, 1], [16, -18], [27, -30], [32, 21], [-66, 12], [-20, 5], [-24, 1], [-66, 1], [-31, -17], [69, -2], [-4, -8], [16, 0], [23, -15], [33, -21], [-54, -3], [-25, -13], [-22, -2], [22, -2], [19, -17], [36, 2], [22, -9], [50, -10], [14, -12], [-26, 0], [-34, -16], [-8, 16], [-70, -2], [-42, 28], [-66, -2], [-9, -11], [34, -22], [30, -24], [-36, 20], [-50, -12], [-31, 27], [17, 17], [-25, 15], [-20, 21], [-1, -3], [48, 6], [30, -24], [-18, 6], [-57, -8], [-10, -12], [14, 18], [4, 12], [2, -8], [26, -24], [-26, -27], [-30, -10], [-62, 12], [-47, -19], [30, 14], [-26, 28], [10, -28], [-16, -24], [50, -2], [-43, -7], [-30, 7], [21, 21], [-27, 25], [-52, 27], [4, 6], [36, -13], [-28, 9], [-4, 15], [-13, -9], [22, -9], [32, -14], [12, -10], [-26, -10], [-11, 7], [-18, 33], [-2, -8], [25, -1], [-20, 11], [19, 26], [72, -2], [6, 18], [9, -26], [52, 4], [28, -5], [-8, 18], [-3, -33], [43, -1], [36, -9], [42, 18], [-49, 34], [-90, 6], [26, -4], [-14, -16], [36, -21], [20, 7], [-55, 11], [-44, 20], [25, 31], [28, 1], [-77, -10], [-25, 35], [-30, 43], [-7, -17], [52, -21], [-34, -2], [13, -12], [-16, -26], [34, -2], [10, 12], [64, -11], [-68, 2], [-11, 10], [4, 27], [37, -39], [-12, -17], [-3, -27], [12, 14], [-47, -1]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3330_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3330_2_a_bc();".
// 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_3330_2_a_bc(:prec:=2)
    chi := MakeCharacter_3330_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_3330_2_a_bc();".
// 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_3330_2_a_bc( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3330_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![1, 1]>,<11,R![-13, -4, 1]>,<13,R![-38, 1, 1]>,<17,R![-5, 1]>],Snew);
    return Vf;
end function;