
// Make newform 3381.2.a.n 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_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_3381_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, -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 := [[-1, -1], [-1, 0], [2, 1], [0, 0], [-3, 4], [3, 1], [1, 0], [3, 0], [-1, 0], [-5, 4], [5, 2], [-1, -4], [1, -6], [5, -5], [2, 6], [-4, -1], [8, 3], [-7, 11], [6, 3], [-7, -5], [1, 6], [3, 4], [3, 0], [-5, -3], [-5, 0], [0, -3], [0, -8], [5, -11], [-2, 7], [2, -7], [17, -3], [13, -8], [3, -10], [12, -7], [-10, 10], [2, 2], [12, -14], [-4, -11], [-17, -4], [3, -10], [6, -3], [-5, 4], [10, 6], [-8, 10], [-11, 3], [10, -13], [19, -12], [16, -7], [2, -11], [16, -9], [10, -15], [-16, 3], [13, -12], [0, 2], [18, -22], [15, -14], [5, -15], [-17, 24], [-21, -3], [24, -6], [24, -5], [4, 12], [19, -6], [-11, 17], [0, -4], [-18, 11], [14, -8], [17, 7], [1, 12], [22, -23], [-3, 14], [-6, 17], [-19, 9], [-17, -8], [20, -20], [25, -4], [1, -12], [26, -6], [-13, -12], [-15, 12], [-6, 15], [-15, 1], [-13, 31], [8, -10], [-3, -10], [12, 12], [26, -17], [22, 5], [0, 1], [29, -14], [-19, 32], [-17, 2], [-9, 6], [3, -21], [-2, 13], [26, -19], [-2, 10], [-26, 12], [2, -20], [-30, 14], [-2, -15], [24, -6], [-21, 19], [-4, 24], [29, -16], [14, -4], [-13, -15], [-8, 18], [11, -5], [20, 7], [-17, 5], [7, 14], [-13, 33], [-43, 1], [-17, 6], [13, -23], [-7, -3], [-14, 7], [-34, 9], [-27, 4], [-13, 32], [-23, 16], [3, -1], [-25, 16], [-7, -13], [-12, 13], [13, -29], [25, -6], [9, 20], [4, 10], [-5, -16], [-14, 13], [-24, 33], [13, 16], [4, 28], [4, -22], [19, -34], [22, -13], [22, -22], [9, 13], [13, 4], [14, -10], [-26, -9], [-28, 9], [35, 8], [17, 3], [-20, -14], [-16, 18], [-2, 24], [8, -10], [-8, 12], [24, -38], [-3, -17], [-39, -3], [32, 13], [-30, 10], [-1, -26], [54, -1], [-33, 18], [-12, 24], [-38, 12], [43, -27], [-2, 32], [-23, 21], [-31, -11], [21, -20], [11, -5], [31, -16], [46, -11], [29, -27], [25, -29], [-6, -24], [19, -17], [-39, 32], [-24, 32], [-1, 2], [15, 3], [18, -17], [2, 33], [48, -25], [-6, 25], [1, 19], [24, -15], [-9, -9], [35, -12], [17, -21], [6, -16], [39, -26], [8, -43], [9, 24], [38, -27], [40, 7], [-4, 14], [31, -14], [19, 15], [-25, 1], [-9, 33], [-44, 2], [-2, 0], [-38, 19], [14, -40], [-40, 8], [6, -13], [-7, 11], [-37, 6], [-22, 20], [1, -2], [-24, -9], [34, -11], [23, -36], [-14, -26], [26, 21], [33, -4], [-40, 4], [15, -13], [12, 7], [-29, 20], [53, -25], [-17, -10], [45, 2], [49, -35], [-10, -12], [15, -11], [-24, -8], [-41, 2], [14, -11], [13, -18], [-16, 0], [1, -23], [-17, -4], [21, -8], [31, -5], [-42, 27], [-48, 12], [-31, -1], [-3, 17], [46, 18], [-9, -22], [-37, 16], [45, -16], [-27, 24], [-4, 1], [20, 22], [-51, -14], [-29, 46], [9, -43], [28, -16], [40, -56], [33, -38], [-19, -10], [-17, 32], [-30, 16], [39, 4], [-31, -6], [-49, 36], [-40, 54], [-47, -4], [33, 20], [-29, 9], [-27, -12], [29, -27], [30, -40], [-50, -15], [3, 14], [-37, 17], [-7, 26], [42, -40], [-21, 2], [-35, -4], [38, -43], [-37, 40], [-11, -14], [51, -19], [42, -26], [-41, 16], [-10, -8], [13, -40], [5, -6], [51, -12], [17, 20], [-6, 26], [43, 3], [-19, 0], [-19, 4], [-2, 11], [1, 20], [39, -56], [-38, -12], [29, -69], [-28, -20], [5, 35], [-35, 31], [51, -35], [41, -6], [28, 16], [-25, 18], [-44, -12], [21, 9], [-11, -38], [-77, 18], [-23, 41], [54, 12], [-15, -8], [28, -19], [0, -28], [47, 2], [-16, 47], [1, -6], [-24, -18], [5, 43], [59, -6], [-43, 54], [14, 13], [-1, -22], [3, -18], [-8, 12], [55, -30], [49, -8], [-13, 2], [-10, -30], [-30, 28], [-55, -5], [-26, 17], [25, 14], [-65, 12], [52, 1], [39, -68], [42, -15], [-30, -10], [-4, 30], [28, -52], [-45, 10], [-25, 31], [82, 3], [42, 14], [-54, -16], [8, -42], [18, -10], [2, 11], [-32, 53], [-55, 32], [-50, 40], [19, 11], [9, -22], [-26, 60], [-11, -15], [-6, 19], [-12, 63], [52, -21], [-37, 40], [37, -33], [1, -24], [15, -6], [-46, -21], [13, 34], [-8, 36], [-64, -10], [21, -2], [-55, -12], [-7, 48], [-39, -14], [-51, -22], [-41, 48], [-20, 28], [26, 22], [16, 8], [-25, 73], [14, 34], [-43, -1], [17, -33], [-5, -22], [12, 40], [-24, -34], [24, -20], [-11, -52], [-16, 29], [28, 1], [-34, 38], [-26, 0], [-1, 32], [59, 8], [-25, 17], [-38, 26], [-49, 5], [10, -35], [-1, 11], [65, -3], [-14, -12], [-93, -2], [0, -22], [23, 2], [-50, 37], [46, -60], [58, -22], [-63, -3], [26, -21], [39, -40], [69, 4], [37, 9], [-19, 46], [45, -28], [-5, -43], [36, 22], [-23, 23], [65, -9], [-21, 39], [-16, -32], [-12, 18], [68, -12], [-56, -7], [55, -43], [82, 6], [36, -24], [-55, -3], [-51, 5], [10, -44], [-45, 54], [-87, 26], [-23, -42], [42, 6], [-25, 6], [21, 44], [-12, 59], [-39, 53], [-55, 47], [38, -18], [11, -15], [36, -74], [44, -19], [17, 9]];
    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_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_3381_2_a_n(: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_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_3381_2_a_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3381_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![1, 3, 1]>,<5,R![5, -5, 1]>,<11,R![-19, 2, 1]>,<13,R![11, -7, 1]>],Snew);
    return Vf;
end function;