
// Make newform 3.14.a.a in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3_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_3_14_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_3_14_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_3_a();"
function MakeCharacter_3_a()
    N := 3;
    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_3_a_Hecke(Kf)
    return MakeCharacter_3_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 := [[-12], [-729], [-30210], [235088], [-11182908], [8049614], [-117494622], [-214061380], [830555544], [-1252400250], [6159350552], [-5498191402], [-4678687878], [7115013764], [-29528776992], [-204125042466], [-29909821020], [-134392006738], [348518801948], [1314335409192], [-1178875922326], [-1072420659640], [1124025139644], [2235610909530], [-14215257165502], [17019415289742], [10990429870784], [-19640332937772], [-9820991409010], [-17026697549166], [-44934655354072], [-1201824485988], [17156152133178], [105644089827860], [-85353252521010], [-61641427803328], [-118020796127122], [154710153734924], [376012022331288], [373562385120294], [42334856171820], [-310446880718218], [-862273101479808], [-937836643014526], [-671714610973362], [-43645139278720], [-1621621640538868], [1473330009613544], [-3748889415542772], [-14799292425850], [3630529144975914], [-4339000266991440], [3023722695906962], [-1751455209412908], [4876036408101378], [4678821543034104], [-1802619594866250], [6100162734217592], [-10702338294035242], [-2454600608693958], [4011554116057604], [2081866149710094], [-13235219589981652], [-8093014030966488], [-14818112378698726], [-24317052393895482], [11623168076685812], [46265248280338898], [4794041266797108], [37689996015206990], [48017883307283394], [40661600992602120], [29673300626881928], [-90134578399742266], [-15484136763879340], [9370880885351424], [29580560950821630], [18061747254883838], [-120412338372532158], [-17752187046553510], [175670394916722060], [184473355563153062], [80553231525379392], [-197091993816489166], [98900719348059440], [-125103588906174636], [-180094876495581870], [-94359735356483062], [80050008235378182], [214173898288828664], [-180680568239247012], [-266712175160724720], [-263551809580416592], [411733255212655572], [399657893624094380], [-283580918412321096], [640326900632610630], [-401347517619457638], [-505985210228308156], [-169124296693931458], [-432104161768514452], [1368041672491465158], [-952405281999985956], [153631850757108810], [-1279564588105932028], [356769908131216898], [1289684669126598708], [-1886401262114136846], [-1446677070600326280], [-44435774160833158], [29805045055555208], [-884082193304375626], [1436836808847628938], [1688623164304257140], [-353490178580679328], [1618021270187842002], [-1960652047007687476], [-596113924824003672], [-2583179311300172826], [2641366001552122860], [4124511248489144582], [2797256078346568994], [-4255534250446409922], [1608930463689743364], [-3063314825700705028], [2991437193856022982], [-5316937814777947930], [-4031529113805099840], [-4776426414945960832], [1716681243640173854], [-869722540682337700], [2402718896241612984], [9371752053600359912], [3091198274554751318], [-7977871652476589718], [7373436784822403330], [-1673351637239220786], [-3753588992895967972], [3388531507318303158], [-3131192643290467110], [10473099865314978692], [-6851622106721884098], [306934487430456704], [7753651513750186308], [2343362495933519390], [-16329737288829562680], [19379431935392705414], [12053714055262658538], [-10061197000379391820], [8014071791272343904], [887791325937727838], [3787737592660941042], [-27542811574029009796], [-28416466180617255672], [7510233078116348228], [-23402776617717897648], [22088090744681728160], [-10563249724354118190], [-17283283288395126262], [21403762991627970342], [-591308436260262612], [-23939617111356889206], [1990454979252485408], [41238481315770147492], [-12537361145705977662], [-18396640167476237976], [-34306388283336427528], [-57245767419897960202]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3_14_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_3_14_a_a(:prec:=1)
    chi := MakeCharacter_3_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_3_14_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_3_14_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,14,sign)));
    Vf := Kernel([<2,R![12, 1]>],Snew);
    return Vf;
end function;