
// Make newform 3381.2.a.j in Magma, downloaded from the LMFDB on 23 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_j();".
// 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_j();".
// 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], [6], [5], [-6], [-1], [-1], [6], [5], [-7], [0], [-1], [6], [12], [-6], [14], [5], [-6], [-7], [5], [-12], [-6], [-10], [0], [11], [0], [11], [6], [-7], [0], [12], [5], [18], [20], [14], [-4], [0], [6], [12], [23], [18], [11], [-12], [-16], [-4], [-16], [-6], [-13], [18], [24], [2], [30], [12], [0], [-24], [-28], [-31], [18], [-13], [18], [-13], [-6], [-19], [18], [11], [-25], [-12], [-22], [-6], [-18], [17], [-31], [-7], [-6], [36], [-25], [0], [5], [-12], [-13], [-36], [-19], [8], [18], [-6], [-1], [-42], [-13], [-18], [-6], [-13], [30], [5], [12], [-12], [30], [41], [-31], [44], [-6], [-36], [-24], [-13], [-13], [-30], [30], [24], [35], [-25], [26], [-18], [-19], [8], [0], [-13], [18], [-42], [0], [-31], [-1], [-18], [-36], [-13], [-18], [-10], [42], [5], [23], [29], [24], [-1], [2], [30], [17], [18], [32], [36], [48], [-28], [-18], [-40], [24], [-19], [-18], [-1], [48], [-52], [42], [-22], [-30], [-1], [-54], [29], [36], [5], [12], [-43], [-36], [6], [-30], [11], [-42], [42], [36], [-31], [-1], [-1], [12], [-30], [-22], [6], [-61], [-19], [42], [-37], [48], [-16], [5], [17], [-30], [-7], [54], [24], [-6], [2], [29], [-55], [-36], [41], [-6], [32], [-30], [54], [-18], [5], [-46], [30], [-12], [-54], [59], [14], [50], [60], [-42], [20], [-66], [42], [20], [-61], [-48], [-64], [12], [6], [-7], [-16], [-66], [6], [-48], [-7], [35], [-60], [56], [-48], [26], [-48], [6], [17], [18], [-43], [-4], [53], [-6], [-1], [6], [-13], [-12], [66], [60], [30], [29], [-31], [-22], [-66], [-42], [-25], [-42], [-28], [6], [47], [-12], [24], [59], [12], [-24], [-1], [17], [54], [-22], [-43], [6], [5], [29], [12], [-1], [36], [-42], [-52], [78], [-13], [-28], [-1], [-19], [-22], [56], [-42], [-37], [5], [18], [18], [-31], [-30], [-19], [-4], [48], [-13], [54], [-55], [24], [-66], [-78], [30], [-12], [71], [-36], [35], [6], [-42], [-13], [50], [12], [-40], [-24], [-13], [29], [18], [-49], [12], [-25], [-84], [-30], [-30], [-4], [0], [-49], [-18], [-12], [-49], [0], [71], [-19], [54], [23], [36], [11], [-7], [29], [-24], [-30], [-67], [18], [5], [-66], [5], [-24], [86], [-24], [29], [47], [-1], [-60], [36], [-13], [60], [-6], [-67], [-28], [-12], [-18], [-4], [-70], [-72], [8], [41], [30], [-66], [54], [-78], [72], [26], [18], [-60], [-60], [65], [-82], [42], [-73], [-70], [18], [65], [12], [-60], [41], [-55], [-60], [-24], [-70], [12], [17], [36], [-54], [8], [-36], [-4], [36], [-25], [-82], [41], [12], [-73], [-66], [-84], [-1], [-72], [26], [-40], [30], [5], [-36], [-34], [-24], [68], [48], [18], [-67], [-46], [-54], [83], [78], [38], [-48], [6], [68], [-13], [-90], [12], [53], [12], [84], [78], [41], [36], [12], [62], [60], [42], [-60], [92], [-30]];
    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_j();".
// 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_j(: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_j();".
// 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_j( : 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![-6, 1]>,<13,R![-5, 1]>],Snew);
    return Vf;
end function;