
// Make newform 147.6.e.h in Magma, downloaded from the LMFDB on 25 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_h();".
// 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_h();".
// 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, -6], [0, 0], [564, -564], [638, 0], [-882, 882], [0, 556], [0, 840], [4638, 0], [-4400, 4400], [0, 2410], [-6870, 0], [9644, 0], [0, 18672], [-33750, 33750], [18084, -18084], [0, -39758], [23068, -23068], [-4248, 0], [41110, -41110], [0, -21920], [82452, 0], [0, 94086], [49442, 0], [143034, -143034], [0, -53144], [0, -90828], [61666, -61666], [10482, 0], [-171088, 0], [0, -258468], [-300234, 300234], [-350164, 0], [0, 105258], [-396392, 396392], [137746, -137746], [0, -352676], [-217560, 0], [0, 163698], [-358740, 358740], [-507130, 0], [0, 648384], [27838, -27838], [611046, 0], [-879032, 879032], [48500, 0], [-999472, 0], [-606180, 606180], [0, -1359926], [0, 392886], [-1325136, 0], [990094, -990094], [147132, 0], [0, 483582], [-813576, 813576], [461106, -461106], [0, -1675136], [-401126, 401126], [-2309766, 0], [1128772, -1128772], [-938874, 0], [692948, 0], [-2943096, 2943096], [0, -885146], [0, -2508798], [0, 216148], [3252626, 0], [2932068, -2932068], [905198, 0], [-1917858, 1917858], [0, 2436984], [984064, -984064], [0, -1703654], [2756540, 0], [0, -456576], [2006394, -2006394], [0, 5770402], [0, -3006258], [-1533626, 1533626], [-3873756, 0], [-1333066, 0], [-6451920, 6451920], [-4165774, 0], [0, -792680], [0, 1399812], [2992482, 0], [0, -6299690], [3403182, 0], [-2230336, 0], [0, 6514092], [-2392320, 2392320], [6130888, -6130888], [-1235892, 0], [0, 9854956], [1167768, 0], [0, -1049406], [9614070, -9614070], [0, -6961484], [0, 712690], [-3626140, 0], [-4848462, 4848462], [-8504052, 8504052], [0, -362874], [-4110236, 4110236], [7876798, -7876798], [603948, 0], [0, 5390766], [-4279992, 4279992], [1024826, 0], [0, -12434192], [-4215062, 4215062], [-4406646, 0], [-4801676, 4801676], [8307272, 0], [-17695650, 17695650], [-12839356, 0], [20846808, -20846808], [0, -12963246], [-5668620, 0], [3114298, -3114298], [105890, 0], [0, 16091034], [-16077996, 16077996], [0, 165964], [17724798, 0], [0, 10602346], [0, -9032112], [18757544, 0], [0, 11777266], [-5889476, 5889476], [-10047576, 0], [0, -4815296], [3129734, 0], [0, 11777286], [-1493758, 0], [22512474, -22512474], [-11954708, 11954708], [540798, 0], [6142230, -6142230], [-31673428, 0], [0, -26617494], [-36281672, 36281672], [1090332, 0], [10301650, -10301650], [-19613400, 0], [32756486, 0], [25795302, -25795302], [0, 19854820], [0, 673056], [0, -5321150], [27889074, 0], [-28378636, 0], [0, -42267768], [-31952588, 31952588], [-11642928, 0], [0, -1398440], [0, 16679166], [-24795574, 0], [-27957390, 27957390], [0, -7649364], [-46217862, 0], [20855672, 0], [0, 45815220], [1095438, -1095438], [-52581720, 52581720], [49038928, -49038928], [-3054614, 3054614]];
    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_h();".
// 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_h(: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_h();".
// 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_h( : 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![36, 6, 1]>],Snew);
    return Vf;
end function;