
// Make newform 2576.2.a.l in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2576_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_2576_2_a_l();".
// 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_2576_2_a_l();".
// 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_2576_a();"
function MakeCharacter_2576_a()
    N := 2576;
    order := 1;
    char_gens := [2255, 645, 1473, 1569];
    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_2576_a_Hecke(Kf)
    return MakeCharacter_2576_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], [1], [0], [-1], [2], [-3], [0], [0], [1], [1], [5], [-8], [-7], [4], [-3], [-12], [-4], [-6], [12], [-13], [3], [-4], [-16], [4], [10], [-10], [-14], [8], [-4], [4], [-7], [-3], [10], [-1], [0], [11], [12], [9], [0], [26], [-3], [-18], [8], [-19], [9], [2], [-12], [-20], [20], [10], [-1], [15], [-18], [2], [5], [-18], [-5], [0], [-3], [22], [10], [-22], [20], [13], [14], [6], [29], [-2], [12], [-25], [7], [-24], [-24], [10], [14], [-14], [4], [-27], [-18], [-21], [-4], [-28], [30], [6], [7], [23], [30], [8], [15], [16], [42], [-18], [-31], [17], [19], [-16], [15], [-32], [10], [45], [17], [30], [-30], [-4], [-10], [19], [-7], [-6], [12], [-45], [8], [6], [12], [-10], [-16], [4], [-28], [21], [-5], [14], [40], [43], [-12], [-23], [-12], [-6], [16], [40], [-20], [-24], [9], [16], [-12], [-46], [-37], [-40], [26], [-8], [32], [42], [37], [6], [11], [12], [34], [-30], [-34], [-51], [29], [57], [22], [-58], [-12], [25], [28], [10], [-18], [-15], [4], [-18], [27], [-10], [41], [52], [-16], [-56], [-8], [34], [-32], [-15], [-36], [-41], [-40], [-6], [-53], [10], [13], [-37], [-28], [12], [17], [-36], [5], [-42], [-54], [0], [2], [-62], [-7], [48], [-31], [-52], [-20], [34], [-4], [-64], [44], [50], [18], [51], [-30], [-32], [-41], [-46], [30], [-45], [-44], [-21], [-25], [-65], [59], [-19], [-10], [-12], [24], [-22], [-65], [-9], [12], [3], [35], [12], [-3], [-34], [-7], [-29], [18], [-19], [-28], [20], [-34], [-58], [-10], [10], [2], [-14], [-8], [-6], [-73], [7], [-22], [-7], [-35], [-13], [26], [5], [-39], [-6], [-32], [66], [-14], [30], [-8], [-50], [66], [17], [-64], [-42], [-29], [43], [50], [-6], [54], [-24], [-62], [-62], [6], [-38], [44], [-47], [-49], [34], [-46], [10], [75], [13], [-20], [82], [-46], [0], [75], [80], [-48], [18], [20], [80], [20], [-30], [24], [50], [66], [-42], [-86], [66], [-31], [30], [-48], [21], [-9], [-37], [28], [-46], [58], [9], [-34], [-1], [52], [-62], [42], [-62], [-63], [-72], [80], [85], [82], [-4], [-73], [55], [24], [87], [50], [62], [-43], [52], [54], [8], [22], [-45], [-46], [64], [37], [55], [-77], [87], [6], [27], [-32], [-52], [-3], [-4], [-57], [12], [58], [10], [44], [-96], [45], [-12], [-26], [66], [-91], [-11], [-26], [10], [-40], [42], [-12], [-72], [53], [-8], [-68], [63], [-3], [18], [51], [-79], [41], [62], [-88], [21], [44], [-37], [-48], [10], [-73], [-73], [-60], [70], [78], [21], [72], [-6], [-80], [59], [66], [83], [45], [22], [4], [-2], [-52], [-78], [51], [48], [-24], [-46], [96], [58], [-26], [-10], [25], [-75], [-60], [-54], [-3], [-71], [10], [-83], [94], [-76], [-35], [27], [92], [42], [28], [35], [36], [68], [94], [78], [74], [46], [-72], [-84], [-77], [33], [-18], [-82], [45], [-59]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2576_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2576_2_a_l();".
// 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_2576_2_a_l(:prec:=1)
    chi := MakeCharacter_2576_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_2576_2_a_l();".
// 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_2576_2_a_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2576_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, 1]>,<5,R![0, 1]>,<11,R![-2, 1]>,<13,R![3, 1]>],Snew);
    return Vf;
end function;