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