
// Make newform 1344.4.a.p 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_p();".
// 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_p();".
// 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], [-14], [7], [4], [-54], [-14], [92], [152], [106], [144], [-158], [-390], [-508], [528], [-606], [-364], [-678], [844], [8], [-422], [-384], [-548], [1194], [-1502], [-398], [-1160], [324], [938], [-622], [-1200], [-1396], [2810], [4], [-1374], [-2104], [-3206], [332], [-1496], [3322], [-900], [-1902], [-4128], [-1342], [3506], [-680], [5372], [1072], [-2868], [-4798], [-5126], [528], [-814], [-1932], [-3294], [7080], [-7814], [-3168], [7858], [6730], [-3020], [6834], [2332], [-8840], [-1046], [-7542], [2756], [3954], [6900], [2426], [-1470], [-6872], [7072], [818], [-5132], [8576], [3730], [-6678], [-3054], [266], [8844], [4482], [-9936], [-11758], [4104], [9748], [-478], [-11174], [11674], [-10528], [16604], [8576], [-9704], [-4092], [17884], [7704], [-14358], [5082], [-1756], [-16230], [17676], [12250], [-10052], [25674], [3732], [-1214], [7108], [6162], [-2472], [-13750], [11376], [-20382], [21178], [-4700], [21736], [-13022], [3308], [13800], [2682], [23836], [11282], [-13726], [-4974], [-8988], [10172], [-27446], [-3998], [-25872], [-12088], [-7974], [-31764], [-888], [34656], [22866], [22570], [-1790], [-2990], [-30756], [-15126], [-6502], [-8252], [21986], [-3736], [-23820], [-7942], [-21016], [-24878], [-6390], [-46444], [25408], [-1078], [-45006], [4028], [29304], [50916], [24432], [20360], [23202], [-1990], [51130], [-47044], [46858], [-30632], [-3804], [-49326], [-11112], [-13616], [56674], [42770], [-19038], [8756], [14042], [37416], [41946], [22848], [11882], [12276], [19634], [36664], [-13622], [-17808], [-19604], [-16414], [-39046], [-45392], [29826], [11034], [-7252], [-24774], [-15360], [-33470], [-44316], [-28100], [-33814], [32844], [6298], [65810], [-23302], [-58014], [33288], [-65862], [37536], [7474], [-45726], [30084], [22314], [-69104], [-3060], [51354], [-86812], [66098], [-24062], [-17688], [-66316], [-54840], [57914], [-22240], [27442], [-93832], [59978], [66242], [2312], [3234], [9440], [27420], [74386], [-75542], [71776], [-37896], [-76092], [9450], [-16964], [-75376], [-41958], [-14044], [-48720], [-50702], [-22686], [2484], [-20984], [38620], [3668], [84664], [-84438], [-65582], [77496], [-60528], [74860], [73508], [65232], [23354], [-113694], [36360], [-10790], [-123910], [81244], [-36494], [88884], [-30158], [32042], [39632], [47660], [-32734], [86138], [-24606], [-97364], [-69414], [-7798], [80308], [63954], [-97878], [-141124], [-95478], [59344], [-15470], [112392], [94804], [108954], [-145190], [89340], [-8832], [-51912], [6936], [34114], [10788], [-40528], [29874], [1058], [126280], [19842], [72090], [-105444], [-1494], [-110140], [146602], [-26070], [-13520], [-46398], [-98700], [102444], [37018], [-63558], [67904], [143004], [16916], [-61342], [61796], [-41174], [83960], [-125438], [91536], [-143678], [-80638], [37196], [106120], [106874], [-58596], [-47168], [-176638], [-54030], [-23812], [104266], [135882], [91664], [53786], [130898], [89516], [191380], [-136768], [39602], [110314], [-30934], [-19632], [-179636], [30148], [-187564], [123066], [84098], [145466], [159680], [-21134], [-60150], [-124206], [-31464], [58410], [28524], [-144254], [14564], [57936], [-40638], [-144532], [-124966], [-20262], [175200], [36434], [113578], [-89696], [-42780], [182578], [-26344], [-136126], [-11846], [-38864], [127924], [201004], [147386], [-138342], [134904], [-207542], [36268], [130948], [-124272], [-72350], [-227608], [-40390], [-216804], [-83200], [184546], [176530], [-64150], [95178], [-54790], [-99832], [-263262], [-22580], [-148952], [-56096], [241874], [148], [129824], [277634], [100146], [-206492], [-90916], [-127304], [-207798], [22064], [-200006], [-200316], [121346], [132058], [-121758], [95264], [176906], [-5454], [-106664], [56618], [82290], [56252], [-80660], [54706], [-267742], [176692], [-291060], [-81478], [131738], [10400], [136184], [222066], [-192008], [149066], [-78686], [-57904], [14580], [35546], [148282], [-9988], [-15606], [131988], [-66664]];
    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_p();".
// 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_p(: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_p();".
// 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_p( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1344_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![14, 1]>,<11,R![-4, 1]>],Snew);
    return Vf;
end function;