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


// To make the character of type GrpDrchElt, type "MakeCharacter_147_a();"
function MakeCharacter_147_a()
    N := 147;
    order := 1;
    char_gens := [50, 52];
    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_147_a_Hecke(Kf)
    return MakeCharacter_147_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 := 6;
    raw_aps := [[-6], [-9], [-6], [0], [-564], [-638], [-882], [556], [-840], [4638], [-4400], [-2410], [6870], [9644], [18672], [33750], [18084], [-39758], [-23068], [-4248], [41110], [21920], [-82452], [94086], [-49442], [143034], [-53144], [90828], [-61666], [10482], [-171088], [-258468], [300234], [350164], [-105258], [396392], [137746], [352676], [217560], [163698], [358740], [507130], [-648384], [-27838], [611046], [-879032], [48500], [999472], [-606180], [-1359926], [-392886], [-1325136], [990094], [-147132], [483582], [813576], [461106], [-1675136], [401126], [-2309766], [1128772], [938874], [-692948], [-2943096], [-885146], [2508798], [-216148], [3252626], [-2932068], [-905198], [-1917858], [-2436984], [984064], [1703654], [2756540], [-456576], [-2006394], [5770402], [3006258], [-1533626], [3873756], [-1333066], [6451920], [4165774], [-792680], [-1399812], [2992482], [6299690], [-3403182], [-2230336], [6514092], [-2392320], [-6130888], [-1235892], [-9854956], [-1167768], [-1049406], [9614070], [-6961484], [-712690], [-3626140], [4848462], [-8504052], [362874], [4110236], [7876798], [-603948], [5390766], [4279992], [-1024826], [-12434192], [4215062], [-4406646], [-4801676], [8307272], [17695650], [12839356], [20846808], [12963246], [-5668620], [3114298], [105890], [16091034], [16077996], [165964], [17724798], [-10602346], [-9032112], [-18757544], [11777266], [5889476], [-10047576], [4815296], [3129734], [11777286], [1493758], [22512474], [-11954708], [-540798], [-6142230], [31673428], [26617494], [36281672], [1090332], [10301650], [19613400], [-32756486], [25795302], [19854820], [-673056], [5321150], [-27889074], [-28378636], [-42267768], [31952588], [-11642928], [1398440], [16679166], [24795574], [-27957390], [7649364], [-46217862], [20855672], [45815220], [-1095438], [-52581720], [-49038928], [-3054614], [49496690], [-7459062], [-43687716], [26022322], [-49542456], [-1720534], [-53993888], [-57889914], [10874300], [21252006], [51822376], [60108610], [-71831920], [37790844], [13671062], [-42401898], [23190864], [38003370], [19108622], [-21170948], [36795338], [11327616], [-17413634], [1988652], [69748148], [18026754], [-15610428], [92146998], [-30128014], [34753262], [-60129858], [-50163336], [-48561618], [32625472], [41342554], [-61587554], [-6298092], [-98944830], [48283216], [11715396], [5052522], [29748916], [12074834], [-119285142], [-64417720], [-32076732], [93700344], [34509622], [33634304], [34781262], [-4722984], [36786174], [46001654], [-73107368], [-24428190], [111809792], [105501612], [-25304890], [-44788026], [-92056896], [95545192], [73170444], [91088990], [84181516], [113842736], [85847658], [-130499852], [34589328], [-82761938], [90650646], [106650204], [59684472], [14184372], [-57860444], [74811304], [-118096738], [55365294], [172675464], [-129246896], [35563260], [-99815284], [-15241104], [57531950], [31162878], [-142521240], [-119708906], [-4936206], [186726708], [-104967898], [-69230012], [79981434], [-56389466], [55266224], [309540], [-79229078], [122450578], [43663998], [10529788], [-220464306], [18010662], [-213343300], [16030662], [-90633374], [151165748], [129727174], [114719408], [-230809778], [60719704], [27279996], [79508750], [-86532694], [-47012340], [182562240], [197520344], [202462440], [197520394], [255313972], [-23428080], [-37932334], [270558294], [99573592], [-115838370], [149650446], [205185132], [-68666214], [139877076], [306839966], [-122823870], [180455728], [-261676758], [127558980], [-308998724], [144842518], [-90943506], [-60780832], [173637492], [-45145252], [-242938078], [-227970132], [229654306], [233645784], [102241334], [283153776], [264205494], [-76236990], [-143341084], [387341928], [-138723914], [136500972], [-60145248], [-119673410], [-270004782], [5125900], [125237690], [-170604126], [-183542416], [292633194], [-331066226], [-54997180], [-357835676], [367718304], [-33165018], [232260062], [-97188258], [85360720], [-229873188], [-450344212], [-148803804], [-43028818], [-151618722], [13440502], [132896608], [-380684506], [-103149510], [222919194], [230563832], [-207430818], [-46385820], [-172737398], [242263436], [269936880], [178675434], [-102373316], [-87903574], [-129846450], [-181384448], [-324524026], [-166337082], [40462176], [-7927884], [162967506], [-533659848], [493988342], [130611606], [212591376], [292446876], [500848252], [168525770], [-129290862], [376276952], [168523706], [-164812956], [-131024684], [-377527440], [518700054], [-29566664], [47949230], [-474015444], [200281248], [162225502], [45734958], [622446502], [-542826462], [-617247606], [-518449624], [-46887198], [-33978788], [337130136], [73937984], [-90828998], [-383888356], [96009312], [-472298494], [658386618], [501163308], [266418220], [-48893352], [-399817414], [-731781200], [298528854], [100898540], [-10211466], [-107946062], [441127458], [198794240], [-68360730], [494515386], [407324680], [-36976738], [-591194094], [563301964], [-505846596], [-538537298], [285479958], [415979100], [-649736284], [472130378], [525587346], [-53948736], [275496232], [-702402930], [-152983608], [615081342], [-85776086], [608583216], [212865924], [-741865898], [398119602], [99758100], [-431811270], [-752966876], [732623784]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_147_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_147_6_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_147_6_a_a(:prec:=1)
    chi := MakeCharacter_147_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_147_6_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_147_6_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_147_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<2,R![6, 1]>,<5,R![6, 1]>],Snew);
    return Vf;
end function;