
// Make newform 1080.2.a.g in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1080_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_1080_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_1080_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_1080_a();"
function MakeCharacter_1080_a()
    N := 1080;
    order := 1;
    char_gens := [271, 541, 1001, 217];
    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_1080_a_Hecke(Kf)
    return MakeCharacter_1080_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], [-4], [-2], [4], [-1], [-5], [-5], [-8], [7], [-6], [-6], [-2], [-8], [-9], [-4], [13], [-10], [6], [-6], [9], [17], [6], [-8], [-12], [4], [-12], [17], [-6], [-18], [-6], [-11], [12], [14], [-16], [22], [-2], [15], [-7], [-16], [-7], [26], [16], [25], [-4], [17], [2], [9], [19], [6], [-6], [-1], [-12], [-3], [12], [26], [15], [-22], [16], [14], [1], [10], [-16], [-28], [-33], [0], [-20], [20], [9], [-34], [-30], [-4], [-20], [13], [-21], [6], [-32], [-30], [-23], [6], [15], [20], [20], [-5], [-3], [0], [-8], [6], [6], [-31], [-26], [34], [-30], [-1], [27], [28], [-14], [-8], [22], [-10], [-22], [4], [-28], [-5], [-10], [-33], [-15], [-36], [-47], [30], [0], [27], [-28], [1], [48], [12], [-33], [-25], [0], [-18], [-36], [34], [9], [17], [-10], [-46], [34], [-4], [24], [9], [48], [-39], [38], [24], [-35], [-33], [38], [21], [18], [-28], [-2], [-50], [3], [34], [-28], [32], [-45], [-31], [39], [-14], [24], [-4], [-17], [-12], [-32], [-40], [-38], [28], [-38], [-11], [6], [-42], [26], [38], [27], [5], [42], [-25], [5], [44], [7], [-52], [-4], [5], [60], [55], [32], [-62], [-62], [2], [60], [-22], [-26], [-19], [-4], [-16], [-28], [-43], [40], [14], [-9], [53], [-40], [25], [-7], [33], [12], [26], [8], [66], [-8], [14], [-45], [0], [2], [-32], [-11], [60], [-4], [-44], [-70], [-16], [-12], [-44], [-15], [58], [8], [39], [43], [26], [-41], [-32], [-18], [21], [-5], [17], [-10], [-34], [-28], [-26], [-57], [32], [58], [-24], [-27], [21], [9], [10], [-30], [4], [-53], [-2], [-23], [14], [14], [52], [-4], [4], [29], [30], [34], [33], [29], [-61], [48], [-43], [12], [27], [-12], [22], [-1], [15], [-4], [-67], [28], [-8], [42], [42], [-38], [-37], [-2], [-18], [47], [50], [44], [7], [-31], [-55], [-78], [0], [75], [11], [31], [-68], [-32], [38], [42], [21], [24], [-56], [-81], [42], [42], [-38], [24], [-25], [6], [16], [24], [54], [-21], [-16], [65], [33], [-38], [12], [-29], [-78], [2], [1], [24], [84], [52], [-7], [-17], [64], [30], [64], [-14], [53], [-4], [0], [4], [-5], [78], [25], [-4], [8], [69], [26], [58], [-32], [-32], [-68], [12], [23], [-81], [-38], [-42], [-28], [15], [-68], [-7], [-14], [46], [67], [-8], [48], [57], [-20], [18], [16], [-28], [14], [7], [60], [-68], [-38], [-61], [-88], [-14], [44], [6], [-24], [4], [51], [-34], [26], [-6], [35], [-67], [6], [87], [-52], [-6], [-46], [-34], [-30], [2], [-64], [1], [72], [3], [-96], [-80], [-59], [36], [54], [-32], [-14], [-21], [18], [34], [-46], [26], [17], [-72], [-91], [24], [-6], [-78], [-68], [54], [12], [-45], [46], [-30], [64], [-24], [22], [-15], [-37], [-41], [-50], [-12], [94], [-80], [-63], [9], [24], [-2], [49], [-62], [90], [-63], [39], [-42], [-19], [-94]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1080_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1080_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_1080_2_a_g(:prec:=1)
    chi := MakeCharacter_1080_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_1080_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_1080_2_a_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1080_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![4, 1]>,<11,R![2, 1]>,<17,R![1, 1]>],Snew);
    return Vf;
end function;