
// Make newform 147.4.e.m in Magma, downloaded from the LMFDB on 24 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_4_e_m();".
// 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_4_e_m();".
// 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 := [25, -5, -4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-25, -4, 4, 1], [5, 9, 1, -1], [-25, 8, 2, 3]];
    Rf_basisdens := [1, 20, 5, 10];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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, 0], [-1, -1, 0, 0]];
    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 := 4;
    raw_aps := [[1, 1, 0, 1], [0, -3, 0, 0], [2, 2, 0, 2], [0, 0, 0, 0], [0, -8, -10, 10], [-14, 0, 12, 0], [0, 2, -2, 2], [44, 44, 0, -24], [-20, -20, 0, 34], [-138, 0, 24, 0], [0, 16, 72, -72], [106, 106, 0, 36], [210, 0, 30, 0], [212, 0, -48, 0], [-40, -40, 0, 68], [0, -554, -4, 4], [0, -460, -116, 116], [-250, -250, 0, 72], [0, 20, -108, 108], [492, 0, -30, 0], [0, -530, 12, -12], [232, 232, 0, 108], [-924, 0, -96, 0], [254, 254, 0, -142], [-266, 0, -276, 0], [0, -586, -26, 26], [-1384, -1384, 0, -144], [-440, -440, 0, -170], [0, -106, 288, -288], [-222, 0, 264, 0], [-1048, 0, -156, 0], [1052, 1052, 0, -136], [0, -974, -124, 124], [-644, 0, -240, 0], [778, 778, 0, -356], [0, -1240, -432, 432], [0, -2294, 192, -192], [-1028, -1028, 0, -324], [-672, 0, 300, 0], [1682, 1682, 0, 494], [0, 1000, 890, -890], [-182, 0, -684, 0], [-356, -356, 0, 22], [0, 2666, -324, 324], [870, 0, -876, 0], [0, 1528, -360, 360], [1724, 0, -264, 0], [3472, 0, 72, 0], [0, 2396, 988, -988], [1766, 1766, 0, -612], [6574, 6574, 0, 4], [-516, 0, 78, 0], [0, -1370, 36, -36], [-3948, 0, 372, 0], [2774, 2774, 0, 2], [0, -1436, -814, 814], [0, 2438, 1378, -1378], [-2896, -2896, 0, 1296], [0, 1910, -180, 180], [-558, 0, -108, 0], [0, -740, -72, 72], [630, 0, 378, 0], [-3164, 0, 1488, 0], [0, -1552, -1148, 1148], [-1846, -1846, 0, -96], [274, 274, 0, 1372], [-7748, -7748, 0, 168], [2018, 0, 1944, 0], [0, -3872, -1366, 1366], [7546, 0, -744, 0], [0, -2350, -1970, 1970], [-3884, -3884, 0, -1346], [0, 3376, 912, -912], [4978, 4978, 0, -1920], [2228, 0, 1032, 0], [-6256, -6256, 0, -1864], [0, 3982, 248, -248], [-2266, -2266, 0, 720], [1030, 1030, 0, -788], [0, -3890, -1548, 1548], [-1764, 0, -2700, 0], [1766, 0, -2988, 0], [0, -5132, 326, -326], [-4802, 0, 3864, 0], [-3736, -3736, 0, -2544], [6784, 6784, 0, 1978], [9858, 0, 192, 0], [4390, 4390, 0, -2016], [-4578, 0, 858, 0], [7688, 0, -756, 0], [-10876, -10876, 0, 1508], [0, -376, 556, -556], [0, -568, 24, -24], [-5808, 0, -570, 0], [-3548, -3548, 0, -1080], [-7560, 0, -144, 0], [9578, 9578, 0, -2446], [0, 1514, 3094, -3094], [1892, 1892, 0, -2160], [6994, 6994, 0, 2076], [212, 0, 492, 0], [0, -2066, 644, -644], [0, 9788, 2884, -2884], [7582, 7582, 0, 3892], [0, -10900, 204, -204], [0, -13970, -792, 792], [-5292, 0, 3276, 0], [-10498, -10498, 0, -1138], [0, 17548, -1978, 1978], [4774, 0, -5928, 0], [2816, 2816, 0, 3936], [0, -2626, -2592, 2592], [1122, 0, -5916, 0], [0, 10348, 1944, -1944], [10544, 0, 420, 0], [0, -6854, 788, -788], [-1148, 0, 552, 0], [0, -11632, -3380, 3380], [-9974, -9974, 0, -2576], [5952, 0, -1950, 0], [0, 2242, -1248, 1248], [18650, 0, -1620, 0], [11930, 11930, 0, 4766], [0, 16960, -3838, 3838], [5420, 5420, 0, -1512], [-4842, 0, -1512, 0], [-9470, -9470, 0, 5328], [13232, 13232, 0, -592], [19768, 0, -4128, 0], [2270, 2270, 0, -5580], [0, 15140, 5868, -5868], [5868, 0, -702, 0], [-4976, -4976, 0, 3696], [-15538, 0, 240, 0], [-32674, -32674, 0, -94], [1582, 0, -1944, 0], [0, 422, -5570, 5570], [0, 5812, -2664, 2664], [20958, 0, -3918, 0], [0, -9542, 3296, -3296], [5740, 0, -7992, 0], [-34502, -34502, 0, -1148], [0, -736, -3996, 3996], [-25464, 0, 5082, 0], [0, 41554, -852, 852], [-16464, 0, 132, 0], [-41006, 0, 1656, 0], [0, 3362, -7502, 7502], [4916, 4916, 0, -2736], [5188, 5188, 0, 5962], [-3926, -3926, 0, -8640], [33810, 0, 2346, 0], [10460, 0, 7992, 0], [-11632, -11632, 0, -7564], [0, -8044, 1296, -1296], [-36300, 0, 4650, 0], [22408, 22408, 0, 1656], [-1594, -1594, 0, 1178], [-26474, 0, 4872, 0], [0, 38054, -1550, 1550], [34672, 34672, 0, 3334], [6666, 0, 10608, 0], [-26752, 0, -4764, 0], [-124, -124, 0, -1888], [0, 5242, 140, -140], [0, -19192, 5800, -5800], [0, 12368, 2520, -2520], [0, -20270, 288, -288]];
    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_4_e_m();".
// 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_4_e_m(:prec:=4)
    chi := MakeCharacter_147_e();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_4_e_m();".
// 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_4_e_m( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_147_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![144, 36, 21, -3, 1]>,<5,R![2304, 288, 84, -6, 1]>],Snew);
    return Vf;
end function;