
// Make newform 2541.2.a.u in Magma, downloaded from the LMFDB on 20 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_u();".
// 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_u();".
// 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 := [-4, -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, 0], [-1, 0], [0, 0], [-4, 1], [-1, 2], [-6, 0], [-4, 0], [-2, 3], [2, -4], [2, 3], [6, -1], [1, -3], [5, 1], [0, -5], [-11, 1], [4, -6], [1, -5], [0, -2], [-4, 2], [0, 2], [1, -5], [-5, 4], [6, 3], [-3, 1], [-8, 0], [0, -2], [-7, -2], [2, 7], [-11, 7], [1, -1], [-14, 0], [0, 4], [10, 3], [-13, -1], [4, 6], [8, -6], [5, -5], [-11, 1], [-8, -2], [-8, -5], [-6, 4], [9, -4], [-10, 11], [-4, 2], [-13, 9], [24, 0], [-1, 3], [-12, -5], [10, -11], [10, -6], [6, -4], [4, 0], [23, 0], [-12, 12], [-6, -1], [0, 0], [7, -4], [-24, -2], [-12, 2], [13, -6], [-26, -2], [3, -1], [4, 1], [-24, 2], [-13, 3], [-10, 11], [14, 0], [-6, -9], [6, -11], [-28, 0], [2, -8], [1, 5], [-13, 1], [19, 1], [16, -1], [-18, -3], [-20, -3], [-14, 13], [-1, 13], [-7, 0], [-20, -6], [-2, 7], [-2, -2], [4, 8], [14, -9], [-5, -10], [1, -6], [4, 2], [-28, -4], [25, -3], [21, 1], [-20, 0], [-13, 17], [15, -3], [21, -3], [33, -7], [22, -2], [-1, 7], [12, -6], [30, -8], [1, 5], [6, 0], [4, -4], [-20, 5], [-5, -1], [35, -6], [-10, 20], [0, 3], [8, -4], [15, -8], [-30, 11], [-34, 8], [-37, 7], [-10, 3], [8, 12], [3, -7], [2, -4], [10, 0], [-8, 15], [-17, 7], [1, -14], [-2, -10], [8, -18], [10, 11], [-25, 7], [0, 14], [-18, 20], [36, 1], [-24, -10], [36, 2], [11, -1], [15, 10], [-11, -16], [0, -13], [3, -17], [-34, 2], [19, -13], [-24, 14], [-22, 0], [14, 8], [-32, 4], [36, -2], [40, -5], [43, -9], [-20, 21], [17, 5], [26, 6], [30, -4], [30, -15], [-30, -7], [15, 11], [-15, -5], [28, 6], [-10, 6], [4, 10], [15, 8], [-6, -7], [30, 1], [2, 0], [14, -19], [1, 13], [21, 3], [28, -19], [-16, -6], [40, -14], [2, -25], [-6, 3], [-26, 0], [-20, -16], [20, -2], [-10, 14], [-2, 7], [12, -8], [9, -2], [-21, -3], [20, 11], [2, 12], [-12, -3], [29, -5], [-32, 18], [-33, 19], [22, 7], [46, 0], [-14, 20], [-1, -12], [-22, 26], [21, -4], [-21, 1], [-28, 2], [2, 20], [35, -5], [10, -9], [-6, -8], [21, -6], [-6, -17], [-21, -5], [22, -9], [52, 6], [8, 18], [18, 12], [-18, 13], [44, -13], [-13, -17], [30, 8], [18, 10], [-6, 2], [-42, 9], [-12, -10], [-65, 2], [-10, 19], [-40, -2], [-15, 15], [21, -5], [10, -25], [-15, -19], [19, 19], [-8, 4], [-6, 9], [-23, -2], [18, 18], [8, 1], [-21, -3], [-29, 3], [-18, -15], [-30, -3], [-22, -8], [-26, 18], [-6, 12], [27, -14], [24, -12], [-19, 5], [22, 5], [-44, 14], [13, -7], [10, -12], [28, -7], [32, -12], [29, -3], [2, 6], [-16, 24], [-14, 0], [10, -13], [-18, 28], [-43, 1], [2, 10], [-12, 6], [-9, -11], [16, -26], [-5, 7], [-1, 18], [-20, 12], [-36, 25], [-6, -23], [-32, -10], [-25, 10], [10, 4], [3, 6], [54, -8], [29, -17], [-10, -8], [44, -11], [12, 18], [14, 9], [14, 12], [-42, 4], [53, 5], [-73, 5], [-56, 6], [34, -15], [20, -32], [-60, -7], [-3, -13], [-16, 6], [4, -14], [-40, 12], [41, 10], [-58, 4], [-1, 3], [-16, 34], [17, -7], [-36, -16], [10, 19], [2, -22], [-26, 0], [-31, -7], [-16, 22], [-18, -14], [53, -12], [-40, -5], [-36, 26], [42, -3], [-65, 9], [1, 13], [-19, 17], [-4, -18], [25, -4], [-16, -26], [20, 16], [24, -6], [40, 9], [17, 3], [-46, 12], [-12, 6], [-53, 7], [-30, 4], [2, 5], [20, -38], [17, -6], [39, -17], [-20, -25], [32, 15], [20, 12], [-8, 6], [30, 5], [-56, 2], [-32, 20], [30, 12], [6, -27], [62, -2], [33, -10], [58, 1], [31, 13], [-4, -13], [-16, 26], [45, 9], [-32, 0], [-18, 14], [-6, -9], [46, -19], [0, 26], [6, -12], [4, 10], [-20, -8], [33, -3], [-14, -5], [33, 21], [-12, -19], [30, 4], [-61, -10], [-52, 22], [-37, 8], [39, 1], [46, -20], [-40, 10], [18, -27], [3, -19], [60, -14], [-1, -6], [12, 22], [-59, -3], [-12, 3], [-8, -14], [-30, 9], [-17, 22], [17, 11], [1, 25], [0, -21], [22, 10], [62, -4], [10, -4], [46, 6], [-6, 10], [34, -22], [-31, 10], [66, -15], [19, 5], [22, -17], [28, -18], [-28, 0], [2, 4], [68, -7], [-18, 0], [37, -28], [-31, -21], [14, -8], [12, 11], [83, -2], [-10, 0], [-30, 12], [36, -31], [7, 5], [26, -12], [-92, 2], [-25, -23], [-29, -17], [-36, -1], [-11, 15], [8, 14], [30, -8], [-11, -31], [-50, 22], [8, -16], [-8, -4], [-58, -4], [46, 0], [-7, 14], [12, -36], [56, -7], [-22, 31], [12, -39], [-8, -32], [31, 11], [-22, -16], [-34, 8], [-23, -12], [30, 5], [-12, 28], [-12, 26], [-54, 5], [16, -23], [-42, -14], [17, 5], [-7, -12], [17, 8], [-82, 2], [-62, 6], [33, 1], [-60, -2], [68, -21], [58, 12], [-86, 12], [-76, 6], [44, -15], [43, -18], [70, 4], [0, -30], [26, 10], [1, -15]];
    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_u();".
// 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_u(: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_u();".
// 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_u( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2541_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-4, 1, 1]>,<5,R![-1, 1]>,<13,R![8, 7, 1]>],Snew);
    return Vf;
end function;