
// Make newform 9520.2.a.g in Magma, downloaded from the LMFDB on 13 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_9520_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_9520_2_a_g();".
// 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_9520_2_a_g();".
// 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_9520_a();"
function MakeCharacter_9520_a()
    N := 9520;
    order := 1;
    char_gens := [5951, 7141, 7617, 1361, 8401];
    v := [1, 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_9520_a_Hecke( : Kf := Kf)
    return MakeCharacter_9520_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]];
    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], [-1], [1], [-1], [-6], [-7], [1], [-5], [-6], [3], [-5], [2], [-6], [-8], [-3], [-3], [3], [-1], [-8], [-15], [-13], [4], [6], [-15], [17], [12], [-8], [12], [5], [-21], [1], [0], [-12], [-8], [6], [-8], [14], [16], [-12], [12], [12], [-10], [-12], [2], [-24], [-11], [4], [19], [21], [-28], [-21], [24], [-28], [-12], [-12], [-9], [-9], [-2], [-22], [15], [25], [-9], [-8], [0], [14], [18], [19], [5], [-15], [-28], [-18], [-12], [22], [2], [-32], [15], [-18], [8], [0], [-37], [24], [-10], [24], [14], [-8], [24], [-18], [26], [-6], [-29], [6], [-27], [-14], [27], [-2], [0], [6], [0], [-14], [26], [1], [-33], [-42], [27], [-44], [8], [-36], [-6], [-6], [-10], [34], [-25], [15], [-20], [-38], [-42], [16], [39], [36], [-33], [32], [-19], [-12], [-27], [-2], [-18], [-1], [3], [13], [26], [31], [18], [-41], [11], [-6], [23], [30], [31], [-6], [0], [16], [-15], [-14], [12], [-10], [21], [2], [-51], [-5], [24], [32], [18], [-44], [12], [7], [36], [-26], [-30], [8], [-27], [-33], [-54], [-20], [27], [-30], [54], [49], [-40], [-46], [54], [-3], [-58], [-39], [44], [-8], [6], [-20], [27], [-47], [-28], [-17], [-12], [26], [18], [-12], [60], [-52], [-8], [-22], [-36], [14], [-57], [-47], [-6], [27], [42], [-58], [62], [3], [39], [30], [31], [50], [-1], [-12], [30], [-20], [24], [30], [13], [-1], [-30], [-62], [-42], [39], [20], [13], [-69], [-30], [3], [-16], [-71], [-30], [-74], [-12], [-28], [9], [-3], [-47], [0], [35], [-2], [28], [-3], [28], [-60], [8], [42], [-30], [-54], [39], [40], [16], [-34], [-21], [45], [-26], [-24], [-68], [-39], [-37], [18], [57], [-52], [69], [-21], [11], [-32], [36], [20], [40], [-12], [-22], [44], [21], [28], [-54], [-27], [31], [-42], [47], [-38], [20], [28], [32], [-11], [42], [56], [-25], [-9], [24], [-23], [48], [2], [-32], [-30], [-61], [24], [46], [-39], [-69], [15], [-12], [18], [2], [87], [52], [-39], [66], [46], [14], [-87], [43], [15], [-32], [-22], [-66], [23], [-6], [-19], [60], [-6], [-42], [-50], [9], [65], [-39], [-24], [-82], [51], [10], [23], [-24], [-80], [-21], [-1], [-80], [-11], [78], [-36], [14], [78], [-8], [-90], [-2], [-36], [2], [30], [-64], [-56], [-25], [-24], [21], [-68], [-18], [-18], [-22], [28], [39], [-78], [4], [-70], [-30], [-8], [56], [0], [24], [60], [-30], [93], [-82], [36], [-24], [-84], [28], [-10], [12], [-53], [38], [-9], [46], [96], [42], [10], [8], [-48], [51], [56], [-63], [-28], [51], [48], [52], [45], [40], [6], [58], [-58], [31], [-48], [98], [36], [57], [10], [-60], [-58], [-20], [-54], [-74], [66], [14], [54], [-23], [-33], [30], [-8], [71], [-90], [-14], [-54], [71], [-9], [-12], [-44], [14], [-51], [72], [-38], [51], [63], [-96], [86], [42], [69], [-61], [3], [-36], [6], [7], [-12], [-73], [15], [82], [54], [56], [-63], [-88], [-28], [4], [-2], [-84], [-6], [2], [-30], [-82], [-60], [-104], [48], [-16], [-46], [-68], [-9], [-9], [105], [38], [-6], [-22], [27], [-22], [-42], [-20], [37], [45], [2], [76], [-88], [22], [-108], [96], [91], [34], [-42], [-24], [104], [96], [-64], [21], [-98], [90], [57], [38], [-6]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9520_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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