
// Make newform 99.4.a.d in Magma, downloaded from the LMFDB on 05 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_99_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_99_4_a_d();".
// 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_99_4_a_d();".
// 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 := [-3, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [0, 2]];
    Rf_basisdens := [1, 1];
    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_99_a();"
function MakeCharacter_99_a()
    N := 99;
    order := 1;
    char_gens := [56, 46];
    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_99_a_Hecke(Kf)
    return MakeCharacter_99_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+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, 0], [-10, -1], [-8, -5], [11, 0], [40, 11], [-82, -6], [-18, 30], [-86, 9], [-54, -56], [-224, 26], [54, -10], [-106, -4], [78, -92], [10, -21], [66, 187], [-344, 102], [-48, -67], [224, 128], [-66, -275], [214, -36], [212, 1], [-360, 30], [-528, 176], [26, -432], [-1466, 102], [200, -272], [1500, 128], [-1400, -5], [-180, -128], [116, 385], [600, -94], [472, -186], [982, 450], [-314, 380], [1172, -51], [-2182, -238], [388, 348], [1056, -176], [-1014, -504], [2340, -558], [-974, -584], [10, -681], [2254, -654], [-3202, 122], [336, 390], [-1038, -476], [-3008, -740], [-108, 1648], [-3966, 534], [1854, -160], [2640, -1056], [3562, -138], [212, -630], [-160, -676], [5428, 446], [-4198, -169], [-1468, -217], [3604, -979], [-4734, 1170], [-266, -1108], [1818, 1922], [-202, -1478], [4242, -183], [806, -1004], [-7102, 513], [-6212, 252], [-7234, 1248], [3800, 2074], [3036, -1453], [3048, 736], [-5692, -1502], [-7544, -964], [1252, 2815], [3220, 2056], [-662, 3181], [-5010, 1127], [-6254, 2076], [7000, -1390], [-12822, 542], [3460, -842], [-3986, -3098], [-3540, 2902], [-7250, -1368], [9448, -79], [11192, -38], [-5732, 1354], [1362, 2174], [-3798, -1922], [-1280, 2664], [1212, -2356], [-3044, -718], [3008, 2734], [-1660, -1376], [-4696, -1420], [-2800, 2668], [-6438, -349], [14760, -1890], [16358, 1104], [-5696, -1989], [-14230, 822], [4346, -2938], [-820, -1732], [15846, 1534], [11882, -1462], [6018, -2124], [-25000, -630], [-3254, -1322], [-7726, -1723], [-12286, 100], [14996, 441], [10568, 2213], [11940, -3184], [3788, 268], [-16184, -4386], [12372, 2786], [-316, -384], [9586, -4735], [-2122, -287], [-12364, 4708], [1834, 4590], [-9586, 614], [-3450, -1918], [11644, 3690], [9236, 1356], [5170, 6754], [12218, 6306], [-14010, -4107], [-21048, 2194], [-14784, -2605], [-14002, 1136], [-21652, -414], [-5288, -3446], [7214, -658], [2022, -4838], [-5890, 1482], [-21486, -2153], [-17042, 4012], [-8114, 7443], [9546, -7104], [14914, -8040], [-6754, 2574], [15432, -196], [-7496, 1446], [6406, 3576], [-44146, -569], [21344, 265], [10930, 4404], [-37716, -104], [-3738, 10019], [-5964, 4491], [-12572, 3332], [-6824, -3472], [-20228, 1594], [23028, -5468], [-4866, -3131], [13876, 1519], [10992, -5470], [9482, 612], [2550, -3122], [15024, 904], [10706, -8440], [28948, -6555], [26032, 4218], [36600, 1570], [-25350, -467], [15136, -2926], [-33748, -757]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_99_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_99_4_a_d();".
// 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_99_4_a_d(:prec:=2)
    chi := MakeCharacter_99_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_99_4_a_d();".
// 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_99_4_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_99_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-11, 2, 1]>],Snew);
    return Vf;
end function;