
// Make newform 6480.2.a.k in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6480_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_6480_2_a_k();".
// 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_6480_2_a_k();".
// 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_6480_a();"
function MakeCharacter_6480_a()
    N := 6480;
    order := 1;
    char_gens := [2431, 1621, 6401, 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_6480_a_Hecke(Kf)
    return MakeCharacter_6480_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], [3], [2], [-2], [4], [8], [-3], [-1], [0], [-4], [5], [8], [-7], [-2], [14], [7], [3], [-2], [4], [6], [-9], [-15], [2], [-18], [-8], [-3], [5], [-8], [5], [6], [12], [16], [17], [2], [14], [4], [9], [0], [2], [-7], [-8], [-10], [-12], [-4], [22], [19], [4], [15], [24], [8], [-11], [0], [-6], [16], [25], [8], [12], [-15], [-21], [12], [-7], [0], [14], [-34], [6], [-8], [-4], [-5], [24], [-24], [-24], [10], [26], [-36], [33], [34], [-18], [14], [-26], [34], [30], [28], [-28], [-15], [-26], [-20], [9], [36], [-20], [18], [16], [-20], [32], [7], [43], [11], [29], [-39], [29], [-30], [21], [-6], [-32], [-10], [33], [20], [10], [2], [41], [-44], [-36], [4], [-16], [33], [9], [17], [-4], [8], [-14], [6], [-42], [-12], [14], [23], [-41], [-6], [-23], [-34], [2], [29], [-10], [-26], [15], [5], [-24], [-28], [26], [26], [42], [15], [-53], [-37], [-3], [40], [-54], [10], [-22], [-17], [-18], [-35], [-23], [-36], [51], [50], [-10], [14], [0], [-7], [-57], [-26], [-41], [36], [-38], [-3], [-26], [-18], [-19], [8], [-12], [-23], [18], [12], [-28], [-30], [36], [54], [49], [49], [-8], [-44], [-8], [-22], [3], [31], [12], [11], [-25], [38], [-50], [29], [-24], [7], [-33], [14], [-59], [36], [-12], [1], [18], [-30], [-48], [-11], [0], [-66], [-62], [11], [-15], [28], [-46], [-58], [15], [-28], [-42], [-7], [-25], [-18], [-59], [-36], [31], [-26], [45], [-31], [-5], [-22], [0], [58], [-7], [-36], [18], [-46], [22], [25], [-23], [-45], [15], [-46], [36], [-42], [51], [-6], [-49], [3], [12], [56], [7], [-28], [-32], [5], [76], [-29], [24], [51], [80], [60], [-2], [-17], [0], [-64], [-61], [-41], [-70], [42], [-42], [14], [58], [23], [1], [-38], [50], [-19], [-4], [18], [12], [-41], [39], [-30], [45], [-42], [-56], [38], [40], [-75], [-15], [60], [82], [48], [4], [63], [26], [52], [24], [-12], [-64], [-63], [-14], [-12], [-6], [-44], [-48], [-32], [44], [36], [-10], [-82], [9], [-37], [60], [24], [0], [75], [-90], [31], [40], [-59], [54], [-18], [-54], [6], [4], [38], [-14], [-16], [54], [13], [4], [-49], [69], [-26], [15], [4], [-44], [4], [58], [7], [65], [-76], [5], [-48], [-44], [20], [33], [-6], [64], [12], [-10], [-43], [-30], [20], [6], [54], [-1], [-31], [3], [-84], [-8], [-60], [-60], [-13], [-8], [7], [-96], [-22], [52], [64], [30], [51], [2], [-12], [-48], [-31], [-99], [-84], [52], [14], [12], [-52], [27], [100], [17], [6], [5], [26], [72], [-88], [-68], [-28], [-45], [51], [27], [-14], [86], [28], [-22], [74], [94], [-54], [50], [18], [1], [66], [32], [92], [-31], [50], [68], [-1], [-21], [42], [-84], [-48], [5], [48], [-22], [13], [46], [32], [0], [55], [89], [-8], [-48], [86], [56], [28], [-71], [30], [-74], [74]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6480_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_6480_2_a_k();".
// 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_6480_2_a_k(:prec:=1)
    chi := MakeCharacter_6480_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_6480_2_a_k();".
// 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_6480_2_a_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6480_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-3, 1]>,<11,R![-2, 1]>,<13,R![2, 1]>,<17,R![-4, 1]>,<19,R![-8, 1]>,<23,R![3, 1]>],Snew);
    return Vf;
end function;