
// Make newform 2160.2.a.a in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2160_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_2160_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_2160_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_2160_a();"
function MakeCharacter_2160_a()
    N := 2160;
    order := 1;
    char_gens := [271, 1621, 2081, 1297];
    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_2160_a_Hecke(Kf)
    return MakeCharacter_2160_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], [0], [-1], [-2], [-3], [-1], [3], [-8], [3], [9], [7], [2], [12], [7], [-3], [-12], [12], [-10], [4], [0], [2], [1], [18], [0], [14], [3], [-14], [6], [-4], [15], [-2], [3], [6], [-14], [-15], [1], [17], [19], [12], [-6], [-12], [8], [18], [14], [-12], [-5], [10], [4], [12], [-16], [18], [18], [5], [-15], [15], [-12], [-21], [16], [-22], [-6], [16], [30], [-29], [12], [-4], [-24], [10], [-16], [-12], [26], [-3], [-24], [10], [29], [-8], [3], [-27], [35], [12], [-25], [3], [8], [0], [14], [-8], [0], [6], [-28], [-30], [22], [0], [0], [4], [-36], [4], [-21], [21], [0], [25], [-22], [19], [0], [18], [-24], [-26], [-16], [-6], [-21], [6], [29], [22], [-25], [21], [10], [-8], [42], [-41], [-12], [-48], [0], [14], [26], [36], [24], [10], [33], [-4], [18], [-44], [14], [16], [33], [13], [-7], [42], [-1], [-6], [31], [0], [0], [10], [30], [-44], [-42], [20], [-6], [-1], [30], [-50], [33], [53], [-18], [28], [-15], [-53], [-24], [-11], [0], [-22], [-3], [42], [57], [58], [33], [-33], [-45], [7], [17], [-7], [6], [63], [-58], [18], [20], [-17], [24], [40], [30], [4], [-34], [-32], [0], [-31], [39], [48], [18], [11], [-5], [-34], [30], [26], [-18], [4], [-21], [36], [-18], [14], [-37], [18], [-24], [15], [-44], [50], [62], [-45], [48], [19], [-54], [-30], [-20], [8], [63], [-8], [-6], [54], [-1], [-26], [-18], [-3], [30], [-52], [67], [0], [-56], [6], [38], [-21], [54], [-68], [-27], [26], [34], [-35], [-30], [-44], [-36], [-13], [36], [-48], [-12], [60], [52], [-14], [-34], [-18], [-60], [10], [-45], [-2], [-33], [5], [60], [-72], [26], [-18], [-3], [-52], [1], [66], [38], [76], [30], [14], [-49], [33], [64], [66], [-48], [31], [-24], [44], [-11], [-46], [-77], [2], [-56], [48], [68], [-19], [15], [21], [-41], [-21], [-64], [31], [6], [-76], [-18], [-17], [-30], [15], [24], [45], [-60], [26], [-66], [-32], [6], [9], [28], [-40], [-48], [-5], [42], [-50], [-58], [-78], [14], [-30], [29], [51], [-66], [-6], [-65], [-45], [23], [-21], [0], [-40], [-54], [-80], [8], [81], [-44], [-57], [65], [52], [28], [27], [-48], [-46], [-6], [13], [-6], [-56], [90], [20], [-6], [-37], [34], [5], [15], [66], [-65], [-60], [-57], [-34], [52], [60], [-78], [-8], [-52], [45], [46], [-40], [-51], [36], [-57], [-27], [-33], [-22], [-12], [-36], [-57], [-29], [20], [24], [-74], [59], [63], [-14], [-99], [18], [40], [53], [27], [-24], [-4], [-42], [26], [-15], [45], [16], [45], [-14], [33], [-80], [14], [7], [69], [-67], [54], [-12], [-5], [-96], [-4], [-8], [96], [10], [15], [68], [33], [-20], [-3], [-6], [16], [-67], [18], [76], [21], [14], [-6], [30], [-26], [26], [-30], [-6], [-68], [-78], [-9], [-63], [-58], [39], [57], [-70], [66], [-60], [60], [-2], [90]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2160_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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