
// Make newform 3328.2.a.o in Magma, downloaded from the LMFDB on 15 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3328_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_3328_2_a_o();".
// 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_3328_2_a_o();".
// 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;
    Rf_num := [[1, 0], [0, 2]];
    Rf_basisdens := [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_3328_a();"
function MakeCharacter_3328_a()
    N := 3328;
    order := 1;
    char_gens := [1535, 261, 769];
    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_3328_a_Hecke(Kf)
    return MakeCharacter_3328_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, 0], [-1, 0], [-1, 1], [-1, 1], [2, -1], [1, 0], [1, -2], [-4, 0], [6, 1], [-6, -1], [0, 0], [-1, 1], [0, -1], [-3, 0], [3, -3], [-6, -1], [-8, -1], [-8, 1], [-2, -2], [13, 1], [8, 1], [2, -2], [6, 0], [-12, 2], [-6, 3], [0, 0], [14, 0], [-4, 4], [-1, 1], [2, 2], [-10, 3], [-3, -2], [-6, 5], [13, -2], [-10, -4], [-11, -3], [-4, -3], [-10, 1], [-16, 2], [6, -6], [-3, -4], [-6, 6], [-6, -1], [-6, 0], [-15, 1], [6, -6], [-7, -2], [13, 1], [12, 1], [13, 1], [9, -4], [-5, 5], [-8, -4], [4, -8], [-7, -2], [-12, 5], [0, -7], [11, 3], [-4, -3], [12, 6], [8, 8], [7, 7], [16, 1], [-4, -10], [3, -6], [-18, 4], [-6, -6], [15, 6], [-15, 0], [-17, 3], [-6, 4], [16, -2], [2, 12], [-8, -6], [2, 9], [-15, 1], [-12, 5], [10, 4], [-14, -1], [-10, -3], [-25, -2], [23, 5], [15, -1], [-25, 0], [-4, -3], [11, 4], [-2, -5], [-4, 6], [3, -3], [0, 0], [-36, -2], [-5, 5], [-8, -6], [29, 4], [-4, 5], [-4, 4], [-10, 10], [3, -4], [4, -2], [25, 3], [-17, -8], [9, 5], [1, 14], [15, 6], [-17, 2], [8, -9], [6, 11], [6, 5], [16, 5], [13, 4], [8, -1], [42, 0], [-12, -4], [4, -5], [-7, -7], [-6, 6], [-26, 8], [-20, -1], [-4, 4], [12, 6], [10, 4], [1, -4], [18, -4], [-34, 2], [-24, -6], [-2, -5], [-2, 2], [6, -6], [10, -3], [1, 13], [40, 3], [-21, 7], [32, 3], [-34, -1], [-36, -1], [-8, 2], [7, -7], [-20, -3], [-40, -2], [27, 0], [12, -6], [3, -3], [8, 13], [8, -8], [6, -6], [24, 4], [1, 13], [30, -4], [12, 6], [-17, -11], [25, 3], [29, -4], [15, 6], [12, -5], [9, 12], [-30, 9], [-4, 4], [-40, -4], [-10, 2], [21, -7], [-6, 11], [-17, -4], [-35, -7], [3, 14], [-26, -10], [15, 13], [-36, -6], [-2, 9], [8, 9], [-26, -2], [-6, 2], [41, 1], [-28, -7], [-16, 10], [22, 6], [23, 2], [4, -18], [6, -6], [-12, 12], [26, 9], [-32, 4], [12, -6], [-14, -7], [-16, 5], [0, -14], [-36, 1], [20, -13], [36, -3], [4, 22], [-44, 2], [37, 0], [-12, 0], [11, 10], [6, -20], [27, 8], [-22, -8], [-4, -3], [30, 12], [6, 18], [24, 11], [-13, -1], [-2, 16], [-27, -15], [-23, 10], [-16, -8], [-44, -5], [13, 1], [0, -12], [-54, 1], [19, -6], [14, 14], [22, -1], [14, 0], [24, -7], [-11, -17], [18, 12], [4, 3], [33, -6], [-48, 6], [-39, -3], [20, 8], [5, -5], [-16, 2], [9, -9], [15, 0], [-8, -13], [31, 8], [26, -5], [44, -9], [-26, -14], [-28, -14], [-24, -12], [8, 20], [3, 14], [31, -2], [-29, 1], [-32, 9], [-23, -5], [-12, -4], [-6, -15], [-24, -12], [-11, 20], [28, 7], [17, -17], [42, 6], [-22, 8], [25, 17], [24, 6], [26, 3], [-32, 11], [-13, -15], [-6, -5], [-15, 1], [-25, 12], [-42, 0], [38, 4], [42, 0], [12, 3], [14, -14], [-20, 9], [-36, -6], [49, 4], [-47, 5], [22, -8], [-24, -6], [-11, 14], [-45, 3], [-14, 13], [40, 2], [-64, 1], [-2, -19], [16, -3], [6, -15], [6, -6], [42, -6], [-4, 18], [-24, 8], [-26, 12], [-12, -21], [23, -4], [54, 2], [5, 9], [30, -2], [6, -6], [-18, 15], [0, 0], [-15, -6], [17, -17], [19, -5], [-10, -8], [-20, 20], [33, -6], [-20, -2], [-38, 7], [-30, -12], [-24, -18], [-2, -5], [-16, 2], [-25, 8], [-2, -6], [-25, -12], [-45, 3], [-22, -20], [36, -12], [-5, 0], [34, -3], [-15, 16], [20, -6], [-36, -6], [-12, -9], [6, -13], [21, 7], [-21, -16], [4, 2], [45, -3], [17, 10], [-48, 10], [0, 14], [4, -17], [-2, 20], [-32, 12], [0, -12], [-10, 17], [49, -7], [24, 12], [-30, 12], [22, -6], [-28, -9], [-30, -12], [-38, -4], [2, 26], [-48, -8], [-22, 1], [6, -15], [0, 0], [48, 4], [-22, -6], [12, -28], [-58, -1], [-18, -3], [-17, 3], [27, 6], [-45, 3], [36, 6], [-39, -3], [41, -10], [36, -15], [46, 1], [29, -1], [58, -2], [-12, 3], [-46, 11], [-13, -1], [-64, -6], [2, 12], [-18, -20], [-35, -7], [6, -12], [39, 4], [1, 13], [-54, 12], [-18, -14], [18, -18], [-42, -14], [-68, 0], [21, 0], [13, -13], [16, 26], [53, 6], [-61, -2], [23, 10], [-45, 3], [-16, 2], [-44, -5], [58, -2], [6, -11], [4, -4], [-34, -13], [42, 4], [-22, -2], [54, 2], [18, 7], [-36, -6], [6, -3], [62, -12], [-33, 5], [3, -3], [-22, 15], [28, -6], [-22, 8], [36, 6], [-39, -3], [36, -8], [53, 12], [-29, -13], [25, -10], [-35, -21], [-43, -2], [-52, -4], [63, -7], [-49, -7], [-86, 2], [-19, -9], [-2, 14], [65, 5], [-12, 12], [5, -33], [4, 14], [-14, -5], [12, -14], [-19, 24], [16, 19], [-9, 26], [17, -16], [33, 6], [42, 14], [9, 19], [44, -16], [-66, -12], [24, -10], [-26, -2], [62, 8], [24, 4], [11, 16], [30, -6], [30, -16], [13, -8], [0, -10], [24, 27], [6, 15]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3328_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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