
// Make newform 2541.2.a.bj in Magma, downloaded from the LMFDB on 06 May 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_bj();".
// 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_bj();".
// 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 := [2, -4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-3, 0, 1]];
    Rf_basisdens := [1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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, 0], [1, 0, 0], [0, 1, -1], [1, 0, 0], [0, 0, 0], [2, 1, 0], [0, 1, -1], [3, -1, -1], [-1, 1, 3], [2, 1, 0], [0, -2, 2], [-1, 0, -1], [5, -2, 1], [4, -4, -1], [-3, 3, -2], [-7, 0, 1], [1, 3, 0], [5, -3, 3], [0, 2, -5], [1, -3, 1], [-3, 5, 3], [4, 6, -2], [-5, 3, -6], [0, -1, -3], [-6, -7, 2], [-13, 1, -2], [9, -1, -1], [9, -3, -3], [5, -6, 6], [3, -4, 1], [-2, -12, 5], [11, 3, 0], [7, -9, -3], [9, 3, -1], [-2, 1, 4], [0, 8, -7], [4, -6, 8], [0, 2, -2], [-9, -5, 2], [-11, -5, 2], [-8, 2, -8], [3, 2, 7], [1, -7, -1], [9, -4, -4], [3, 4, 1], [9, -7, 3], [4, -2, -5], [-4, 4, 0], [-1, -11, 8], [-1, 2, -5], [4, 1, -2], [0, 0, 2], [19, -1, -1], [-4, -8, 8], [0, -13, 3], [-5, 9, -5], [9, -6, -1], [14, 2, 2], [7, 0, 10], [2, -16, 2], [8, 2, 4], [8, -1, -3], [1, 7, 5], [-13, -3, 6], [-9, 4, -9], [-21, 7, -1], [-4, -12, -1], [9, -6, -3], [7, -5, 13], [-5, -8, 5], [19, -2, 1], [9, -1, 9], [-5, 15, -5], [-10, 2, -3], [6, -12, 1], [7, -1, 10], [-15, 4, -3], [17, 0, 3], [17, 2, 1], [2, -5, 6], [-1, -5, -4], [-3, -4, 4], [-16, 2, 4], [-6, 5, 6], [4, -4, -6], [-29, 5, 3], [7, -4, 5], [-19, 8, 6], [-2, 5, 7], [-4, 18, -6], [18, -6, 8], [3, -15, 6], [-20, 4, 3], [-5, 5, -1], [-10, 12, 5], [3, 7, -8], [-7, 3, 10], [-19, 3, -8], [-3, 11, -7], [16, 4, 1], [-2, -8, -4], [-2, 8, 0], [-11, -5, -2], [-13, -9, 9], [4, -12, 0], [22, 1, 8], [-3, 7, -2], [10, -13, 9], [12, 6, 2], [-14, -5, 8], [-21, 9, 1], [5, -6, 10], [-13, 8, -3], [2, -4, -4], [-10, 6, 1], [24, 3, -6], [-12, 8, -4], [7, -17, -2], [-15, -7, 7], [7, -9, 9], [7, -14, -5], [4, -4, -1], [-16, 7, -13], [10, -2, 12], [-12, 6, 0], [-12, 11, -10], [16, 12, -7], [24, -6, 4], [-15, 17, -3], [12, 7, 2], [-12, 18, -6], [-27, 9, -7], [16, -8, 13], [-31, -4, 6], [12, 13, 1], [-5, 2, -13], [11, -11, -14], [18, 18, -8], [21, 7, -8], [-7, 13, 1], [5, -11, 1], [-6, -4, -4], [34, -2, 4], [-5, -17, 7], [-6, 19, 0], [-23, 15, -12], [-3, -6, 13], [-15, 11, 0], [-4, 24, -2], [14, -12, -8], [13, -8, 1], [7, -14, 5], [-6, 14, -21], [5, 5, 2], [10, -8, 4], [4, 12, 10], [-12, 10, -2], [-22, 1, 9], [42, -9, 2], [21, -16, 3], [-10, 4, 16], [-43, 0, -5], [32, 4, -7], [5, -7, 2], [4, 19, -4], [26, -4, 2], [16, 10, -2], [-29, 8, 1], [21, -6, 1], [30, 0, 4], [22, -10, 14], [-11, -7, 3], [9, 19, -13], [25, 2, -9], [-28, 8, 4], [-20, -5, 5], [4, -6, 7], [-7, -20, 5], [10, -12, -8], [17, -10, -7], [-22, 16, -9], [4, -10, 18], [-16, -4, -1], [-7, 6, -9], [-20, -14, 10], [26, 8, -2], [-21, 20, -6], [-12, -12, -6], [9, 4, -10], [9, -19, 4], [-23, 1, 7], [-2, 0, 4], [26, 2, 15], [-3, -10, -5], [18, -8, -4], [-20, 11, 5], [-15, 26, -3], [18, 2, -9], [-11, -14, -1], [24, -10, -16], [20, 10, 0], [-17, -1, -5], [35, -10, -3], [0, 33, -14], [-1, 1, 18], [22, 24, -12], [7, -23, -1], [23, -19, -11], [10, 3, -16], [31, -1, 1], [-11, 20, -8], [41, -14, -9], [-18, 24, 4], [-7, 33, -4], [19, 3, 12], [13, -18, 3], [-2, 6, -13], [-1, 5, -6], [-30, -14, 6], [1, -12, -9], [-3, 2, -2], [-12, -4, -18], [4, 19, -16], [54, -8, 5], [19, -19, -4], [7, 10, -15], [25, 4, 3], [41, 5, 3], [-6, -6, -12], [22, -8, 12], [-43, 16, 4], [6, 22, -2], [54, -10, -3], [-2, 19, -12], [-20, 10, 12], [-27, 7, 8], [13, 25, -11], [-38, 1, 2], [-20, 16, 4], [-35, 11, 16], [21, -13, -1], [19, -3, 17], [-36, 6, 10], [-29, 20, -17], [-6, -24, 12], [-9, -23, 12], [-16, 8, -14], [12, -6, -8], [56, 2, 3], [0, 26, 0], [-36, 12, -11], [0, -7, 23], [-13, 17, -17], [14, -3, 20], [-3, -20, -1], [53, 1, -3], [19, -30, -4], [17, -11, 21], [2, -29, 11], [7, 15, -21], [16, 10, -1], [-9, 11, 21], [17, -10, -19], [3, -17, -3], [21, 16, 7], [-17, -21, 7], [-51, 9, -1], [3, -11, 22], [-2, 0, 11], [-16, 10, -20], [19, -26, -11], [14, -6, 20], [-45, 8, -21], [-10, 4, -17], [10, -24, -14], [11, 7, -19], [13, -5, -3], [-21, 8, -12], [20, 10, 4], [35, -11, 10], [-4, -10, -18], [-26, -18, -3], [-42, 10, -12], [10, -3, 14], [2, 6, 16], [9, 1, -5], [-38, 22, -13], [-31, 13, 13], [5, -1, 13], [12, -27, -1], [37, 4, -7], [-10, 4, -18], [-35, 4, 3], [13, -19, 0], [-14, 10, 11], [-21, -7, -2], [15, 15, -7], [-8, 37, -13], [-24, -14, 20], [28, -24, 24], [-2, -16, 26], [23, 12, 7], [8, -10, 1], [-7, -15, 15], [74, -4, -2], [-8, -4, -21], [-30, 4, -4], [-6, 19, -2], [20, 14, 4], [5, 6, 18], [11, -11, 20], [-22, 27, -14], [32, -7, 12], [-8, -36, 6], [-6, -40, 22], [-48, 9, -8], [-4, 2, 4], [-23, -9, -3], [4, 14, -6], [33, 4, 11], [24, 8, -2], [-5, 20, -24], [-7, 8, -17], [28, 6, 11], [-23, 12, -23], [-40, -26, 4], [8, 0, 3], [32, -16, 8], [-16, 16, 6], [-31, 14, -13], [-1, 10, 19], [1, 25, -3], [1, 9, -11], [30, 4, 14], [-32, 8, -14], [11, 5, 4], [25, -14, 11], [-29, 9, -10], [32, -9, -2], [-5, -1, -9], [-5, -14, 24], [-13, 23, -9], [-38, -7, -1], [10, -8, -17], [11, 3, -27], [39, 11, -1], [-4, -7, -16], [-44, -2, 1], [20, 2, 22], [-32, -7, 7], [38, -8, -18], [16, -32, 1], [-18, -5, -18], [-37, 11, -15], [-17, -6, 11], [-40, -3, -1], [-11, 37, 2], [-49, -7, -10], [-53, 14, -9], [-64, 4, 10], [-14, -4, 6], [-80, 6, -2], [28, -12, -10], [54, -22, -12], [54, -14, 20], [-1, 4, -16], [-53, -6, -9], [14, -8, 33], [-27, 10, -27], [-10, -24, 22], [29, -1, 3], [25, -7, 7], [37, 2, -19], [-36, 18, -2], [-1, -26, -12], [-13, -1, -8], [-10, 32, -32], [19, 22, -5], [-2, 27, -23], [0, 14, -6], [14, 24, -12], [-40, -13, -4], [-2, 0, -29], [-82, 0, -8], [-3, -19, 15], [-5, -25, 10], [-26, -6, 7], [-4, -23, -10], [-34, 24, -33], [18, -8, -14], [22, -36, 2], [1, 15, -18], [-20, -12, 22], [-36, 20, -24], [-31, 15, -3], [66, 0, -8], [24, -26, 18], [40, -29, -9], [-14, -14, 8], [16, -25, -20], [23, -12, 17], [25, -26, 9], [24, -4, 26], [-27, -17, -14], [-30, 12, 22], [33, -11, -7], [43, -22, 6], [-55, 12, -5], [-1, 17, -23], [-28, 34, -18], [18, -21, 2], [-9, 12, -5], [7, -15, 37], [-22, 6, 13], [31, -6, -4], [-34, -17, 19], [-4, 8, 6], [-45, 17, -29], [55, -27, 4], [-36, 18, -40], [-72, -15, 4], [4, -6, 22], [-35, -11, 19], [4, 6, -10], [23, -20, 11], [18, -5, 27], [-18, -12, 0], [-2, -24, -2], [-53, 5, 7], [13, 15, -6]];
    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_bj();".
// 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_bj(:prec:=3)
    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_bj();".
// 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_bj( : 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, -4, -1, 1]>,<5,R![-1, -7, -1, 1]>,<13,R![-2, 12, -7, 1]>],Snew);
    return Vf;
end function;