
// Make newform 6017.2.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6017_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_6017_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_6017_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_6017_a();"
function MakeCharacter_6017_a()
    N := 6017;
    order := 1;
    char_gens := [3830, 2190];
    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_6017_a_Hecke(Kf)
    return MakeCharacter_6017_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], [2], [4], [2], [1], [1], [8], [-5], [6], [5], [10], [0], [0], [-10], [-9], [-10], [-14], [-14], [-1], [12], [-2], [4], [6], [-6], [-13], [8], [-10], [18], [10], [17], [13], [3], [3], [-5], [-21], [-16], [-1], [-4], [-3], [4], [15], [-10], [-13], [13], [8], [-8], [12], [12], [9], [-28], [-5], [8], [-26], [24], [-6], [-1], [-17], [16], [-10], [-6], [14], [31], [-4], [5], [7], [13], [20], [-24], [-9], [-25], [-21], [22], [0], [-32], [5], [8], [30], [2], [-5], [5], [-1], [28], [22], [-30], [-29], [5], [-6], [22], [-20], [-16], [-25], [21], [-14], [12], [5], [34], [-2], [6], [20], [-44], [-1], [-11], [4], [-6], [-13], [-20], [45], [46], [11], [19], [-8], [-43], [8], [-22], [-27], [0], [-31], [-12], [6], [22], [-22], [-41], [-3], [-5], [20], [19], [36], [26], [4], [-24], [-34], [-36], [-41], [-41], [17], [0], [-34], [9], [-30], [-20], [48], [-32], [-49], [-16], [23], [3], [46], [26], [-31], [-42], [-34], [6], [-4], [4], [-28], [-30], [0], [-4], [44], [53], [36], [17], [36], [18], [-22], [-52], [-45], [46], [26], [34], [5], [-26], [16], [18], [43], [-49], [56], [61], [41], [-8], [-1], [-48], [-6], [24], [39], [-45], [-4], [-40], [-33], [62], [34], [-1], [-56], [64], [-43], [37], [14], [-11], [14], [-8], [33], [-5], [29], [22], [29], [-57], [8], [3], [21], [-32], [48], [50], [-3], [20], [0], [-35], [23], [-18], [-37], [-26], [-7], [51], [-31], [10], [-6], [18], [-72], [-34], [-20], [-30], [4], [14], [-5], [-2], [-6], [18], [-18], [-22], [-6], [-72], [-6], [20], [23], [-6], [3], [38], [30], [40], [58], [69], [-64], [-2], [-12], [37], [-45], [13], [-77], [18], [8], [55], [-32], [2], [12], [-9], [-53], [-48], [36], [-12], [-12], [6], [42], [-14], [-34], [16], [-57], [29], [20], [2], [65], [-18], [-48], [59], [48], [-16], [22], [-30], [74], [28], [8], [-17], [-12], [-21], [-70], [-9], [-13], [22], [54], [-24], [4], [-49], [50], [-34], [-70], [-72], [-40], [84], [24], [-5], [-62], [60], [47], [2], [50], [44], [11], [6], [13], [47], [-73], [50], [7], [-88], [42], [13], [-54], [-15], [72], [-11], [-17], [-42], [-82], [54], [11], [42], [-50], [0], [-74], [45], [7], [-43], [26], [82], [6], [16], [10], [-42], [10], [12], [0], [-66], [17], [70], [17], [-32], [-10], [-73], [24], [-4], [76], [20], [-90], [88], [28], [-44], [-8], [-62], [-30], [31], [34], [56], [70], [0], [-30], [-70], [44], [17], [24], [38], [-50], [10], [36], [-23], [-55], [-32], [-28], [-60], [44], [50], [-26], [-11], [44], [49], [-28], [28], [2], [-47], [10], [12], [-44], [-3], [5], [-42], [88], [-76], [-9], [-97], [33], [55], [18], [-21], [-20], [-72], [-82], [-100], [22], [-51], [39], [-74], [-60], [-42], [13], [-98], [2], [-59], [83], [-64], [36], [22], [88], [-1]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6017_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_6017_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_6017_2_a_b(:prec:=1)
    chi := MakeCharacter_6017_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_6017_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_6017_2_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6017_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![0, 1]>,<3,R![-2, 1]>],Snew);
    return Vf;
end function;