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