
// Make newform 2541.2.a.g in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2541_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_2541_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_2541_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_2541_a();"
function MakeCharacter_2541_a()
    N := 2541;
    order := 1;
    char_gens := [848, 1816, 2059];
    v := [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_2541_a_Hecke(Kf)
    return MakeCharacter_2541_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 := [[1], [-1], [-3], [1], [0], [7], [3], [-2], [-4], [7], [-10], [1], [-5], [6], [-6], [-5], [-6], [10], [-8], [-10], [10], [-2], [-16], [3], [-19], [-2], [-16], [-14], [-7], [9], [8], [-4], [2], [-4], [-21], [2], [-14], [10], [12], [-10], [-10], [23], [22], [11], [-1], [-2], [8], [-24], [2], [-13], [-23], [12], [14], [-12], [-29], [24], [-23], [-8], [5], [18], [-22], [-27], [-12], [-10], [9], [18], [18], [-5], [26], [3], [-21], [12], [10], [6], [16], [16], [7], [-33], [-15], [5], [16], [-15], [-14], [1], [4], [4], [-27], [-33], [-15], [22], [8], [-26], [-38], [-36], [-32], [16], [-42], [-2], [20], [30], [2], [-6], [-18], [10], [-16], [37], [-42], [7], [10], [33], [4], [-7], [-3], [-42], [14], [9], [12], [0], [-18], [6], [35], [46], [33], [-4], [22], [-29], [-6], [30], [-14], [-1], [-30], [-6], [14], [-39], [15], [-15], [-18], [40], [2], [18], [38], [10], [28], [46], [-25], [30], [-37], [-26], [0], [-14], [45], [35], [6], [-8], [18], [12], [-46], [-21], [25], [17], [-54], [29], [14], [4], [-23], [58], [2], [43], [15], [22], [-12], [10], [60], [-9], [-4], [-21], [-60], [-17], [-54], [-37], [-52], [38], [-2], [3], [54], [-10], [5], [-20], [7], [-8], [-18], [58], [-6], [-15], [6], [59], [-37], [34], [11], [6], [-66], [10], [55], [45], [-10], [-26], [52], [60], [-47], [26], [23], [49], [-2], [-20], [28], [-35], [-26], [46], [-4], [-29], [25], [20], [33], [-20], [-42], [-51], [-9], [-62], [-24], [-38], [-3], [4], [10], [45], [18], [46], [-50], [51], [12], [6], [-48], [-8], [66], [41], [6], [-18], [-68], [-54], [-56], [50], [-14], [27], [-32], [-31], [21], [-30], [33], [34], [-43], [34], [-16], [-22], [67], [26], [55], [-6], [-54], [30], [-24], [-70], [-41], [-12], [-39], [60], [50], [38], [52], [37], [22], [-22], [-42], [6], [60], [-9], [-4], [-58], [-66], [50], [40], [-45], [-21], [-50], [-7], [-12], [14], [46], [2], [-39], [-70], [4], [-26], [-81], [-32], [14], [10], [-50], [86], [-45], [30], [77], [30], [75], [-55], [-72], [34], [-47], [42], [12], [-86], [5], [-20], [15], [21], [88], [81], [-30], [50], [-56], [84], [-65], [41], [34], [14], [26], [28], [-30], [9], [22], [-67], [-10], [1], [-22], [-15], [-44], [26], [14], [3], [12], [-46], [-67], [-10], [22], [47], [-66], [1], [39], [96], [42], [-27], [88], [22], [90], [-68], [20], [20], [-77], [-43], [76], [-17], [-30], [76], [26], [51], [-62], [1], [32], [82], [-83], [75], [-54], [30], [-51], [-40], [-30], [2], [68], [46], [-17], [64], [78], [-26], [10], [-52], [-56], [-68], [94], [-6], [-69], [-8], [7], [11], [-75], [64], [86], [6], [18], [-43], [-11], [0], [54], [-83], [47], [64], [6], [-5], [13], [-40], [64], [-26], [30], [55], [10], [54], [30], [49], [-99], [-38], [102], [-4], [58]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2541_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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