
// Make newform 1840.2.a.f in Magma, downloaded from the LMFDB on 22 December 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_1840_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_1840_2_a_f();".
// 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_1840_2_a_f();".
// 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_1840_a();"
function MakeCharacter_1840_a()
    N := 1840;
    order := 1;
    char_gens := [1151, 1381, 737, 1201];
    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_1840_a_Hecke( : Kf := Kf)
    return MakeCharacter_1840_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 := 2;
    raw_aps := [[0], [0], [1], [-1], [6], [-2], [-3], [6], [-1], [3], [3], [1], [9], [8], [-4], [1], [-1], [8], [7], [5], [-6], [0], [11], [4], [6], [-9], [16], [-5], [-6], [-7], [-8], [-12], [10], [-7], [20], [-4], [3], [14], [8], [-16], [0], [0], [-8], [-4], [-18], [22], [19], [-26], [4], [4], [24], [-5], [2], [2], [20], [-3], [-31], [-19], [10], [30], [17], [-21], [-22], [8], [-29], [-8], [7], [-34], [-24], [-25], [-8], [-36], [-35], [-38], [-20], [21], [26], [-32], [18], [-35], [14], [-34], [0], [17], [-32], [36], [3], [-25], [22], [-22], [27], [4], [40], [-11], [13], [17], [18], [-10], [-12], [38], [-8], [-31], [-11], [-42], [16], [12], [18], [28], [24], [-5], [32], [-22], [-21], [4], [4], [-22], [-37], [-42], [6], [22], [-42], [28], [47], [-44], [-4], [-24], [8], [-13], [21], [25], [19], [24], [14], [15], [29], [44], [-22], [-13], [-33], [47], [-37], [42], [-24], [57], [33], [-30], [-2], [14], [-1], [-22], [6], [0], [36], [30], [7], [20], [32], [-29], [22], [-24], [8], [54], [56], [-40], [-33], [-1], [15], [-32], [4], [48], [-24], [-7], [46], [-39], [16], [-16], [-4], [23], [-31], [26], [-62], [28], [30], [-42], [-51], [50], [-18], [-39], [1], [33], [22], [54], [62], [1], [48], [31], [-50], [-38], [2], [18], [12], [-12], [22], [-50], [-12], [2], [16], [34], [-23], [-20], [-6], [-50], [48], [20], [63], [-36], [-14], [9], [-19], [-54], [33], [26], [-21], [21], [-6], [27], [18], [-21], [4], [-55], [-62], [-46], [-58], [-37], [-57], [44], [-40], [6], [-45], [-3], [59], [-37], [-14], [-10], [-60], [-76], [-32], [-66], [-70], [7], [42], [18], [53], [-10], [-66], [-21], [-20], [-8], [26], [34], [5], [17], [31], [31], [-60], [64], [-24], [54], [1], [44], [-11], [-49], [-63], [18], [60], [-70], [16], [41], [-20], [-84], [18], [-52], [16], [40], [14], [-75], [-23], [-34], [64], [42], [-30], [-11], [36], [4], [-40], [-16], [-46], [-66], [-35], [-18], [-49], [57], [28], [4], [-68], [-28], [78], [70], [-64], [68], [-76], [18], [40], [-8], [57], [60], [31], [-29], [49], [-66], [56], [11], [-30], [78], [-15], [16], [4], [-70], [-24], [43], [79], [38], [-35], [-38], [-44], [12], [82], [30], [-38], [7], [-70], [54], [74], [-16], [-57], [-51], [-31], [6], [-26], [-49], [-15], [52], [-45], [49], [-64], [50], [42], [-82], [-6], [50], [-55], [-75], [-30], [36], [-14], [-18], [84], [75], [28], [-7], [-21], [6], [-92], [-52], [78], [-5], [50], [-59], [36], [-54], [-18], [-46], [64], [50], [16], [18], [7], [28], [-73], [63], [22], [-30], [49], [-64], [10], [61], [-94], [72], [-28], [-51], [35], [18], [-27], [-85], [75], [9], [-17], [82], [-76], [-36], [-4], [-66], [12], [-20], [87], [18], [-24], [64], [45], [76], [-50], [2], [-18], [12], [16], [-88], [60], [11], [-92], [-31]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1840_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1840_2_a_f();".
// 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_1840_2_a_f(:prec:=1)
    chi := MakeCharacter_1840_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 2));
    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_1840_2_a_f();".
// 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_1840_2_a_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1840_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![0, 1]>,<7,R![1, 1]>,<11,R![-6, 1]>],Snew);
    return Vf;
end function;