
// Make newform 4320.2.a.z in Magma, downloaded from the LMFDB on 12 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4320_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_4320_2_a_z();".
// 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_4320_2_a_z();".
// 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 := [-7, 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_4320_a();"
function MakeCharacter_4320_a()
    N := 4320;
    order := 1;
    char_gens := [2431, 3781, 2081, 3457];
    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_4320_a_Hecke(Kf)
    return MakeCharacter_4320_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], [-1, 1], [-1, 1], [1, 1], [2, 1], [0, 1], [-1, 2], [3, 1], [0, -1], [0, -2], [3, -1], [1, -3], [0, -2], [4, -3], [-1, -1], [-3, 4], [6, 0], [5, -1], [7, -3], [-10, 1], [-5, 0], [11, -1], [-6, 2], [6, 2], [6, -2], [6, 0], [-3, 2], [4, 4], [-10, -4], [-4, -6], [10, -3], [12, 4], [11, 3], [-12, -2], [-5, -3], [4, -2], [-7, -2], [14, -1], [-2, -2], [-11, -2], [8, -2], [3, 7], [-2, 9], [-10, 6], [18, -1], [-20, 2], [3, 6], [1, -4], [14, 4], [-8, 2], [-17, -2], [-2, 2], [8, -5], [-16, -2], [0, -6], [-12, -1], [1, -1], [9, -1], [29, 1], [16, -5], [2, 0], [1, 5], [22, -2], [10, -3], [2, -2], [-3, 5], [12, 0], [7, -6], [-4, 2], [-18, 4], [-31, -1], [28, 0], [22, -5], [11, 0], [-1, -5], [-5, 11], [-4, 2], [3, 4], [7, 1], [-3, -10], [11, 3], [23, 3], [-6, -11], [1, -14], [19, -7], [-4, -4], [-2, 4], [-4, -2], [11, -10], [11, -7], [3, 7], [-6, 4], [20, -9], [7, -10], [0, 12], [7, -5], [1, 15], [-20, -4], [-17, 3], [-28, -2], [6, 0], [-7, 11], [28, 3], [-3, -13], [13, -4], [8, -13], [3, -9], [-27, 2], [-3, 5], [20, 0], [-18, -9], [2, 6], [2, -7], [-20, -8], [11, 5], [-9, 0], [-12, 3], [41, 1], [-24, 8], [33, 5], [-16, 4], [17, 4], [-26, -5], [-24, 6], [-16, 12], [-42, -4], [-2, -6], [34, -2], [-8, -15], [-8, -8], [10, -3], [-18, -4], [-35, -1], [-5, -8], [-36, -5], [1, 9], [0, -9], [3, 3], [8, -2], [-14, -4], [23, 11], [9, 14], [10, -10], [22, 10], [2, -2], [-12, -9], [44, -3], [29, 4], [-25, 1], [3, 17], [-29, -3], [-7, 12], [-22, -6], [5, -11], [12, 4], [-19, -7], [-28, 4], [-10, 4], [-1, -12], [6, 4], [40, -6], [-4, -10], [-32, 10], [-39, 8], [-14, 9], [-3, 3], [11, -18], [18, 11], [26, 2], [-17, -12], [45, -3], [40, 8], [-10, 5], [-39, 3], [24, 1], [-23, -13], [27, -13], [-8, 0], [-11, 1], [-40, -8], [58, 0], [32, -4], [3, -6], [-20, -4], [23, -13], [-32, 4], [1, 18], [43, 7], [16, -6], [-13, 4], [-20, 11], [18, -18], [43, -8], [-38, -1], [1, 2], [8, -8], [-24, 6], [10, 8], [-19, -7], [-22, -14], [-8, -6], [15, 16], [-37, -5], [-2, -16], [-14, 6], [-5, -10], [-2, -6], [4, 4], [5, 17], [45, 1], [40, 8], [30, 4], [2, -18], [-29, 6], [-9, -5], [-26, 14], [7, -20], [2, -7], [-38, -12], [36, 13], [11, -11], [-11, 1], [27, 0], [19, -2], [50, -7], [-57, 1], [8, 14], [-54, 6], [35, 1], [2, 11], [-8, -24], [-32, 14], [1, -21], [-47, -8], [-19, -2], [-30, 15], [-10, -16], [27, 9], [-34, 0], [22, -13], [0, -14], [1, -2], [20, -18], [52, 2], [32, -12], [-9, 3], [-40, -8], [39, -6], [0, 6], [42, -12], [-49, 4], [-9, -6], [8, 11], [-2, 26], [-17, 18], [55, 1], [0, -3], [-19, -7], [3, -13], [21, 22], [-9, 2], [-2, 14], [4, 11], [-28, -4], [6, 18], [38, 12], [14, 0], [48, 6], [-67, -2], [-44, 6], [27, -7], [-36, 3], [-5, 5], [65, 3], [23, 2], [-13, -14], [-13, -20], [-35, 11], [14, 8], [32, 17], [-5, 0], [21, 6], [-42, -6], [45, -11], [4, 10], [34, 16], [6, 7], [22, 18], [22, -2], [15, 18], [-6, -24], [-26, -20], [-9, 13], [16, -8], [40, 9], [-6, -20], [-48, -12], [-43, -5], [-45, 1], [18, -3], [32, 0], [15, -12], [-12, 11], [7, 9], [-2, -8], [5, 12], [11, 25], [-19, -17], [-31, -4], [-10, -14], [-12, -16], [-69, -7], [-51, 6], [5, 18], [78, -2], [61, -1], [29, -11], [62, 0], [30, -11], [-4, -4], [36, -4], [2, -2], [-4, 17], [24, -16], [-28, 3], [8, 24], [-12, 12], [26, -15], [50, -8], [-26, 20], [16, -14], [-60, -6], [-26, -18], [30, -12], [-27, 8], [22, 21], [30, 10], [-3, -3], [-34, -6], [52, 7], [21, -9], [20, -11], [48, 4], [-33, -19], [33, -14], [6, 26], [24, 24], [56, -3], [-16, -14], [26, -8], [27, -3], [28, -12], [-6, -18], [30, -3], [-29, -25], [1, 5], [16, 2], [-27, -24], [43, -21], [40, 2], [4, 10], [18, 24], [-4, -20], [-5, 23], [30, -9], [-34, 16], [26, 20], [-42, -12], [8, 7], [27, 0], [11, -1], [-40, -9], [18, -14], [56, -10], [44, -10], [5, -13], [20, -10], [0, 22], [0, 12], [-6, 21], [5, 27], [-42, -15], [-46, 10], [-28, 14], [40, 3], [3, -13], [47, -5], [28, -28], [-4, 4], [8, 11], [-32, 2], [20, 2], [-42, 12], [-71, -9], [-8, -11], [41, -21], [-76, 3], [-4, 8], [10, 4], [-12, 18], [-45, 1], [10, 8], [11, 13], [28, -5], [22, 12], [16, -30], [4, 16], [85, 1], [-14, 24], [-58, 11], [55, -2], [-30, 9], [-2, 8], [37, 11], [-74, 4], [24, -16], [-50, -19], [-21, 18], [2, 10], [-12, -2], [-1, 14], [-52, 18], [16, -14], [-64, 11], [-75, 2], [-4, -2], [90, 1], [67, 1]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4320_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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