
// Make newform 154.4.a.h in Magma, downloaded from the LMFDB on 07 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_154_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_154_4_a_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_154_4_a_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 := [-14, -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_154_a();"
function MakeCharacter_154_a()
    N := 154;
    order := 1;
    char_gens := [45, 57];
    v := [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_154_a_Hecke(Kf)
    return MakeCharacter_154_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]];
    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 := [[2, 0], [-2, -1], [-10, 3], [-7, 0], [-11, 0], [-42, -2], [-20, -26], [-70, 4], [-2, 21], [86, -48], [-40, 9], [-76, 17], [-332, 46], [328, -52], [-66, 134], [-10, 172], [-114, -115], [282, -184], [498, 31], [-562, -45], [-68, 42], [396, 50], [-722, -42], [-240, 119], [140, -55], [258, 174], [-538, -198], [900, 150], [-778, 190], [-1444, -101], [-1188, 118], [-1722, 124], [-1052, -197], [710, 168], [-622, 148], [-2420, 162], [-1294, 655], [1828, -464], [208, -66], [-818, -150], [482, -1069], [1546, 257], [-1662, 499], [1362, -120], [-538, 116], [5294, -2], [316, -1018], [648, 665], [1938, 670], [1442, 621], [-1666, -1012], [1192, -72], [-1736, -1132], [-4714, -83], [3786, -1016], [-172, -358], [-684, -266], [-1156, -168], [622, 634], [-6510, -174], [5250, 234], [122, -952], [5154, 1178], [-222, -1446], [-3372, 1083], [396, -1053], [5654, -563], [-6242, 956], [-6892, 626], [-754, -2220], [3456, -2489], [-6632, 244], [1080, -963], [-1910, 1190], [6574, -1447], [7024, -1855], [-4520, -1361], [1944, 678], [374, 2548], [-1312, -678], [-1608, -480], [-4010, 1320], [-11576, -124], [-5900, 1469], [4680, 238], [8122, 1443], [10480, -2099], [-9282, -1906], [10590, 544], [-450, -3129], [-7998, -671], [-13360, 1952], [-4626, -1011], [1024, 542], [7908, 88], [7500, -696], [3554, 1029], [-17908, 749], [-8126, 1206], [-3854, -378], [-7628, 1860], [10366, -44], [-6878, -4250], [2118, 4252], [-3804, 3856], [12064, 697], [6572, -592], [15132, 834], [-8256, -3000], [7628, 3548], [-2644, -3766], [-10918, -1662], [12018, -272], [-13426, -125], [-11666, 1493], [-2236, 7865], [20030, -1667], [988, 6751], [6268, 5397], [23940, -1118], [-21354, 1583], [7314, -2894], [14658, 2520], [11956, 1528], [-7130, 2377], [-19354, -2818], [-908, 8693], [-12872, 6469], [-6504, -6235], [-4938, 1898], [13160, -1602], [7712, 4768], [3738, 8263], [-11614, -772], [-14852, 7240], [4284, -1384], [-3488, -4378], [35442, 496], [-3030, 10015], [7706, 5052], [7806, 7480], [-45814, 88], [7454, 3969], [27828, -3412], [5774, -7553], [4672, -3851], [6962, 5020], [3676, -320], [-9970, -1167], [-16372, -7472], [13578, -9536], [-18872, 349], [-27980, 3272], [-29916, 2842], [34860, -3324], [1500, -4844], [-33248, 2892], [-22398, -4256], [-33392, -1408], [-41526, 2064], [1546, -2609], [-10066, -3854], [-244, 12638], [-32314, -1849], [-39888, 5131], [13116, -7631], [39464, 624], [-22830, 7470]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_154_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_154_4_a_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_154_4_a_h(:prec:=2)
    chi := MakeCharacter_154_a();
    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_154_4_a_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_154_4_a_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_154_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![-8, 5, 1]>],Snew);
    return Vf;
end function;