
// Make newform 4332.2.a.n in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4332_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_4332_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_4332_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
        poly := [-1, -3, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, 0, 1]];
    Rf_basisdens := [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_4332_a();"
function MakeCharacter_4332_a()
    N := 4332;
    order := 1;
    char_gens := [2167, 1445, 3973];
    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_4332_a_Hecke(Kf)
    return MakeCharacter_4332_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, 0, 0], [-1, 0, 0], [1, -1, 0], [-1, 1, -2], [-1, -2, 3], [3, -2, 0], [-1, 3, -1], [0, 0, 0], [-4, 2, 1], [-1, 4, 0], [-2, 3, -3], [2, 2, -3], [6, 0, 3], [-7, 2, 2], [-5, 0, 1], [-2, -5, 2], [-3, -1, 5], [5, 2, -1], [2, 0, -2], [-3, -6, 6], [-2, -1, 4], [9, -2, 0], [-5, -4, -3], [-4, -7, 1], [-2, 2, 5], [-7, 2, -2], [5, 6, -5], [8, 6, -4], [1, 4, 1], [-4, 6, -10], [-11, -4, -1], [-4, 1, -9], [-1, 6, -10], [-7, 3, -6], [14, -5, 6], [1, 7, 4], [-1, -13, 8], [-17, 3, -4], [-6, -8, 4], [-2, 5, -6], [-10, -7, 10], [-11, 2, 5], [-10, -1, -5], [8, -6, 13], [-5, 4, -16], [-12, -6, 1], [-6, -2, 12], [1, 2, -13], [6, 8, -10], [1, 8, 4], [1, -12, 7], [-5, -14, 14], [-2, 8, -4], [10, 12, -5], [2, 0, 8], [-4, 12, 2], [-4, -2, 12], [-4, 3, -9], [11, 1, -2], [-17, 3, -11], [-4, -2, -5], [-5, 5, -21], [1, -15, 0], [-1, -1, -2], [-1, -3, 9], [-17, -2, -1], [12, -13, 4], [10, 6, 3], [-6, -1, -7], [-14, 0, 8], [-12, -5, 7], [-11, -10, 15], [-7, -14, 10], [10, -12, 6], [1, 2, -13], [-15, 5, -1], [28, 0, -2], [-1, 5, 8], [1, -6, -17], [13, -12, 3], [-20, -17, 11], [4, 15, -9], [10, -10, 0], [-14, -1, -1], [6, 3, 11], [-5, 9, 4], [-5, 9, -14], [1, 14, -11], [12, 6, 12], [12, -8, 11], [-3, -4, 2], [-9, 15, 3], [-5, -4, 5], [-5, 19, -22], [9, -1, -3], [-4, 7, 6], [-8, -15, 13], [-23, -2, -1], [7, 10, -20], [-17, 1, -6], [25, -2, 1], [-14, 16, 2], [-1, 7, 6], [-31, -6, 2], [7, 3, -13], [23, -1, -4], [-1, 8, -17], [-2, -24, 7], [-3, 13, -23], [13, -17, 13], [-14, 5, 2], [29, 13, -8], [-3, 22, -5], [-13, 3, -3], [-14, -19, 1], [9, -10, 2], [-6, -17, 8], [1, -29, 2], [-11, -1, -15], [14, -4, 4], [25, -1, 14], [9, 11, -11], [-14, 19, 5], [-14, -25, 15], [5, 8, 8], [11, 4, 3], [33, 2, 0], [42, -4, -1], [-24, 11, -3], [20, -4, -13], [16, -8, 12], [6, -15, -6], [14, 2, -15], [-13, -13, 5], [-3, 17, -25], [21, -6, 19], [-10, 9, 11], [13, 11, -13], [32, -8, -6], [16, 11, -15], [16, 22, -23], [6, 0, -18], [-4, -11, 10], [4, 19, -4], [-3, 20, -23], [-8, 14, 3], [4, 19, -6], [-11, 15, -23], [7, -11, 33], [-1, 9, 8], [-16, -17, 23], [20, 0, 11], [-6, -13, -6], [31, -1, 12], [4, -16, 2], [0, 0, -9], [3, 12, 19], [34, 6, 1], [-23, 0, -10], [20, 14, -11], [-8, -20, 5], [-21, 18, -3], [-11, 7, -12], [1, 3, -26], [-10, -5, -3], [-19, -13, -2], [-29, -18, 18], [18, 20, 0], [4, 15, -12], [23, -8, 30], [15, -18, 15], [9, 22, -6], [19, -15, 4], [-14, 13, -6], [9, -4, 13], [3, -15, 9], [2, 27, -6], [5, 1, -15], [-3, -25, 10], [13, 5, -23], [-31, -8, 1], [-17, 25, 2], [-30, -18, 2], [-9, 3, 9], [6, 30, -15], [5, 17, -8], [-17, 7, -11], [-1, -14, 8], [-2, 0, 3], [-11, -3, 13], [12, 29, -2], [-31, 13, -6], [17, -17, 29], [-5, -21, 13], [18, 23, -19], [-11, 21, -38], [-1, -3, 3], [24, -26, -13], [10, -29, 2], [-21, -20, 10], [23, -24, -10], [10, 38, -25], [-15, -41, 21], [43, -3, 9], [2, 8, -11], [2, 7, -15], [29, -15, -12], [1, -27, -8], [3, -16, 11], [26, -16, 15], [19, 2, 13], [-40, -4, 7], [-11, -9, -7], [19, -7, 9], [-14, -20, 11], [32, -1, -3], [-4, -17, 1], [1, -5, -1], [23, 25, -18], [-10, 27, -1], [3, -6, -1], [0, -4, -2], [-17, 6, 4], [-8, -3, -7], [15, 6, 0], [33, 9, -8], [-13, -27, 20], [16, 8, -6], [19, 2, 8], [-13, -25, 31], [8, -16, 14], [-49, 1, -10], [-8, 4, 1], [-9, -38, 25], [5, -29, 43], [-17, 21, -35], [-18, 23, -6], [-25, 0, -10], [-33, -10, 2], [9, -20, -17], [-21, 10, -11], [14, 9, -18], [-1, 4, -2], [-46, 4, -4], [-25, 13, -6], [15, 18, -18], [5, -38, 7], [13, 2, 6], [37, 27, -21], [6, -23, 10], [0, 24, -17], [-13, -11, 33], [-18, -25, 14], [-17, -41, 13], [-10, 5, 28], [-11, 6, 13], [-32, 2, -8], [8, 28, -10], [-27, -17, 4], [-5, 13, -6], [24, -14, 48], [-3, 25, -29], [19, 19, -3], [-5, -29, 31], [6, -4, -1], [26, -12, -14], [-17, 37, -4], [-41, -7, 3], [1, 25, -29], [12, -18, 9], [-17, 29, 11], [1, -11, -5], [-16, -15, -24], [-19, 32, 5], [23, -27, 28], [-8, -10, 19], [5, 16, -12], [14, -13, 3], [-1, -31, 33], [-20, -8, -25], [21, 11, 14], [-10, 34, -32], [26, 3, 20], [59, 1, -1], [-7, -26, 43], [-5, -17, -13], [-9, -32, 35], [-21, -11, 7], [35, 25, -14], [27, 19, -32], [5, 3, 17], [-7, 17, -8], [3, -3, -3], [-47, -4, -12], [-21, -12, -25], [33, -17, -26], [19, -33, 0], [7, -39, 13], [38, -10, -14], [-27, -35, 38], [-31, -2, -8], [4, 11, 30], [41, 1, 25], [1, 29, 15], [5, 23, -16], [14, 0, 1], [20, -20, -24], [1, -37, 23], [28, -28, -27], [-30, 20, -24], [-15, -27, 3], [31, -5, 35], [-17, -1, 0], [-15, 35, -11], [-37, -23, 24], [-5, -2, -50], [-30, 28, 4], [-5, -34, 39], [-40, 3, -21], [-9, 13, -23], [46, 9, 3], [-45, -7, -15], [-21, 19, -35], [-11, -38, 28], [-51, 4, 10], [7, -14, 10], [-15, -40, 37], [-33, 30, 17], [62, 20, -5], [-26, 6, -47], [-10, -32, 10], [-14, 26, -15], [14, 7, -14], [-21, 37, 7], [-22, -18, 12], [44, 24, -28], [41, -25, 21], [10, 22, -31], [-40, -5, 16], [3, 0, -26], [-5, -10, -10], [-45, -19, 2], [-53, -3, 19], [25, 22, -20], [-13, 19, -12], [11, 38, 4], [-34, 27, 18], [4, -42, 36], [3, 20, 29], [15, 6, 36], [-13, -16, -27], [-33, -41, 24], [-62, -1, 15], [-5, -16, -28], [28, 5, 11], [9, 12, -6], [-19, 5, -32], [-7, 19, -15], [-64, -11, 0], [23, -8, -33], [8, 10, -17], [-15, -9, -12], [23, 30, -10], [32, 0, 17], [17, 51, -15], [-11, 26, 20], [-25, 3, 2], [-21, 12, 35], [3, -50, 33], [28, -4, -30], [4, -48, 12], [2, -32, 18], [31, -23, -4], [24, 29, -39], [36, -15, 4], [2, 15, 26], [-20, -30, 16], [-13, -26, 22], [-12, -57, 15], [23, -30, -17], [-8, 16, 32], [-16, 30, -28], [-28, 24, -24], [-35, 31, 14], [-6, -3, -40], [27, 9, 9], [15, -43, 48], [29, 8, 23], [-19, -23, 22], [28, -33, 40], [22, 5, 41], [11, -22, -26], [33, 22, -5], [43, -21, 56], [42, 19, 7], [-17, 22, -41], [20, -29, -10], [-40, 7, -21], [-19, 13, -37], [-36, 12, 0], [26, 7, -25], [-30, -12, -27], [22, -23, -2], [11, -15, -1], [-9, 7, -8], [-31, 30, -33], [36, 18, -2], [24, -28, 14], [-2, 2, 14], [28, -5, 5], [-8, -1, 2], [93, -5, -2], [-31, -45, 35], [-19, -3, -39], [-22, 37, 7], [6, -17, 31], [-23, 1, -22], [31, -19, 53], [-26, 25, -22], [34, -36, 19], [12, -14, 58], [-23, 25, -47], [53, 11, 4], [-69, 18, -27], [-7, 30, 19], [-1, 9, 41], [62, -24, 11], [-65, 3, 1], [66, -20, 5], [2, 43, -21]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4332_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4332_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_4332_2_a_n(:prec:=3)
    chi := MakeCharacter_4332_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_4332_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_4332_2_a_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4332_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![3, 0, -3, 1]>,<7,R![-17, -6, 3, 1]>,<13,R![17, 15, -9, 1]>],Snew);
    return Vf;
end function;