
// Make newform 3381.2.a.q in Magma, downloaded from the LMFDB on 25 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_q();".
// 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_q();".
// 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 := [-4, -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_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], [-1, 0], [0, 1], [0, 0], [-2, 0], [3, -2], [6, -1], [-3, -1], [1, 0], [-6, 2], [-1, 3], [5, 1], [4, -4], [-5, -3], [-2, 3], [0, 5], [-6, 2], [-6, 0], [-11, 2], [6, -5], [-9, -2], [-7, -1], [-8, -2], [10, 0], [-6, 6], [4, 2], [-11, 2], [0, 4], [-9, 1], [2, -5], [-3, -3], [-8, 3], [0, 5], [-1, 7], [-6, -3], [-12, 4], [2, 4], [4, 0], [8, 3], [14, -6], [-4, -7], [-3, -7], [2, 2], [11, 2], [-12, 6], [8, 0], [-4, 2], [8, -8], [-2, 0], [9, -13], [-14, -2], [-12, 4], [-2, 0], [-2, 0], [16, 6], [-8, -8], [-4, -8], [4, -10], [-23, 4], [18, -1], [21, -2], [-10, 9], [-7, 3], [-6, 1], [-9, 13], [-18, -4], [-17, -1], [-13, 5], [-8, 3], [10, 7], [-2, -6], [-6, -12], [-1, -6], [5, -3], [-7, -10], [2, 4], [0, -10], [-7, 10], [4, 3], [3, 14], [16, -8], [-9, -3], [8, 0], [-1, -5], [0, 10], [18, 3], [-14, 0], [3, 1], [-2, -4], [15, -3], [2, 2], [10, -6], [-1, -15], [-26, 3], [-31, 3], [0, 6], [-8, -2], [26, -3], [-9, 6], [-15, -12], [20, -6], [26, -4], [8, -4], [-8, 11], [19, -14], [-15, -3], [-2, -1], [10, 0], [-20, 11], [9, 9], [-35, 5], [26, 0], [10, 7], [19, -2], [20, 9], [-36, -1], [-39, -1], [-10, -6], [-2, -8], [-16, -10], [-21, 5], [3, 3], [-30, 7], [0, 9], [-7, 5], [-10, -11], [30, 6], [10, -17], [-33, -3], [-19, -13], [1, 19], [12, -10], [19, -7], [-6, 10], [22, 10], [11, -5], [10, -13], [44, -1], [16, -9], [-28, -6], [4, -10], [6, 12], [8, -14], [4, -12], [11, 8], [30, 6], [21, -7], [-8, 6], [20, -4], [14, 13], [-10, 9], [-10, -3], [-13, -7], [-10, -6], [-11, -12], [28, -8], [-27, 8], [-16, -8], [-9, -7], [12, -2], [-22, -3], [6, 0], [-33, 5], [6, -2], [-10, -1], [4, 6], [13, 13], [-11, 13], [-13, 15], [-4, -20], [-34, 8], [-14, 11], [42, 2], [39, -11], [7, -13], [-22, 0], [23, -3], [-20, -6], [-20, 19], [-17, 5], [-19, -9], [10, -10], [-15, 20], [14, -4], [-12, 24], [6, -7], [6, -3], [-33, 9], [17, 2], [-28, 11], [-37, -9], [-30, -6], [20, -11], [50, -2], [42, 10], [10, -17], [-23, -5], [-22, 24], [30, -13], [-8, -5], [38, -1], [33, 7], [14, 5], [-10, 6], [-16, -6], [2, -12], [-24, 23], [2, 5], [14, 4], [-4, -10], [35, -20], [-32, 0], [32, -8], [-4, 10], [22, 2], [11, -3], [16, 8], [-26, 16], [-6, -18], [-12, 16], [41, -14], [-11, -2], [8, 2], [-36, -9], [-4, 8], [-34, 7], [24, 10], [58, 3], [-25, 6], [14, -21], [45, -4], [48, -13], [-31, 7], [-14, 16], [-31, 8], [-14, -14], [-15, -9], [-8, 14], [-14, 7], [-24, 0], [-10, 0], [15, 5], [-5, 3], [6, 23], [46, -4], [22, -6], [-19, -3], [-14, 14], [20, -8], [18, 6], [-5, -13], [56, -7], [20, -8], [-31, 11], [52, -10], [-56, 12], [11, 3], [-5, 1], [-6, 18], [38, 8], [21, -6], [10, -4], [-5, -8], [31, -17], [40, -10], [-19, 11], [-36, -11], [26, 12], [24, 13], [-2, 4], [13, -4], [12, 16], [5, -13], [-27, 0], [-6, -5], [24, -12], [-38, 23], [-37, -14], [17, -21], [-14, 12], [22, 13], [1, 18], [-74, -4], [-41, 5], [44, -22], [12, 12], [23, 5], [26, 7], [-13, 11], [52, -8], [-22, 24], [18, -6], [38, 18], [-28, -6], [-1, -8], [44, 9], [21, -26], [-26, 22], [14, 17], [-7, 9], [-18, -10], [20, -11], [28, -11], [-80, 0], [-37, 8], [-23, 21], [46, -9], [21, -25], [12, 22], [-53, 7], [52, 0], [6, 3], [54, -5], [12, -40], [-8, 10], [21, -11], [-30, 34], [44, -12], [37, -17], [-52, 10], [-15, 16], [25, 15], [-22, 0], [3, -11], [44, -25], [17, -27], [5, -5], [-25, 11], [64, -12], [50, -20], [-27, 36], [-14, 10], [-17, 27], [14, -3], [-43, 22], [48, -1], [-10, -2], [40, -18], [-13, -24], [-43, 7], [-17, -2], [8, 7], [-4, 18], [31, -7], [48, -22], [-10, 17], [-37, -14], [4, 22], [48, -16], [66, 0], [4, 30], [-26, -3], [44, -24], [52, 9], [69, -11], [-22, 8], [-38, -4], [-2, 24], [-58, -6], [24, 9], [-70, 16], [-26, 4], [8, 16], [-16, 22], [51, -3], [2, 19], [18, -18], [-29, 17], [18, 0], [30, -26], [51, 17], [-84, 1], [24, -14], [63, -4], [-15, -18], [4, 32], [-56, -8], [30, 24], [-64, 15], [-37, 23], [28, 26], [-38, 7], [8, 10], [-12, 18], [-40, -15], [16, -19], [75, -13], [58, 12], [1, -32], [-16, 46], [-29, 23], [-14, 8], [-32, 29], [-27, 17], [-4, -6], [-78, -2], [-20, 37], [6, -19], [-27, -28], [24, -42], [6, -27], [-56, 22], [-12, 12], [88, 1], [-58, 18], [31, -27], [18, 0], [46, 20], [53, 4], [30, -46], [6, -1], [-40, 4], [-22, -24], [0, -19], [55, -17], [42, 18], [-28, 11], [-17, -13], [-32, 27], [-60, 12], [66, 16], [-85, 7], [12, -16], [-12, 39], [38, -15], [-24, -8], [-30, -24], [20, -26], [36, -26], [26, -2]];
    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_q();".
// 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_q(:prec:=2)
    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_q();".
// 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_q( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3381_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-4, 1, 1]>,<5,R![-4, -1, 1]>,<11,R![2, 1]>,<13,R![-13, -4, 1]>],Snew);
    return Vf;
end function;