
// Make newform 1840.2.a.l in Magma, downloaded from the LMFDB on 18 April 2024.
// 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_l();".
// 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_l();".
// 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
        poly := [-1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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)
    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+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 := 2;
    raw_aps := [[0, 0], [0, -1], [1, 0], [-1, 1], [-2, 3], [1, -5], [-2, 5], [3, -3], [-1, 0], [-6, -2], [-1, -5], [0, 4], [-8, 7], [0, 0], [-6, 6], [-6, 4], [8, -6], [2, -7], [-8, -4], [-4, 5], [0, 2], [-8, 4], [-6, 8], [-4, -4], [14, -1], [-10, 6], [-2, -9], [-2, -10], [3, -9], [14, -8], [0, -10], [10, -8], [1, -7], [-16, 2], [-3, -3], [-12, 9], [6, -6], [-3, 1], [8, 0], [-16, 9], [14, -10], [-17, 3], [4, 6], [4, -2], [-8, -9], [-2, 0], [-14, 0], [8, 8], [22, -2], [10, 0], [16, -6], [-12, 20], [0, 0], [-11, 3], [-24, 10], [10, -19], [2, -10], [19, 7], [10, 12], [26, -2], [14, 8], [6, -16], [-12, 25], [-4, 0], [-5, 11], [-3, -5], [-16, -2], [25, -7], [8, -1], [-2, 0], [24, 0], [6, -20], [0, -4], [4, 10], [-18, 25], [-8, 16], [10, 7], [-4, 7], [8, -6], [7, -23], [-8, -4], [-17, 19], [-26, 14], [-19, 17], [15, -1], [-17, -5], [13, -9], [14, -32], [-36, -2], [-2, 0], [-12, 18], [6, 16], [36, -10], [10, -10], [20, -20], [-25, -7], [14, 14], [-22, 4], [26, 0], [-16, 24], [19, -29], [16, -24], [-12, 16], [-2, 0], [18, -21], [-10, 4], [-12, -1], [36, 2], [20, -1], [-9, -15], [-20, -4], [-26, 28], [26, 5], [-15, 11], [0, -26], [-32, 20], [18, -6], [-22, 36], [13, -13], [-12, 36], [30, -7], [2, -8], [-38, -8], [17, 9], [26, 2], [-23, 41], [-18, -5], [-11, -15], [-46, 11], [-10, -12], [-12, 16], [23, -35], [-8, 34], [8, -16], [-13, 37], [-26, 12], [16, 8], [32, -18], [4, 6], [-16, -19], [-38, 10], [-46, 8], [-6, -12], [-12, -6], [6, 8], [22, -34], [7, -11], [-26, 12], [-18, 26], [-36, 2], [-12, 19], [28, -12], [-1, -9], [44, -24], [22, -18], [6, -26], [-40, 0], [-10, 4], [-19, -11], [-50, 17], [11, 27], [-14, 11], [-32, 30], [-10, -15], [-9, -9], [-10, 15], [0, -17], [-14, 44], [-28, 34], [-20, 11], [28, -20], [-6, 26], [-2, -10], [-2, -5], [-32, 1], [26, -30], [-34, 4], [36, -24], [10, 23], [49, 5], [-16, 18], [-14, 33], [33, -29], [-10, 2], [-38, 3], [-11, 41], [-15, 11], [-16, -24], [-40, 23], [52, -15], [40, -2], [-8, 8], [60, 0], [12, -32], [-22, 0], [17, -11], [18, 10], [2, 28], [30, 0], [-26, 14], [22, -39], [-25, -13], [24, -21], [-16, -8], [24, -24], [-2, 12], [-46, 16], [8, -15], [-43, -5], [26, 6], [-46, 6], [26, -44], [43, -15], [-30, -6], [28, 4], [-14, 26], [-52, 22], [26, 4], [17, 3], [-10, 16], [-30, 34], [18, -8], [-44, 17], [-51, -5], [-52, 8], [6, 26], [23, -57], [43, -5], [17, 27], [-48, 12], [15, 5], [14, -33], [12, -8], [-8, 15], [14, -50], [12, -12], [-26, -6], [22, -6], [-2, -6], [4, -5], [30, 2], [-26, -8], [2, 4], [20, -30], [22, -2], [-3, 37], [-6, 22], [31, 9], [-30, -19], [18, -43], [-20, 14], [16, 6], [-59, -5], [4, -44], [-6, 20], [-8, 26], [46, -13], [12, 30], [-27, 23], [-18, 56], [-18, 1], [18, -20], [-16, 20], [-14, -2], [50, 16], [2, -13], [-14, 13], [14, 20], [48, -14], [-45, 13], [38, -14], [-4, 4], [-1, 7], [8, -24], [-26, 0], [24, -34], [-4, 49], [26, -10], [18, -16], [3, -11], [-20, 44], [0, 48], [-14, -13], [-54, 28], [3, -25], [40, 17], [42, -4], [4, -54], [8, -21], [32, -47], [18, -24], [-64, 34], [-28, 18], [1, 31], [-48, 17], [14, 33], [54, -34], [-4, -39], [-2, -16], [-8, 11], [31, -29], [14, 34], [-36, -18], [48, -40], [-31, 63], [20, -34], [40, -11], [-19, -35], [18, -58], [-16, 17], [66, -10], [-9, 11], [4, -14], [36, 31], [49, -9], [-34, 14], [-38, 40], [13, 5], [-42, 0], [-12, 35], [66, -27], [-54, 31], [-10, 32], [-26, 26], [-14, -19], [-16, 0], [-1, 41], [53, 5], [-1, -43], [-58, 18], [-8, -42], [-12, -15], [-64, -16], [5, 29], [20, -40], [-19, 29], [74, -23], [-14, 0], [20, -67], [-30, 13], [54, -12], [26, -15], [8, 20], [30, -30], [6, 32], [-24, 48], [26, -4], [-27, 33], [2, -44], [-30, 14], [-58, 10], [-2, 12], [-44, 18], [54, -43], [-14, -48], [-46, -6], [-16, 42], [15, 45], [10, -36], [-46, 60], [-48, 22], [50, -40], [-44, -18], [-62, 33], [16, -27], [26, -4], [-40, 19], [8, 32], [-34, -10], [-38, 20], [-28, -34], [-10, -12], [14, 7], [-72, 40], [-69, 5], [-22, 26], [-32, 42], [58, 7], [70, -14], [-4, -32], [6, -17], [-54, 45], [-36, 18], [-78, 24], [-4, -4], [0, 16], [15, -9], [17, -69], [-34, 36], [-53, 37], [-38, 0], [47, -67], [40, -36], [-24, -44], [-94, 0], [45, -23], [10, -28], [-4, 40], [-35, 27], [-42, -8], [-64, 38], [40, -42], [4, 0], [-1, -1], [-32, -4], [-24, 23], [6, -6], [63, -11], [-14, -10], [-8, 56], [16, -38], [28, -73], [44, -40], [42, 19], [61, 9], [-14, -40], [57, -55], [26, 14], [14, -27], [-37, 45], [-18, 39], [-74, 22], [-18, -18], [62, -30], [54, 10], [38, -40], [20, -20], [-37, 1], [-12, -58], [-16, -53]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1840_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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