
// Make newform 3330.2.a.u 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_u();".
// 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_u();".
// 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], [-1], [-3], [-7], [3], [-1], [3], [-6], [-10], [1], [0], [-4], [12], [-9], [-6], [-10], [-10], [6], [11], [8], [-9], [9], [-16], [6], [-4], [-3], [-7], [-18], [-7], [12], [18], [14], [6], [23], [-4], [-1], [-3], [9], [0], [-22], [-3], [-10], [-3], [20], [2], [8], [-24], [-10], [-18], [-12], [26], [-6], [-9], [0], [-9], [20], [23], [-27], [-13], [15], [-22], [24], [8], [18], [-28], [-13], [-36], [8], [-18], [12], [23], [14], [26], [9], [-18], [-4], [-15], [-4], [27], [-10], [3], [-31], [20], [12], [30], [8], [-18], [-4], [0], [39], [-22], [9], [41], [-36], [33], [12], [44], [-25], [-37], [24], [18], [39], [14], [2], [36], [36], [12], [-37], [14], [-34], [-42], [20], [-4], [-24], [-31], [-45], [36], [-12], [-13], [-1], [-3], [36], [-10], [48], [-25], [-6], [2], [14], [-34], [-6], [-4], [-13], [-6], [-22], [-15], [2], [-30], [21], [-28], [9], [-7], [12], [29], [-30], [-19], [9], [5], [0], [32], [12], [35], [18], [-37], [-48], [-4], [18], [-10], [-18], [24], [-6], [-22], [-36], [-9], [-42], [-52], [-1], [14], [-36], [6], [-46], [48], [-61], [-37], [-42], [8], [-15], [32], [38], [-4], [42], [5], [-18], [-24], [15], [-16], [29], [-52], [0], [62], [60], [11], [15], [-12], [-36], [-40], [26], [-36], [-3], [6], [35], [-40], [-46], [12], [6], [-25], [-39], [9], [32], [-22], [24], [-40], [-3], [-27], [-19], [-22], [42], [-57], [21], [2], [-25], [-6], [38], [12], [11], [-24], [-24], [-4], [0], [-70], [56], [-43], [-72], [-28], [18], [5], [66], [18], [24], [-60], [-1], [17], [-49], [-72], [-75], [-70], [-30], [38], [33], [-67], [-72], [6], [-4], [-66], [-48], [-58], [62], [51], [-34], [2], [-48], [-4], [-10], [-9], [-28], [27], [30], [-58], [66], [59], [47], [-64], [32], [41], [29], [-21], [26], [35], [42], [-6], [50], [6], [-28], [-52], [-30], [2], [-42], [-10], [9], [-30], [-18], [-18], [27], [32], [54], [-1], [-33], [0], [80], [-22], [18], [32], [30], [-31], [56], [60], [50], [66], [-79], [-48], [57], [-18], [-52], [45], [-22], [-45], [15], [-70], [-81], [89], [-73], [12], [35], [24], [-16], [-64], [-40], [9], [66], [-16], [-48], [74], [-30], [-19], [-24], [-22], [-30], [-34], [56], [-76], [6], [0], [32], [-84], [-30], [56], [-22], [0], [-54], [-34], [65], [-60], [74], [2], [72], [-39], [78], [-72], [-72], [-46], [42], [-51], [-36], [38], [62], [-48], [2], [50], [72], [-64], [54], [39], [-22], [38], [45], [42], [-97], [27], [59], [54], [-9], [38], [18], [68], [-12], [-97], [86], [20], [57], [-19], [66], [-48], [23], [-96], [-73], [-22], [-72], [-22], [-75], [-40], [-57], [-28], [18], [18], [47], [-61], [84], [80], [36], [-67], [63], [78], [-43], [-28], [81], [-12], [65], [90], [0], [18], [77], [-18], [57], [47], [78], [-51], [18], [-94], [-108]];
    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_u();".
// 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_u(: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_u();".
// 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_u( : 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![3, 1]>,<13,R![7, 1]>,<17,R![-3, 1]>],Snew);
    return Vf;
end function;