
// Make newform 3381.2.a.i 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_i();".
// 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_i();".
// 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 := [[0], [1], [0], [0], [5], [4], [2], [3], [1], [-8], [-10], [4], [7], [10], [-3], [3], [-13], [-9], [8], [-6], [8], [0], [18], [14], [18], [-5], [13], [12], [-16], [10], [1], [21], [-9], [14], [-11], [-19], [19], [1], [-19], [6], [-6], [2], [-13], [9], [18], [-1], [-23], [2], [-12], [-13], [16], [-10], [-7], [2], [-13], [-7], [18], [6], [-21], [6], [-20], [22], [-22], [27], [-13], [2], [1], [-22], [14], [10], [-18], [32], [23], [-4], [-20], [18], [-6], [-14], [25], [-12], [28], [-6], [5], [21], [8], [-28], [8], [-22], [18], [13], [-12], [32], [40], [-12], [28], [6], [-1], [20], [7], [-25], [-28], [18], [-36], [-19], [-10], [38], [-3], [-6], [2], [-34], [4], [-24], [-22], [28], [-12], [41], [29], [-16], [30], [-28], [31], [-7], [22], [-4], [-48], [27], [-38], [-44], [-27], [-14], [-20], [41], [-10], [-46], [9], [-34], [-18], [1], [-8], [4], [-12], [-32], [-9], [-3], [-6], [-44], [4], [7], [-6], [52], [-49], [-12], [12], [-20], [-52], [48], [10], [6], [-31], [6], [-24], [-33], [40], [-40], [-29], [36], [-19], [-50], [-42], [-41], [57], [10], [-39], [38], [-58], [36], [3], [28], [-5], [-10], [-37], [8], [9], [43], [-3], [-34], [-19], [-4], [-31], [-8], [-34], [-66], [28], [-65], [9], [-30], [-32], [16], [-30], [17], [15], [-30], [-28], [27], [10], [-51], [-32], [-28], [-18], [0], [29], [21], [58], [24], [-48], [-10], [27], [-50], [48], [-30], [-33], [-5], [-6], [4], [-7], [33], [-74], [42], [56], [-44], [21], [40], [-46], [-20], [-23], [-6], [5], [-6], [-60], [-8], [-37], [-20], [26], [-73], [-41], [-41], [56], [18], [-10], [-9], [30], [44], [3], [15], [18], [46], [36], [17], [9], [76], [34], [-3], [-26], [7], [-51], [-53], [19], [-32], [-6], [-2], [10], [42], [-14], [-44], [76], [56], [-6], [31], [-64], [78], [-39], [-60], [32], [2], [8], [84], [-44], [-29], [-2], [10], [-51], [16], [-74], [-34], [-10], [62], [28], [41], [55], [-36], [66], [82], [40], [-14], [2], [-45], [-74], [-19], [13], [34], [39], [-54], [11], [-4], [-23], [-11], [-9], [-50], [69], [0], [-15], [-44], [7], [23], [-61], [5], [-34], [70], [45], [25], [-54], [48], [72], [53], [56], [19], [-22], [10], [-22], [8], [26], [5], [9], [-24], [-9], [12], [20], [-53], [56], [-42], [86], [-5], [-32], [-79], [-38], [11], [-40], [-18], [-72], [18], [44], [-35], [96], [15], [-70], [-25], [-71], [66], [-68], [-30], [62], [-24], [57], [77], [-66], [-100], [79], [1], [-92], [10], [48], [26], [27], [-24], [28], [-1], [43], [-64], [44], [-26], [-8], [99], [74], [78], [51], [34], [-17], [-52], [90], [60], [-40], [98], [-32], [-84], [25], [-82], [28], [-38], [-4], [32], [-46], [-46], [81], [-62], [4], [82], [-16], [52], [90], [-66], [-78], [-35], [-72], [44], [-54], [-53], [-101], [-76], [96], [39]];
    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_i();".
// 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_i(: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_i();".
// 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_i( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3381_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![0, 1]>,<5,R![0, 1]>,<11,R![-5, 1]>,<13,R![-4, 1]>],Snew);
    return Vf;
end function;