
// Make newform 3330.2.a.bi in Magma, downloaded from the LMFDB on 17 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_bi();".
// 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_bi();".
// 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, -4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-3, 0, 1]];
    Rf_basisdens := [1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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, 0, 0], [-1, 0, 0], [-1, 2, 0], [2, -1, 1], [1, 0, -1], [-1, -2, 2], [1, 0, -1], [-1, 4, -1], [4, -2, 1], [3, 3, 0], [-1, 0, 0], [3, -3, -2], [5, 1, -2], [-6, 2, -4], [4, -3, -1], [4, 2, 2], [-1, 1, -2], [6, 2, 2], [2, -2, -2], [7, -4, 7], [12, -2, 2], [-7, 2, 3], [3, 0, -3], [5, -3, -2], [2, -4, 2], [8, -6, -4], [5, -4, 7], [4, 1, -3], [-4, 0, -5], [7, 2, 3], [-4, -2, -2], [4, 0, -2], [8, -2, 3], [4, 2, 2], [11, -6, 1], [-4, 8, -7], [4, -7, 3], [-7, 4, 5], [4, 3, -3], [8, 6, 0], [-8, 2, 6], [-7, 8, -8], [4, 4, -4], [-9, 6, -1], [-2, 6, 0], [-8, 6, -1], [2, 4, -1], [-12, -4, -1], [10, -6, -2], [0, 0, -8], [-4, -6, 5], [2, -8, 0], [-6, 12, -6], [-15, -6, -1], [3, -13, 10], [7, 2, -1], [-12, -4, -4], [-3, 6, 5], [-7, 2, 3], [-9, 4, 9], [-10, 3, 7], [-4, 10, -10], [-16, 2, 5], [4, 4, -4], [-15, -1, 0], [-2, -6, 6], [7, -12, 5], [-10, 0, 2], [12, 6, -4], [0, -12, 5], [20, 8, -2], [7, 6, 2], [-12, -4, -2], [12, -8, 4], [-11, 0, -7], [6, -2, 3], [22, -8, 4], [-3, 10, -9], [2, -16, 0], [-15, 0, -5], [-2, 0, -6], [3, 0, 4], [-7, -4, 1], [-9, -3, 2], [0, -8, -2], [-12, 14, -4], [7, -13, 10], [-10, -2, -3], [0, -16, -2], [-8, -12, 3], [-1, -14, 11], [-8, -20, 6], [15, 4, -3], [-17, 16, 7], [12, -12, -8], [9, -6, 1], [5, -15, 8], [6, -18, 8], [-5, -10, -5], [-6, 11, -17], [12, -14, -2], [0, 8, 7], [9, 4, -1], [-8, 10, -11], [0, 12, -8], [-2, -20, 9], [10, -8, 4], [-6, -2, 2], [-17, 0, 6], [-4, 8, 6], [-28, -8, 5], [-2, 8, 4], [4, 10, -9], [-15, 7, -2], [1, 11, -6], [-30, -7, 5], [-17, 8, -17], [20, 10, -8], [10, -6, -8], [-8, -13, -1], [-13, 8, 3], [-33, 6, -1], [4, 4, -9], [30, 6, 1], [20, -4, -2], [28, -9, 11], [-6, 20, 0], [-2, 6, -8], [2, -12, 5], [18, 2, 9], [3, -1, 2], [-10, -2, 2], [17, -6, -5], [-3, 19, -10], [-16, 0, 6], [18, 17, -3], [-6, -2, 10], [-26, 16, 6], [23, 4, -1], [-14, 12, -2], [21, -2, 7], [-33, 6, 3], [14, -16, 11], [10, 7, -15], [12, -18, -2], [-27, -4, -1], [-15, -14, 8], [-17, 0, -11], [-11, -1, 0], [20, -4, -13], [9, 1, 12], [12, -11, 7], [-11, -17, 0], [-9, -20, 9], [-20, 4, 12], [-8, 2, -14], [29, -3, 4], [-2, -16, 10], [-2, 12, -4], [4, 4, 7], [8, -14, -8], [2, -22, 2], [-7, 1, 0], [-1, 18, -4], [-23, 7, -4], [9, 3, 6], [-27, 14, -13], [22, -2, 3], [-28, 18, 6], [2, 2, -16], [-19, -21, 10], [24, -2, -9], [15, -8, 11], [-5, -10, 17], [42, 0, 8], [38, 6, 4], [11, -16, -3], [30, 0, 1], [16, -6, -8], [-36, 2, 0], [10, -12, -2], [-5, 26, -1], [0, 12, 9], [19, 3, 2], [3, 14, -11], [2, 0, -1], [-4, 15, -7], [-8, 26, -22], [8, 8, 8], [-10, 28, -22], [14, 14, 2], [11, 6, 9], [-17, 2, -17], [-10, 10, -8], [-18, 6, -8], [4, 10, -8], [10, -12, 10], [-14, 12, -16], [7, -10, 11], [4, 8, -18], [11, 2, 1], [-4, 8, 7], [-6, 10, 1], [15, 7, -4], [40, 8, -2], [7, -18, 17], [19, -26, 13], [25, 2, -3], [-10, 10, -11], [-17, -19, 0], [-22, 6, 1], [6, 6, 16], [-37, 10, 5], [-43, -4, -2], [13, 16, -2], [40, -6, -6], [-8, -2, 4], [-17, 12, 9], [-18, 1, -7], [-12, -14, 0], [15, 10, 9], [-13, -7, -2], [-4, -12, 16], [-18, 30, 6], [35, 2, -5], [10, 20, -14], [6, 20, -2], [-26, -4, 9], [-12, -2, 2], [18, -16, -13], [32, -4, 12], [3, 16, 5], [14, -12, 22], [8, -28, 2], [-37, 7, -6], [-19, 10, 15], [18, -16, 0], [26, -10, -8], [24, -4, 8], [-32, 16, -21], [-1, 6, 7], [19, -14, -1], [22, 13, -5], [-36, -4, 14], [-15, -12, 14], [-58, 4, 6], [4, 12, 8], [46, -6, 1], [-41, 10, 1], [23, -2, 7], [18, 12, 2], [5, -21, 8], [-28, 8, -6], [18, 14, -16], [27, 11, 8], [4, 20, -6], [-26, 18, 8], [12, -19, 11], [43, -15, -8], [-4, -14, 10], [-10, 0, -26], [-26, -4, 14], [-22, -20, 3], [37, -2, -13], [-14, 26, -29], [-25, 14, -5], [0, 10, -12], [-22, -8, 6], [-4, -4, 10], [-17, 14, 11], [-2, 27, -9], [10, -20, 6], [-17, -5, -16], [-5, -6, 1], [9, 22, -22], [25, -30, -1], [42, -12, -14], [15, 0, -2], [-32, 20, -30], [-26, 30, -8], [15, -7, -2], [-13, 27, -22], [14, -32, 10], [-42, -10, 4], [8, 8, -2], [27, 11, -10], [-25, 13, -12], [24, -30, 6], [-29, 8, 3], [-6, 10, 1], [28, 16, -8], [30, -10, 8], [20, 7, -5], [-18, 8, -11], [-48, -10, -2], [45, 4, 7], [17, -2, -15], [4, 12, 8], [6, 16, -18], [-3, 9, -2], [22, 0, -10], [34, -2, -18], [-20, 20, -28], [-15, 6, -23], [-65, 11, -6], [6, 0, 29], [2, -28, 6], [0, -38, 18], [-13, -14, 15], [-34, 18, -8], [7, -28, -1], [-9, 5, 14], [4, 6, 22], [3, -2, 5], [14, -6, 2], [12, 7, 11], [3, -2, 11], [-38, -12, -4], [31, -6, 23], [-35, 6, 9], [-17, 0, 1], [58, 2, 3], [-1, -6, -14], [12, -10, -12], [24, 0, -16], [-24, 2, -1], [61, -15, 14], [15, -22, -15], [-13, -7, 4], [-4, -26, -2], [-38, -4, -8], [-19, -5, -2], [-6, -32, 22], [27, 0, 21], [34, 6, 12], [40, -16, 18], [10, -8, 20], [-66, 10, -8], [-12, -4, 4], [76, 0, 2], [0, 28, 2], [0, -24, -6], [35, -19, 26], [30, 2, 4], [-8, 38, -2], [-32, 16, 4], [-44, 20, -18], [20, -14, 25], [53, -9, -4], [44, -18, -13], [5, 12, -27], [-50, 10, 13], [40, -2, -16], [-56, 14, -6], [-20, 20, -6], [-11, 20, -18], [40, 0, 18], [56, -4, 2], [40, -4, 16], [-2, 20, -24], [30, -12, -2], [-9, -22, 17], [-48, 0, -10], [34, -24, 0], [-5, -39, 20], [10, -20, -8], [8, -14, -6], [-32, -2, 10], [-6, 38, -2], [24, 0, 26], [-31, 15, 4], [49, 0, -9], [-1, -33, 4], [8, -4, 29], [-13, 28, -11], [-10, 24, 18], [-37, 24, 15], [5, -4, 21], [31, -16, -1], [-8, -18, 11], [-51, 22, 3], [22, 4, 18], [28, -2, 28], [-14, -2, 12], [-65, -7, 8], [51, -14, -7], [-62, 4, 12], [-59, 21, 14], [15, 10, -25], [33, -28, -10], [-18, -16, -14], [-38, 16, -12], [-38, -1, 19], [-12, -8, -4], [43, 0, 19], [-20, -10, -6], [33, -7, -20], [40, -26, -5], [-25, 32, -29], [-40, -30, 10], [23, -14, -6], [36, -20, 22], [-34, -24, 12], [-80, 4, 2], [5, -28, -7], [-13, 28, -5], [23, 1, -18], [-30, -22, 2], [1, 5, 16], [-29, -2, 7], [-16, 33, -1], [34, -16, -23], [-31, 26, -21], [7, 11, 10], [23, -36, 7], [-10, -2, -16], [5, 10, 24], [34, 24, -6], [-64, -8, 10], [-24, -24, 10], [-33, 2, -17], [-26, 2, -4], [24, 1, -15], [17, -10, 25], [-33, 17, 8], [55, -10, 3], [-51, 19, -2], [2, -4, 14], [-56, 18, 15]];
    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_bi();".
// 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_bi(:prec:=3)
    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_bi();".
// 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_bi( : 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, -17, 1, 1]>,<11,R![11, 1, -5, 1]>,<13,R![4, -4, -3, 1]>,<17,R![-17, -21, 5, 1]>],Snew);
    return Vf;
end function;