
// Make newform 1078.4.a.a in Magma, downloaded from the LMFDB on 03 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1078_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_1078_4_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_1078_4_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_1078_a();"
function MakeCharacter_1078_a()
    N := 1078;
    order := 1;
    char_gens := [199, 981];
    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_1078_a_Hecke(Kf)
    return MakeCharacter_1078_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 := [[-2], [-4], [-14], [0], [-11], [50], [-130], [108], [-96], [142], [-40], [382], [118], [220], [-520], [238], [852], [-190], [-12], [-112], [6], [304], [-820], [-202], [1406], [634], [-304], [780], [-370], [-558], [-112], [-1716], [-134], [132], [-394], [-3016], [-1398], [-3788], [-1368], [882], [2628], [-3950], [3400], [-4942], [1446], [1456], [-2124], [2536], [1356], [-1438], [-5094], [-5504], [2430], [-604], [-1602], [2120], [-790], [-2032], [-6058], [970], [-5308], [7114], [-4388], [8752], [-4458], [2614], [4172], [-4942], [11676], [-10894], [-9378], [4264], [2056], [8086], [-9812], [744], [1294], [10362], [2706], [7366], [1020], [11886], [-1888], [-7218], [16760], [5148], [-7998], [-10166], [10626], [14776], [-3076], [-14704], [6688], [-756], [7060], [488], [3386], [-1722], [2692], [8574], [-2492], [-226], [12924], [8090], [18188], [9086], [2980], [-15122], [-21504], [7446], [-3728], [-19402], [-14854], [-17020], [-9696], [-24702], [-6788], [-14960], [11670], [532], [-26334], [-4942], [-23798], [16940], [-14932], [8670], [-5954], [14504], [-19600], [1730], [38756], [-15688], [-30920], [-30594], [8198], [-4402], [5378], [6076], [5450], [30538], [16164], [-28394], [28800], [-4884], [5514], [-26592], [-25350], [1606], [47156], [24168], [-29954], [-18898], [-17548], [-50952], [-35028], [-12088], [13448], [-22338], [-23962], [41394], [5620], [-3942], [-36328], [-30284], [-27694], [-25232], [30472], [-44678], [-17854], [-34702], [-61716], [-47830], [38712], [23322], [30760], [-33162], [59596], [-20322], [12952], [-21342], [-18968], [9276], [-1458], [-58138], [-64888], [12978], [-39026], [-17876], [-14614], [-4800], [-5506], [21788], [43444], [28170], [65380], [-43066], [60082], [-21658], [38142], [-38728], [39614], [58704], [-47502], [-6370], [80116], [-50390], [34936], [69124], [-6726], [53524], [-37822], [56370], [6032], [33604], [-26856], [87430], [-49264], [52446], [-42160], [-75546], [30758], [-96264], [-81310], [70632], [57244], [20086], [77878], [-27240], [90984], [-3060], [-24442], [94556], [-91712], [-68070], [4804], [-58016], [-83666], [15158], [-22884], [29760], [3972], [46564], [-22544], [64102], [-25010], [19144], [4712], [-92964], [77084], [-89472], [76526], [-10082], [-8480], [6118], [62994], [86244], [-32642], [-24764], [-108750], [-60922], [-40736], [-101932], [-64758], [26962], [-80418], [49836], [-66858], [58326], [-93284], [-8554], [-44742], [20516], [41142], [129056], [90030], [-91776], [-105876], [-76642], [17786], [-120308], [-146000], [-121104], [89544], [-4550], [21012], [33192], [48210], [-15002], [95760], [47934], [109158], [29500], [151114], [73300], [37566], [95538], [-126952], [-21774], [-22652], [138796], [-109386], [102462], [-146592], [-93708], [-112932], [-6110], [11292], [71482], [-146640], [-81114], [-43232], [-90370], [-27950], [75668], [-128376], [-127066], [-140564], [169392], [-157826], [-170478], [31068], [-8662], [-62558], [149688], [-153462], [-60050], [-37116], [-31468], [-5200], [-83466], [57150], [192534], [-14944], [26844], [-63092], [53652], [-215754], [-30978], [88454], [64000], [57278], [-178966], [-41270], [-67936], [147366], [-21244], [155810], [33676], [-184688], [4466], [-16180], [-65574], [55350], [16896], [-118586], [-232202], [-154680], [-118316], [90786], [211520], [153270], [227270], [163368], [39468], [185100], [-159558], [-65614], [-33784], [216090], [3108], [-32284], [24816], [134518], [-79448], [-184234], [126780], [-115888], [144158], [154302], [-188986], [-121590], [156698], [-100808], [-201694], [-146196], [158448], [-12072], [22146], [-244132], [-108056], [-11518], [200898], [-10980], [65596], [34272], [71770], [174400], [-104698], [-187588], [54342], [-44158], [203874], [14768], [68214], [-149910], [-107224], [224646], [265186], [-82132], [111436], [-141362], [-97226], [-36532], [-69948], [-204870], [-141766], [-28848], [-70144], [-132146], [-134936], [244838], [225578], [-188472], [220116], [137382], [248042], [117220], [81642], [97012], [-39400]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1078_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1078_4_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_1078_4_a_a(:prec:=1)
    chi := MakeCharacter_1078_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_1078_4_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_1078_4_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1078_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![4, 1]>],Snew);
    return Vf;
end function;