
// Make newform 3229.2.a.b in Magma, downloaded from the LMFDB on 05 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3229_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_3229_2_a_b();".
// 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_3229_2_a_b();".
// 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 := [-1, -1, 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_3229_a();"
function MakeCharacter_3229_a()
    N := 3229;
    order := 1;
    char_gens := [6];
    v := [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_3229_a_Hecke(Kf)
    return MakeCharacter_3229_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], [-2, 0], [1, -1], [-1, 0], [2, 2], [-3, -1], [4, -2], [1, -3], [-2, 4], [7, -1], [-9, 1], [5, -5], [7, -5], [-3, 5], [-1, 2], [-5, 4], [1, -2], [2, 0], [9, -6], [3, 6], [-5, 12], [-12, 6], [8, -1], [-10, 11], [1, -3], [10, -11], [1, 5], [1, 4], [-3, 6], [3, -4], [-1, 10], [6, -6], [-5, -5], [-18, 0], [6, 3], [-21, 1], [5, 0], [-8, 9], [-16, 5], [-9, 6], [8, -7], [-9, 6], [-6, 6], [0, -9], [13, -14], [15, -10], [-17, 9], [-6, 6], [-14, 5], [-16, 0], [3, 16], [-19, 4], [-8, -12], [-16, 4], [9, -4], [-9, -4], [3, 15], [2, -15], [0, -14], [9, 3], [23, -9], [21, -4], [-12, 3], [19, -19], [3, 10], [8, 2], [-9, 5], [-6, -4], [-12, -3], [-34, 0], [-11, 4], [15, -27], [-16, 4], [11, -2], [-21, 10], [27, -12], [2, 4], [-9, -15], [7, 2], [18, -2], [0, -15], [-23, -6], [16, -25], [-25, 16], [-21, 3], [3, -15], [-8, 16], [-21, 10], [21, -17], [-2, 12], [4, -16], [9, 9], [24, -21], [2, 22], [-21, 4], [-13, 13], [16, -19], [32, 1], [0, -12], [11, 10], [-25, 30], [21, -21], [-5, -11], [-5, 10], [-9, 6], [22, -10], [-16, 10], [9, -4], [24, 11], [15, -9], [-7, -24], [43, -3], [-17, -2], [-47, 2], [31, -22], [20, 5], [15, -4], [-12, 11], [-27, 8], [4, 20], [-9, -2], [-21, -3], [27, 0], [20, -10], [21, -24], [-14, 14], [-24, 12], [-28, 16], [5, 16], [27, -42], [21, -20], [-23, 34], [-15, 3], [23, -9], [-42, 13], [13, -18], [-6, -1], [-48, 5], [10, -10], [0, -24], [-41, 18], [-6, 28], [18, -46], [6, -14], [29, -29], [-21, 0], [-5, 24], [-33, 0], [-31, 12], [27, -12], [2, 4], [0, 21], [-3, -10], [17, -16], [-34, 0], [5, -2], [-30, 17], [5, -22], [-21, 18], [-15, 24], [-3, -5], [-26, 8], [-37, 31], [-31, -4], [-45, 27], [6, 16], [-13, 30], [21, -12], [11, 30], [-32, 46], [-43, -7], [-14, 38], [-7, -23], [-45, 17], [-39, 4], [-19, 8], [-43, 31], [15, -42], [-33, 2], [0, -30], [25, 5], [-15, 10], [29, -41], [-26, 41], [-1, 20], [16, 13], [29, -24], [35, -20], [-4, 6], [18, -39], [-45, 28], [-19, -26], [19, -24], [-21, -18], [23, -22], [-6, -27], [16, -24], [23, 22], [-6, -32], [3, -12], [29, -8], [-33, -9], [17, -3], [2, 27], [30, 0], [-36, -4], [28, 14], [9, -48], [17, 20], [2, -15], [-8, 29], [-21, 13], [-5, -12], [30, 16], [-23, -8], [11, -27], [-6, 18], [24, 11], [16, 28], [51, -23], [-11, 23], [-21, -5], [-49, 34], [8, -35], [18, -30], [8, 9], [1, 25], [33, -21], [32, 10], [-17, -28], [27, -1], [-10, -18], [33, 9], [50, -16], [-15, 31], [-52, -2], [23, -33], [-19, 46], [-59, 20], [-1, 16], [-27, -9], [34, -51], [40, -10], [31, -22], [-29, 55], [-27, -14], [51, -20], [-51, 8], [-48, 24], [13, 38], [-42, 24], [-51, 22], [35, -22], [54, -40], [28, -20], [-3, -18], [-20, -6], [-63, 15], [-53, -2], [35, 6], [23, -35], [45, -16], [-23, -1], [-30, 12], [13, -56], [9, -12], [46, -5], [20, -10], [17, -20], [-60, 18], [10, -48], [25, -9], [17, 0], [-12, 21], [41, -42], [24, 29], [-24, -21], [-46, 19], [-33, -10], [-36, 59], [-24, 51], [-22, -2], [-2, 35], [-27, 8], [9, 26], [-11, -32], [-8, -39], [51, -27], [-8, 36], [-15, 3], [23, -22], [76, 4], [45, -43], [-28, 35], [44, -6], [-48, 7], [-18, 30], [-54, 36], [33, -45], [3, -37], [1, 39], [-3, -6], [29, 21], [23, 23], [-19, -12], [-5, 36], [-24, 12], [33, -48], [49, 19], [10, -40], [5, 14], [64, 1], [-28, 43], [-22, 60], [-28, -10], [-36, 45], [29, -35], [46, -20], [26, -24], [30, 19], [42, -33], [35, -20], [-39, 50], [-3, 32], [30, -6], [46, 8], [6, 47], [-18, -19], [-15, 19], [20, -19], [78, -13], [-4, -16], [27, -24], [40, 13], [9, -21], [-8, -10], [3, -57], [45, -18], [-3, 27], [-9, -8], [20, -24], [-44, -19], [9, -18], [48, 20], [37, -62], [-4, -56], [-24, -3], [17, 9], [-45, 57], [-17, -25], [41, -12], [-42, 42], [-3, 52], [-21, -24], [32, -18], [27, -6], [-37, 34], [23, 8], [-46, 14], [68, -8], [-38, 32], [-2, 1], [16, 11], [59, -52], [-47, 39], [0, -53], [-69, 12], [31, -57], [85, 6], [-68, 25], [-4, -42], [18, -44], [32, 13], [-23, 33], [-59, 39], [-22, 29], [-11, -44], [-14, 48], [-45, 73], [-6, -22], [27, 38], [39, -82], [21, 36], [-23, -23], [9, -18], [-64, 56], [35, -30], [-26, 27], [55, -15], [17, -1], [-11, 45], [47, 4], [69, -21], [-17, -15], [-11, 43], [82, -27], [-7, 66], [-32, 1], [34, 23], [59, -11], [-78, 2], [-27, 3], [-37, 27], [-12, 2], [10, -29], [-36, -8], [-36, 66], [27, 9], [-15, 51], [52, -1], [58, -30], [9, 51], [40, -47], [-31, 28], [66, -24], [-5, 10], [61, -5], [-9, -57], [77, -31], [60, 1], [27, -54], [-14, -24], [89, -11], [45, -63], [3, -12], [14, -4], [68, -29], [-69, 32], [74, -18], [-14, 11]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3229_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3229_2_a_b();".
// 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_3229_2_a_b(:prec:=2)
    chi := MakeCharacter_3229_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_3229_2_a_b();".
// 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_3229_2_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3229_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-1, 1, 1]>],Snew);
    return Vf;
end function;