
// Make newform 6041.2.a.b in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6041_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_6041_2_a_b();".
// 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_6041_2_a_b();".
// 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_6041_a();"
function MakeCharacter_6041_a()
    N := 6041;
    order := 1;
    char_gens := [864, 4320];
    v := [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_6041_a_Hecke(Kf)
    return MakeCharacter_6041_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, 1], [1, -1], [-1, 0], [-1, -1], [4, 1], [0, -2], [4, -5], [-3, 5], [2, -4], [-4, 4], [-7, 3], [-4, 8], [4, -6], [-2, 2], [7, -7], [-9, -1], [4, 6], [-12, 3], [4, -4], [3, -3], [-12, -1], [12, -2], [6, -8], [0, -3], [2, -4], [9, 5], [6, 0], [-5, -3], [-16, 1], [-4, 0], [-16, 6], [6, 4], [8, -12], [14, -12], [-10, 2], [8, -3], [0, -4], [-4, 10], [-13, 9], [-8, 7], [10, -8], [-8, 16], [-22, 2], [-16, 8], [-4, 3], [-12, -9], [15, -9], [6, 8], [6, 10], [-18, 10], [-28, 2], [12, -1], [12, -14], [-4, -8], [-8, 19], [2, -6], [4, -18], [-12, 4], [-1, -15], [12, -7], [-10, -4], [-12, -11], [-4, 8], [6, 10], [-6, -14], [-16, -4], [8, 5], [8, -25], [6, 16], [-4, -6], [16, 10], [-30, 0], [-8, -7], [-8, 13], [-8, -3], [-12, 21], [-18, 16], [-14, 8], [2, -14], [-20, 0], [20, -11], [-24, -8], [14, -20], [-12, 18], [4, -11], [-4, 27], [-23, 27], [-8, 22], [-8, 0], [0, -13], [16, -14], [-6, -12], [-9, 19], [-4, 16], [-20, -5], [28, 3], [13, -5], [0, 19], [-10, 30], [-2, -26], [22, -18], [-12, 26], [26, 0], [-20, 26], [-32, 9], [4, 0], [34, -6], [-10, 14], [-24, 14], [20, 8], [-17, 25], [22, -20], [-4, 6], [-7, -19], [20, -34], [14, 20], [6, -6], [28, -8], [-12, 13], [-14, -2], [-16, -17], [-27, 3], [-18, 4], [24, 6], [28, -16], [-18, 0], [22, -20], [-18, 30], [-36, 7], [16, 4], [-6, -6], [-28, 6], [-28, 6], [13, 7], [36, -20], [-24, 16], [-18, -6], [34, 12], [-2, 16], [41, -3], [31, -7], [-24, -16], [-28, 27], [44, 3], [-30, 2], [12, -10], [14, -4], [-20, -4], [-1, 0], [-10, -8], [14, -2], [-48, 8], [24, -44], [24, -19], [24, -24], [-28, -1], [33, -25], [0, 14], [-2, 8], [9, 21], [-8, 16], [48, -21], [-24, 40], [18, -12], [16, 0], [32, 0], [-4, 21], [-8, -28], [-16, 4], [-25, 3], [57, -9], [-25, -1], [-20, 10], [-14, 22], [4, 6], [19, -25], [-40, 18], [24, 0], [46, -18], [16, -29], [5, 33], [-42, 32], [-2, 0], [-13, 35], [46, -22], [-37, 17], [24, -19], [6, -2], [49, 9], [9, -1], [-2, -20], [-24, -22], [34, -10], [-18, 32], [-18, 14], [23, -7], [-14, -18], [-16, 48], [-20, 20], [28, 13], [-6, -2], [15, -15], [-15, -25], [-34, 38], [-46, 4], [8, -2], [18, 24], [20, 1], [41, -3], [48, -16], [-44, 17], [4, 28], [0, 1], [-4, -37], [-6, -18], [-14, -6], [27, -19], [-4, 12], [40, -8], [26, 4], [20, 6], [28, 1], [6, -44], [58, -10], [-21, 1], [49, -37], [44, -2], [22, 22], [-22, 42], [14, 10], [-24, 41], [-20, -8], [-27, 27], [10, -38], [-22, -22], [-44, 46], [0, -39], [14, 16], [-27, 49], [4, 18], [-24, 2], [0, -28], [-50, 40], [-2, 10], [39, -41], [12, -4], [12, 35], [-69, 11], [45, -27], [2, -20], [0, -12], [38, 20], [30, -34], [-8, 19], [-16, 1], [-54, 16], [-5, 31], [30, 14], [2, -18], [-44, 6], [32, 0], [55, -27], [22, 30], [11, 13], [38, -54], [70, -8], [32, 14], [64, 5], [-40, -10], [-25, 41], [-4, 24], [0, -24], [32, -29], [2, -4], [-33, 51], [14, 12], [-14, 12], [-1, 45], [-24, 42], [4, 24], [6, -40], [-21, -25], [-4, -31], [-38, -2], [4, -28], [32, 3], [26, -32], [8, 35], [52, -26], [64, -32], [43, -17], [32, -30], [8, -18], [28, -62], [16, -36], [-60, 20], [35, -15], [-32, 7], [21, 21], [33, -21], [-12, -22], [-4, 4], [-27, -15], [-56, -20], [28, 16], [-8, -47], [-3, 23], [-44, -10], [29, 15], [-28, 7], [-20, 16], [58, 4], [-45, -5], [24, 16], [-38, 22], [15, -1], [-31, 69], [72, -14], [46, -40], [8, -36], [-62, 8], [24, -44], [5, -11], [2, 32], [-28, 3], [-22, 18], [-40, 4], [37, -11], [-12, -9], [-72, 7], [24, -69], [-23, 13], [32, -48], [44, -63], [32, -74], [-23, 59], [14, 20], [-22, -20], [2, -6], [-48, 54], [-19, 35], [84, 1], [6, 12], [52, -14], [-4, 10], [-38, -4], [0, 1], [-16, 45], [20, -35], [0, -30], [24, -8], [21, -33], [0, 6], [-8, 6], [52, -13], [-17, -29], [-28, 53], [8, 18], [-16, -6], [32, -6], [-68, 17], [-16, -25], [17, 33], [14, -34], [34, 34], [10, -4], [-27, 37], [-6, -10], [3, 11], [0, 58], [6, -38], [12, 45], [-50, 2], [48, -42], [-32, 35], [-2, 58], [-62, 14], [5, -49], [-36, -5], [-26, 62], [34, -2], [-2, 8], [4, 43], [-15, 61], [-9, 35], [40, -44], [47, -23], [-47, -27], [-20, -36], [-81, -3], [74, -36], [42, -8], [37, -35], [-56, -6], [48, -29], [19, 15], [26, -40], [-2, -20], [53, -41], [-46, -32], [90, -4], [25, 5], [-78, -12], [-18, -6], [-58, 10], [20, -44], [-68, 37], [-28, -19], [8, 24], [38, -54], [-4, 57], [-64, -24], [-52, -5], [6, 44], [46, -24], [63, -61], [-24, 45], [-18, 0], [12, -44], [10, 4], [14, -12], [-44, -12], [-34, -12], [-52, 36], [8, -47], [49, -23], [-84, 8], [-28, -45], [-46, 6]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6041_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_6041_2_a_b();".
// 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_6041_2_a_b(:prec:=2)
    chi := MakeCharacter_6041_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_6041_2_a_b();".
// 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_6041_2_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6041_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![1, 1]>],Snew);
    return Vf;
end function;