
// Make newform 3366.2.a.o in Magma, downloaded from the LMFDB on 19 May 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_o();".
// 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_o();".
// 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], [0], [4], [1], [4], [1], [-4], [6], [10], [-8], [-10], [2], [8], [12], [-2], [-10], [-6], [-12], [-2], [-4], [4], [0], [-12], [10], [10], [-16], [12], [2], [14], [18], [-12], [-12], [-2], [-14], [22], [18], [-8], [8], [10], [-26], [6], [8], [4], [18], [-8], [-2], [28], [-4], [-6], [-10], [0], [-24], [-2], [-8], [0], [12], [-26], [-6], [-2], [18], [10], [-8], [-30], [-14], [-20], [-28], [20], [-4], [8], [-24], [32], [8], [16], [-20], [20], [30], [-14], [-26], [-22], [0], [-10], [-36], [-38], [8], [-18], [-26], [18], [-30], [28], [6], [-32], [8], [20], [-28], [36], [-6], [-10], [28], [-18], [-2], [14], [44], [-6], [-6], [-2], [-42], [14], [28], [-40], [-4], [-24], [-38], [0], [-16], [26], [-36], [8], [-20], [0], [-30], [-12], [18], [-48], [44], [26], [-34], [-6], [44], [-44], [-24], [-24], [48], [-2], [-14], [-14], [42], [38], [-22], [42], [-38], [-6], [48], [12], [-18], [-6], [-26], [26], [36], [36], [14], [-6], [-4], [-44], [36], [-26], [34], [42], [-42], [-54], [-20], [34], [26], [6], [12], [14], [-24], [-46], [-20], [16], [16], [-22], [12], [26], [52], [-18], [-10], [-56], [42], [-4], [44], [12], [-62], [-22], [12], [2], [-18], [28], [-20], [64], [-54], [44], [-20], [-42], [36], [18], [8], [-46], [22], [24], [62], [-44], [-38], [6], [38], [66], [0], [-24], [-18], [-4], [12], [22], [16], [-22], [16], [10], [-22], [-50], [26], [42], [-60], [0], [60], [-8], [-32], [8], [-18], [-38], [-22], [12], [-58], [-10], [-42], [10], [-36], [-48], [-14], [54], [-16], [-28], [4], [8], [40], [-14], [-18], [48], [-32], [40], [40], [24], [-4], [78], [4], [54], [18], [60], [-10], [10], [36], [54], [-12], [-68], [30], [-26], [30], [-20], [42], [12], [30], [54], [30], [60], [62], [10], [82], [-28], [66], [-44], [-18], [-60], [0], [16], [-16], [56], [-26], [64], [58], [-30], [28], [-34], [8], [-36], [-18], [-44], [26], [6], [64], [62], [4], [-48], [-14], [-18], [40], [-24], [-32], [62], [-18], [-50], [84], [52], [4], [-8], [-30], [-36], [16], [-30], [70], [60], [-62], [66], [-66], [62], [-50], [68], [42], [26], [-24], [-20], [92], [-30], [-14], [68], [44], [-8], [-58], [32], [66], [42], [-50], [34], [18], [-72], [6], [20], [-50], [-20], [-22], [-36], [-52], [-48], [-4], [-66], [-62], [20], [8], [-54], [60], [-20], [54], [-24], [74], [-6], [-44], [4], [-4], [-26], [30], [6], [-16], [78], [-68], [-84], [6], [-18], [2], [4], [8], [10], [-54], [26], [0], [-56], [-8], [30], [74], [-38], [-52], [70], [-18], [-32], [18], [-24], [102], [68], [100], [-32], [70], [24], [32], [14], [-86], [-36], [-14], [-78], [-6], [-68], [-26], [-58], [36], [-84], [64], [-42], [-86], [82], [-98], [20], [-8], [-40], [-62], [-40], [-78], [86], [-90], [-36], [-70], [-102], [40], [106], [20], [28]];
    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_o();".
// 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_o(: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_o();".
// 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_o( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3366_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![0, 1]>,<7,R![-4, 1]>,<13,R![-4, 1]>,<19,R![4, 1]>,<23,R![-6, 1]>],Snew);
    return Vf;
end function;