
// Make newform 3300.2.a.i in Magma, downloaded from the LMFDB on 17 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_i();".
// 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_i();".
// 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], [5], [1], [4], [-5], [7], [9], [2], [4], [-7], [-7], [0], [-9], [2], [-7], [2], [2], [5], [-2], [-3], [-8], [6], [-5], [15], [-16], [-10], [14], [-18], [3], [0], [-18], [-12], [17], [-20], [-14], [12], [18], [23], [9], [-9], [17], [16], [23], [24], [-12], [16], [-26], [-1], [-23], [6], [8], [0], [-6], [26], [-4], [-7], [14], [-3], [29], [-3], [-12], [8], [-13], [12], [6], [-10], [20], [22], [16], [-24], [-18], [14], [-32], [-36], [-8], [-18], [-32], [-14], [-21], [1], [-12], [-2], [-7], [5], [10], [-26], [14], [-34], [-28], [4], [10], [14], [38], [-30], [30], [2], [-5], [-4], [-1], [-14], [-12], [-33], [-40], [25], [-19], [-6], [-33], [-20], [0], [-2], [-24], [-28], [-20], [10], [16], [-17], [4], [-12], [47], [-26], [-10], [39], [-42], [-15], [35], [-4], [52], [26], [1], [-32], [8], [6], [30], [22], [38], [23], [-16], [15], [-9], [50], [-6], [24], [-2], [44], [-58], [3], [-32], [24], [12], [12], [44], [12], [-2], [-19], [1], [-30], [-32], [-21], [-51], [-21], [24], [27], [24], [-41], [58], [2], [-10], [6], [-2], [-54], [10], [-46], [2], [-22], [55], [6], [43], [-46], [40], [36], [25], [-42], [12], [30], [28], [52], [50], [-66], [-10], [-56], [-38], [56], [-26], [0], [-2], [-30], [3], [-24], [-19], [-28], [-61], [-10], [-3], [-6], [-34], [-30], [55], [0], [32], [50], [20], [-21], [72], [-13], [-31], [-43], [-43], [54], [-26], [-56], [64], [40], [-12], [-34], [74], [-15], [49], [26], [-47], [-7], [-13], [67], [22], [-32], [-49], [-11], [47], [-63], [-25], [43], [-10], [11], [17], [-20], [54], [-23], [20], [-54], [16], [-3], [-48], [-46], [18], [32], [29], [-52], [-18], [-42], [-75], [70], [-2], [-36], [28], [-28], [34], [-36], [-21], [23], [31], [-58], [-34], [-3], [42], [-56], [-25], [10], [-48], [30], [4], [44], [-12], [-42], [28], [41], [47], [-1], [-40], [-39], [60], [-13], [29], [46], [-8], [-10], [62], [36], [-44], [-71], [22], [-63], [19], [84], [-4], [50], [69], [-59], [-59], [-68], [-14], [4], [-87], [-72], [36], [-20], [57], [-48], [47], [46], [-35], [2], [45], [-42], [18], [5], [-14], [-62], [-18], [19], [-38], [2], [17], [40], [-29], [-21], [66], [-30], [-67], [41], [-9], [52], [54], [56], [54], [-32], [43], [34], [66], [-50], [53], [-43], [-24], [1], [28], [43], [55], [-70], [55], [8], [-70], [-63], [17], [-50], [2], [53], [21], [-88], [66], [40], [-36], [90], [-61], [-11], [49], [4], [-28], [52], [-82], [-28], [12], [20], [61], [-26], [-92], [-25], [-70], [21], [-83], [0], [-9], [-50], [-35], [76], [-80], [16], [-35], [-40], [98], [-45], [-40], [46], [-40], [58], [49], [-65], [-2], [18], [-42], [-89], [50], [57], [-45], [-43], [-52], [-18], [-32], [38], [-80], [52], [12], [-22], [31], [-72], [-29], [0], [3], [57], [-16], [66]];
    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_i();".
// 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_i(: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_i();".
// 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_i( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3300_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-5, 1]>,<13,R![-4, 1]>,<17,R![5, 1]>],Snew);
    return Vf;
end function;