
// Make newform 9522.2.a.n in Magma, downloaded from the LMFDB on 09 January 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_9522_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_9522_2_a_n();".
// 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_9522_2_a_n();".
// 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_9522_a();"
function MakeCharacter_9522_a()
    N := 9522;
    order := 1;
    char_gens := [2117, 1063];
    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_9522_a_Hecke(Kf)
    return MakeCharacter_9522_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 := [[1], [0], [2], [2], [4], [4], [-1], [7], [0], [4], [6], [2], [-6], [1], [-6], [-8], [9], [4], [-7], [-6], [-9], [-12], [-5], [-6], [-6], [4], [-14], [4], [6], [17], [-2], [3], [-21], [-19], [-20], [22], [-12], [-8], [20], [-6], [-5], [-18], [-8], [17], [6], [14], [-17], [14], [27], [-28], [9], [0], [-11], [-23], [17], [10], [20], [2], [-2], [-15], [-5], [14], [-19], [-8], [-11], [10], [-1], [19], [-5], [-28], [1], [6], [0], [38], [1], [-6], [-30], [20], [15], [25], [9], [-32], [0], [34], [2], [19], [31], [-22], [30], [10], [-16], [20], [28], [0], [25], [0], [-24], [-26], [20], [36], [-12], [38], [-19], [19], [-31], [27], [-41], [9], [-40], [-38], [26], [-4], [21], [4], [2], [-17], [-8], [-20], [6], [51], [12], [25], [6], [-33], [20], [0], [26], [-36], [36], [-20], [-20], [24], [24], [0], [15], [10], [-8], [3], [-2], [-9], [29], [48], [0], [33], [6], [-42], [22], [3], [25], [-52], [48], [30], [21], [-30], [-29], [-38], [26], [-42], [-17], [18], [-9], [-34], [-46], [-13], [18], [6], [-2], [58], [-35], [4], [5], [-50], [-20], [19], [2], [33], [20], [6], [26], [-28], [20], [31], [22], [-30], [16], [0], [-16], [21], [-7], [-16], [59], [-11], [-16], [62], [5], [33], [-34], [-4], [46], [-46], [18], [6], [8], [-10], [-15], [14], [-64], [44], [-15], [-52], [-3], [56], [-62], [-68], [20], [9], [16], [39], [42], [36], [-24], [22], [39], [36], [27], [-54], [-26], [72], [-4], [1], [8], [5], [40], [-47], [64], [6], [-47], [56], [-52], [-32], [-76], [11], [4], [34], [-39], [60], [-28], [-67], [4], [12], [-12], [17], [18], [22], [-54], [11], [-28], [-59], [58], [50], [74], [-15], [-4], [-46], [-21], [-15], [-80], [-26], [40], [-74], [-38], [-37], [54], [-60], [27], [18], [-12], [2], [3], [-15], [-24], [38], [-36], [28], [-69], [-60], [26], [-8], [-4], [57], [4], [-9], [21], [-3], [32], [6], [-74], [66], [27], [12], [21], [-42], [-44], [-71], [-13], [-47], [-51], [-4], [-36], [76], [-16], [-88], [-33], [-43], [-74], [47], [32], [-52], [19], [-19], [-11], [61], [2], [16], [6], [-27], [-56], [13], [80], [76], [-16], [-48], [14], [81], [92], [7], [-88], [-23], [47], [-52], [-56], [59], [40], [-2], [-10], [28], [-70], [39], [30], [58], [0], [37], [30], [-14], [-16], [66], [56], [84], [-58], [44], [-62], [41], [-38], [51], [-59], [3], [-2], [12], [77], [68], [-60], [28], [42], [40], [-66], [-76], [0], [-46], [-45], [-75], [-66], [6], [-62], [57], [20], [46], [100], [-28], [56], [46], [59], [94], [21], [28], [96], [-23], [50], [87], [-37], [2], [4], [-37], [-48], [-74], [38], [-30], [62], [-13], [-73], [-57], [94], [90], [-57], [17], [-33], [48], [6], [32], [-33], [-92], [56], [-24], [72], [-9], [10], [52], [-105], [-83], [63], [-44], [82], [-84], [49], [-84], [22], [3], [7], [10], [-53], [-18], [-84], [-67], [-86], [-78], [110], [93], [68], [-60], [21], [32], [25], [-24], [36], [-49], [-69], [-72], [64], [39], [96], [-35], [85], [58], [60], [8], [28], [22]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9522_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_9522_2_a_n();".
// 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_9522_2_a_n(:prec:=1)
    chi := MakeCharacter_9522_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(3313) - 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_9522_2_a_n();".
// 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_9522_2_a_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9522_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-2, 1]>,<7,R![-2, 1]>,<11,R![-4, 1]>,<29,R![-4, 1]>],Snew);
    return Vf;
end function;