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