
// Make newform 6171.2.a.k in Magma, downloaded from the LMFDB on 05 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6171_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_6171_2_a_k();".
// 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_6171_2_a_k();".
// 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_6171_a();"
function MakeCharacter_6171_a()
    N := 6171;
    order := 1;
    char_gens := [4115, 970, 2179];
    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_6171_a_Hecke(Kf)
    return MakeCharacter_6171_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], [2, 0], [-1, 1], [0, 0], [2, 0], [1, 0], [-2, -2], [2, -2], [-1, 3], [0, 0], [0, -2], [-5, 3], [-4, 0], [5, 3], [-5, -1], [7, -3], [2, -4], [-9, 1], [8, 0], [-7, 5], [0, 4], [-4, 0], [-9, -1], [-4, -2], [-8, -2], [5, -1], [5, 3], [-1, -5], [-10, 0], [0, 0], [-17, 1], [-3, 9], [-15, 3], [-16, -2], [0, 0], [-11, 5], [-4, 0], [-8, 0], [5, -11], [-3, 7], [-4, -6], [4, 8], [2, 0], [-2, -8], [16, 0], [-5, 1], [-8, 0], [13, -5], [11, -1], [-21, 3], [2, -2], [-7, -3], [0, -8], [9, 5], [12, 4], [-16, 10], [6, 2], [21, -3], [-2, 4], [-4, 4], [8, 6], [28, 0], [18, -10], [10, 0], [-2, 12], [-24, -4], [-11, 1], [5, 3], [12, -10], [-11, -7], [-10, 10], [6, 10], [24, 2], [-10, 14], [4, 8], [11, -1], [-12, -6], [-28, 2], [-4, -6], [-12, 0], [-17, -5], [13, 7], [-7, -11], [17, -9], [16, -8], [-16, 6], [-20, -6], [-22, 4], [-15, 3], [-23, 3], [19, -15], [-14, 6], [-8, -4], [-6, -14], [11, -15], [13, -11], [4, 10], [-14, 10], [21, 5], [32, -8], [-2, 0], [6, -2], [-10, -12], [-17, -11], [-5, -5], [4, 4], [-22, 16], [19, -3], [14, -4], [-24, -4], [38, 4], [6, -16], [18, -14], [11, -7], [22, -16], [4, 8], [-23, 7], [22, -12], [-4, 8], [-1, 11], [-25, -1], [7, 3], [-6, -6], [22, -2], [-38, 4], [0, 6], [-6, -10], [31, 5], [-18, 20], [18, -22], [-7, -13], [38, -6], [-10, 0], [28, -10], [6, 16], [-2, -16], [11, -15], [6, 16], [1, 21], [0, 16], [14, -8], [-16, -8], [5, -5], [-42, 8], [30, 2], [7, 11], [-18, 12], [-25, -7], [0, 4], [14, 0], [10, 4], [31, -15], [8, 8], [4, 16], [-16, -16], [-28, -4], [-42, 0], [-26, -8], [43, -9], [-20, 8], [34, -8], [-16, -8], [8, 0], [45, 1], [30, 2], [-26, 10], [15, 3], [-2, 4], [26, -16], [18, -6], [-13, 15], [-13, 9], [30, -8], [1, -21], [-8, 14], [-33, -3], [22, -12], [-22, 14], [-4, 14], [44, -4], [9, -25], [-22, 12], [-14, 8], [-19, 3], [22, 0], [-6, -4], [7, -7], [6, 20], [-12, -12], [-22, 8], [28, -16], [-37, 13], [51, -1], [8, -4], [8, 6], [7, -1], [-50, 8], [5, -23], [-40, 8], [-42, -8], [33, -17], [-37, -1], [-4, -14], [-7, -13], [10, -12], [17, 11], [38, -2], [-39, -3], [-31, -1], [35, -13], [-35, 13], [22, -6], [-15, -21], [-27, -9], [6, 20], [12, 4], [-10, 4], [0, 0], [10, 4], [32, -6], [-48, 4], [-7, 21], [12, -28], [-10, 6], [-32, -6], [14, 0], [-6, 14], [-38, -10], [39, -15], [47, 3], [-31, -13], [-42, 18], [42, 0], [-36, 24], [10, 6], [40, -14], [5, 13], [-44, 8], [43, -3], [2, -22], [-14, -14], [41, -13], [-31, -7], [-6, 24], [23, -3], [22, 18], [-14, 2], [-12, 16], [30, 2], [14, 12], [-34, -12], [39, -15], [50, -16], [52, 2], [-2, -2], [16, -18], [-28, 28], [2, -16], [-46, 20], [-23, 7], [10, -14], [37, 13], [-23, 17], [-14, -4], [-32, -12], [26, 20], [11, -21], [31, 13], [20, -14], [2, -28], [-28, 16], [-45, 11], [26, 14], [-28, 18], [-40, 0], [-36, -4], [-46, 24], [-10, -8], [-20, -16], [4, -12], [-42, 18], [7, -3], [-50, 4], [45, 7], [-27, 19], [0, 10], [-25, 19], [40, 0], [-22, 32], [48, -14], [-54, 10], [8, -30], [-1, 9], [-14, -12], [27, 23], [-24, 16], [2, 4], [20, 24], [16, -12], [4, -14], [-30, 12], [35, -3], [32, 12], [16, -20], [22, -28], [5, -1], [-12, 34], [-17, 25], [42, -16], [9, 3], [54, -12], [26, -24], [-40, 12], [-32, -16], [-20, 2], [-67, -1], [-17, 13], [4, -26], [-22, -24], [44, -4], [-50, -12], [10, -28], [-45, 25], [-27, 1], [53, -7], [-60, 0], [16, 12], [-47, 27], [23, -13], [-23, 1], [-26, -4], [-24, 4], [24, 12], [-43, 23], [-30, 10], [-30, 28], [25, -11], [-40, 2], [-12, 28], [-5, 31], [-37, 11], [18, 4], [-1, -19], [-25, 27], [11, -3], [16, -10], [9, 15], [-7, 19], [18, -16], [6, -18], [62, -12], [1, -31], [-29, -3], [22, 20], [28, -10], [-1, 9], [-35, 11], [-10, -20], [13, -5], [69, -3], [63, -1], [33, -1], [-68, -8], [-20, -8], [11, -29], [-62, -4], [26, 14], [-19, 17], [1, 3], [-12, -8], [8, 8], [-16, 6], [18, -26], [66, -4], [94, -2], [-24, 24], [-38, -12], [-58, -4], [-60, 2], [44, 6], [-25, -17], [29, -21], [-33, 39], [19, -15], [-2, 2], [25, 19], [-23, 25], [-9, -35], [0, -36], [-44, -10], [26, -16], [-1, 5], [12, 24], [45, 19], [9, -11], [-14, 6], [-21, 11], [16, -44], [20, -30], [-39, 17], [33, -19], [-42, -6], [-50, 16], [16, 14], [21, 3], [-55, 17], [60, 6], [55, -15], [-16, -12], [-26, -4], [-4, 2], [27, -23], [25, -5], [38, -8], [40, -6], [-19, 7], [-66, 2], [-2, 24], [-14, 6], [-21, 23], [18, -36], [20, -20], [38, -26], [-6, 16], [78, -8], [18, 18], [74, 0], [13, -13], [-11, 23]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6171_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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