
// Make newform 147.6.e.j in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_147_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_147_e_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_147_6_e_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_147_6_e_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
        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_147_e();"
function MakeCharacter_147_e()
    N := 147;
    order := 3;
    char_gens := [50, 52];
    v := [3, 2];
    // 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_147_e_Hecke();"
function MakeCharacter_147_e_Hecke(Kf)
    N := 147;
    order := 3;
    char_gens := [50, 52];
    char_values := [[1, 0], [0, -1]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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 := 6;
    raw_aps := [[0, 6], [9, -9], [0, -78], [0, 0], [-444, 444], [-442, 0], [126, -126], [0, -2684], [0, -4200], [-5442, 0], [-80, 80], [0, 5434], [7962, 0], [-11524, 0], [0, 13920], [9594, -9594], [-27492, 27492], [0, -49478], [59356, -59356], [32040, 0], [61846, -61846], [0, 65776], [40188, 0], [0, 7974], [-143662, 0], [2706, -2706], [0, -131768], [0, 128916], [100978, -100978], [220146, 0], [-74320, 0], [0, 155316], [264246, -264246], [224612, 0], [0, 82074], [287032, -287032], [-129878, 129878], [0, -555284], [43512, 0], [0, 18330], [153324, -153324], [-382066, 0], [0, 273408], [-153602, 153602], [154422, 0], [366856, -366856], [520244, 0], [304736, 0], [-288588, 288588], [0, -772190], [0, -252234], [-1451136, 0], [146398, -146398], [607860, 0], [0, -95586], [2200344, -2200344], [-1770246, 1770246], [0, 223504], [342778, -342778], [480378, 0], [29980, -29980], [-198066, 0], [-1045636, 0], [-1837176, 1837176], [0, 365494], [0, 28338], [0, -1933916], [-1888174, 0], [-2919372, 2919372], [-780682, 0], [-1334370, 1334370], [0, -1017432], [-837680, 837680], [0, 1519930], [2644652, 0], [0, -2013360], [726234, -726234], [0, -4575782], [0, 33870], [5861782, -5861782], [302748, 0], [-5367082, 0], [-1177056, 1177056], [-3662494, 0], [0, 2536744], [0, -6015036], [5659650, 0], [0, 6461590], [-3373530, 0], [-4549744, 0], [0, -2011356], [7604016, -7604016], [-673112, 673112], [-2471700, 0], [0, -6081524], [-846216, 0], [0, 7667850], [9689382, -9689382], [0, 7516780], [0, -7343246], [2182964, 0], [-12546558, 12546558], [-5159724, 5159724], [0, -11745210], [7547284, -7547284], [-9284834, 9284834], [1476228, 0], [0, 12400686], [3691272, -3691272], [9122234, 0], [0, 5679136], [14010586, -14010586], [-253686, 0], [-4300340, 4300340], [10415000, 0], [-4527138, 4527138], [14968700, 0], [17301960, -17301960], [0, -4074702], [-3794748, 0], [-16426142, 16426142], [5506754, 0], [0, -18395742], [-1758348, 1758348], [0, 5363140], [-21260610, 0], [0, -2077286], [0, -4236192], [21452360, 0], [0, 14889178], [-6993236, 6993236], [1904280, 0], [0, -19536128], [1251830, 0], [0, -20447178], [2210642, 0], [-12915054, 12915054], [13549924, -13549924], [-24595626, 0], [-15523722, 15523722], [-26626156, 0], [0, 12389082], [3656296, -3656296], [28046268, 0], [-21115334, 21115334], [13394712, 0], [30151310, 0], [-23989386, 23989386], [0, 8875756], [0, 8712864], [0, 29578834], [24566994, 0], [14568212, 0], [0, -16171368], [-31444556, 31444556], [15142656, 0], [0, -41487608], [0, 17849454], [29639882, 0], [32228202, -32228202], [0, -48488484], [-20326374, 0], [-3662920, 0], [0, -1487412], [-40793010, 40793010], [9263256, -9263256], [52205056, -52205056], [18660946, -18660946]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_147_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_147_6_e_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_147_6_e_j(:prec:=2)
    chi := MakeCharacter_147_e();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 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_147_6_e_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_147_6_e_j( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_147_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<2,R![36, -6, 1]>,<5,R![6084, 78, 1]>],Snew);
    return Vf;
end function;