
// Make newform 2541.2.a.be 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_be();".
// 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_be();".
// 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
        poly := [-3, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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], [-1, 0], [-2, -1], [-1, 0], [0, 0], [1, -1], [-2, 1], [3, 3], [5, 1], [3, 1], [4, -2], [-6, 2], [4, 0], [-3, -2], [-4, 3], [6, 2], [2, 7], [3, -3], [-7, 0], [-3, -7], [-5, 1], [-4, -2], [6, 1], [-6, 5], [5, 1], [8, -1], [-1, -3], [3, -9], [7, -6], [12, 2], [-7, -2], [0, -3], [5, -5], [13, 3], [-1, 1], [-1, -6], [-10, -2], [4, -2], [6, -11], [0, -3], [6, 6], [-8, 0], [11, -3], [-5, 0], [16, 2], [-15, 3], [3, 12], [16, 0], [6, -5], [0, -4], [15, -3], [18, -4], [7, -5], [-8, -4], [-8, -7], [11, 3], [10, -8], [14, 6], [-5, 12], [0, 16], [-2, 14], [-18, -1], [11, -9], [0, -11], [-10, -6], [7, -13], [-5, -18], [-28, 0], [7, -11], [12, 6], [-18, -4], [13, -7], [-11, 1], [-17, 4], [7, 2], [-22, 7], [6, -6], [4, 10], [-16, 0], [-15, 5], [8, -1], [25, -8], [6, -10], [1, -19], [-14, -8], [-11, -3], [8, 14], [-27, 4], [-12, 9], [28, -2], [-24, 6], [-4, -21], [11, -18], [19, 11], [23, -10], [4, 13], [-12, 3], [6, 15], [11, -9], [31, -6], [2, 0], [10, -4], [-8, 1], [27, 1], [8, 4], [15, 17], [4, 7], [20, -9], [-8, -18], [-1, -19], [11, 9], [-25, -6], [-36, 2], [2, 0], [13, 8], [3, -19], [-28, 8], [-22, 3], [-5, -15], [19, 13], [-32, 8], [-19, -6], [12, -9], [-4, 14], [2, 6], [5, -5], [-19, -6], [-6, 22], [15, -13], [15, 17], [-16, -10], [-5, 7], [1, -6], [-1, 4], [-12, -19], [-4, 20], [0, 1], [16, 18], [14, 19], [-9, -17], [13, -19], [10, -8], [-12, 10], [-7, 13], [37, 3], [-2, -29], [-4, 0], [14, -7], [-30, 0], [6, 4], [-6, -18], [26, -8], [7, -12], [2, 7], [-18, -12], [6, -12], [4, 6], [6, -5], [-19, 17], [-24, -6], [42, -4], [-32, 10], [19, -6], [-34, -7], [45, -3], [-12, -12], [28, 6], [-16, 26], [-4, -24], [-54, -4], [-22, 6], [-23, -5], [23, -23], [-10, -4], [44, 8], [-10, 25], [23, -4], [26, 10], [26, -4], [-8, -24], [-43, -6], [8, 10], [3, -14], [-30, -20], [-24, 2], [24, -8], [-9, -4], [-6, -4], [5, 28], [-4, 15], [29, 11], [34, -4], [3, -4], [-30, 8], [54, -4], [-18, -15], [-16, -20], [19, -20], [-18, 20], [-34, 6], [-18, 6], [-45, -1], [-6, -24], [25, -5], [8, -11], [10, -4], [39, -1], [57, 7], [-3, 11], [31, -11], [39, 8], [18, -16], [30, 0], [16, 1], [48, 1], [-46, -8], [-7, 4], [36, -1], [-42, 10], [-12, 2], [-53, -10], [30, 16], [57, 1], [-13, 10], [-30, -1], [-10, -8], [-12, -2], [-45, -11], [-8, -30], [-14, 28], [25, 28], [22, 6], [31, 4], [-7, 27], [-50, -10], [-14, 33], [15, 7], [9, -15], [4, 28], [-62, 3], [-11, -1], [23, 1], [-36, -6], [-6, -18], [70, 0], [18, -9], [26, 24], [38, 2], [39, 12], [-38, -10], [19, -10], [-34, 17], [33, -19], [-25, -11], [0, 14], [31, 3], [49, 10], [-3, -23], [54, -11], [-41, -21], [-17, -4], [39, -23], [-28, 8], [-39, 3], [12, 6], [-11, -19], [39, 13], [0, 17], [11, 18], [-30, -14], [-34, 24], [-36, 6], [10, -2], [-13, 18], [8, 24], [-13, 9], [3, 23], [1, -36], [-14, 6], [0, -9], [4, 18], [5, 16], [-40, -22], [-49, -5], [-8, 34], [-45, 1], [-45, -4], [-9, 3], [-35, 5], [-50, 9], [14, -6], [-16, 0], [12, -6], [-12, -25], [-39, 16], [20, -13], [-53, -7], [-2, -13], [6, 34], [56, -4], [-12, -16], [-30, 26], [37, -4], [31, -19], [20, -24], [31, -30], [-6, 0], [39, 3], [-30, -2], [-9, -2], [-18, 19], [1, 21], [-27, 1], [26, -4], [-12, 20], [-13, 39], [-46, -14], [13, 1], [-32, -6], [8, -2], [10, -32], [-65, -8], [-16, 34], [-67, -4], [-50, -2], [2, 38], [-9, 22], [56, -4], [-42, -20], [-26, 32], [-44, 8], [21, 1], [-31, 1], [-68, 4], [54, -8], [42, -15], [-2, 20], [76, -5], [13, -29], [-49, -1], [17, -26], [-21, -29], [-12, -15], [-7, -22], [-31, 31], [1, -27], [1, 13], [-49, -8], [0, 38], [-42, 3], [8, 16], [57, -6], [-27, -23], [-15, -17], [-8, -44], [30, 9], [6, 17], [68, -17], [-28, 24], [-22, -4], [-40, -24], [-12, 26], [42, 12], [52, 18], [24, 10], [-29, 24], [-36, -32], [5, 10], [12, 20], [-12, -24], [3, 41], [-35, 19], [40, -12], [52, 14], [1, -14], [20, 15], [-10, 16], [-28, 8], [-48, -1], [-48, -30], [10, -28], [33, 9], [-13, 42], [-22, -28], [-17, -3], [90, 3], [-9, 36], [47, 17], [47, -2], [-40, -24], [44, 12], [-20, -25], [14, -4], [76, -12], [-9, -41], [-50, 8], [-4, -30], [-24, 1], [28, -10], [35, -5], [-40, -6], [16, 24], [42, 12], [-36, -13], [4, 4], [9, 1], [27, 6], [28, 22], [5, -13], [-40, -30], [-3, 29], [34, -18], [55, -23], [23, 48], [11, 42], [-30, 9], [14, -12], [39, 3], [-70, -19], [-42, 26], [47, -5], [-56, 2], [27, 25], [4, -30], [-74, -2], [54, -27], [-6, 36], [64, -12], [43, -29], [-8, -27]];
    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_be();".
// 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_be(:prec:=2)
    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_be();".
// 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_be( : 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, -2, 1]>,<5,R![1, 4, 1]>,<13,R![-2, -2, 1]>],Snew);
    return Vf;
end function;