
// Make newform 3042.2.a.h in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3042_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_3042_2_a_h();".
// 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_3042_2_a_h();".
// 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_3042_a();"
function MakeCharacter_3042_a()
    N := 3042;
    order := 1;
    char_gens := [677, 847];
    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_3042_a_Hecke(Kf)
    return MakeCharacter_3042_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], [3], [-2], [6], [0], [3], [-2], [6], [-3], [4], [7], [-3], [-10], [6], [-3], [0], [-7], [10], [6], [13], [-4], [-6], [18], [-14], [-15], [14], [6], [-14], [3], [-4], [0], [9], [-4], [-9], [10], [5], [4], [0], [6], [-6], [-7], [12], [-23], [6], [-10], [-16], [-8], [18], [22], [6], [6], [1], [12], [3], [6], [-18], [16], [17], [9], [14], [-21], [10], [30], [-10], [3], [4], [23], [30], [10], [-15], [6], [2], [29], [-20], [24], [-39], [-14], [-3], [1], [-24], [-29], [-6], [-13], [14], [36], [18], [-11], [15], [-38], [18], [0], [-2], [18], [-32], [-6], [3], [-33], [-34], [-29], [-34], [3], [0], [-6], [-22], [-11], [0], [9], [-24], [-37], [32], [31], [-15], [-8], [-20], [3], [16], [-24], [-42], [-24], [-5], [-13], [-18], [-48], [-26], [30], [-5], [0], [14], [31], [16], [-36], [14], [-34], [-30], [-14], [-30], [28], [-30], [51], [4], [18], [-40], [-48], [17], [-12], [19], [-21], [26], [18], [-41], [-33], [8], [48], [44], [24], [-16], [33], [47], [42], [24], [-54], [22], [-60], [-3], [-36], [38], [5], [34], [-15], [-42], [31], [-36], [37], [32], [-45], [-62], [3], [-64], [-10], [-62], [0], [29], [-42], [18], [45], [-34], [28], [-11], [-30], [35], [-30], [-22], [-18], [6], [-18], [61], [5], [45], [12], [51], [-40], [-29], [-61], [-66], [-27], [-62], [-36], [9], [-40], [-13], [6], [2], [66], [0], [1], [8], [27], [-12], [-6], [29], [-20], [-51], [-20], [-18], [41], [-57], [36], [20], [60], [-67], [-4], [4], [-21], [-16], [36], [34], [-9], [24], [30], [-18], [74], [14], [-65], [-63], [36], [-44], [36], [-26], [-6], [34], [30], [24], [-61], [-30], [-78], [38], [46], [54], [25], [44], [-60], [-17], [41], [9], [-34], [30], [57], [-50], [-51], [17], [52], [73], [68], [-58], [34], [36], [-53], [49], [6], [-6], [40], [84], [-53], [22], [42], [-37], [3], [34], [-9], [78], [-18], [9], [0], [14], [69], [8], [-75], [12], [22], [-25], [-69], [74], [-12], [62], [-47], [-78], [-31], [84], [65], [-6], [-78], [15], [2], [42], [-85], [-42], [30], [34], [-45], [-82], [-74], [-3], [-2], [66], [-61], [-26], [46], [6], [-39], [55], [57], [-34], [-18], [46], [-30], [31], [-6], [-23], [-28], [10], [87], [15], [-82], [18], [-54], [-55], [-80], [-30], [-18], [-86], [-38], [87], [-58], [-31], [3], [42], [-36], [3], [6], [-89], [-21], [0], [84], [-64], [38], [66], [-8], [-61], [12], [-34], [42], [-51], [-22], [-7], [-18], [6], [46], [87], [-1], [-78], [21], [-2], [18], [-20], [6], [82], [86], [-8], [84], [-35], [-45], [-18], [26], [-48], [62], [-38], [-6], [-4], [87], [46], [54], [-80], [33], [27], [86], [-50], [57], [94], [0], [11], [-27], [30], [-100], [-13], [57], [-12], [14], [78], [36], [-51], [-29], [-66], [54], [58], [15], [84], [-15], [-98], [-84]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3042_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3042_2_a_h();".
// 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_3042_2_a_h(:prec:=1)
    chi := MakeCharacter_3042_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_3042_2_a_h();".
// 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_3042_2_a_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3042_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-3, 1]>,<7,R![2, 1]>,<11,R![-6, 1]>,<17,R![-3, 1]>],Snew);
    return Vf;
end function;