
// Make newform 2023.4.a.b in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2023_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_2023_4_a_b();".
// 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_2023_4_a_b();".
// 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_2023_a();"
function MakeCharacter_2023_a()
    N := 2023;
    order := 1;
    char_gens := [290, 1737];
    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_2023_a_Hecke(Kf)
    return MakeCharacter_2023_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 := [[-1], [6], [20], [7], [-60], [-68], [0], [-70], [176], [90], [-196], [-22], [138], [328], [-12], [-234], [-54], [-44], [-596], [-200], [-1122], [-480], [-838], [778], [-1142], [32], [-1300], [256], [-318], [-1918], [1832], [-262], [-1902], [-82], [-1258], [-1840], [-2156], [1692], [2644], [1808], [-1716], [-3304], [4088], [-3038], [1230], [-1276], [-632], [-840], [-4566], [1236], [-1386], [6392], [-990], [4398], [3018], [-7384], [-1408], [1952], [-4274], [-150], [586], [1248], [-6390], [3800], [3274], [6838], [-6104], [-4678], [7396], [4020], [-5622], [7368], [-11080], [2142], [-7612], [7572], [6858], [-10008], [8402], [13382], [14662], [-7370], [-3480], [13070], [-9352], [1828], [7334], [-5370], [14284], [-11272], [16722], [6972], [-17936], [8780], [5080], [1392], [-11204], [2962], [-4014], [-19866], [14084], [5382], [3662], [-22154], [6764], [7738], [18334], [-7206], [4176], [18158], [2896], [4762], [-10158], [10050], [6352], [-8334], [-4634], [6164], [-31566], [18352], [296], [-15794], [16392], [-10880], [10130], [-12558], [19282], [-19012], [-32228], [31096], [-7928], [26640], [-10904], [-28406], [9590], [-33338], [-34496], [-13582], [30244], [28906], [5906], [11430], [13168], [24600], [25328], [11596], [-39176], [25106], [9406], [-32240], [14394], [-10754], [37284], [29388], [27544], [-344], [16152], [-20910], [26074], [-36204], [-12956], [51050], [28848], [-24294], [18090], [44988], [-22632], [30788], [-23710], [4416], [-3060], [-17092], [12720], [10282], [-25652], [-42050], [-37448], [-15606], [33168], [-49856], [-44064], [2142], [-44222], [41562], [-37720], [12564], [-2866], [-34998], [-51274], [-29340], [-7850], [56584], [49228], [-45388], [19104], [-1850], [66074], [-61022], [-50754], [-22520], [-33986], [63588], [60656], [61138], [-43958], [-9808], [-81020], [62136], [-18774], [5534], [23554], [-5736], [-78864], [60018], [20984], [31850], [808], [32046], [-24216], [67138], [-62878], [-84264], [-80046], [-76880], [16418], [47482], [-64458], [-36456], [-31216], [-63924], [-38734], [103102], [-9520], [-30198], [108178], [-16720], [51226], [-32834], [-47208], [49600], [-83932], [25466], [17808], [82998], [-116970], [-12412], [59872], [45866], [-19256], [-77424], [87310], [6686], [56536], [-8834], [35760], [-100748], [127202], [-123586], [58796], [-15850], [-82272], [-69000], [-76372], [80572], [-112294], [-12138], [-128090], [122722], [10880], [-942], [1776], [-66032], [123922], [-86816], [-24806], [96848], [12312], [80806], [129310], [70378], [-98136], [24984], [122012], [78728], [75450], [55656], [-82506], [-92226], [-20980], [30178], [116022], [153162], [68342], [-71102], [129822], [-87840], [68832], [48996], [-139810], [-147394], [-40818], [43962], [-154064], [-126684], [42412], [-134210], [24496], [-97636], [-115512], [-16642], [-140440], [-16430], [-11106], [-85912], [-138520], [50098], [109030], [46112], [-5338], [10530], [-26666], [-12090], [-97120], [-26768], [113658], [-86514], [-129276], [158402], [21696], [46098], [-145922], [-109158], [194772], [78222], [176560], [169458], [60034], [48318], [-130810], [24212], [107118], [58566], [-96312], [13448], [173106], [115812], [94376], [157376], [-9816], [-66252], [91710], [-14146], [-24378], [-8108], [80534], [154938], [-39316], [178442], [-62586], [-88584], [152382], [70078], [149904], [81600], [-236214], [-223770], [-92944], [160656], [-1574], [66260], [34358], [-104856], [-242370], [-70824], [-153266], [-104334], [153328], [68770], [-77506], [-205282], [1920], [-37098], [71648], [146366], [-186994], [113160], [128720], [119704], [-135504], [-87820], [-200966], [-165092], [187844], [-210354], [101568], [-210778], [940], [-256114], [-45752], [172210], [-166636], [16498], [-137736], [128474], [-260396], [182300], [256358], [-7554], [-56970], [1974], [259086], [49698], [-102952], [242320], [-226630], [-156768], [-33616], [-157636], [-51966], [58944], [-129566], [-191476], [-118824], [132910], [86306], [87704], [68100], [244886], [-198938], [-150828]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2023_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2023_4_a_b();".
// 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_2023_4_a_b(:prec:=1)
    chi := MakeCharacter_2023_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_2023_4_a_b();".
// 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_2023_4_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2023_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![1, 1]>,<3,R![-6, 1]>],Snew);
    return Vf;
end function;