
// Make newform 81.4.a.a in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_81_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_81_4_a_a();".
// 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_81_4_a_a();".
// 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 := [-8, -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_81_a();"
function MakeCharacter_81_a()
    N := 81;
    order := 1;
    char_gens := [2];
    v := [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_81_a_Hecke(Kf)
    return MakeCharacter_81_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], [-8, 1], [2, 3], [-37, 8], [2, -15], [-45, -9], [-25, -27], [-26, 19], [26, -1], [20, 3], [-52, 54], [-17, -98], [47, -6], [-154, -91], [-108, 162], [-467, 136], [272, -105], [461, 66], [-612, -144], [-349, 243], [-556, 309], [-460, 107], [234, -72], [317, 102], [872, -91], [-1042, 21], [837, -81], [1136, -270], [1234, -161], [-1078, 54], [-80, -467], [1835, 242], [533, 30], [550, 847], [182, -573], [-988, 939], [1460, 216], [-1196, -215], [-10, -937], [-2340, 288], [-1402, -756], [-1054, 1025], [749, -618], [3420, -234], [110, 702], [-124, -789], [-1510, 435], [1475, -640], [-754, 1497], [1845, -1575], [-3158, -737], [3089, 1038], [-2709, -423], [3493, -44], [-5122, -415], [-2376, 1566], [-2428, 324], [-5290, -915], [-766, 1763], [-3940, 255], [-5534, 559], [2297, -1701], [-7118, -17], [-1195, -1086], [1682, -2737], [-6964, 1137], [-6721, -744], [3943, 1234], [8408, -1581], [53, 1808], [-1062, -666], [-1492, 1317], [3278, -627], [-2833, -351], [3880, 2053], [-1090, -289], [-1564, -1890], [4915, -2222], [6473, -186], [604, 2413], [1532, -141], [-1674, -2430], [569, -27], [-7072, 1785], [9197, 386], [-1017, 2475], [5465, 498], [-10450, 1403], [3692, -861], [12303, 1413], [-6310, 1421], [10370, 702], [6823, -3698], [-1213, 3576], [-7200, 5796], [-4184, 2989], [-7245, 603], [-6532, -972], [11180, -918], [2783, -7962], [-6282, 504], [14959, -3518], [107, -6130], [13115, -4236], [-781, -3645], [6569, 2366], [2106, -6264], [-980, -3023], [299, 1272], [5132, -4929], [-21328, -486], [-3131, 3448], [-4093, 5466], [-376, 5400], [-20485, -1738], [-25369, 1128], [-14202, 2970], [-1898, -269], [14948, 1223], [-2428, -6549], [146, -1095], [-22060, 1241], [1665, -1341], [-4408, -3327], [-16470, -324], [17444, 759], [-6444, -5652], [-18214, -1689], [12062, -6099], [7319, 5481], [-2150, -7469], [-3472, 2019], [5834, 8100], [1342, 2863], [2378, 9165], [4086, 936], [-4084, 6375], [-12140, -5039], [11583, 7371], [5753, -7317], [13058, 1439], [-12472, 8895], [22500, -1440], [1838, 12852], [-11476, -4537], [-21094, 3909], [-6094, 1259], [7571, -7836], [22680, -7344], [4592, -8709], [-342, -1008], [28973, -4401], [-32696, 1801], [23321, 8688], [-17704, 2231], [-41794, -1134], [26558, -3961], [-9826, -1080], [-10124, -1115], [-10099, 1430], [18999, 2115], [-6982, 885], [-11556, -8964], [-11429, -242], [-13672, -8929], [29216, -7128], [37244, -3417]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_81_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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