
// Make newform 3042.2.a.ba in Magma, downloaded from the LMFDB on 23 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_ba();".
// 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_ba();".
// 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, -2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, 0, 1]];
    Rf_basisdens := [1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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_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, 0], [0, 0, 0], [1, -3, 1], [4, -1, 2], [0, -2, 3], [0, 0, 0], [2, 2, 0], [0, 0, 4], [0, 2, 2], [-4, 2, 1], [0, 0, -5], [-2, -4, -2], [-4, 4, -6], [4, -2, -2], [0, 0, 4], [1, -3, 5], [2, -3, -2], [8, -2, 0], [0, 4, -2], [6, -2, -2], [-1, -3, 7], [8, 5, -2], [-10, 7, -10], [6, 2, 6], [-11, 7, -3], [6, -1, 4], [3, -5, 5], [-2, -1, -6], [-12, 4, 4], [-4, 8, -8], [1, -7, 3], [7, -7, 5], [4, 0, 0], [-12, 6, -10], [4, 6, 1], [4, 12, -3], [-14, 2, 2], [10, -10, 0], [-6, -2, -6], [8, 2, 3], [-13, 9, -3], [10, -2, 2], [-6, 10, -4], [10, -4, 12], [-2, 17, -8], [-8, 8, 1], [12, -6, 8], [8, 12, -4], [-5, 9, -3], [6, -14, 0], [6, -4, -2], [12, -10, -2], [13, -1, 5], [10, -5, 2], [10, 2, 0], [6, 0, -14], [-11, -7, 1], [5, -7, 3], [-4, 14, 6], [-18, -2, -6], [6, -4, 12], [-4, 10, 11], [-10, 18, -8], [0, 2, 2], [5, -5, -7], [18, 1, -4], [2, -4, -2], [4, 16, -5], [2, -3, 2], [-10, 14, 2], [6, -18, 0], [0, 14, -14], [4, -11, 2], [-8, 22, -20], [12, -8, 0], [4, 2, 2], [8, -26, 11], [-20, -6, 8], [-14, 10, -8], [0, 1, 0], [-8, 14, 9], [-12, 10, -16], [-22, 2, 2], [16, 0, -9], [-8, 1, -22], [20, -6, 21], [-8, 20, -8], [-12, -4, 3], [-6, 11, 4], [8, -16, 4], [1, -1, -17], [0, 16, -6], [-25, 15, -27], [-4, -6, 13], [0, 0, 0], [22, -20, 26], [-14, 16, -12], [-6, -20, 4], [6, -12, 10], [-16, 8, -2], [8, -20, 4], [6, 8, 0], [-3, 7, -25], [-4, 0, -4], [18, -8, 8], [-12, 17, -28], [20, -2, 25], [-10, 20, 4], [14, -34, 20], [0, -8, 5], [4, 1, -2], [-16, 8, 10], [-16, -2, -12], [6, 2, -10], [4, 9, 18], [-12, -26, 14], [-14, 14, -20], [10, -28, 18], [-14, -7, -16], [-14, 7, -10], [14, 12, 6], [4, 1, 20], [6, -7, -8], [-7, -1, -21], [2, -20, 16], [-22, -12, -4], [-22, 4, -2], [-38, 16, -16], [8, 25, -6], [6, 6, 4], [28, -16, 34], [28, -12, 18], [4, 16, -13], [-16, -8, -8], [20, -24, 16], [14, -12, 8], [-10, -3, -4], [0, -4, -10], [28, -6, 23], [-24, -12, 6], [-4, -32, 12], [23, -5, 15], [-16, 1, -6], [-16, 6, -7], [8, -26, 18], [18, -16, 18], [-36, 4, 2], [4, 22, 4], [-16, 22, 8], [20, -24, 14], [-12, 0, 2], [26, 6, 8], [-10, 10, -4], [-24, 8, -22], [18, -16, 24], [-10, 34, -16], [11, -1, 17], [-22, -20, 14], [0, 0, -15], [-20, 22, -19], [29, -9, -1], [24, -26, 26], [12, 28, -1], [-4, 30, -17], [-2, -22, -4], [-46, 14, -28], [-11, 21, -29], [10, 2, 26], [-20, 24, -9], [14, 16, -16], [20, 8, 0], [2, 26, -24], [-18, -4, 18], [22, -20, -12], [-8, -16, -12], [-16, 2, -18], [32, 2, -6], [-18, -21, 8], [-4, 36, -4], [-2, -18, -18], [-15, -3, 15], [8, -14, 17], [18, -2, -20], [12, -2, 18], [50, 0, -6], [2, 17, -20], [-2, 6, -36], [-16, -10, 6], [0, 1, 8], [-8, 22, -18], [-28, 20, -5], [-18, -11, 2], [6, -18, 24], [9, -11, -15], [-41, 9, -7], [36, -16, -4], [-8, 12, 3], [10, -22, -18], [4, -2, 24], [30, 6, 16], [-14, 12, -36], [28, 16, -9], [-8, 28, -28], [-19, 7, -35], [-2, 11, 18], [36, -14, 45], [-19, 9, -29], [2, -27, 14], [12, -2, 32], [6, 8, -22], [49, -5, 13], [-8, 6, -21], [20, 12, -4], [44, -12, 0], [26, -26, 12], [-36, 39, -48], [52, -4, 7], [4, 24, -8], [-4, 14, -30], [5, 13, -23], [-14, 24, -8], [35, -21, 1], [-30, -20, 2], [-16, -16, -3], [0, -28, 4], [-8, 2, 0], [30, -12, -4], [2, -26, -2], [-37, -1, -11], [13, 7, -5], [-8, 20, -34], [-46, 36, -52], [52, -5, -10], [-6, -10, -22], [26, 18, -2], [28, -14, 18], [8, 9, 4], [18, -21, 40], [0, -24, 4], [8, -48, 18], [-60, 18, -35], [-16, 10, -6], [32, -12, 4], [-40, 28, -44], [-40, 16, -8], [0, 28, -18], [16, 8, 1], [-56, 22, -31], [-46, 12, -10], [0, -8, -10], [-26, 12, -14], [-42, 6, -36], [12, -6, 26], [0, -7, 16], [18, -33, -28], [-38, 5, -34], [36, -10, 34], [24, -24, -14], [-34, 31, -28], [36, -12, 41], [-48, 28, -21], [-23, 31, -13], [-2, 30, -16], [30, -16, 28], [24, -30, 24], [-40, -12, -4], [1, -3, 1], [10, 32, -12], [26, -18, -6], [15, 19, 3], [-12, 8, 20], [-12, -20, -18], [-29, 5, -33], [5, 9, 11], [40, -3, 8], [12, -12, 7], [15, 13, 33], [22, -36, 40], [24, -39, 40], [22, -35, -10], [28, -2, 24], [29, -27, 19], [-32, 48, -32], [-38, -12, 12], [6, -40, 32], [-36, 4, -44], [-25, 25, -37], [5, 5, -35], [-20, 16, -21], [20, -12, 6], [6, -7, 40], [-40, 10, -37], [-56, 2, -16], [11, 45, -19], [18, -18, -30], [54, -35, 28], [9, -15, -5], [30, 31, -12], [-12, 42, -27], [-22, 26, 12], [20, -8, 27], [45, -31, 17], [-64, 9, 10], [17, -17, -17], [10, -44, -16], [33, 3, -3], [64, -36, 48], [-22, 0, -24], [0, -54, 14], [-6, 28, -20], [-26, 44, -16], [2, 5, 40], [0, -12, 2], [18, -44, 18], [26, 2, -8], [24, -28, 11], [-27, 11, 11], [32, 6, -16], [42, -24, 40], [-54, 18, -52], [-14, 6, 0], [16, -32, 37], [-46, 23, -32], [-12, 44, 1], [-14, -46, 28], [-20, 56, -15], [-22, 26, -52], [-26, 12, 0], [-48, -24, 18], [34, 0, 0], [-2, 68, -34], [39, -37, 55], [32, -12, 20], [-33, 29, 41], [-10, 20, 0], [-15, 3, -21], [-28, 40, -38], [28, 16, -22], [28, -20, -31], [-16, 12, -37], [-26, -2, 12], [46, -40, 22], [28, 2, -1], [-8, -40, 9], [-12, 30, -37], [-46, 31, 2], [18, 18, 0], [56, -8, 34], [-2, 38, 6], [29, 5, -19], [0, -40, -8], [20, 17, -4], [-68, 22, -29], [4, 32, 12], [-10, 8, -12], [-20, 34, -10], [8, 16, -18], [-26, 7, -26], [50, -18, -16], [-8, 52, -14], [-32, 0, -14], [-40, -8, -12], [-56, 40, -58], [6, -11, 22], [-26, 16, 36], [35, -23, 63], [22, 13, -8], [-21, -17, -11], [-36, -16, 7], [47, 13, -19], [20, 12, 24], [-2, 10, -4], [-28, 26, -23], [47, -5, -23], [-16, 44, -22], [-45, 17, -7], [-2, 0, 18], [-8, -15, 4], [-10, 30, 8], [-12, -5, -36], [-32, 38, -3], [-12, -34, 28], [-48, 40, -37], [18, 0, 6], [-36, -4, -24], [16, -36, 14], [-60, 13, -14], [-34, 26, -34], [-14, 50, -2], [42, -50, 36], [28, -44, -7], [-59, -11, 1], [-30, 3, -38], [-24, 38, -16], [-18, -14, 14], [32, -44, 33], [52, -31, 30], [-16, -50, 22], [30, -14, -18], [28, 2, 45], [-22, 32, -2], [-60, 8, 0], [31, -1, 9], [32, 16, 2], [-44, 26, -13], [1, 45, -1], [14, -54, 26], [94, -8, 12], [-52, 12, -30], [12, 4, 0], [12, 3, 28], [-48, 2, -1], [4, -10, -23], [26, -34, 50], [5, -37, -11], [42, -28, 4], [-22, 16, 42], [8, 16, -51], [48, -46, 54], [68, -32, 32], [-18, 25, 16], [2, 28, 6], [42, -14, -18], [-16, 28, -12], [-88, 4, -19], [14, -32, 4], [-81, -3, -3], [-6, -28, 28], [10, -10, 34], [-42, 10, 10]];
    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_ba();".
// 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_ba(:prec:=3)
    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_ba();".
// 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_ba( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3042_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-29, -16, 1, 1]>,<7,R![1, 20, -9, 1]>,<11,R![1, -8, 5, 1]>,<17,R![8, 12, -8, 1]>],Snew);
    return Vf;
end function;