
// Make newform 9016.2.a.a in Magma, downloaded from the LMFDB on 10 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_9016_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_9016_2_a_a();".
// 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_9016_2_a_a();".
// 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_9016_a();"
function MakeCharacter_9016_a()
    N := 9016;
    order := 1;
    char_gens := [2255, 4509, 1473, 1569];
    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_9016_a_Hecke(Kf)
    return MakeCharacter_9016_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]];
    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], [-3], [-2], [0], [2], [1], [0], [-2], [-1], [-3], [1], [-2], [1], [8], [5], [-6], [0], [-6], [-10], [7], [-13], [-2], [4], [12], [12], [-2], [4], [6], [-16], [8], [13], [1], [12], [-1], [-18], [15], [24], [21], [-16], [-6], [-7], [26], [2], [25], [-3], [-6], [12], [24], [-28], [-4], [11], [-1], [-10], [12], [1], [-6], [-25], [24], [1], [-24], [2], [0], [8], [-23], [-16], [-30], [-19], [-28], [-12], [-9], [-1], [18], [-4], [-14], [-16], [-12], [16], [-31], [6], [35], [-32], [-8], [-8], [2], [-9], [27], [-2], [4], [-33], [-16], [14], [12], [5], [-39], [35], [2], [-25], [-12], [-28], [-43], [21], [-4], [28], [14], [40], [23], [-27], [-22], [24], [-5], [28], [24], [34], [-40], [-36], [-12], [38], [-43], [31], [12], [-14], [19], [-42], [9], [-28], [0], [36], [-8], [-10], [-8], [25], [20], [16], [-38], [3], [22], [-44], [4], [-30], [26], [-43], [26], [15], [-40], [-2], [-12], [10], [-27], [13], [-35], [6], [0], [28], [13], [-34], [6], [-4], [41], [26], [-12], [-53], [6], [-7], [42], [38], [-42], [32], [-46], [-46], [9], [-6], [23], [-60], [-16], [-25], [-60], [-23], [55], [-6], [-20], [-11], [12], [-31], [6], [14], [0], [30], [50], [13], [64], [-35], [-44], [-2], [30], [-18], [-12], [-10], [24], [-8], [-21], [-20], [16], [27], [-6], [-20], [-37], [18], [23], [15], [-21], [-13], [-51], [8], [18], [-48], [-8], [15], [51], [-42], [43], [11], [2], [-59], [-48], [-7], [51], [-18], [-67], [26], [4], [50], [-48], [26], [10], [-52], [-44], [-32], [-46], [15], [-65], [-16], [-23], [-23], [-61], [42], [-15], [37], [-22], [-50], [-6], [-32], [6], [58], [-44], [-66], [-63], [22], [-2], [55], [-57], [52], [-36], [38], [64], [26], [14], [-58], [52], [-30], [53], [-1], [18], [64], [-80], [15], [-11], [-28], [10], [46], [-24], [-9], [-38], [-70], [10], [-24], [8], [2], [-2], [-56], [2], [42], [-48], [-62], [30], [61], [-34], [8], [-39], [43], [31], [60], [10], [-16], [21], [-76], [-17], [76], [44], [-54], [50], [-15], [-26], [-52], [-59], [18], [32], [-1], [-57], [8], [-25], [16], [-68], [-55], [-8], [34], [56], [74], [-53], [30], [20], [17], [-53], [23], [43], [10], [59], [6], [48], [65], [28], [59], [-24], [90], [34], [-38], [-92], [17], [36], [-26], [-22], [81], [-3], [-50], [-6], [-76], [38], [8], [90], [45], [-92], [-78], [-53], [13], [14], [11], [33], [21], [-86], [-30], [-51], [76], [-41], [-64], [88], [-1], [99], [42], [-54], [-12], [-7], [-78], [-90], [36], [39], [-48], [39], [-47], [42], [82], [-28], [80], [58], [-33], [28], [-92], [-78], [52], [-40], [16], [86], [-31], [-3], [36], [-64], [-11], [-19], [-42], [-51], [32], [-60], [-71], [-9], [54], [-6], [74], [43], [-56], [-28], [0], [-12], [-84], [-40], [-48], [-100], [51], [-35], [62], [-90], [-51], [-99]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9016_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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