
// Make newform 3969.2.a.w in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3969_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_3969_2_a_w();".
// 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_3969_2_a_w();".
// 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, 6, -6, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [-3, 0, 1, 0], [1, -5, 0, 1]];
    Rf_basisdens := [1, 1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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_3969_a();"
function MakeCharacter_3969_a()
    N := 3969;
    order := 1;
    char_gens := [2108, 3727];
    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_3969_a_Hecke(Kf)
    return MakeCharacter_3969_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, 1, 0, 0], [0, 0, 0, 0], [-1, 1, 1, 1], [0, 0, 0, 0], [1, 0, 1, 2], [1, 0, 1, 0], [-1, 0, -2, 1], [2, 1, -1, -1], [-3, 1, -1, 0], [-3, 0, 2, 0], [5, -2, 0, -1], [1, -1, -3, -2], [0, 3, 2, 0], [-2, -1, 2, 1], [5, 1, 0, 1], [-2, -3, -1, -1], [-2, -1, 3, -1], [6, -4, 0, 1], [0, -2, -3, -1], [-3, 3, 0, 0], [1, 3, -1, -3], [-1, -3, -3, -3], [-2, 1, -2, -4], [6, -1, -1, -3], [3, -4, 1, 4], [4, -1, -1, 2], [4, 3, 0, 6], [3, 4, -2, 0], [-2, -1, -3, 1], [8, -1, -2, 1], [-6, 4, 5, 2], [-7, -5, 1, -2], [9, -1, 1, 0], [9, -1, -2, 1], [3, 1, 2, -6], [-1, 3, 0, 3], [-4, -5, -4, -1], [0, -2, 6, -1], [1, -2, -4, -1], [3, -4, -6, -3], [-6, -1, 5, 3], [-3, 8, 0, 7], [-1, 3, 0, -2], [5, -6, -3, 0], [7, -2, 3, -1], [8, 4, -3, 2], [-1, -3, -1, 6], [0, 5, 3, -2], [3, -5, 2, 0], [10, -3, 0, -6], [-1, 0, 1, 4], [10, -2, -5, -4], [4, 3, 0, 6], [2, -7, 0, -5], [-8, 6, 1, 2], [7, 3, -5, 2], [14, -3, 4, 1], [9, 5, 3, 7], [-18, -2, -3, 2], [-5, -5, 2, -7], [3, -10, 0, 1], [-9, -6, 5, 3], [10, -9, -8, -9], [1, 5, 0, 2], [6, 5, 2, -2], [1, -1, -6, -4], [12, -2, -2, -7], [-3, 1, 6, 2], [4, -6, 3, -1], [10, 0, -8, -3], [-4, 0, -7, -11], [13, 2, 7, 2], [13, 0, -1, 0], [3, 7, -2, 2], [18, -2, -4, -4], [-3, -4, 1, -9], [6, -10, -5, -9], [5, -11, -3, -4], [12, -11, -7, -3], [13, 3, -3, -3], [11, -4, 2, 4], [2, 6, 3, -3], [-5, 9, -1, 5], [10, 0, 7, 3], [10, 3, 8, -3], [-23, -4, 0, -1], [-3, 3, -3, 3], [-9, 10, 3, 8], [-10, -7, 7, 4], [9, -5, 3, -1], [-5, -3, -1, 8], [-2, -13, -4, 5], [-5, 11, -2, 4], [0, 0, 4, -6], [3, 4, 6, 8], [12, -3, -12, -6], [10, -12, 1, 8], [11, -9, -8, -8], [-6, 2, 3, 4], [0, -2, -5, -4], [11, -3, 2, -6], [2, -5, -4, 7], [21, 1, 0, 3], [-4, 6, -3, 7], [-15, 7, 1, 8], [-6, 2, -9, -5], [-15, -6, 1, -6], [19, 9, 2, 2], [2, -5, -9, -2], [9, -1, 1, -2], [3, -7, -10, -8], [0, -5, 6, 2], [-11, 4, -8, -7], [15, -10, -6, -2], [4, 11, 5, 16], [15, -10, -2, 3], [-10, -5, 1, 2], [-24, 10, 4, 9], [9, -11, -10, -3], [26, 5, 2, -2], [-16, -2, 11, 2], [15, 1, 0, 5], [-13, 13, 15, 4], [-23, 3, -1, -4], [7, 3, 3, 6], [21, -18, -9, -9], [10, 14, 0, 7], [1, 0, 14, 8], [14, -2, 4, 11], [11, -11, 0, -1], [-14, -4, -3, -14], [-2, -2, 4, -4], [10, -10, -5, -11], [19, -1, 5, -5], [18, 8, -11, -3], [-2, 9, 12, 6], [24, -5, 1, -6], [15, -7, -15, -5], [4, 13, -1, 2], [10, -12, -1, -16], [19, 6, -9, -3], [9, -2, 3, -3], [1, 2, 6, 4], [-3, 18, 6, -3], [-21, -7, 2, -2], [-3, -6, -8, -6], [-8, -6, -3, -9], [-4, -6, 2, -17], [-5, 9, 15, 0], [17, -9, -8, -5], [21, 1, -3, -4], [15, 3, 12, -9], [2, -12, -12, 0], [7, -9, -8, 2], [-7, 12, -6, -12], [8, 2, -1, -8], [-22, 18, 12, 3], [-24, -7, -3, 0], [-12, 2, -2, -11], [-10, 0, -3, 7], [3, 4, 18, 3], [20, 14, 0, 4], [14, -6, -1, -12], [-8, 15, 2, -7], [-4, -8, -9, -2], [-3, 10, 11, 9], [-10, 3, 6, 9], [-9, -1, -12, -14], [8, -3, -7, 0], [-5, 18, 1, -1], [-9, -4, 7, 3], [2, 1, 0, 5], [9, -11, 5, -9], [-21, 16, 4, 11], [7, 21, 0, 0], [-15, 6, -4, 0], [20, 6, 2, 6], [8, 7, -1, 4], [24, 2, 4, -2], [6, -13, -12, -14], [15, 4, -5, 8], [-2, 16, 10, 8], [-2, -16, 11, 10], [-16, -5, -13, -11], [2, 4, -18, -8], [1, -7, -7, 8], [-3, -2, 0, 14], [23, 1, 5, -7], [9, -20, -6, 8], [-11, 2, -5, -7], [-4, -2, -1, 5], [8, 2, 20, 16], [-1, 0, -6, -3], [-13, 7, -9, -11], [-25, -2, -4, 4], [-20, 14, -3, -10], [-6, 16, 10, -1], [-5, -4, 18, 4], [-17, 7, 4, -7], [22, -6, 8, -4], [-8, -12, 0, 8], [26, 4, 1, 2], [-25, -2, 3, 8], [-18, -10, 18, 4], [-17, 4, 9, 14], [9, 10, -11, -6], [26, 1, 18, 11], [-27, 1, 2, 6], [-16, -1, 14, 10], [6, -7, -9, -20], [-8, 2, 0, 16], [5, 14, 8, 4], [-4, -3, 8, -9], [-8, 6, 0, -4], [-4, -12, 12, 4], [6, 2, -3, 1], [24, -11, -5, 5], [-10, 12, -13, -17], [42, 1, 1, 6], [7, 10, 13, -10], [23, 3, 3, -9], [19, 12, 4, -12], [14, 6, -19, 1], [3, 22, 4, 20], [39, -18, -7, -15], [-5, -10, -1, -8], [7, -3, -3, 8], [12, 11, -1, 0], [42, 8, -4, 7], [-14, 8, -3, 17], [-5, -7, -20, -17], [26, -5, 2, 2], [1, -1, -25, -14], [-36, 20, 18, 15], [15, 20, 4, 1], [12, 13, 8, -6], [29, 7, 2, -1], [-32, -1, 4, 2], [-14, 10, 1, 23], [-35, 1, 0, -4], [27, -22, -17, -12], [34, 0, 11, 9], [40, -3, 11, 6], [-12, 16, 0, 5], [-40, 8, -1, -8], [25, -15, -3, 11], [1, -6, -8, 12], [29, 6, 12, 7], [10, -7, -2, -14], [12, -3, -9, 3], [19, 2, 2, -11], [-14, 18, 11, 8], [-15, 8, 3, 9], [-20, -3, 4, -12], [-13, 3, -4, 7], [31, 2, -5, -1], [2, 18, 12, -3], [-37, 25, 9, 8], [-21, 21, 6, 15], [-9, 8, -10, 4], [30, 4, 4, 5], [-29, 13, 2, 2], [-2, 24, 3, -3], [-7, -11, 22, 8], [-28, 21, 15, 13], [10, 6, 12, -12], [-9, -12, 3, 9], [18, 6, -8, 9], [15, -5, 15, 11], [33, -1, 6, -9], [-7, 19, -4, 26], [-7, -27, -3, -6], [9, 17, -6, 10], [2, -21, 0, 6], [-15, -13, 18, -2], [-20, -9, 6, 3], [-5, -22, 2, -13], [-5, 2, 15, 16], [27, 7, 9, 8], [6, -25, -9, -18], [-19, 12, 15, 16], [-8, -1, -6, 4], [26, 11, -2, -2], [1, 24, 16, -6], [1, -3, -15, 0], [26, -18, -8, -23], [12, 19, 6, -4], [10, 7, -11, -13], [25, -3, -12, 0], [27, -15, -3, -15], [7, 3, 19, -1], [-8, -7, 1, -16], [12, -8, 23, 18], [5, 29, -7, 1], [-49, 21, 8, 0], [-48, 1, -9, 6], [15, -16, -24, -23], [14, -28, -5, -23], [-13, -23, 5, 4], [41, 4, -8, 5], [-4, 10, 9, -4], [-46, 6, -2, 1], [31, -1, 7, -5], [-31, 11, -8, 4], [-2, 2, -9, 19], [-39, 16, 2, 8], [-45, 23, 7, 12], [29, 10, -21, -4], [-14, -10, 8, -4], [-8, 5, 13, 7], [50, 1, -6, -11], [9, 5, 8, 18], [46, 8, 6, 14], [46, -1, 6, 4], [-25, -7, -9, -29], [-14, -9, 15, 0], [7, -14, -5, 5], [-7, -11, 15, -11], [-41, 18, 10, 3], [-32, 1, -14, -10], [24, 5, -7, 4], [4, -7, 1, -5], [6, 9, 21, 12], [4, 26, 14, -11], [-33, 23, 19, 6], [3, 17, -15, -8], [9, -14, -15, -7], [-20, 3, 0, 3], [12, -29, 0, 6], [-9, -6, -6, -30], [42, -17, -6, -13], [-19, 16, 21, 10], [37, -30, -21, -18], [33, -11, -10, 3], [18, 16, -9, -7], [-46, 14, 9, 1], [10, 11, -19, 4], [-45, -7, 16, 9], [-7, 1, -11, -7], [18, -13, -3, -11], [24, -8, -33, -13], [13, -2, -13, -7], [-40, -4, -19, 16], [-33, -2, -12, -19], [12, -14, 6, 24], [-2, -26, 6, -4], [-15, -1, 3, -14], [-20, 11, 16, 10], [-2, -2, -8, 14], [-34, -26, 2, -2], [-9, -1, -3, 19], [-11, -28, 6, -8], [2, 2, -14, -8], [34, 18, 5, 6], [-6, -14, 3, -10], [28, 19, 0, 8], [-54, 5, -11, -21], [-16, 12, -14, -8], [-10, -9, 8, 22], [8, -1, -10, -26], [12, -32, -1, -1], [37, 12, 0, -4], [28, -15, -22, -7], [-18, 13, 12, -6], [-20, 21, 0, 15], [0, -17, -6, 17], [-4, 5, 7, -17], [21, 1, -9, -13], [-9, -5, 2, 14], [-9, -10, -26, 3], [-43, -8, -21, 17], [-36, -2, -16, -27], [-28, 2, 5, 16], [27, -7, -1, 7], [21, -5, -9, 14], [48, -5, -3, 15], [-26, -8, -6, -1], [-23, 15, 6, 30], [10, 24, -10, -13], [-25, 7, 18, 20], [37, 2, 8, 14], [-14, -8, 7, 5], [8, -6, 2, 24], [5, -11, 7, 16], [-18, 14, 19, 19], [-15, 22, 10, 27], [32, 3, 15, -21], [-21, 20, 9, 16], [-3, 1, -2, 8], [26, 11, 2, -20], [-16, -21, -4, -18], [-4, 22, -3, -5], [-26, -36, 2, -1], [-29, 0, 3, -18], [36, 13, -12, 0], [30, 4, -15, -4], [63, -10, -3, 1], [49, -2, 13, 23], [18, 4, 26, 26], [-13, -20, -9, -20], [-45, -7, -10, -14], [19, 14, -2, 11], [13, 14, -20, -5], [30, -16, -2, -9], [5, -15, -8, 10], [21, 17, -15, -11], [-11, -13, -3, -23], [22, -2, -10, -4], [-8, -15, 0, 3], [-19, 13, 6, 7], [-21, -1, 20, -2], [-12, -11, 6, -3], [20, 32, -12, 10], [-15, -5, -9, -1], [18, -26, -16, -1], [-32, 15, 5, 8], [8, 27, 7, -17], [21, -1, 8, -11], [21, -27, 6, -3], [-18, -7, -2, 12], [-39, -1, -2, -3], [-18, 2, 15, 1], [1, -20, 11, 8], [0, -12, 8, -9], [-44, 21, 6, 6], [-11, -16, 3, -13], [29, -9, -33, -8], [15, 27, -12, -3], [4, 9, 26, 12], [-3, -14, 23, 9]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3969_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3969_2_a_w();".
// 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_3969_2_a_w(:prec:=4)
    chi := MakeCharacter_3969_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_3969_2_a_w();".
// 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_3969_2_a_w( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3969_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![3, 6, -6, -1, 1]>,<5,R![-21, -33, -12, 2, 1]>,<11,R![-249, 174, -24, -5, 1]>,<13,R![-7, 20, 0, -5, 1]>],Snew);
    return Vf;
end function;