
// Make newform 99.6.a.f in Magma, downloaded from the LMFDB on 24 April 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_6_a_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_99_6_a_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 := [-44, -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_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 := 6;
    raw_aps := [[3, -1], [0, 0], [-34, 10], [-148, 10], [121, 0], [-102, 38], [430, -60], [-732, -12], [1868, -366], [-3094, -412], [-3936, 344], [-14890, -136], [2534, 712], [-7580, -1496], [-5188, 2526], [-5986, -2206], [-9388, 1476], [-2638, 2330], [14068, 3200], [15356, 3098], [26554, 7536], [5676, -9482], [30444, -2592], [-38666, -15056], [14210, -21300], [24578, -8556], [-36192, 11380], [-5156, 4648], [-36070, 310], [-206722, 5336], [-108036, -31994], [-140564, 31384], [293918, -13620], [-74660, 47244], [-3942, 47728], [98132, -35754], [-16058, 2996], [-257300, -34368], [113888, 65804], [100602, -6300], [222828, -61236], [385646, 41452], [-140356, 32166], [126922, 74124], [-611278, -15260], [-220560, 50532], [168604, -39224], [-274240, -88448], [1176692, 25232], [-68514, -38652], [557398, 87280], [836472, -15780], [-517910, 127548], [-192668, -182628], [250430, 36592], [838872, -138872], [-1302490, -135422], [-1032764, 187034], [-454278, -60142], [-1288026, -103788], [1444740, -16936], [1196794, 183112], [1173444, -42260], [-460500, -111894], [-237894, 78976], [-229490, 167646], [1186900, 122472], [721586, -501288], [-487020, 393896], [-1784038, 382142], [681902, 192104], [2110656, -138652], [2034960, 288920], [-2328718, -170114], [3179972, 7408], [1251124, 345722], [1516414, -70934], [-1087418, 100740], [-2987802, 248452], [-242062, 77852], [-4865876, -157156], [-3304858, 14920], [5194976, 6992], [779650, 652488], [440436, -954178], [-2592252, 45980], [569302, -646012], [-850750, -590956], [2840546, 135824], [263752, 802620], [2288500, 141256], [-778272, -711800], [-2543472, 492796], [-1775844, -582784], [-145316, 942728], [-38304, 1062188], [-3113450, -218942], [-5494866, 690300], [5178548, -182400], [-8555462, 437022], [-361948, -68700], [-1732078, 90328], [91412, -859544], [1872902, -826060], [-1441844, 285116], [3261666, 115692], [335524, -265284], [8628046, 34772], [8708108, -214838], [-9862518, 708400], [4229996, -1778538], [-5202278, 1639466], [-4580618, -1040384], [13736940, -37520], [-13887176, 424536], [-1867562, 1563316], [3143348, 1458696], [12317772, -63614], [-1365810, -1524202], [-10136124, 772664], [-11035106, 78900], [-11516486, 865100], [2718466, -625748], [12031988, 305508], [-16560412, -1044336], [-2593262, 1067552], [3837758, 2082948], [-8815260, -144486], [1407328, -2667380], [1778762, -2665930], [-9866900, 644048], [1301152, -938076], [-7291168, -1078904], [17250550, -1621312], [-5908570, -1841068], [25347146, -697908], [-5796466, 2109698], [-2333332, 2125768], [-15403402, 919410], [-10814442, 1422528], [13023460, 4704], [4937842, -4900692], [16477496, 427184], [-15581980, 1222536], [2707870, 648384], [7283748, 4407782], [-19421094, 1152226], [21259718, 256272], [-15306908, 344656], [17034268, 1789222], [-20886582, -2466066], [4933806, 3672856], [9488436, -2848360], [-27104000, -2290276], [1120108, -1417592], [-890044, 1060994], [1521260, -1636574], [-9560618, 350212], [5688602, -567984], [10906010, 2300852], [-38670796, -581944], [4625318, -4570208], [-36060092, 557358], [-30517516, -2620332], [30309062, -2470996], [14940236, -3001030], [33372288, -1465672], [9795250, -3919834]];
    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_6_a_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_99_6_a_f(:prec:=2)
    chi := MakeCharacter_99_a();
    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_99_6_a_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_99_6_a_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_99_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<2,R![-38, -5, 1]>],Snew);
    return Vf;
end function;