
// Make newform 2166.2.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2166_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_2166_2_a_b();".
// 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_2166_2_a_b();".
// 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_2166_a();"
function MakeCharacter_2166_a()
    N := 2166;
    order := 1;
    char_gens := [1445, 1807];
    v := [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_2166_a_Hecke(Kf)
    return MakeCharacter_2166_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], [-1], [0], [1], [-2], [-3], [4], [0], [4], [0], [-3], [-5], [4], [-9], [10], [-4], [-14], [11], [3], [14], [-11], [1], [8], [-14], [2], [-10], [-3], [-10], [14], [10], [-8], [-6], [-6], [-19], [-22], [-20], [-21], [11], [6], [-24], [4], [-18], [-8], [-13], [-2], [5], [25], [9], [8], [-11], [18], [-12], [25], [-18], [-12], [18], [2], [-16], [-2], [-8], [-20], [14], [-12], [10], [6], [12], [15], [-19], [16], [-29], [-12], [-36], [23], [26], [-13], [14], [-16], [15], [18], [-30], [18], [10], [-10], [-9], [19], [24], [-36], [5], [6], [-9], [8], [6], [32], [30], [5], [16], [26], [34], [37], [-31], [11], [34], [-6], [24], [-7], [-14], [-2], [34], [-46], [27], [5], [2], [-42], [1], [17], [-14], [37], [48], [-42], [-14], [10], [-1], [18], [-36], [4], [-44], [-31], [40], [17], [-2], [-5], [-18], [13], [5], [-34], [27], [-18], [-47], [26], [0], [4], [48], [4], [36], [-31], [-12], [3], [42], [7], [-22], [-25], [36], [-9], [18], [52], [-60], [55], [12], [-25], [-32], [10], [-30], [51], [6], [-42], [-6], [-17], [-49], [-14], [-36], [-18], [-17], [-18], [61], [-59], [54], [3], [2], [9], [55], [-44], [-8], [-11], [44], [-46], [6], [50], [-5], [-26], [-22], [-15], [24], [-17], [42], [48], [-36], [22], [25], [-4], [54], [-6], [-32], [-63], [-11], [24], [-48], [56], [-24], [-36], [5], [47], [-22], [-9], [14], [4], [3], [20], [0], [-68], [-46], [5], [-29], [40], [-31], [-52], [23], [54], [26], [36], [-48], [-19], [43], [-16], [36], [24], [-50], [5], [72], [-20], [52], [-44], [28], [-35], [-45], [-62], [8], [-16], [66], [-31], [18], [73], [0], [0], [10], [2], [-12], [57], [17], [72], [-61], [-35], [-30], [45], [-53], [34], [71], [-40], [-12], [-75], [-24], [11], [0], [34], [-8], [41], [29], [-26], [-37], [-37], [-26], [24], [-28], [-36], [-21], [16], [60], [-19], [68], [-65], [62], [-42], [-4], [38], [30], [31], [18], [76], [-58], [10], [-25], [-2], [74], [27], [34], [-59], [-83], [58], [65], [-60], [-53], [-70], [-82], [30], [-59], [36], [10], [14], [6], [1], [14], [17], [67], [-60], [33], [88], [-11], [-11], [49], [-18], [-36], [-47], [88], [-48], [72], [-72], [60], [15], [-14], [-27], [-11], [34], [32], [18], [-96], [-22], [60], [-74], [44], [24], [4], [-47], [61], [-12], [24], [-38], [-30], [54], [-84], [-50], [-96], [-38], [84], [42], [-10], [-28], [30], [-18], [-19], [3], [-80], [17], [-56], [-14], [-69], [-3], [-56], [82], [-34], [76], [33], [-36], [-50], [-72], [-34], [-61], [-90], [47], [-90], [76], [-66], [-63], [78], [54], [77], [24], [-14], [-7], [-32], [29], [36], [-61], [-4], [88], [-22], [22], [47], [-3], [-24], [-4], [-26], [-1], [-44], [-12], [-49], [-42], [58], [0], [32], [62], [-52], [-72], [7], [70], [46], [31], [-42], [-30], [-22], [49], [-6]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2166_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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