
// Make newform 1344.4.a.z in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1344_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_1344_4_a_z();".
// 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_1344_4_a_z();".
// 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_1344_a();"
function MakeCharacter_1344_a()
    N := 1344;
    order := 1;
    char_gens := [127, 1093, 449, 577];
    v := [1, 1, 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_1344_a_Hecke(Kf)
    return MakeCharacter_1344_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 := [[0], [3], [16], [-7], [-18], [54], [-128], [52], [202], [-302], [200], [150], [172], [164], [460], [190], [96], [-622], [744], [54], [742], [92], [-228], [-116], [-554], [780], [-24], [78], [1034], [186], [316], [-988], [950], [-2124], [-2242], [-2776], [3258], [-2432], [3156], [3724], [-2398], [-2906], [1478], [-1098], [-5058], [4432], [5444], [5352], [3752], [5446], [-2602], [1810], [310], [-32], [3348], [-4086], [-1416], [3856], [1358], [-1698], [-6340], [-9204], [-2180], [-7212], [1322], [-474], [-11284], [6986], [-7926], [3058], [7008], [11286], [2752], [-206], [684], [-9048], [3242], [-1702], [8742], [510], [5144], [-514], [-13626], [8794], [-2616], [-838], [-9662], [-3590], [-11628], [2116], [-5088], [-5068], [-11008], [11466], [-11900], [1832], [-16968], [-14976], [9812], [-21370], [-8120], [19934], [352], [-10698], [-14824], [-15318], [-5672], [-25968], [20846], [-4430], [23744], [-9982], [-21090], [-12900], [-140], [-2642], [-1388], [11196], [-9398], [-14050], [-22382], [-6050], [-7500], [-26430], [14228], [12338], [-7054], [-8640], [-20120], [-14554], [-23600], [-28970], [-18848], [26594], [-3012], [-12614], [-40684], [28564], [-7140], [26682], [18012], [3270], [41692], [12078], [24046], [12204], [5930], [-5268], [-10028], [37306], [39178], [-36872], [-19964], [-12924], [44804], [26102], [176], [23116], [-20142], [-19896], [-7658], [12826], [-53148], [30340], [-6714], [-22128], [-41928], [-28894], [58350], [-29764], [11110], [13818], [32670], [-4442], [41592], [-59320], [-1408], [-27510], [9968], [-9862], [-42844], [-9292], [11342], [-35552], [4602], [1140], [34258], [37196], [48534], [21912], [29510], [22734], [-7540], [-30636], [-53714], [62664], [-30434], [-7466], [47404], [-61588], [-20954], [1560], [-34754], [47350], [-14148], [-1188], [4984], [13206], [-14566], [21268], [23778], [-77880], [89312], [49140], [33316], [-29838], [42936], [-34600], [-34374], [28898], [42114], [46808], [76874], [-36048], [39124], [81766], [11676], [36466], [-68536], [-2190], [-48118], [-36196], [85976], [-88166], [-68348], [69504], [32054], [-41994], [-34574], [-67704], [19850], [62540], [-22352], [-61786], [54780], [-94300], [-35224], [-69016], [-20520], [101722], [62194], [-81184], [19166], [-92394], [115004], [112638], [-39314], [100484], [-27180], [3666], [-114740], [-87030], [17622], [106850], [78024], [63332], [-93346], [26080], [-59760], [100218], [2], [748], [8246], [17544], [-94866], [83512], [-117754], [-28666], [152606], [-108456], [34728], [40900], [-105168], [-83982], [5972], [390], [-145230], [53894], [-99816], [-108444], [-31530], [-61292], [65198], [-126728], [-69062], [143996], [3192], [-23528], [-29472], [106076], [-143974], [-462], [-155916], [-90594], [3288], [52394], [27042], [-59266], [74294], [94894], [4168], [-34334], [45766], [-100700], [157498], [82342], [-12332], [22226], [48718], [-147570], [82196], [64646], [36468], [-132952], [96626], [-59362], [76888], [-9812], [-49554], [-139542], [44466], [75258], [210128], [-28712], [-53632], [47600], [20570], [15796], [-159346], [71984], [168978], [142782], [-26616], [-193664], [-86126], [165150], [-8098], [194164], [-23896], [-56756], [168620], [192022], [195318], [-124152], [-67086], [-47736], [37584], [-44812], [-114486], [92190], [155410], [-109980], [159126], [-184890], [151908], [-75418], [-118080], [-112668], [68398], [11786], [-114748], [-100002], [69590], [184712], [153126], [-48380], [171906], [79370], [25184], [172870], [-81964], [-81718], [-148048], [-32310], [-30580], [-130080], [87472], [52990], [-96304], [-119500], [-29938], [93488], [153918], [-207732], [-154666], [245282], [42488], [234376], [170848], [-54126], [37698], [2186], [216732], [-5124], [187408], [41704], [242134], [-252158], [-206868], [-68952], [-104334], [-2694], [-251422], [52924], [297798], [135744], [63674], [-142960], [-174648], [213764], [-93342], [155746], [246198], [302220], [12138], [-33492], [-82070], [-148818], [127316], [208964]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1344_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1344_4_a_z();".
// 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_1344_4_a_z(:prec:=1)
    chi := MakeCharacter_1344_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_1344_4_a_z();".
// 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_1344_4_a_z( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1344_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![-16, 1]>,<11,R![18, 1]>],Snew);
    return Vf;
end function;