
// Make newform 7.8.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_7_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_7_8_a_b();".
// 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_7_8_a_b();".
// 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 := [-216, -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_7_a();"
function MakeCharacter_7_a()
    N := 7;
    order := 1;
    char_gens := [3];
    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_7_a_Hecke(Kf)
    return MakeCharacter_7_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 := 8;
    raw_aps := [[-1, -1], [46, 2], [160, 10], [-343, 0], [1480, -116], [1708, -882], [-906, 324], [16834, -858], [-3312, 48], [15010, -9380], [-197172, 2508], [170106, 27180], [379190, -23716], [-237424, -5628], [-563004, 36900], [1432014, -5184], [53274, 53622], [-403544, -57558], [-189788, 50928], [-3684672, -130872], [2054658, 191928], [-3342760, 277080], [5895834, -132594], [4704538, 363184], [5428710, -94668], [6608480, -438646], [-1313236, -1137084], [11725636, -329960], [794538, 831204], [7527046, 1214332], [-21196656, -752136], [10555474, 2200726], [15611970, -553368], [7644126, -2150022], [-7653738, 500856], [219320, 529224], [-9256628, 3241758], [-41093816, -2187564], [9073540, 3447052], [25643204, -2843194], [-79478788, -1861864], [-61925948, -574770], [-30085784, -2797208], [23967758, -1348092], [11628598, 3328696], [-18684732, 5224284], [22747972, 8164800], [-61008248, 13318200], [153680370, -7867578], [-36754516, 12689862], [244611786, -3469488], [-117684264, -12499872], [-81355354, -8756508], [-210596834, 3295642], [139931378, -8320624], [-357544352, 13679368], [-302521404, 6602598], [9567088, 417168], [-131634626, -9998496], [-121291798, 17316320], [412883818, -13962282], [-223314952, -19215742], [323060962, 29110998], [411124872, -16524240], [-16023610, -12609636], [-168057954, 14654520], [118149296, -23630748], [95194318, -53913804], [-254580400, -48567628], [545766676, -18522], [-136739438, -15930080], [123674648, 73248104], [668762280, 9522312], [198383030, 39546744], [-816213480, -30008580], [-177381324, -9975228], [1593161034, -43102308], [164253060, -76689498], [-1082913042, -15070812], [-403062754, 83148228], [20550390, 25770570], [429293342, -88322640], [-1025433672, 24421008], [-450698738, 170805180], [2642950344, 31359072], [86434956, -136591008], [2111286354, -17936688], [-2161215386, -89015676], [-454877668, -166598530], [-1741253456, -2108064], [491011002, 97332798], [-946245940, -97844500], [30063232, -92997552], [-265483636, 175300496], [2998423588, -113441616], [-2081079840, -48840456], [2702567636, 68053478], [-1302666858, 316337100], [1818841282, 101857710], [-979947970, -231778296], [-42114560, -149427348], [4615683838, 65395816], [-4370761202, 22512898], [-1902609962, 214381684], [2265235304, 53779596], [972110626, 89332560], [-905351930, -386060318], [4134563418, 194842728], [-2098097880, 496394160], [3015054714, -146922384], [-4789732480, 71598432], [4474267066, -300935268], [-2167479690, -20832588], [-363315494, -429338922], [4042719536, -136110408], [3081857166, 46961652], [-9530497186, -79056894], [587802884, 308731964], [2693644218, -114913212], [-9100768416, 322902972], [5897339032, 229930290], [4582365370, 339152184], [4865012676, 201836190], [-2911675764, -543879648], [-4666100398, -188917530], [-8480952870, -244275276], [704873994, 759352572], [3479947260, 298921500], [8305747772, -346737132], [-9092238408, -696923910], [6471846216, -51074412], [16395146280, 337499064], [-5004188456, 1082184816], [-13525838758, -197846292], [13013987454, -243855084], [1438982174, 224433132], [2444967032, 1185178850], [-17362277558, 29911758], [-14999961788, 296306038], [-7381722954, -814610892], [14600101922, -199283490], [-415722994, -516401488], [9334434840, -99463536], [9621184596, -1048727400], [3935875640, -272986950], [17678749564, 681475732], [-18829294372, -895072122], [20069197726, -181345820], [-5313444130, 841851930], [282150840, 1453112184], [-15183814094, -941545620], [-9258664758, -988245720], [-12355919812, -597743832], [24630547724, 706440164], [12133118084, -118579416], [3351016272, -691021800], [11340389528, 429873744], [15634665206, -1574472652], [-17774256990, -77548968], [15303859400, -884153926], [-6025167856, -1035719476], [22775571674, 114649136], [6329363728, 2054799936], [-928219398, -2318384730], [2288835258, 2683933416], [-6852022068, -1340424300], [-28282310368, -846873360], [47829969352, -10659822]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_7_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_7_8_a_b();".
// 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_7_8_a_b(:prec:=2)
    chi := MakeCharacter_7_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 8));
    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_7_8_a_b();".
// 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_7_8_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_7_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<2,R![-214, 3, 1]>],Snew);
    return Vf;
end function;