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