
// Make newform 6046.2.a.b in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6046_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_6046_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_6046_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_6046_a();"
function MakeCharacter_6046_a()
    N := 6046;
    order := 1;
    char_gens := [5];
    v := [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_6046_a_Hecke(Kf)
    return MakeCharacter_6046_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], [2], [-2], [2], [-3], [2], [0], [6], [-6], [1], [3], [1], [-10], [-1], [-8], [-6], [4], [1], [-2], [2], [16], [-1], [-14], [-6], [3], [10], [1], [8], [-19], [14], [-2], [0], [-3], [-6], [-18], [-16], [-11], [-6], [-8], [6], [20], [-12], [27], [9], [-14], [4], [-12], [11], [-2], [-26], [-18], [5], [-4], [0], [-3], [-16], [3], [-14], [-2], [17], [-22], [-16], [-11], [-4], [-15], [-2], [-36], [18], [-30], [14], [0], [-13], [-17], [33], [6], [33], [4], [14], [8], [-23], [-30], [14], [33], [29], [14], [7], [30], [18], [12], [-26], [13], [30], [2], [-12], [-40], [-28], [24], [-12], [16], [10], [2], [-32], [-21], [14], [10], [34], [-28], [43], [24], [-38], [-13], [-4], [3], [-35], [-16], [19], [12], [12], [46], [12], [22], [-28], [-30], [9], [45], [27], [16], [-16], [44], [-6], [25], [-24], [-40], [-48], [53], [-44], [-15], [-47], [-15], [1], [20], [30], [-4], [8], [36], [-18], [16], [-26], [0], [18], [-4], [-25], [-47], [-18], [-4], [20], [1], [-50], [21], [6], [54], [-51], [-32], [-57], [0], [-1], [35], [60], [2], [27], [19], [-32], [-8], [-1], [22], [0], [44], [12], [-16], [29], [48], [-36], [3], [-15], [46], [3], [54], [21], [10], [0], [-50], [15], [15], [-27], [-40], [15], [-32], [-32], [-37], [13], [49], [-39], [40], [26], [60], [-28], [40], [48], [46], [28], [26], [-30], [29], [1], [31], [20], [14], [-54], [-24], [45], [38], [-16], [-42], [-24], [12], [-62], [24], [54], [48], [-28], [-2], [-34], [-20], [-36], [-64], [32], [3], [6], [20], [0], [46], [31], [-20], [61], [19], [-25], [38], [-45], [-20], [54], [62], [-6], [-16], [-30], [51], [72], [-62], [31], [21], [-47], [61], [9], [60], [10], [-48], [7], [58], [-75], [76], [-36], [20], [28], [23], [-12], [19], [11], [12], [40], [35], [-10], [-71], [-30], [51], [80], [28], [31], [-6], [42], [25], [-6], [30], [16], [-29], [24], [62], [4], [53], [-62], [1], [-13], [30], [-36], [-16], [33], [76], [-40], [83], [68], [10], [-19], [-62], [-50], [-54], [10], [-68], [7], [-20], [-41], [-28], [-75], [-54], [13], [-16], [-88], [-61], [-26], [2], [-28], [-36], [37], [-63], [24], [-48], [-4], [-11], [3], [-83], [6], [-41], [-41], [-68], [-36], [-75], [-52], [55], [-57], [-78], [36], [-51], [36], [53], [42], [-11], [-8], [29], [-3], [4], [-54], [-18], [-71], [51], [22], [0], [93], [-7], [-9], [-5], [44], [64], [-28], [52], [74], [66], [-41], [-69], [-13], [9], [-20], [-90], [-34], [28], [-6], [83], [74], [-4], [34], [0], [-59], [72], [-7], [-16], [-4], [24], [-22], [14], [-2], [-55], [-30], [-72], [-48], [-44], [-14], [48], [22], [9], [-49], [-62], [-90], [-5], [26], [39], [-21], [-60], [8], [-12], [-30], [60], [18], [86], [-86], [6], [9], [-3], [51], [51], [-63], [-96], [-18], [74], [-64]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6046_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_6046_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_6046_2_a_b(:prec:=1)
    chi := MakeCharacter_6046_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_6046_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_6046_2_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6046_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-2, 1]>,<11,R![3, 1]>],Snew);
    return Vf;
end function;