
// Make newform 2352.4.a.q in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2352_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_2352_4_a_q();".
// 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_2352_4_a_q();".
// 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_2352_a();"
function MakeCharacter_2352_a()
    N := 2352;
    order := 1;
    char_gens := [1471, 1765, 785, 2257];
    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_2352_a_Hecke(Kf)
    return MakeCharacter_2352_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], [15], [0], [9], [88], [84], [104], [84], [51], [185], [44], [168], [-326], [-138], [639], [159], [-722], [166], [-1086], [-218], [583], [-597], [1038], [169], [-642], [464], [-393], [14], [-2184], [373], [-1173], [30], [-82], [-1434], [2671], [-2252], [-1676], [3030], [-3438], [1212], [-3032], [-2520], [365], [-1590], [-5380], [5362], [-1573], [921], [-4052], [-468], [-4932], [1537], [5319], [-5346], [-774], [-2415], [-475], [3728], [1602], [686], [1101], [2780], [4296], [-5489], [4491], [3964], [161], [5916], [142], [4440], [-2286], [-2869], [-3064], [6040], [1842], [7830], [14764], [6264], [-4751], [-4704], [-4474], [12804], [5074], [-1267], [6933], [11688], [551], [-13386], [6376], [5700], [19794], [-15935], [-9963], [-19142], [-12192], [19809], [1794], [-6448], [7262], [-14204], [15825], [1059], [3960], [2530], [-11831], [4809], [-21804], [-14166], [-5891], [-2737], [-26188], [-2358], [13766], [-21287], [21426], [9962], [-18174], [-19167], [-13080], [15190], [4397], [-4029], [-15021], [-13984], [-31053], [15086], [-6378], [-7363], [-32810], [24034], [8022], [-29519], [-3742], [10896], [-17285], [-11826], [17714], [24939], [-29064], [-15370], [44031], [4192], [-33195], [-16448], [16860], [-29054], [-41958], [5546], [32538], [32096], [8490], [48352], [15492], [8116], [4446], [-26504], [5430], [-33803], [48483], [-37296], [-38478], [-27257], [34341], [27426], [12324], [-47597], [11242], [22631], [48219], [5127], [-32114], [45816], [20789], [-20689], [-11388], [6214], [40653], [-49408], [56410], [-27881], [-3063], [28358], [-4962], [8910], [66663], [52994], [20870], [6995], [50952], [45247], [9879], [-74576], [13242], [-74523], [-18558], [-48298], [19010], [-24726], [47670], [36561], [72920], [2338], [-43490], [-66144], [-27243], [-11680], [68871], [-33660], [-78982], [-8254], [-54885], [-25913], [-10383], [-50040], [-23090], [57373], [-86772], [-92226], [39258], [-26512], [-100576], [-56214], [-29765], [30141], [-58696], [100338], [-63276], [-80659], [-7740], [100718], [27992], [71575], [-24630], [20690], [113136], [32053], [-20739], [52356], [31932], [92772], [-73024], [97859], [56882], [112464], [-20028], [-23839], [-43692], [-6176], [54240], [48554], [75246], [426], [68206], [34185], [-95631], [72950], [-102604], [-15159], [-53237], [44752], [-6459], [-43718], [-53972], [66342], [39830], [-70515], [16560], [-111548], [115341], [-24272], [46294], [68107], [59809], [-21341], [22088], [75879], [87044], [-36394], [-100704], [26622], [-106133], [-91464], [-61340], [83918], [-13140], [-101761], [37245], [-36841], [108204], [-19890], [-69519], [99636], [24804], [-131968], [104427], [-93472], [-19278], [-109716], [-37006], [-84710], [127485], [153601], [4893], [36550], [21218], [167949], [-68150], [123426], [-83146], [-51522], [49386], [24312], [-150320], [79500], [99178], [118503], [150420], [-22061], [33912], [146072], [42314], [63429], [90232], [13968], [-148067], [-110150], [41102], [106686], [101337], [53078], [-99675], [-115048], [-48312], [126334], [49044], [166088], [164658], [-165017], [-159085], [136802], [162672], [-72531], [10045], [81099], [-97533], [-131966], [66634], [-55950], [-77859], [-11320], [141059], [169218], [-119689], [-27670], [64806], [50886], [-111081], [64956], [-184806], [2054], [-122520], [-101898], [-191796], [-135772], [228683], [214494], [91168], [-41335], [120303], [95408], [32850], [28275], [35096], [234524], [175656], [-17232], [188053], [-150534], [-129422], [-21942], [-32562], [-43832], [39984], [109586], [-117810], [46072], [-103556], [173272], [744], [93521], [-9942], [-102507], [207854], [52386], [240815], [-170017], [180810], [133564], [-128379], [21544], [-202932], [200572], [-255558], [-77511], [-112453], [-114370], [-97014], [-41350], [-87669], [137041], [-180663], [171603], [216430], [-114814], [17769], [201684], [77000], [-194364], [52446], [123873], [22570], [-91398], [-143253], [-286202], [114558], [242415], [-201468], [-80782], [184164]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2352_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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