
// Make newform 27.8.a.c in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_27_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_27_8_a_c();".
// 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_27_8_a_c();".
// 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, 6]];
    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_27_a();"
function MakeCharacter_27_a()
    N := 27;
    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_27_a_Hecke(Kf)
    return MakeCharacter_27_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, 1], [0, 0], [0, -34], [-559, 0], [0, 454], [-8671, 0], [0, 2418], [-32461, 0], [0, -7930], [0, 15184], [229892, 0], [-541177, 0], [0, 34016], [-465112, 0], [0, -79922], [0, -98748], [0, -75562], [-137773, 0], [-314041, 0], [0, -270372], [2669537, 0], [1101815, 0], [0, 584956], [0, -316230], [-2979379, 0], [0, -264368], [-5871091, 0], [0, 562770], [8946938, 0], [0, -1257334], [19673084, 0], [0, 824408], [0, 5351446], [27798917, 0], [0, -7489876], [10463219, 0], [-65781418, 0], [-30096547, 0], [0, 7611374], [0, -2706080], [0, 2933268], [2988029, 0], [0, 3266146], [-157243801, 0], [0, -6062874], [192815381, 0], [-127804927, 0], [-66565252, 0], [0, 14088748], [-255727966, 0], [0, 5311176], [0, -17569684], [17244305, 0], [0, -4699032], [0, 7904156], [0, -19977932], [0, -32914206], [67403531, 0], [208092194, 0], [0, -24509408], [182075792, 0], [0, 52377290], [-534564160, 0], [0, -38459902], [-913257973, 0], [0, 39515452], [-929744803, 0], [-573925099, 0], [0, -105169636], [728528255, 0], [0, -56475620], [0, 115455378], [-1315505755, 0], [-1077252097, 0], [773953895, 0], [0, 122243024], [0, -104536562], [95219462, 0], [0, -138867724], [2387241545, 0], [0, 221833742], [-1209376207, 0], [0, 276437862], [-1793022478, 0], [-310244356, 0], [0, -58952660], [0, -83930610], [3900560534, 0], [0, 202881826], [1443486785, 0], [0, -196006434], [0, -67437596], [-215479303, 0], [0, -111341230], [-3244798336, 0], [0, -164822970], [0, -21886138], [0, 403713102], [1773936623, 0], [-3745595815, 0], [-2143524619, 0], [0, -341044014], [0, -130253968], [0, 106913170], [-1857650125, 0], [3260561915, 0], [0, 817694314], [0, 447811452], [0, -114972208], [7189324730, 0], [-373407223, 0], [-2577605173, 0], [0, 826168670], [-7912643869, 0], [1123263947, 0], [0, 443971996], [-282325576, 0], [0, 1058262192], [0, 149358404], [0, 789106396], [-823211077, 0], [1467214235, 0], [0, -335986586], [0, -1453423416], [12628046600, 0], [0, 1467952434], [-17599822903, 0], [0, 980261412], [736506836, 0], [1654038698, 0], [15633282080, 0], [0, -1107196330], [11216282063, 0], [-17072186911, 0], [0, -260544814], [-8213379889, 0], [0, 975116796], [-7908055285, 0], [0, -1317090424], [0, 928772676], [29356546232, 0], [0, -140572778], [22981853867, 0], [0, -1215029442], [-16324899307, 0], [0, -1122501500], [-1227015361, 0], [0, -2145160316], [11132525417, 0], [0, -1036807650], [-4318857985, 0], [0, -2398147986], [8754210101, 0], [0, -69125212], [20297539511, 0], [0, 2806081720], [-20738350348, 0], [0, -794987564], [-47888367973, 0], [0, -2444470390], [0, -3964031846], [0, -1675026366], [-9305799529, 0], [0, -1963685646], [0, -806213284], [0, -3088356638], [-2264487307, 0], [9293254766, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_27_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_27_8_a_c();".
// 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_27_8_a_c(:prec:=2)
    chi := MakeCharacter_27_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_27_8_a_c();".
// 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_27_8_a_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_27_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<2,R![-108, 0, 1]>],Snew);
    return Vf;
end function;