
// Make newform 3420.2.a.h in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3420_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_3420_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_3420_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
        poly := [-3, 0, 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_3420_a();"
function MakeCharacter_3420_a()
    N := 3420;
    order := 1;
    char_gens := [1711, 1901, 2737, 3061];
    v := [1, 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_3420_a_Hecke(Kf)
    return MakeCharacter_3420_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, 0], [0, 0], [-1, 0], [2, 0], [0, -2], [-1, 1], [0, -2], [1, 0], [0, -2], [0, 2], [2, 2], [5, -1], [6, 0], [2, 4], [-6, 4], [9, -1], [0, 4], [2, -6], [5, -1], [6, 2], [-4, -2], [-4, -4], [0, 2], [12, -2], [-1, 9], [6, -6], [5, -5], [-3, -5], [-4, 6], [3, 9], [5, -9], [12, -4], [0, 10], [8, -2], [-6, -6], [2, 6], [20, -2], [14, 4], [9, -1], [-9, -3], [0, 12], [14, 0], [0, 4], [-13, -3], [6, 4], [20, -4], [2, -10], [-1, 5], [-3, 3], [14, -2], [-6, -4], [0, -8], [2, 8], [24, 0], [3, -7], [-12, -2], [0, 6], [8, 6], [8, -6], [6, 4], [8, -6], [-3, -5], [-13, -3], [12, 6], [-10, -12], [9, 7], [2, 14], [-19, -1], [0, 18], [14, -4], [-12, -6], [12, 2], [-10, -8], [-1, 9], [8, -4], [-15, -5], [-18, 0], [-16, 2], [-18, 12], [-4, 6], [12, 0], [-34, 0], [30, -2], [-31, -5], [-4, 16], [-24, -6], [0, -2], [14, -12], [-6, -16], [8, 2], [12, -10], [12, -6], [-7, 3], [-12, 4], [20, -2], [-12, 2], [-12, -10], [-6, -12], [-1, 21], [2, 18], [5, -1], [12, 10], [-39, 3], [12, -10], [8, -6], [14, 12], [-12, 18], [-18, 8], [0, -12], [-10, -20], [-7, 19], [-4, -6], [-12, -6], [8, 6], [8, -2], [18, -4], [-4, 6], [-12, 2], [-24, 2], [12, 12], [26, 12], [-49, 1], [9, 15], [9, -5], [20, -18], [30, 2], [-10, 12], [0, 10], [14, 8], [-10, -12], [-28, 0], [-33, -7], [-22, 2], [-10, 0], [-30, -6], [-34, -2], [-3, 11], [-7, 3], [-3, -5], [18, 16], [26, 6], [30, 8], [2, 24], [21, -5], [-16, -14], [-12, 4], [-10, 20], [-9, -3], [20, -16], [39, 1], [-25, -15], [18, 10], [-22, 0], [-39, -9], [23, 9], [-18, -2], [-4, -28], [6, -24], [20, -18], [-18, 20], [-24, -10], [-45, 9], [-28, 10], [18, 2], [-27, 3], [-27, -17], [2, 6], [20, -6], [-40, 6], [-48, -2], [12, -20], [14, -8], [0, -18], [38, 0], [20, 20], [-30, 10], [-52, -4], [6, -6], [-31, -17], [-22, -10], [44, 6], [-6, -6], [11, 5], [21, -21], [36, 14], [-18, 12], [17, 27], [-13, 1], [-28, 18], [12, 8], [5, 15], [-12, -10], [2, 18], [6, 32], [18, 16], [-21, -7], [2, -24], [-10, -8], [-30, -8], [30, -16], [-36, -2], [50, 6], [-13, -19], [-4, 14], [36, -14], [24, -10], [-40, -6], [-3, 15], [54, 4], [62, -2], [2, 16], [6, 14], [20, 14], [-51, -5], [-12, 8], [-10, 16], [-22, -12], [18, 8], [-9, 13], [6, -24], [38, -12], [8, 8], [-36, -10], [8, -18], [45, -13], [14, 32], [21, 19], [12, -12], [11, 21], [24, -24], [14, -24], [-4, 36], [2, -6], [-6, -24], [-28, -18], [66, -4], [50, -2], [30, -4], [-12, -22], [6, -2], [-33, -23], [8, 0], [74, 0], [8, 26], [21, -21], [0, 0], [14, 24], [30, 10], [-28, -16], [18, -16], [-34, 12], [6, 40], [12, 14], [8, 18], [-54, -8], [36, 16], [-22, 0], [17, -21], [33, 7], [-4, -18], [-31, -9], [-15, 7], [26, -2], [11, 21], [6, -20], [20, -24], [12, -30], [42, 6], [11, 9], [-42, -16], [-34, 0], [11, 21], [-28, 18], [44, -6], [-1, 1], [2, 36], [-30, 4], [32, -14], [50, 16], [-36, 22], [57, 3], [8, 18], [-48, 18], [50, -16], [-40, -6], [-48, 6], [14, -4], [21, 19], [-4, -28], [-12, 6], [-18, -8], [-6, 24], [-39, -1], [-54, -2], [41, 19], [-18, 2], [62, 6], [66, -8], [0, -16], [14, -20], [-28, 6], [3, 25], [44, -14], [3, -19], [20, 28], [-55, 11], [-27, -21], [56, 6], [0, -8], [-22, 4], [6, -16], [-18, 26], [6, -28], [23, 21], [-24, 10], [8, 18], [-24, -10], [-18, -10], [26, 20], [-6, 28], [2, -22], [8, 2], [-18, -16], [-1, -19], [-24, 10], [38, -8], [-28, 16], [-49, -11], [-21, -31], [36, 10], [-22, -38], [27, 9], [56, -6], [-6, 32], [-16, -30], [6, -24], [-64, -6], [-9, 29], [-22, 40], [-4, 6], [29, -9], [0, -6], [0, -30], [-10, -30], [45, -13], [0, -20], [-10, 48], [35, -3], [-18, 26], [-48, 26], [2, -18], [11, -11], [30, 30], [-25, 21], [32, -18], [33, 23], [-24, 8], [12, 28], [-24, 14], [15, -31], [44, 18], [-54, 18], [-57, -19], [48, -8], [-4, -42], [-7, -21], [12, 10], [35, -23], [2, -44], [36, 26], [56, 0], [54, -4], [-60, 6], [8, -28], [44, 18], [48, -8], [0, 20], [-10, 12], [42, 16], [35, -3], [-30, 32], [-54, 0], [-46, -24], [-42, 8], [8, 52], [-9, 1], [20, -2], [-64, 18], [-22, 0], [-57, -3], [-64, 2], [15, 21], [0, 4], [20, 10], [-30, 14], [-1, 9], [-40, -8], [0, 50], [-34, 26], [-54, -24], [44, -6], [30, -20], [-49, 9], [-15, 15], [-48, 2], [-28, -24], [-28, 30], [-42, 8], [59, 9], [0, 20], [-91, -1], [-6, -40], [-51, 7], [-64, 10], [8, -18], [-42, -24], [0, 40], [17, -45], [12, -30], [45, 23], [-48, 30], [59, -11], [-54, -16], [60, 0], [-19, -33], [-45, -15], [9, -29], [-30, 2], [-28, 20], [0, -26]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3420_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3420_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_3420_2_a_h(:prec:=2)
    chi := MakeCharacter_3420_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_3420_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_3420_2_a_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3420_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-2, 1]>,<11,R![-12, 0, 1]>,<13,R![-2, 2, 1]>],Snew);
    return Vf;
end function;