
// Make newform 2541.2.a.n in Magma, downloaded from the LMFDB on 24 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_n();".
// 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_n();".
// 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 := [-1, -1, 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, 0], [-1, 0], [2, -3], [1, 0], [0, 0], [2, -2], [3, 3], [1, 1], [5, -5], [-2, 0], [4, -7], [-4, -5], [-6, -3], [2, -2], [2, 0], [-4, 10], [10, -2], [-4, 8], [8, 0], [-4, 8], [-2, -2], [-14, 0], [-10, 8], [-7, 5], [-10, 4], [6, -5], [1, -7], [13, -5], [-4, 9], [4, -2], [0, -2], [12, -6], [10, -2], [-13, 11], [-20, 0], [-2, 0], [0, 6], [14, -12], [-8, 10], [15, 3], [10, -1], [-6, -8], [6, -15], [20, -1], [4, 0], [-1, -1], [-2, 0], [3, 11], [2, 12], [18, 2], [4, -6], [13, -9], [-8, 8], [8, 10], [1, 1], [17, -9], [2, 16], [16, 5], [-25, 9], [14, -8], [28, -9], [2, 11], [0, -15], [-4, 2], [-26, 10], [8, -22], [-2, 20], [-32, 3], [-14, 1], [26, -2], [6, 4], [-14, 13], [-8, 15], [-12, -7], [-4, 4], [-26, 8], [6, 10], [-8, 14], [12, 4], [32, -2], [18, -2], [7, -7], [6, -5], [-14, -2], [-4, -11], [13, -5], [16, 16], [6, 4], [26, -20], [-8, 12], [16, 2], [-24, 8], [-36, -2], [11, -7], [10, 10], [-20, 12], [-14, 5], [-10, -1], [11, -5], [12, 3], [-30, -4], [-2, -12], [8, 20], [-2, -10], [-20, 30], [8, -14], [-22, 16], [18, -9], [13, -9], [6, 0], [1, -7], [-1, -3], [22, 14], [16, -17], [4, 16], [-14, 14], [1, 29], [2, -20], [0, -16], [-14, -13], [18, -6], [10, 16], [-2, 16], [-6, 29], [-7, 29], [-18, 26], [24, -27], [34, -18], [-5, 19], [2, 18], [-6, 34], [6, 7], [-4, -4], [-11, 7], [-6, 32], [34, -12], [-26, -8], [-12, 15], [-14, 15], [-6, -14], [16, -20], [-2, 20], [18, 4], [7, -7], [16, 8], [14, 12], [-6, -6], [-6, -8], [20, -16], [-6, 27], [-14, -20], [7, -25], [-2, 6], [2, -30], [-44, -2], [-24, 8], [-12, 12], [-11, -11], [2, 6], [-7, -3], [-38, 9], [-34, 24], [4, 28], [0, 26], [-46, 12], [42, 4], [-4, 14], [-4, -30], [14, -28], [-13, -9], [18, -37], [-44, 20], [10, 25], [35, -3], [28, 4], [2, -9], [-12, 20], [14, -44], [12, 12], [16, -36], [2, -6], [0, -10], [25, -29], [6, -13], [3, -23], [-21, 15], [-37, 5], [4, 35], [-36, 35], [-12, -20], [-16, -10], [-26, -7], [16, 22], [26, -5], [-17, 45], [46, -32], [-12, -3], [-20, 17], [-38, 4], [24, -48], [-12, 12], [23, -13], [10, 6], [-6, 26], [30, -4], [-18, -1], [-4, 20], [-32, -16], [-10, 26], [-7, 45], [-51, -1], [-35, 25], [-6, -2], [38, -52], [6, -20], [-10, 10], [-34, 34], [34, -48], [-9, 29], [38, 6], [-43, 15], [-28, 53], [48, -8], [22, 22], [-6, -26], [27, -51], [2, -15], [22, -1], [31, -25], [12, 0], [17, -41], [12, -16], [-6, 22], [40, 8], [-20, 23], [-2, -16], [-38, 28], [10, -44], [46, -23], [38, 0], [12, -32], [52, -37], [12, -9], [-39, 11], [14, -19], [-14, 24], [-28, -5], [-34, 52], [4, -6], [24, -20], [-36, 7], [26, -37], [-24, -16], [24, -30], [14, 15], [-39, -5], [28, 13], [-52, 8], [2, 0], [-38, -8], [16, 4], [46, -9], [2, -10], [-46, 14], [-17, 43], [-11, 25], [-30, 18], [42, -36], [4, 20], [-14, -6], [-30, -4], [-12, -32], [-16, 16], [8, 10], [-30, 30], [-35, 37], [14, 31], [-47, 19], [-9, -15], [50, -50], [-40, 4], [20, -36], [-26, 56], [8, 36], [-4, -27], [5, 15], [32, 5], [38, -56], [1, 49], [-22, -15], [-46, 12], [-12, 40], [-4, -26], [8, -22], [-15, -13], [70, -9], [-33, -5], [61, -7], [-36, 0], [45, -31], [4, 2], [20, -6], [-44, 28], [-18, 47], [42, 6], [-77, -7], [-16, 32], [-16, 32], [-29, 33], [12, -39], [36, -16], [24, 24], [-6, 46], [-66, 14], [6, 49], [26, -16], [48, -6], [12, 36], [-22, 8], [-12, 54], [-40, 5], [-31, 15], [-17, 7], [52, 10], [-44, 26], [0, 6], [-50, 50], [59, -21], [42, -3], [-24, 44], [-26, 36], [-24, -16], [53, -19], [-2, 4], [-30, -22], [28, -46], [35, -79], [-35, 17], [-36, -26], [40, -61], [56, -19], [-84, 2], [-23, 57], [52, -8], [30, -58], [-54, 25], [24, -40], [-2, 48], [26, 28], [27, -33], [28, -71], [-67, 11], [-34, 10], [56, 11], [69, -45], [-53, -5], [-16, -6], [-68, 0], [52, 10], [-36, 12], [-35, 47], [37, 29], [-1, -27], [-23, -9], [37, -59], [12, -19], [-18, 4], [-48, 2], [51, -43], [-9, -7], [47, 27], [65, -37], [6, 52], [-11, -7], [10, -16], [-52, 54], [-57, 33], [-16, 26], [34, -33], [-56, 44], [-1, 31], [-4, 10], [-32, 6], [40, -16], [8, -41], [-76, -6], [46, -60], [-40, -24], [26, 26], [-62, -8], [-85, 17], [57, -75], [29, -33], [-27, 13], [-2, -13], [23, -19], [-56, 19], [14, 3], [-22, 30], [44, -24], [-20, -20], [-40, 14], [92, 0], [-38, 65], [-31, -15], [24, -45], [-10, 4], [62, -16], [-52, 9], [72, -20], [24, 34], [84, -4], [-3, -9], [32, 44], [-9, -55], [-19, 5], [18, 11], [-12, -19], [-46, 8], [-40, -28], [24, -14], [10, 16], [46, -21], [54, -20], [-48, 84], [39, 7], [23, 13], [4, 44], [16, -63], [34, -72]];
    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_n();".
// 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_n(: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_n();".
// 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_n( : 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![-11, -1, 1]>,<13,R![-4, -2, 1]>],Snew);
    return Vf;
end function;