
// Make newform 3330.2.a.b in Magma, downloaded from the LMFDB on 19 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_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_3330_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
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], [-1], [-3], [5], [-2], [-3], [-6], [4], [1], [-3], [-1], [7], [3], [0], [-5], [-6], [5], [-4], [12], [0], [-4], [-6], [18], [-13], [12], [16], [-2], [5], [-9], [8], [-10], [10], [5], [18], [10], [11], [11], [24], [-13], [-12], [10], [3], [18], [2], [-4], [19], [7], [21], [12], [0], [-29], [-10], [24], [-26], [-5], [16], [8], [-16], [-2], [4], [-7], [-2], [31], [-30], [27], [-20], [-6], [20], [-20], [37], [30], [-3], [26], [32], [20], [3], [18], [2], [6], [12], [2], [7], [-8], [-29], [-14], [36], [-5], [-21], [34], [15], [16], [-6], [-4], [0], [-12], [-24], [-3], [-40], [-30], [19], [6], [17], [-46], [-21], [-34], [-1], [-14], [32], [-31], [-34], [-43], [-34], [-27], [29], [23], [31], [-28], [-16], [-16], [-29], [48], [-10], [29], [-33], [10], [49], [18], [-22], [-51], [1], [3], [-12], [30], [-25], [38], [17], [-32], [-40], [12], [20], [-42], [56], [-23], [25], [26], [-14], [3], [-26], [-27], [27], [39], [-3], [7], [-28], [16], [-8], [1], [-40], [-46], [29], [40], [-32], [35], [25], [29], [7], [-18], [21], [18], [-30], [-43], [-41], [56], [42], [-10], [62], [30], [-15], [-16], [4], [-48], [48], [-45], [33], [-42], [49], [11], [-54], [-60], [-2], [-60], [6], [-8], [-30], [18], [40], [48], [-42], [4], [18], [-58], [31], [-59], [-61], [-2], [50], [66], [4], [51], [-5], [-63], [6], [42], [-51], [-17], [38], [4], [62], [1], [0], [50], [-41], [16], [24], [30], [48], [36], [-23], [-6], [-49], [52], [58], [46], [-58], [59], [-58], [30], [-18], [-8], [39], [-20], [24], [11], [30], [3], [-8], [48], [31], [-74], [-16], [50], [53], [-46], [-74], [-37], [-2], [22], [55], [-3], [-14], [40], [4], [-17], [50], [63], [-16], [64], [-56], [-6], [66], [-3], [-38], [25], [38], [31], [14], [-34], [-31], [6], [-24], [23], [27], [20], [84], [-70], [-61], [11], [48], [-16], [-21], [52], [-18], [-3], [-13], [86], [22], [-42], [76], [-16], [59], [-78], [32], [8], [-4], [-7], [33], [-74], [58], [-52], [-16], [16], [19], [-22], [54], [4], [47], [12], [30], [48], [-64], [54], [-17], [71], [24], [20], [-53], [49], [-74], [69], [-70], [-6], [-47], [12], [46], [-66], [-30], [-42], [90], [72], [-18], [18], [-54], [77], [-58], [-62], [-20], [30], [47], [27], [-85], [-28], [-63], [8], [14], [26], [29], [30], [-26], [-36], [38], [66], [26], [-26], [-14], [-87], [-98], [58], [30], [96], [58], [-9], [-66], [-57], [43], [56], [0], [20], [-60], [20], [29], [-54], [-52], [60], [-6], [-81], [-34], [-26], [-31], [-68], [59], [-100], [-30], [77], [-60], [96], [64], [101], [-41], [-12], [-46], [39], [54], [42], [70], [76], [40], [-15], [-4], [-15], [88], [69], [27], [-58], [-47], [-48], [-66], [31], [52], [78], [-2], [16], [-4], [-23], [44], [-57], [24], [-3], [-52], [-33]];
    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_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_3330_2_a_b(:prec:=1)
    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_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_3330_2_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3330_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![3, 1]>,<11,R![-5, 1]>,<13,R![2, 1]>,<17,R![3, 1]>],Snew);
    return Vf;
end function;