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