
// Make newform 3381.2.a.w in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3381_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_3381_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_3381_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 := [2, -1, -6, 0, 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_3381_a();"
function MakeCharacter_3381_a()
    N := 3381;
    order := 1;
    char_gens := [2255, 346, 442];
    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_3381_a_Hecke(Kf)
    return MakeCharacter_3381_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], [1, 0, 0, 0], [-1, 0, 0, 1], [0, 0, 0, 0], [-1, 0, -1, 1], [-2, 1, -1, -1], [-3, -1, 1, 0], [-1, 2, 1, -1], [-1, 0, 0, 0], [1, 1, 1, -2], [-1, -1, 1, 0], [5, 3, -1, 0], [-1, 0, -1, -1], [2, 3, 1, -1], [-2, 1, 0, -1], [-1, 1, -2, 2], [1, -7, 0, 2], [-6, 2, 1, 0], [1, 2, 0, 3], [-4, -1, 1, 1], [-5, 1, -1, -4], [-3, -1, 1, -2], [-1, -1, -3, 4], [0, 1, -1, -3], [1, -3, 1, 2], [3, -3, -2, -2], [-2, -3, 6, -1], [-8, 1, -1, -1], [5, -6, -2, 1], [-7, 0, 2, -1], [0, -10, 1, 4], [-7, 2, -3, 1], [7, 0, 3, -5], [-9, 4, -2, -1], [-2, 5, -2, 1], [-6, -5, 4, -3], [-4, 3, 0, -3], [3, -1, 6, -2], [-1, 0, -1, -3], [-9, 3, -1, -2], [-3, -8, 2, 1], [1, 1, -3, 2], [-6, 3, 0, 5], [0, -3, 4, -5], [4, -3, -1, 5], [1, 3, 0, 0], [5, 2, -1, 9], [3, -10, -2, 1], [3, 2, -2, 9], [-5, -11, 0, 2], [-3, 4, 0, 3], [-23, 2, 0, -1], [1, 2, -3, -1], [10, 6, 0, 4], [-2, 7, 2, 3], [-1, -2, -7, -1], [0, -3, 3, -3], [-1, 3, -1, 8], [14, 2, -3, 0], [-10, -2, -6, 6], [13, -2, 4, 3], [-4, 8, -2, -2], [-3, 7, 3, -2], [6, 0, -9, 0], [-14, -5, 0, -1], [7, -2, 4, 3], [-4, 3, -4, -5], [-2, -3, -1, -5], [3, -1, -7, 0], [1, -4, -2, -5], [-7, -3, -3, -6], [-3, 18, 0, -5], [4, -14, -1, 8], [-3, -5, 7, 0], [0, 2, 4, -2], [13, 1, 3, -4], [9, -9, -1, 8], [8, 8, 0, 6], [-11, -2, -1, -1], [9, 7, -5, 0], [-3, -10, -2, -1], [0, 7, -1, -5], [-14, -14, 7, -2], [4, -3, -4, 11], [7, -11, -3, -2], [0, -8, 0, -4], [-5, 6, 4, -5], [-5, 0, -6, 1], [-3, 6, 8, -3], [-3, 4, 9, 3], [1, -3, -5, 0], [-3, 1, 1, 0], [-3, -7, -3, -8], [-10, -9, 7, -5], [15, 4, -6, 7], [7, 4, 2, -5], [10, -1, 2, 3], [14, -4, 4, 4], [-20, -7, -4, 9], [6, 1, -6, 5], [7, -4, -2, -1], [-10, 2, 6, -2], [-10, 3, 5, -5], [2, 7, 4, 7], [-21, -1, -1, -4], [6, -12, 4, 4], [-8, -4, 13, -2], [-14, 6, 2, 6], [2, 15, -5, -5], [25, -8, 0, 7], [-14, 3, 1, -13], [11, 7, -5, 10], [-10, -3, -5, 3], [6, 3, -7, 11], [19, 3, 9, -8], [-18, 0, 3, 6], [-16, 0, -1, 6], [-7, 2, -4, 7], [9, 4, 4, 3], [-27, 1, -3, -4], [11, 14, -1, -3], [-9, -8, -5, 11], [-10, 15, 3, -3], [13, 3, -1, 10], [-18, 5, -1, -7], [5, 9, 10, -8], [18, -3, -1, -5], [13, 11, 7, -10], [-31, 10, 3, 1], [2, -22, -2, 6], [-9, -7, 5, -2], [-1, 13, -6, -4], [9, 12, -6, 5], [-1, -1, -11, 2], [-14, 11, -4, -1], [-2, 6, 2, -6], [-11, 1, 7, 2], [-7, 13, -4, -10], [12, -4, -8, 6], [-2, 5, 15, -5], [5, -1, 9, -6], [20, -10, -8, -4], [3, 3, 0, 10], [9, 3, 0, 4], [23, 5, -3, 4], [12, 7, 7, -3], [2, 8, -6, -4], [8, 1, 4, 3], [-10, -18, 6, 4], [6, -2, 8, -4], [-26, 11, -4, -5], [2, 10, 2, 2], [-4, 9, 3, 11], [20, -5, -3, 5], [-7, 4, 6, -7], [-20, -2, -6, -10], [11, -13, -7, 0], [1, 12, -10, -1], [13, 0, 3, -11], [8, 0, -2, -6], [2, 6, 2, 8], [8, -12, -7, 8], [10, 6, -6, -4], [-26, -1, -1, -5], [14, -2, 7, 12], [-19, 1, -5, 4], [-8, 8, -5, 10], [-1, -21, 5, 10], [-33, 0, 2, -3], [-14, 0, -11, -2], [-48, 4, -3, -2], [-6, -4, -8, 4], [10, -6, -5, -2], [15, -5, -3, -6], [-4, -6, -8, 14], [1, -9, 11, 0], [8, -18, 3, 4], [11, 14, 0, 7], [-9, 11, -4, -2], [5, 0, 10, 3], [13, -9, 12, 0], [-12, -1, -1, 9], [-25, 3, 12, 0], [-22, -18, 11, -4], [-31, -2, 1, 1], [0, -1, 5, -5], [-8, -1, 10, -3], [27, 3, 3, -8], [-13, 19, 8, 0], [-25, 7, -9, -12], [9, -16, 6, -5], [13, -18, 0, 3], [2, -4, 4, -6], [13, -10, 1, 11], [6, -6, 3, -2], [-8, 11, -9, -1], [10, -13, 5, 1], [-2, -22, 10, 6], [14, -14, -8, 10], [-17, 5, 10, -4], [8, -9, -2, 13], [-4, -16, -4, 12], [11, -4, -6, 13], [-10, 6, 5, -4], [9, -13, -3, 18], [-4, 13, 2, 11], [1, -7, 9, 12], [9, 0, -8, 9], [-31, 8, -4, 3], [-29, -11, -13, 18], [-30, -1, -2, 3], [17, -7, 4, 4], [11, -3, 3, -10], [20, 10, -4, 2], [-16, -7, -1, -5], [9, -12, 10, -9], [5, 4, -9, 15], [-14, 9, -5, -9], [5, 19, -1, -10], [17, -5, -5, -12], [-12, 0, -1, 8], [-8, -3, 0, -3], [-8, 1, -3, 5], [-24, 0, 12, 0], [-15, 4, -13, -1], [-31, -11, -10, 16], [-21, -3, -7, -4], [-24, 12, -4, -4], [-10, -1, 13, -5], [17, 11, 3, -2], [23, 12, -9, 11], [2, 17, 11, -7], [37, 10, -4, -5], [20, 4, -10, 10], [10, 8, -1, -8], [18, 1, 11, -11], [10, -9, 10, -9], [49, -3, -7, 2], [17, 3, 15, -2], [15, -9, -1, -12], [7, 10, 15, -5], [-21, -16, -6, 7], [14, 24, -8, -2], [-27, -2, -5, -5], [13, 2, -3, -13], [-10, 0, -5, -8], [-16, 10, 0, 2], [-44, 2, 0, 10], [-15, 9, -1, -12], [1, -10, 3, -11], [5, -7, 11, -10], [6, 10, 0, -6], [-23, 8, 9, -5], [-9, -8, 5, -5], [5, 11, 7, -20], [-30, 2, 8, -8], [7, -25, -3, 2], [-3, 6, 3, -3], [24, 4, -1, 12], [17, -5, -5, -4], [-32, -1, 1, -3], [-16, -7, 16, -11], [25, 0, 14, -1], [-3, -2, -1, -17], [4, 4, 15, -12], [19, -12, 7, -7], [32, -1, -2, 13], [53, -9, -1, -4], [-9, 15, -1, -8], [1, 0, 20, -1], [-1, 3, 5, 10], [9, 21, 3, -8], [0, -5, 13, 11], [-36, 12, -2, -20], [19, 15, -3, 14], [-26, 0, -2, 2], [-13, 5, -7, 6], [-3, -6, 7, 7], [1, -11, 3, -14], [-27, -17, -1, -2], [10, -11, 20, 7], [-8, -11, 3, 15], [5, 25, -5, -12], [7, -13, 19, 6], [-1, -8, -4, 15], [7, -21, 9, -4], [1, -9, -9, 4], [4, -9, 10, -7], [26, -7, -9, -3], [20, 14, -6, 8], [-16, -16, 3, -12], [-24, 19, 11, -7], [-28, 3, -7, -13], [-21, 39, 3, -12], [16, 22, 10, -20], [5, -19, -13, -2], [-36, 4, 8, 0], [-14, -6, -7, 16], [7, 12, -7, 17], [9, 5, -9, 0], [34, 1, 3, 15], [-22, 8, -4, -20], [23, -7, -9, -2], [11, -6, -8, -3], [-44, 6, -4, 6], [7, 0, 17, -9], [5, -2, 0, -21], [-1, 22, 5, -11], [12, 27, 2, -5], [0, 7, -7, 7], [33, -2, -1, -3], [27, 15, 7, -6], [-17, -5, 4, -12], [13, 1, -3, 4], [39, -26, -3, 7], [10, 5, -16, 17], [1, 22, -1, 9], [11, -11, 1, 10], [-3, -4, 27, -7], [-40, 18, 0, -16], [-24, 7, 4, -21], [26, 9, 3, 3], [-31, -17, -8, 10], [25, 14, -3, 1], [-47, 4, -5, -15], [1, -9, 0, -2], [-11, -15, -3, -4], [-7, -14, 4, 11], [2, -11, 0, -17], [12, -29, 8, 17], [-8, 2, -2, -8], [31, -5, 5, 6], [-44, 3, -7, -3], [7, -19, -10, 0], [-32, -22, -2, 2], [-4, -13, -8, 3], [-22, 18, -10, -14], [8, 28, -4, -2], [-13, -8, 6, -3], [23, 6, -16, -7], [49, -7, -7, -2], [8, -7, -22, 11], [14, 16, 15, -10], [-3, 5, -1, 0], [-4, 19, -2, 13], [-30, 1, 9, 5], [-1, -2, 18, 3], [-51, 9, -2, -14], [-29, 12, 8, -5], [-25, 11, -1, -10], [6, 11, 23, -9], [15, 10, -9, -11], [3, -5, -23, 2], [37, 1, -4, -14], [13, 1, 9, -14], [30, 13, 12, -3], [46, -10, 4, 4], [-13, -15, -1, -2], [11, -25, -5, -2], [23, -9, -3, 24], [49, -17, 5, 12], [-17, 22, 3, -19], [-11, 23, -5, -6], [-6, -7, -10, 19], [-16, -6, -6, -14], [26, -17, -12, 7], [48, -8, -5, -8], [-16, 16, 6, -4], [42, 1, 3, -5], [24, -37, -7, 13], [-39, 11, 11, 6], [36, -10, -4, 2], [-24, 23, 12, 9], [-22, -7, -2, 7], [-11, -11, -15, 22], [-55, -8, 2, -3], [41, 21, 12, -22], [-10, 11, -2, -1], [-38, 6, -4, 2], [11, 27, -11, -6], [15, -7, 15, 20], [-26, 13, 3, 3], [26, 29, 4, -17], [32, -7, -19, 5], [-23, -16, 10, -3], [-22, -26, -11, 14], [-8, 8, -27, 6], [6, -22, -4, -2], [-45, -7, 5, 2], [-14, 8, 4, 10], [15, -3, -1, 0], [-55, 7, 8, 2], [-62, 0, 6, 2], [-12, -26, -14, 18], [-18, -14, -3, 6], [-11, 24, 6, 3], [-1, 22, -3, 7], [45, 25, -11, 10], [-20, -27, 9, 3], [-7, -17, -1, 16], [13, 17, -1, -22], [-4, 21, 1, -27], [-38, -18, 12, -8], [-8, -17, 1, -17], [36, 6, -3, -2], [-32, 17, -7, -1], [20, 4, 14, -26], [30, 10, 10, -14], [12, 4, 0, -12], [33, 4, -2, 9], [70, 5, -9, -1], [44, 8, 12, -2], [-22, -13, -2, -3], [48, -5, -11, -3], [32, 17, 7, -15], [-42, -8, 16, -4], [-5, 5, 23, -18], [59, 1, 5, -2], [-5, 7, 9, -2], [-8, 10, 2, -10], [-27, 5, 5, -2], [49, 18, 1, -5], [37, 16, 8, -23], [-40, -13, 5, -11], [46, -11, 17, 11], [34, 3, -18, 7], [-8, 0, -13, 14], [46, -6, 14, 2], [3, -14, 14, -7], [6, 16, -13, 8]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3381_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3381_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_3381_2_a_w(:prec:=4)
    chi := MakeCharacter_3381_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_3381_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_3381_2_a_w( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3381_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![2, 1, -6, 0, 1]>,<5,R![8, -14, -1, 5, 1]>,<11,R![-68, -65, -9, 5, 1]>,<13,R![-236, -152, -11, 7, 1]>],Snew);
    return Vf;
end function;