
// Make newform 840.4.a.b in Magma, downloaded from the LMFDB on 03 January 2026.
// To make the character of type GrpDrchElt, type "MakeCharacter_840_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_840_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_840_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_840_a();"
function MakeCharacter_840_a()
    N := 840;
    order := 1;
    char_gens := [631, 421, 281, 337, 241];
    v := [1, 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_840_a_Hecke( : Kf := Kf)
    return MakeCharacter_840_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]];
    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], [-5], [-7], [0], [-54], [74], [-20], [-160], [-246], [84], [306], [-370], [-88], [460], [686], [-684], [186], [-904], [912], [-26], [-320], [732], [1150], [1526], [1826], [1264], [-124], [-442], [498], [344], [-2116], [1482], [2748], [3330], [-1200], [-374], [-2752], [1748], [1650], [-2576], [4106], [2512], [378], [4070], [1428], [828], [-4608], [3156], [-6198], [2026], [480], [-5542], [-4756], [2682], [-4464], [5178], [2828], [1978], [2338], [-3980], [1634], [4756], [-8896], [4078], [-10922], [-8204], [922], [8052], [-686], [5058], [-2624], [-1304], [-5302], [-9380], [-4796], [-7710], [1514], [12690], [3658], [-9788], [-6738], [3560], [-1658], [15772], [-10812], [9322], [226], [-6518], [16096], [-484], [13128], [-1296], [9400], [-8956], [13092], [22074], [-14386], [-7308], [-10882], [18184], [-9226], [14076], [-8854], [-12044], [-13898], [-3036], [7370], [-7448], [25538], [21456], [-12142], [-6310], [7940], [-1760], [14418], [9660], [22668], [29070], [29728], [-13598], [-10222], [9546], [972], [13396], [-21334], [-20858], [-7920], [19256], [38042], [13540], [11448], [19368], [24522], [502], [-990], [-21022], [7236], [9938], [41058], [4716], [-36878], [28424], [11036], [17546], [19600], [-26222], [-3374], [39708], [19808], [-6014], [-15314], [-38936], [28716], [-41552], [-30304], [-23936], [17886], [-5194], [6602], [-10492], [44778], [18728], [38876], [-40654], [14436], [-22696], [-53790], [-6046], [6090], [-34384], [-21502], [-4520], [-24110], [-53476], [-25722], [30780], [29058], [-36840], [8234], [38296], [-43748], [-30902], [6786], [-24168], [-24638], [69866], [15068], [-12806], [-47040], [-4634], [24468], [45268], [-54390], [-69164], [27970], [3394], [30298], [-61878], [32972], [8530], [4796], [-12014], [-30190], [-11348], [-64374], [44012], [5300], [68826], [21628], [-39454], [-27182], [-73688], [-1196], [-7088], [72010], [-4328], [48038], [-29152], [48846], [67718], [-51580], [-15558], [81992], [82460], [99862], [32706], [81936], [49096], [-25752], [-86422], [18668], [28688], [20570], [-64260], [43332], [32434], [4710], [-88408], [-63760], [-37668], [7700], [-66408], [4814], [105938], [-90888], [-6080], [56604], [-32316], [9568], [-6446], [62742], [-20824], [16034], [-34966], [-113184], [108142], [65044], [-105486], [-99010], [-67472], [59404], [88354], [-38182], [30130], [32596], [-53726], [31118], [-49016], [126838], [-11422], [-58440], [-110738], [101248], [-16362], [-144528], [-6108], [79662], [83370], [-19668], [37260], [-7208], [-36004], [100786], [7060], [-40056], [132714], [14206], [41644], [41454], [-79246], [58260], [26394], [76292], [97642], [168826], [124636], [-41862], [60444], [156356], [-67850], [-60242], [-140576], [13956], [76500], [145874], [-23324], [-69798], [60416], [146282], [-109508], [104418], [108610], [-156920], [171208], [-161462], [101396], [106632], [-134778], [50682], [-13372], [-36390], [141402], [-41712], [166634], [29994], [24124], [165500], [28944], [50878], [134950], [-208274], [-45500], [13300], [66892], [-152276], [-18066], [-13038], [25402], [26280], [6826], [-83622], [-146638], [-73240], [-81010], [222488], [58826], [14872], [-10456], [-129294], [-86236], [-47486], [-85542], [72992], [120134], [30602], [-39360], [127852], [-197374], [135632], [23354], [-79866], [-218616], [148360], [-14540], [76082], [223602], [-20512], [-213190], [129240], [-202660], [44744], [-111670], [-131788], [-172094], [-48948], [-136152], [-228186], [-156906], [-58858], [51450], [5354], [5760], [-152094], [-10588], [104956], [220304], [102258], [-123536], [152308], [-6894], [31778], [44792], [93364], [-21808], [-61206], [-164108], [-177722], [151004], [187178], [-127766], [-6846], [-33144], [136666], [-65646], [269164], [204274], [31194], [129196], [280764], [-82962], [213054], [-147820], [-247772], [44210], [-59398], [29664], [298544], [111762], [32076], [214762], [113218], [-124608], [-265460], [154558], [-102726], [152324], [267474], [50692], [-180528]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_840_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_840_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_840_4_a_b(:prec:=1)
    chi := MakeCharacter_840_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_840_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_840_4_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_840_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<11,R![0, 1]>],Snew);
    return Vf;
end function;