
// Make newform 3300.2.a.e in Magma, downloaded from the LMFDB on 08 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3300_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_3300_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_3300_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_3300_a();"
function MakeCharacter_3300_a()
    N := 3300;
    order := 1;
    char_gens := [1651, 2201, 2377, 1201];
    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_3300_a_Hecke(Kf)
    return MakeCharacter_3300_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], [0], [1], [-1], [4], [-3], [5], [-3], [-6], [8], [7], [9], [-8], [3], [-6], [3], [14], [-2], [-9], [-2], [-1], [-12], [18], [-11], [15], [4], [6], [2], [6], [7], [12], [18], [-4], [9], [-4], [22], [4], [6], [-15], [3], [23], [3], [16], [-15], [-16], [-4], [16], [-18], [-1], [-9], [6], [20], [24], [6], [-6], [24], [11], [-2], [-3], [1], [27], [28], [-24], [-11], [12], [-10], [-26], [36], [-10], [0], [-12], [22], [10], [-4], [-12], [12], [-14], [0], [-10], [-15], [17], [0], [34], [-13], [-39], [6], [10], [-18], [22], [-36], [12], [-38], [-30], [-34], [6], [18], [18], [-17], [20], [19], [-18], [-24], [15], [32], [-17], [9], [-42], [-27], [-28], [-32], [-2], [-36], [-40], [-40], [-18], [4], [-21], [24], [12], [-49], [46], [42], [51], [-10], [33], [11], [-36], [4], [-14], [11], [0], [-40], [10], [-42], [-22], [6], [43], [12], [-9], [5], [-30], [22], [-24], [38], [-36], [-14], [21], [32], [-48], [-32], [-36], [40], [-12], [46], [-9], [-37], [30], [-20], [3], [-15], [-3], [40], [-15], [-36], [51], [-46], [-26], [26], [6], [-6], [2], [6], [-26], [14], [30], [29], [30], [7], [-46], [-20], [48], [-65], [42], [-12], [30], [-8], [-20], [-46], [-30], [-14], [24], [38], [-36], [18], [12], [50], [46], [-3], [48], [21], [-28], [13], [26], [-9], [-66], [50], [66], [-9], [8], [4], [-66], [-32], [-9], [60], [-37], [-59], [21], [-39], [-54], [62], [56], [24], [-20], [72], [-10], [-54], [-45], [61], [-6], [31], [11], [41], [75], [-62], [-48], [-49], [-45], [45], [3], [-69], [-55], [34], [-37], [39], [-36], [-14], [-21], [-28], [-42], [4], [69], [-24], [-22], [78], [-60], [-19], [-44], [42], [-2], [25], [-18], [50], [-56], [24], [80], [-6], [72], [-41], [9], [-49], [34], [10], [47], [-26], [28], [51], [74], [-4], [78], [24], [-4], [12], [-58], [-20], [-69], [-47], [57], [44], [33], [-36], [15], [3], [66], [-68], [-42], [-34], [0], [36], [-35], [-86], [39], [65], [36], [-4], [-74], [9], [-43], [-81], [-20], [-6], [-84], [-39], [-44], [-72], [-52], [51], [-12], [-47], [78], [-1], [22], [-27], [22], [54], [29], [-10], [58], [78], [21], [14], [-18], [11], [-36], [-55], [-81], [26], [54], [5], [61], [-47], [36], [-18], [-64], [66], [48], [35], [10], [42], [-66], [-49], [-29], [60], [85], [20], [-75], [-3], [66], [57], [72], [-74], [-63], [-27], [-66], [-2], [19], [27], [16], [-34], [-96], [92], [66], [-69], [-49], [31], [-12], [-48], [-92], [-42], [76], [-12], [84], [-47], [42], [68], [-21], [-58], [-5], [25], [-72], [-25], [-66], [-33], [16], [0], [16], [23], [-36], [-58], [3], [32], [-42], [-8], [78], [-15], [-43], [-38], [-78], [34], [45], [70], [-33], [39], [23], [-32], [78], [-48], [-74], [-84], [-96], [-24], [-74], [27], [84], [-11], [72], [63], [33], [-28], [-42]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3300_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3300_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_3300_2_a_e(:prec:=1)
    chi := MakeCharacter_3300_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_3300_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_3300_2_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3300_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-1, 1]>,<13,R![-4, 1]>,<17,R![3, 1]>],Snew);
    return Vf;
end function;