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