
// Make newform 3381.2.a.d in Magma, downloaded from the LMFDB on 26 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_d();".
// 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_d();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
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], [7], [-3], [-8], [1], [-8], [-6], [6], [-4], [-4], [-9], [3], [12], [-2], [1], [9], [3], [-4], [-18], [10], [0], [-10], [-1], [4], [0], [-5], [-6], [-11], [3], [6], [-15], [-4], [10], [-20], [-11], [-16], [5], [22], [-8], [-19], [-26], [24], [-14], [-20], [18], [20], [24], [0], [14], [10], [-10], [-32], [-12], [-2], [13], [-25], [19], [23], [4], [-27], [8], [-6], [-10], [4], [-13], [19], [4], [-10], [-15], [38], [9], [-6], [-6], [21], [-15], [23], [-12], [8], [0], [-38], [6], [-3], [6], [16], [-10], [-8], [-12], [8], [-8], [-7], [-20], [-6], [-34], [-9], [37], [-19], [-26], [-14], [16], [13], [11], [-2], [19], [18], [15], [-30], [14], [-26], [39], [25], [-1], [-27], [20], [-40], [30], [-30], [-32], [-14], [-39], [-15], [2], [45], [-24], [15], [8], [0], [0], [54], [16], [-4], [0], [-10], [29], [-17], [7], [18], [26], [2], [-18], [48], [-43], [4], [-10], [34], [56], [-21], [-29], [3], [-40], [-40], [47], [-48], [35], [56], [0], [2], [47], [2], [-20], [12], [-45], [42], [14], [10], [34], [8], [6], [39], [0], [8], [50], [50], [-44], [10], [3], [-20], [44], [24], [29], [-22], [-12], [23], [31], [4], [19], [-23], [34], [12], [35], [0], [-32], [9], [-52], [-30], [-39], [21], [-37], [-16], [-63], [16], [-46], [-34], [3], [-9], [6], [-2], [-57], [36], [16], [46], [32], [-32], [-28], [46], [-44], [24], [35], [19], [-22], [41], [-28], [-55], [-38], [-67], [-67], [-39], [1], [31], [44], [46], [9], [24], [-20], [-22], [-39], [-16], [-42], [-12], [-42], [49], [-30], [16], [-2], [32], [-68], [-72], [62], [-15], [52], [-56], [34], [-52], [-34], [-48], [-48], [26], [-31], [2], [53], [2], [14], [-28], [-9], [70], [-65], [-14], [55], [20], [28], [-13], [-41], [8], [49], [21], [-56], [62], [5], [45], [-70], [76], [-10], [0], [-32], [-9], [-70], [48], [-74], [-80], [60], [-54], [7], [37], [43], [-4], [41], [14], [-12], [-33], [19], [-56], [-27], [-6], [45], [0], [34], [46], [36], [47], [63], [-24], [-46], [26], [12], [-72], [40], [6], [-55], [-32], [42], [-16], [21], [-2], [52], [-46], [32], [66], [1], [-24], [62], [-3], [61], [73], [44], [-66], [-19], [88], [85], [65], [-54], [-92], [38], [79], [43], [58], [60], [74], [22], [-33], [40], [-39], [10], [-34], [-66], [-54], [44], [33], [-18], [-14], [-96], [-94], [32], [49], [44], [40], [-90], [-36], [72], [69], [78], [-85], [-17], [4], [-20], [2], [-33], [42], [14], [-57], [-26], [2], [-75], [11], [14], [10], [-25], [42], [-98], [6], [-75], [62], [-58], [-20], [61], [-69], [87], [54], [41], [2], [88], [-63], [-84], [-44], [-42], [-46], [-79], [0], [23], [-4], [38], [-41], [-18], [-24], [39], [2], [51], [72], [-66], [-38], [-28], [-19], [-19], [-84], [10], [90], [-42], [72]];
    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_d();".
// 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_d(:prec:=1)
    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_d();".
// 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_d( : 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, 1]>,<5,R![-1, 1]>,<11,R![2, 1]>,<13,R![-7, 1]>],Snew);
    return Vf;
end function;