
// Make newform 3344.2.a.e in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3344_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_3344_2_a_e();".
// 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_3344_2_a_e();".
// 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_3344_a();"
function MakeCharacter_3344_a()
    N := 3344;
    order := 1;
    char_gens := [2927, 837, 2433, 705];
    v := [1, 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_3344_a_Hecke(Kf)
    return MakeCharacter_3344_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 := [[0], [-1], [2], [3], [1], [-1], [-7], [1], [-3], [-1], [-2], [-10], [-6], [-8], [0], [1], [-5], [-8], [3], [-6], [3], [4], [8], [12], [4], [6], [-4], [11], [-3], [8], [12], [-6], [15], [-14], [-10], [4], [-10], [-10], [2], [-14], [12], [-22], [-3], [-12], [-12], [-15], [-15], [26], [-7], [16], [-10], [15], [12], [10], [-6], [-4], [-2], [-1], [12], [8], [14], [17], [-16], [-19], [-19], [3], [-19], [-24], [-18], [14], [-7], [17], [-16], [13], [27], [2], [-14], [-26], [-6], [10], [18], [41], [30], [40], [-22], [-34], [-24], [19], [-24], [-8], [16], [28], [-4], [-34], [8], [27], [34], [-20], [-5], [-8], [8], [-34], [40], [20], [-2], [47], [44], [-30], [4], [-28], [-18], [44], [-6], [-40], [8], [24], [-2], [29], [-24], [-45], [-13], [-24], [13], [-8], [-30], [0], [-30], [49], [17], [-50], [-28], [36], [46], [2], [5], [-25], [31], [-23], [-27], [27], [-33], [-44], [35], [37], [39], [-12], [-42], [22], [46], [30], [-35], [-14], [16], [-8], [5], [-34], [5], [-3], [-61], [-49], [52], [52], [-24], [-20], [-32], [46], [-32], [-14], [20], [-36], [55], [17], [-8], [61], [42], [14], [-44], [12], [38], [-16], [7], [23], [10], [-12], [61], [-32], [41], [-20], [-34], [-61], [64], [-16], [-52], [51], [12], [-12], [-53], [-8], [53], [-39], [6], [4], [49], [-22], [34], [-2], [-35], [-27], [15], [55], [31], [32], [-24], [-7], [-46], [-48], [-33], [-20], [-18], [-62], [-6], [-52], [-6], [29], [39], [-42], [0], [8], [-62], [-28], [18], [41], [48], [-24], [16], [69], [30], [46], [-32], [-18], [36], [4], [31], [-27], [-72], [72], [8], [-52], [-20], [75], [-16], [-67], [-39], [2], [-18], [-38], [16], [20], [-6], [-22], [-62], [36], [40], [-21], [42], [-41], [63], [71], [-53], [36], [9], [-43], [-55], [5], [-44], [51], [20], [19], [-34], [-48], [-66], [-39], [-61], [63], [-8], [16], [-51], [22], [-25], [68], [4], [74], [-12], [20], [-50], [46], [4], [68], [-73], [-72], [-14], [5], [-25], [31], [72], [-62], [3], [35], [-16], [-60], [36], [-52], [-60], [-68], [48], [54], [8], [-70], [-13], [-50], [-13], [3], [-37], [34], [49], [52], [-37], [-19], [-68], [-6], [-76], [-77], [-53], [70], [-30], [18], [-29], [-24], [-41], [-35], [-65], [43], [11], [44], [-75], [20], [52], [57], [12], [-24], [-92], [78], [-90], [6], [70], [12], [37], [-12], [-79], [30], [38], [42], [18], [53], [-14], [22], [-14], [66], [-22], [12], [-63], [51], [-53], [-88], [34], [-3], [-80], [34], [23], [24], [-54], [54], [17], [30], [41], [-66], [-17], [40], [-38], [78], [-14], [-9], [-90], [88], [90], [94], [-44], [44], [68], [58], [46], [-62], [-36], [30], [-74], [19], [-34], [24], [1], [-76], [-26], [-12], [35], [-20], [-13], [72], [18], [58], [-37], [54], [34], [-79], [51], [-33], [94], [-21], [3], [-95], [-2], [-21]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3344_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3344_2_a_e();".
// 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_3344_2_a_e(:prec:=1)
    chi := MakeCharacter_3344_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_3344_2_a_e();".
// 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_3344_2_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3344_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 1]>,<5,R![-2, 1]>,<7,R![-3, 1]>],Snew);
    return Vf;
end function;