
// Make newform 1323.4.a.l in Magma, downloaded from the LMFDB on 03 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1323_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_1323_4_a_l();".
// 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_1323_4_a_l();".
// 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_1323_a();"
function MakeCharacter_1323_a()
    N := 1323;
    order := 1;
    char_gens := [785, 1081];
    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_1323_a_Hecke(Kf)
    return MakeCharacter_1323_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 := 4;
    raw_aps := [[3], [0], [-6], [0], [-57], [62], [-12], [-124], [156], [261], [-109], [368], [54], [152], [-78], [222], [285], [-712], [170], [396], [-475], [-163], [27], [642], [1835], [1257], [872], [-636], [-64], [900], [-1936], [-1113], [1968], [-496], [-141], [71], [614], [818], [-3474], [3891], [2499], [-1888], [570], [2141], [-531], [-91], [1610], [-4273], [15], [1988], [5070], [1872], [-5185], [-7479], [-6288], [7764], [-1299], [6128], [-3646], [4950], [4904], [-7623], [-226], [3426], [3242], [-5091], [-6640], [2981], [-9561], [13004], [-642], [3576], [2972], [-7108], [-5878], [4332], [-1173], [-8866], [8778], [-5482], [3780], [1262], [5010], [-8263], [3755], [11721], [7398], [6182], [15291], [-18349], [-5883], [11742], [6923], [-8172], [9944], [-6606], [-20103], [4056], [-20494], [7490], [6326], [10239], [9132], [21324], [17840], [-1189], [20124], [-7260], [19122], [5318], [7175], [4346], [10926], [-388], [-16579], [-19038], [20438], [-9678], [-3786], [3051], [-15910], [-13534], [-28221], [-8841], [27728], [21798], [-6904], [-15270], [-3664], [10124], [10034], [-20898], [-9268], [12086], [-2688], [-9871], [-24546], [30194], [26451], [-1650], [1190], [26805], [-23707], [30897], [47126], [-2916], [-25198], [42090], [-25984], [24942], [-9190], [30870], [27002], [10866], [25190], [-3852], [9557], [-15180], [494], [20679], [2667], [-19602], [-3160], [45012], [32658], [-20460], [-42904], [19430], [-63946], [17823], [-46947], [-53476], [-48840], [-18547], [30392], [-29358], [16766], [55743], [53603], [24326], [-23461], [4608], [-18550], [33204], [47508], [-19353], [66554], [42662], [55151], [-696], [-15379], [33021], [51848], [50043], [-64695], [43566], [-49201], [6380], [24462], [-69378], [59766], [29717], [39392], [31661], [13599], [-30171], [61064], [61932], [-18450], [36332], [23345], [-56385], [-39952], [54777], [30012], [-11755], [35147], [47508], [-90762], [69642], [70838], [46520], [-33720], [-98539], [68508], [51704], [-42018], [64374], [-16387], [-16008], [14600], [10946], [-12223], [-56010], [83942], [-2748], [-110761], [-29337], [-42993], [18], [-40944], [49502], [-75931], [96524], [-71496], [93864], [-43849], [-83739], [65792], [51966], [32642], [4566], [-77214], [20285], [94209], [-16737], [52352], [34514], [-73719], [-5254], [-13093], [12816], [-91684], [-38656], [-87204], [-60310], [-121365], [28782], [-111904], [-126561], [-69694], [-80398], [39914], [-39655], [-64843], [129545], [54156], [-106912], [-49753], [-8412], [12066], [-29161], [117396], [36080], [-21808], [-97098], [-61390], [-37278], [-131041], [76536], [88026], [-24384], [-78342], [-90477], [-128512], [95487], [-53299], [-90027], [72273], [-130312], [-22438], [104259], [40349], [-78237], [115856], [-101161], [31893], [93728], [-29976], [-107152], [-121812], [-163299], [145488], [-10828], [-97326], [-8686], [-91449], [-25158], [93977], [25650], [124148], [177545], [-61722], [-109405], [-106572], [50291], [24326], [54530], [-85992], [193635], [127592], [-25401], [177869], [-195564], [57428], [78669], [3284], [203502], [-136666], [-90559], [113480], [133794], [-44730], [123815], [-76083], [-135729], [26108], [-175876], [-158112], [-99339], [-188470], [89414], [-23202], [-118528], [-17758], [-188856], [188958], [-125976], [-41478], [-60714], [-21736], [-69558], [65472], [-124485], [-35308], [38987], [182142], [-19135], [146402], [-211188], [-122806], [-199338], [165645], [73865], [43586], [-201828], [-160578], [-226702], [-53088], [91118], [82635], [288], [-75304], [-33006], [143900], [24828], [-22420], [126080], [167870], [-70092], [-43723], [136398], [193605], [219842], [-229200], [57923], [52523], [-38628], [250658], [54906], [-247084], [-89772], [87932], [-193002], [-114915], [-142552], [45854], [207000], [-114796], [-36960], [-54997], [202215], [-96480], [4634], [170225], [26814], [116814], [30296], [216], [-174372], [166839], [-104050], [-8028], [132543], [-88423], [142881], [185592], [21474], [-243718], [4434]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1323_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1323_4_a_l();".
// 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_1323_4_a_l(:prec:=1)
    chi := MakeCharacter_1323_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_1323_4_a_l();".
// 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_1323_4_a_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1323_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-3, 1]>,<5,R![6, 1]>,<13,R![-62, 1]>],Snew);
    return Vf;
end function;