
// Make newform 12.8.a.b in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_12_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_12_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_12_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_12_a();"
function MakeCharacter_12_a()
    N := 12;
    order := 1;
    char_gens := [7, 5];
    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_12_a_Hecke(Kf)
    return MakeCharacter_12_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 := [[0], [27], [270], [1112], [-5724], [-4570], [-36558], [51740], [22248], [-157194], [-103936], [-94834], [659610], [-75772], [405648], [-1346274], [-1303884], [1833782], [1369388], [2714040], [2868794], [-1129648], [5912028], [-897750], [13719074], [1933038], [4761272], [2210436], [-20092858], [7940754], [-22739488], [-22778388], [41698746], [-6194716], [-5416578], [70844360], [-79080106], [-24449140], [35553816], [102794022], [-41558724], [-33501250], [49080384], [60448322], [5004558], [-221134888], [-71664100], [26144000], [-34099380], [227427854], [56906010], [302715792], [-25369582], [-14654412], [-23465214], [425583288], [-393061626], [58955984], [-21597346], [-334717974], [326806484], [-728964306], [-126039364], [-715768056], [-291195958], [42428502], [-547256668], [-185332174], [1297110996], [-622135786], [620687394], [606882456], [-1405384528], [-1129248322], [1635149492], [-227684736], [-623119986], [-1495834], [-306500814], [356868074], [-2284281972], [263134670], [1490529168], [1105390802], [296352872], [358944372], [-3419482878], [-2858275270], [962699526], [601892624], [1720599516], [-537261984], [-4496210056], [3329173764], [-2049857284], [776491272], [-4566792042], [-2211592518], [-2341539676], [2354970518], [3414933644], [-479293146], [6503021820], [-4516247286], [2473754420], [-531720766], [8171104932], [-1599227982], [-8803296216], [8212347818], [5091919616], [-7012818226], [4637135898], [1339167236], [-11235380248], [9823803714], [3562395692], [5911190136], [-770538042], [4952706012], [-1203185122], [13082272610], [-14937960978], [-9504153468], [1878926588], [-10659148074], [-15846448594], [-17848243728], [14112349064], [-5553243562], [9726138380], [837461592], [-3054215248], [17725000958], [1282680522], [15077765378], [12673665486], [-1466843236], [-14962924746], [10626300954], [-24430594300], [26856767646], [5829138344], [-13176118476], [19963398518], [21943306296], [-21310189474], [4659955434], [-1684203820], [1086323616], [11144291078], [-37884469230], [15813099836], [-15217090488], [-15239522524], [-14944664592], [-16529148088], [-25817045598], [-18342780454], [-6333459930], [20195775036], [48617240202], [12377800280], [-17059670172], [39694489074], [-75539736], [-29902583872], [12425438798]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_12_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_12_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_12_8_a_b(:prec:=1)
    chi := MakeCharacter_12_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_12_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_12_8_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_12_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<5,R![-270, 1]>],Snew);
    return Vf;
end function;