
// Make newform 9702.2.a.da in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_9702_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_9702_2_a_da();".
// 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_9702_2_a_da();".
// 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 := [-2, 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_9702_a();"
function MakeCharacter_9702_a()
    N := 9702;
    order := 1;
    char_gens := [4313, 199, 5293];
    v := [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_9702_a_Hecke(Kf)
    return MakeCharacter_9702_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], [-1, 1], [0, 0], [1, 0], [4, -2], [5, -2], [2, -2], [1, -3], [0, 2], [-2, 6], [4, 4], [1, -4], [0, -2], [5, 3], [-8, 2], [-2, 4], [3, -1], [-5, -6], [2, 8], [2, 2], [9, -3], [7, 2], [-4, -6], [13, -2], [0, 10], [12, -2], [-11, -4], [-1, 3], [-2, 4], [-3, 3], [-4, -8], [-2, -2], [6, 0], [0, 8], [-3, 3], [12, -2], [3, 0], [8, 6], [-2, 10], [-6, 2], [8, -4], [2, -8], [-12, 2], [-12, 6], [14, -4], [12, 2], [2, 8], [-1, -8], [0, 8], [-3, 8], [20, 0], [-8, -12], [-12, -10], [-28, 2], [-2, 8], [1, 5], [2, 8], [-4, -2], [11, -6], [16, -10], [-10, -2], [12, 16], [11, -15], [18, 8], [-7, -19], [-5, 8], [18, -6], [19, -10], [3, 9], [14, 2], [0, -6], [-14, 6], [-3, -9], [25, -4], [6, 20], [11, -9], [-4, -6], [4, -14], [-14, 18], [6, 14], [12, 2], [-22, 2], [21, 4], [15, -13], [20, -4], [-8, 22], [12, 16], [18, 4], [-8, 22], [-14, 12], [-8, 2], [-10, -8], [7, -16], [20, -4], [8, -12], [-28, 2], [-18, -8], [6, 18], [-21, 11], [-22, -10], [4, -6], [-20, -4], [-22, 0], [-16, -4], [9, -10], [14, 10], [-6, -24], [5, 17], [20, 0], [-25, -15], [-21, -7], [-16, -6], [-9, 6], [-18, 12], [-16, -8], [-12, -4], [-13, -21], [9, 25], [-3, -24], [22, 12], [2, 16], [-6, -26], [12, 4], [-15, -10], [14, 10], [18, -10], [-5, 15], [24, 6], [19, -3], [-34, 0], [-8, -4], [10, 14], [14, 4], [1, -18], [-18, -6], [-27, -3], [2, 26], [-43, -7], [-15, 4], [2, 4], [30, -6], [-30, 6], [5, -2], [28, 0], [13, -15], [19, 3], [-33, 8], [17, 24], [-1, -27], [9, 11], [-26, 0], [9, -6], [-14, 4], [25, -10], [29, 15], [-13, -7], [18, -18], [-6, -4], [-34, 16], [-10, 16], [5, -8], [13, -7], [10, 0], [-2, 24], [-41, 3], [28, -4], [-28, -14], [36, 10], [-28, -6], [-23, -18], [18, -18], [-10, -32], [22, 2], [8, -34], [-24, -18], [14, -16], [12, 6], [-11, 1], [-32, 22], [6, 14], [43, 2], [-22, 6], [-15, 28], [-6, -4], [-12, -2], [-15, 25], [13, -6], [12, -36], [-8, -4], [-14, -32], [5, 30], [-51, 0], [4, -10], [-12, -12], [-34, 0], [0, 4], [-14, -34], [-26, -8], [6, 14], [18, -12], [-38, 8], [14, 22], [4, -12], [-12, 8], [4, -26], [-2, 2], [-7, 22], [17, -18], [28, 16], [4, 8], [-33, -9], [-36, -18], [-22, -22], [24, -12], [10, -8], [18, 16], [43, 10], [49, -13], [-24, -10], [-12, 34], [-33, -3], [-50, -6], [-48, 16], [19, 36], [-11, 31], [24, 2], [-9, -31], [63, 3], [4, 8], [-12, 16], [-20, -38], [-6, -8], [41, 6], [-15, -36], [52, 10], [6, 28], [-4, -14], [-34, 6], [-63, 7], [-22, -12], [-66, -8], [14, -6], [-24, 26], [10, -8], [18, -2], [-10, 28], [-48, 8], [28, 22], [24, 24], [-36, -22], [38, 12], [7, -9], [-35, -26], [-18, -28], [20, 28], [30, -4], [-2, 20], [-20, -26], [-32, 2], [35, -11], [8, -4], [-1, -23], [13, -43], [-2, -10], [-20, -12], [-73, -3], [26, 8], [6, -6], [-10, -4], [32, -26], [-23, 6], [62, 8], [-15, -11], [8, 30], [-16, -20], [-14, 34], [-65, 1], [-36, -26], [-39, 32], [68, -2], [56, 6], [18, -18], [-33, -3], [10, -46], [-7, -37], [-18, 4], [60, -6], [-28, 18], [10, 0], [35, -17], [-48, -22], [-9, -38], [39, -20], [19, 23], [26, -42], [-14, -8], [-47, 21], [9, 2], [-2, -8], [-32, -18], [-56, -22], [-18, -44], [-2, -8], [-23, -22], [-17, -4], [-54, -4], [-32, -18], [78, 8], [1, -1], [50, 8], [16, 30], [-61, -8], [43, -24], [30, -8], [-68, -2], [60, -4], [-4, 56], [25, -16], [-6, 56], [-24, -4], [17, 6], [-30, 28], [34, 26], [18, 48], [-18, 4], [65, 2], [-47, 24], [-26, 22], [-30, 34], [-21, 1], [-8, -6], [-30, -36], [45, 12], [-6, 8], [36, 2], [28, -16], [-6, 16], [-18, -20], [-66, 4], [18, 38], [-16, -42], [-52, -20], [2, -8], [-76, 2], [-44, 20], [-26, 28], [12, 24], [82, -2], [17, -23], [10, 50], [-21, -8], [-7, -45], [15, 9], [-4, 30], [-18, 24], [71, -3], [-7, 48], [-78, 12], [43, -7], [-68, 10], [17, 22], [-30, -32], [-15, 24], [-12, -16], [-31, 8], [-28, 6], [46, -24], [60, 24], [52, -26], [-25, 16], [-34, -24], [-30, 6], [-2, 0], [-40, -12], [-8, 34], [-16, 16], [58, -10], [15, -28], [-14, 38], [-41, -15], [-36, 8], [57, 7], [14, 56], [10, 8], [45, -21], [-12, -18], [-6, -30], [-66, -8], [-15, 37], [-3, 6], [-3, -9], [4, 24], [-21, -16], [-17, -1], [-16, 50], [-54, 4], [54, 6], [-44, -24], [40, 12], [-16, 42], [10, -2], [-2, -16], [-14, -10], [12, -4], [29, 3], [-36, 46], [-33, -44], [23, -34], [30, -46], [44, 20], [10, -44], [-26, -36], [-28, -48], [-16, 12], [49, 11], [-32, 18], [-20, 2], [-58, -2], [48, 24], [37, 41], [-55, -9], [62, 0], [-4, 36], [-22, -20], [-41, -15], [82, 2], [22, 0], [11, -32], [64, 14], [21, -40], [9, -2], [11, 10], [57, 21], [-30, -4], [-38, 8], [-66, 22], [-46, -18], [27, -12], [-73, 19], [0, -22], [22, 42], [-52, -18], [-50, -6], [24, 8], [9, 48], [56, 20], [-14, 46], [19, 6], [-45, -23], [-28, 34], [-14, 28], [-49, 16], [53, 30], [-19, 8], [-92, 6], [39, 19], [45, -22], [33, 49], [-58, -34], [-36, -12], [2, 70], [-28, -32], [-46, 18], [-32, 0], [6, 2], [1, -11], [8, 54], [13, -50], [-75, 6], [55, 11], [-12, -18], [-6, -12], [-78, 10], [-32, 32], [45, -1], [-33, 53], [60, 20], [58, -30], [-1, 7], [27, -26], [-41, 42], [6, -32], [-108, 6], [16, 28], [-27, -24], [74, 16], [36, 10], [61, 6], [7, 63], [29, 25], [-54, 4], [-25, -22], [62, 32], [-4, -48], [3, 39], [-43, 2], [-17, 45], [54, -16], [-16, 36], [-24, 40], [87, -9], [7, -26], [19, 1], [12, 44], [-62, -16], [55, -31], [38, 40], [25, -41], [-18, 48], [31, -6], [72, 12], [-30, -58], [59, 13], [4, -48], [-34, -4], [-16, 18], [-19, 15], [19, -47], [-30, -42], [-40, 36], [12, 32], [15, 22], [43, 13], [26, 8], [35, 0], [37, 34], [-4, 54], [7, 20], [-20, -26], [66, 16], [68, -2], [-73, -33], [74, -8], [12, -52], [-20, 24], [10, -46], [-12, -26], [-24, -54], [68, 24], [16, -8], [44, -2], [100, -8], [-45, 22], [-10, -52], [-17, -8], [40, 46], [28, 4], [37, 23], [-60, 14], [85, 4], [36, 58], [-90, 4], [71, 21], [-58, -24], [20, 50], [52, -16]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9702_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_9702_2_a_da();".
// 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_9702_2_a_da(:prec:=2)
    chi := MakeCharacter_9702_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(4027) - 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_9702_2_a_da();".
// 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_9702_2_a_da( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9702_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-1, 2, 1]>,<13,R![8, -8, 1]>,<17,R![17, -10, 1]>,<19,R![-4, -4, 1]>,<23,R![-17, -2, 1]>,<29,R![-8, 0, 1]>],Snew);
    return Vf;
end function;