
// Make newform 3300.2.a.n in Magma, downloaded from the LMFDB on 21 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_n();".
// 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_n();".
// 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], [1], [-2], [-5], [-2], [-6], [3], [-2], [-6], [3], [-8], [6], [8], [-11], [-3], [-4], [2], [4], [2], [-2], [-9], [0], [16], [-6], [-13], [-6], [8], [-18], [-8], [-4], [-6], [-9], [13], [11], [-16], [0], [-2], [-7], [-12], [19], [0], [9], [-9], [9], [8], [-11], [4], [6], [15], [-6], [24], [6], [-6], [16], [-3], [-28], [9], [-2], [-23], [6], [21], [-2], [20], [1], [-16], [-10], [-30], [8], [23], [15], [11], [-38], [2], [29], [-10], [-25], [20], [22], [30], [-19], [-3], [-6], [-24], [-10], [-18], [-12], [-34], [12], [23], [30], [21], [14], [28], [18], [37], [35], [-4], [-12], [-26], [-10], [-13], [17], [6], [-38], [-12], [37], [-8], [2], [36], [-15], [15], [2], [16], [-24], [16], [-8], [26], [-26], [18], [-26], [20], [-12], [-19], [14], [41], [-46], [-24], [50], [40], [-25], [-2], [-7], [-46], [-3], [48], [-4], [-5], [40], [13], [-6], [-2], [54], [-21], [-46], [52], [-12], [-13], [54], [25], [42], [4], [6], [3], [0], [-5], [-32], [10], [-42], [60], [-10], [26], [-16], [39], [26], [-9], [-6], [-46], [-13], [36], [41], [-11], [-30], [-61], [-60], [-37], [29], [25], [-12], [-25], [-52], [-48], [10], [-7], [35], [19], [40], [-21], [36], [-57], [4], [-48], [48], [30], [-46], [28], [22], [6], [-28], [-58], [-34], [-14], [36], [5], [54], [6], [-17], [-49], [-6], [-23], [-36], [-20], [58], [19], [-44], [-6], [-6], [-33], [11], [14], [0], [-12], [-35], [-26], [-40], [-51], [-36], [-61], [36], [-29], [30], [12], [38], [-34], [50], [-10], [58], [54], [-55], [31], [23], [36], [-66], [-61], [-76], [-43], [-68], [-34], [-56], [54], [33], [12], [-60], [21], [-16], [-32], [-38], [-65], [-62], [-10], [-9], [-34], [60], [-76], [12], [-44], [16], [-29], [16], [-10], [17], [-19], [-83], [-16], [29], [-59], [48], [46], [-24], [-32], [-83], [-15], [36], [47], [48], [-56], [-72], [-16], [-50], [42], [-44], [-77], [-42], [-9], [60], [6], [20], [46], [-44], [5], [-36], [-64], [-1], [46], [17], [24], [-10], [6], [-24], [56], [-71], [22], [23], [-24], [-62], [-33], [-12], [-76], [53], [88], [-47], [16], [-6], [5], [-53], [62], [74], [-1], [58], [61], [16], [-65], [-44], [-49], [86], [5], [64], [77], [24], [72], [61], [-36], [-62], [25], [-65], [22], [-54], [-29], [14], [-30], [-95], [85], [10], [82], [6], [48], [-92], [-41], [-78], [-68], [-66], [92], [61], [-2], [24], [61], [74], [27], [-6], [66], [-24], [-81], [-42], [62], [-13], [8], [29], [-32], [-44], [72], [48], [28], [-14], [47], [-10], [-20], [12], [-55], [-4], [-38], [29], [20], [-51], [-7], [24], [-13], [8], [22], [-88], [13], [42], [-90], [-8], [-37], [12], [-49], [0], [-25], [18], [-34], [-57], [-83], [98], [-18], [79], [24], [-74], [6], [-61], [78], [-16], [-54], [-42], [102], [68], [27], [-52]];
    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_n();".
// 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_n(: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_n();".
// 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_n( : 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![-1, 1]>,<17,R![2, 1]>],Snew);
    return Vf;
end function;