
// Make newform 3366.2.a.k in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3366_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_3366_2_a_k();".
// 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_3366_2_a_k();".
// 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_3366_a();"
function MakeCharacter_3366_a()
    N := 3366;
    order := 1;
    char_gens := [749, 1531, 2179];
    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_3366_a_Hecke(Kf)
    return MakeCharacter_3366_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], [2], [0], [1], [-2], [-1], [-4], [0], [2], [-8], [-10], [6], [4], [-8], [-6], [12], [-10], [4], [-16], [2], [-8], [12], [-10], [2], [-6], [-8], [12], [6], [6], [8], [-12], [6], [20], [-22], [16], [-2], [-4], [0], [18], [4], [-10], [-8], [-6], [26], [16], [-4], [0], [-12], [-26], [6], [-16], [10], [-20], [-2], [-24], [26], [24], [-2], [6], [-28], [-6], [-4], [0], [-6], [-22], [12], [-22], [12], [-18], [-2], [8], [-8], [-26], [-12], [24], [-6], [14], [-10], [-38], [-28], [6], [-24], [-14], [-16], [-4], [-10], [-22], [-14], [16], [36], [24], [32], [20], [44], [16], [34], [30], [-28], [-26], [28], [-46], [28], [6], [-44], [34], [28], [14], [-16], [18], [-8], [-26], [-34], [20], [-8], [6], [12], [16], [10], [-4], [22], [-6], [-6], [44], [-20], [34], [38], [-8], [40], [14], [28], [16], [-40], [22], [6], [-14], [-6], [-4], [18], [22], [20], [10], [-48], [28], [30], [-48], [-34], [6], [-4], [24], [6], [-42], [-28], [48], [36], [-8], [-16], [-10], [58], [18], [4], [6], [16], [-4], [-34], [-16], [40], [14], [26], [-46], [4], [-2], [16], [26], [16], [-34], [-12], [-30], [0], [-18], [0], [-28], [-10], [54], [24], [-38], [-10], [36], [50], [-8], [-30], [-52], [-28], [-30], [-44], [46], [10], [-2], [30], [8], [-30], [40], [22], [34], [28], [-30], [-48], [44], [22], [-36], [-22], [-54], [64], [12], [0], [-54], [-8], [46], [-32], [18], [-58], [0], [46], [24], [60], [-58], [-66], [24], [-32], [-52], [-18], [-20], [8], [54], [-52], [48], [-14], [58], [60], [0], [-28], [36], [-40], [14], [-34], [0], [56], [20], [-76], [-48], [30], [46], [-48], [-70], [18], [-20], [6], [4], [-30], [-70], [-8], [12], [14], [-74], [-74], [52], [66], [-26], [-44], [-70], [14], [-44], [-38], [-8], [2], [-40], [-4], [46], [-70], [60], [0], [-80], [-48], [-10], [36], [40], [82], [42], [56], [-34], [-38], [36], [70], [-36], [-42], [50], [64], [26], [-4], [-84], [-70], [18], [24], [36], [-60], [18], [12], [-2], [64], [6], [24], [-30], [46], [36], [56], [26], [-60], [-40], [-62], [-82], [44], [-86], [-62], [16], [38], [82], [-84], [20], [24], [26], [-74], [-6], [-56], [60], [-28], [-20], [-34], [14], [26], [-40], [54], [-42], [-54], [-40], [-14], [-60], [-26], [28], [-40], [66], [-4], [-6], [-78], [8], [22], [38], [72], [44], [-18], [-48], [62], [6], [88], [12], [-28], [10], [-30], [-48], [34], [4], [20], [24], [74], [48], [78], [-60], [24], [2], [14], [58], [-22], [-58], [96], [-66], [-20], [-96], [32], [22], [-60], [24], [2], [34], [28], [-28], [48], [-14], [56], [-26], [-28], [42], [22], [14], [88], [14], [-6], [48], [46], [-18], [68], [-12], [-22], [42], [-20], [-52], [106], [-70], [-40], [80], [-10], [-24], [-46], [14], [72], [36], [-86], [-78], [-12], [86], [28], [-64]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3366_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3366_2_a_k();".
// 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_3366_2_a_k(:prec:=1)
    chi := MakeCharacter_3366_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_3366_2_a_k();".
// 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_3366_2_a_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3366_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-2, 1]>,<7,R![0, 1]>,<13,R![2, 1]>,<19,R![4, 1]>,<23,R![0, 1]>],Snew);
    return Vf;
end function;