
// Make newform 8.14.a.b in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8_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_8_14_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_8_14_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 := [-195, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-64, 128]];
    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_8_a();"
function MakeCharacter_8_a()
    N := 8;
    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_8_a_Hecke(Kf)
    return MakeCharacter_8_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 := 14;
    raw_aps := [[0, 0], [436, -1], [9238, -12], [55464, 222], [8237020, -531], [9372286, 10452], [-76896814, -38184], [-59373820, 93819], [359134456, -111462], [154670670, 2374788], [2883752096, 1482936], [-5810776650, 10246116], [655584138, 8079216], [-14797810052, -9339771], [6156808944, 16143540], [-19003003514, -50827740], [126672955852, -136967391], [-323622192146, 14150004], [809996903156, -36810993], [-520135071256, -352709874], [2002641954346, -86109192], [-1260888786032, -525520788], [-145243115452, -996493749], [-4361877828870, -639382536], [4800856149986, 4881770040], [-3101042841354, -2944922940], [11658523719304, -5210397690], [4051661354940, 11376070485], [16786827221854, -9296420700], [-12556592317518, -9217305024], [11322365270656, 24141708192], [24352808286260, -14665322433], [1433808585834, -48964251984], [-4102456461732, 38389361709], [-162197984777626, 50506925556], [11276572575608, -65548894854], [-73801644338, -52333914924], [-301060311684396, 58713127767], [458823886125000, 17555177142], [208797724576926, -160919708172], [171666195735588, -298166415693], [-376495322580474, 123796035924], [325656638675904, -80024420976], [-408547934751550, -489057346824], [-52362052919850, -140861844588], [-117022950882200, 103359928878], [-468706686168380, 744995405931], [-2299277020298144, 39073292040], [778534069399572, 1488372269319], [2396274501355894, -942823051500], [-722097394599350, -58900998216], [-280184600707408, -476718006972], [-335267108191950, 203169254232], [1941172814563724, -1390336173711], [-1505907588133630, 1088873041920], [1021333332465064, 420815713374], [-7480562218338946, -1471885324860], [-1956607211694256, -621263639940], [8393514704213734, -468893260764], [-6134134754271046, 62383168536], [-1535687264145364, 412785910857], [11583553795139766, 3091368564948], [1628215716645540, -6960405806733], [-10072973282077032, -527627643246], [914104520078682, 14287928899824], [3914614444696302, -4360911028860], [-26479130594700772, -5863210764195], [-14685983781328110, -11374100432544], [6858305285618028, 14561759554521], [-28137628351791474, -6655801891356], [16682771653653986, -17036638208544], [-15523841598882936, 8438596153542], [-39248394462602960, -16883822584860], [-18585141020238394, 22965356100996], [5467451109567500, 23170521058065], [-50170009079560320, -7613198561376], [57498198467239318, -40608054611292], [29193765109669886, -6266688672348], [-59452343664459438, 28676602889496], [21016769471358522, -3078394565712], [77485040924453844, -35775692277513], [106452572472320822, -12317990501196], [-53910148336991632, -110585297068908], [-53516348061707534, -28442384775144], [142662746516793624, 4633154114418], [-105051463482313396, -9457055826015], [-52294956553268798, -105444506590344], [238233131323788458, -70524513707088], [109609409426205502, 84525052747140], [282707356072321808, -74114149536756], [-5434718382228540, -85029551526837], [104542607652432224, 151676712209352], [-278242375531612664, -98176109551386], [147478333565965116, 251892487020885], [-303129727955739036, -99295764516957], [262672643338033752, 159088573997346], [-342779948060886354, 143110356353172], [56584914112745450, 36292980979440], [169029014438482396, 173615776790061], [195999016900892238, -139667248140300], [-938179644349675948, -13613436667689], [-143401455370836962, 270719514108756], [315581911832223652, -177593414541261], [-1015178536654691750, -277720475707152], [-306512524404740148, -282092232442623], [45881768665508162, 48550882452960], [1052296833062207260, 29205798152157], [802898946133316626, 358249319047872], [-835031828698001352, 570059990280138], [-268794042292017926, 440562479020536], [513854320541602784, 719209615561704], [-1288153330226378506, -72812603738316], [-277547979980717622, 643849436254296], [911794463994628540, -557690983668459], [436343291402693848, -375268277126142], [-84509990238645374, 1647876310003512], [130955073574742580, 176045075285247], [-2265352841851843288, -245467411591170], [-107630038004614914, -105436621882620], [1311373770914436996, 349359065868987], [-676616696861618074, -819248031284268], [2636230233353348386, 253744446621432], [3706108246233519670, 362823288813732], [-122076058573765892, -899331434014299], [-190669233329476060, -92290511830221], [-197058604623942546, -1361393192738508], [-3865119766478990890, -1348280819616732], [-373158879350728688, 3328582868721996], [2784522984207852216, -351736738053462], [2246620378553562638, -3177738183239868], [-878755884036343276, -819039298721913], [-3709699775683421432, -1776969375436122], [-2331672323041519440, 1199836209979716], [2119173922122954566, 3726821858358324], [-3861216437157977062, 2264219712328176], [-3024881121144393214, 1980049779973368], [-1840110033041936106, 2377689055059684], [2189940008111675908, -1919273471879205], [-6717660507502643378, 3433533653768340], [4710535011943957130, -204044777180688], [-2784694362845673092, 2507137587242469], [7378553097218203782, -4296578927008380], [4099288676134600856, -4639650215811822], [13918576603488595148, 2091701101677825], [-5766907666193877266, 2130279085790484], [2369669114101944552, -5910807902756082], [12117087310040401958, -2964201452464860], [-4835255050680251142, -66428762538384], [-12010535761478061204, -326546818323495], [-2216261801586171680, -3808425667709208], [-3052797433303642530, 4084291830493956], [-3535103566368885070, 8137112344917600], [-14142973364448253468, -3229648967646813], [-19186016502676993576, -1486929666248574], [6438000077680786780, 2768820431534253], [6797310004013151184, 14890942151482716], [21927312338110408312, 3907090996832826], [-8094028947862748638, -11171490668034696], [-389175278797064694, -15006899163630024], [-25331438629420099170, -790903224575004], [32224161953878490916, -1152534590074029], [6191839328474828250, -12797838192923856], [-14454390944454204568, -7556601544753938], [17651490345730331292, -12899516632136547], [-20200637757347223918, -4692412203430440], [20182615726343559096, -7176694881982230], [16247806767578614624, 892449303570696], [-17028285195418375562, -21402942717482460]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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