
// Make newform 147.6.e.f in Magma, downloaded from the LMFDB on 20 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_f();".
// 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_f();".
// 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, -1], [9, -9], [0, 34], [0, 0], [340, -340], [454, 0], [798, -798], [0, -892], [0, 3192], [-8242, 0], [2496, -2496], [0, -9798], [19834, 0], [-17236, 0], [0, -8928], [-150, 150], [42396, -42396], [0, -14758], [1676, -1676], [14568, 0], [-78378, 78378], [0, 2272], [-37764, 0], [0, 117286], [10002, 0], [108770, -108770], [0, 199192], [0, 79972], [46098, -46098], [262706, 0], [196608, 0], [0, 77140], [-208170, 208170], [-275580, 0], [0, 296106], [426472, -426472], [-178486, 178486], [0, -252772], [508088, 0], [0, 221834], [113564, -113564], [663118, 0], [0, -505664], [432382, -432382], [-131962, 0], [-298536, 298536], [-1170620, 0], [399376, 0], [-707916, 707916], [0, 735778], [0, 208758], [713376, 0], [505246, -505246], [317108, 0], [0, 1442846], [-271496, 271496], [-850614, 850614], [0, 540128], [-513574, 513574], [-1356422, 0], [-286756, 286756], [-1707266, 0], [-546788, 0], [-3234264, 3234264], [0, -1813130], [0, 1276578], [0, 1736212], [2072146, 0], [1651460, -1651460], [1266454, 0], [-573218, 573218], [0, -4463224], [4507968, -4507968], [0, -1665350], [-2532324, 0], [0, -796368], [-1947990, 1947990], [0, -1081158], [0, -2767698], [-2363498, 2363498], [-2986692, 0], [-3463306, 0], [-2336928, 2336928], [-3508382, 0], [0, -3548328], [0, -1768332], [-5525790, 0], [0, 2962262], [2118838, 0], [3192256, 0], [0, 7426212], [3396848, -3396848], [3713816, -3713816], [5574940, 0], [0, -3926980], [6420792, 0], [0, -146278], [-7709370, 7709370], [0, 569420], [0, 9448018], [-1353212, 0], [-8193902, 8193902], [10579636, -10579636], [0, 12020518], [2489476, -2489476], [-8213218, 8213218], [-1218268, 0], [0, 8423790], [-8212536, 8212536], [3254778, 0], [0, -7821008], [9516698, -9516698], [-7048950, 0], [6321740, -6321740], [8612360, 0], [5228286, -5228286], [16137308, 0], [15874856, -15874856], [0, 5941122], [-7644300, 0], [7586882, -7586882], [-20668094, 0], [0, -7895406], [19601508, -19601508], [0, 17270980], [-5363442, 0], [0, 17373306], [0, -424608], [21829016, 0], [0, -21767526], [-6217860, 6217860], [3776472, 0], [0, 2887952], [1255190, 0], [0, 14262294], [-20226094, 0], [-26228830, 26228830], [9928292, -9928292], [10903334, 0], [-6063978, 6063978], [-8594380, 0], [0, 2013962], [26467896, -26467896], [-3902292, 0], [19559482, -19559482], [-24544840, 0], [33830510, 0], [-31800938, 31800938], [0, -638420], [0, 4222560], [0, 24504338], [-27762990, 0], [33016964, 0], [0, -4344616], [-19649948, 19649948], [-7265184, 0], [0, -9825320], [0, -27115186], [-45352182, 0], [-46578022, 46578022], [0, -25379860], [15294778, 0], [-5714648, 0], [0, -13024980], [17035950, -17035950], [13698456, -13698456], [34908784, -34908784], [-875662, 875662]];
    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_f();".
// 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_f(: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_f();".
// 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_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_147_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<2,R![1, 1, 1]>,<5,R![1156, -34, 1]>],Snew);
    return Vf;
end function;