
// Make newform 1840.2.a.p in Magma, downloaded from the LMFDB on 25 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_p();".
// 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_p();".
// 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;
    Rf_num := [[1, 0], [-1, 2]];
    Rf_basisdens := [1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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], [1, 0], [-1, 0], [1, 1], [1, -1], [-4, 1], [-2, -2], [-1, -3], [1, 0], [-5, -2], [-2, 1], [-3, 3], [-3, 2], [3, 3], [-5, 2], [-6, 0], [0, 4], [1, -5], [-3, 3], [4, -1], [0, 3], [-11, -1], [2, -2], [5, 1], [5, 5], [-6, -4], [2, -2], [-3, -5], [-5, 3], [3, -1], [5, -6], [2, 1], [0, -4], [-4, -3], [-16, 2], [16, -1], [1, -3], [5, 6], [12, 0], [2, 4], [-4, 1], [23, 1], [6, 0], [-6, -3], [-16, -1], [1, 3], [14, 2], [-4, 0], [-3, -7], [1, 5], [-2, -5], [0, -1], [16, -2], [-8, 4], [0, 5], [-15, 3], [-17, -2], [-6, -6], [-10, 5], [1, 1], [-16, -2], [-4, 4], [18, 6], [18, 5], [-6, -6], [-26, 4], [8, -3], [-1, -9], [-28, -4], [-1, -10], [16, 7], [27, 1], [12, 0], [-20, -6], [-24, 0], [-7, 5], [-24, 0], [20, 1], [0, 2], [5, 0], [3, -1], [15, -9], [5, -7], [4, -2], [16, -3], [-13, -2], [6, 12], [2, 6], [21, 0], [2, -2], [-20, -2], [0, 12], [11, 8], [14, 7], [16, -9], [-6, 0], [-3, 4], [-4, 8], [-3, 9], [-19, -8], [-17, 0], [-3, -11], [-2, 10], [-20, -10], [-4, 12], [22, -3], [-9, 14], [-24, -10], [12, -16], [5, 4], [14, -2], [14, 14], [1, -5], [14, -10], [-3, -3], [39, -5], [-6, -12], [-3, -12], [14, 7], [16, -4], [-26, 0], [-6, 15], [-6, 0], [9, -6], [10, -2], [-21, 11], [-17, 3], [-6, -6], [2, -12], [-6, -6], [-2, 19], [-22, -2], [-41, 3], [-33, 9], [-15, 8], [32, -10], [-3, -19], [-14, 14], [-3, 3], [30, 8], [-14, 13], [-18, -8], [-1, 2], [-18, 14], [-12, -18], [-45, -5], [-6, -12], [24, -11], [12, -9], [-17, -16], [-16, -6], [0, -8], [16, -8], [-23, 4], [25, -1], [-57, 1], [19, 3], [33, -2], [-37, 1], [6, 2], [11, 14], [-12, -10], [-13, -4], [6, -12], [-35, -5], [-28, -2], [20, 4], [-32, 2], [45, 3], [18, 19], [-21, 9], [-19, 8], [0, -4], [-4, 12], [-14, 15], [3, -9], [-6, 3], [27, -16], [19, -15], [28, 0], [-1, 4], [9, -15], [36, -9], [-16, 22], [-16, -10], [-18, 6], [-8, -2], [19, 1], [-17, 2], [0, 16], [-56, -3], [26, 10], [-26, -4], [-6, -4], [0, -10], [-13, -13], [-27, 15], [20, -12], [-30, 2], [21, -2], [-8, -4], [-44, 8], [26, -3], [-25, 1], [-14, -2], [12, 15], [5, -9], [-27, 4], [3, 24], [-6, -9], [-22, -9], [9, -22], [23, -1], [12, 18], [-30, 2], [-25, -9], [-17, -18], [-3, 24], [22, 4], [-16, 25], [33, 6], [-1, 1], [11, -10], [1, -7], [63, 2], [15, 0], [-35, -1], [16, -11], [44, 2], [0, -20], [26, 16], [33, -3], [-16, -14], [-6, 0], [-16, 12], [43, -1], [-2, 6], [-9, -23], [-22, -11], [-18, 15], [-24, 8], [20, 7], [21, -12], [-1, 0], [28, -10], [-30, 9], [33, 12], [14, 4], [44, -6], [36, -18], [28, -12], [0, -2], [58, 4], [3, 15], [32, -14], [-14, 23], [-7, 3], [-36, 6], [-16, -17], [2, 1], [-32, 4], [67, -1], [8, -6], [-1, 11], [-12, 18], [63, -3], [-39, 3], [15, 27], [-5, 5], [-10, 17], [-11, 0], [2, 4], [-25, -5], [-41, -5], [28, -1], [23, 26], [-28, -8], [-8, 10], [-38, -12], [-32, 16], [51, -4], [-16, -6], [-27, -5], [16, -6], [12, -12], [-48, -8], [-2, 4], [3, 9], [-2, -10], [12, 2], [6, -20], [-4, -28], [-10, 4], [-27, 3], [36, -9], [-36, 18], [22, -10], [6, 27], [4, 1], [5, -30], [-37, -21], [-7, -5], [16, 16], [33, 6], [-8, -12], [-72, -3], [-24, 28], [26, 6], [-42, 0], [14, -4], [21, -8], [9, -5], [-32, 4], [21, -6], [-29, 5], [7, -33], [68, 5], [-12, 7], [-22, 2], [19, -8], [-25, 19], [13, -3], [-29, 4], [-28, 24], [0, 30], [-60, 12], [-32, -14], [-47, 2], [55, 5], [-6, 6], [3, 6], [36, 9], [0, -3], [27, 24], [47, -3], [-37, -8], [2, 12], [-27, -23], [7, -20], [1, -19], [-16, 9], [3, -17], [-12, 18], [-30, 12], [30, -28], [16, 28], [-55, 6], [-34, 14], [-20, -10], [-18, 8], [26, 5], [-14, 9], [-54, 12], [-14, 0], [56, -4], [0, 14], [14, -34], [36, 10], [-12, -1], [-42, -2], [18, 0], [47, -2], [21, 18], [6, 36], [3, 0], [-18, 21], [-30, -9], [49, 19], [-34, -20], [18, -13], [-56, 20], [-29, 2], [-70, 2], [-24, 0], [-46, -21], [-38, -1], [-9, -27], [-64, -6], [72, 4], [14, -9], [44, 14], [61, -5], [-46, -6], [6, -17], [-29, 23], [9, -30], [50, 3], [-8, 2], [-84, 6], [-24, 4], [42, 18], [24, -2], [-16, 35], [68, 4], [-3, 21], [-46, -16], [41, 25], [-45, 19], [-24, 0], [56, 14], [21, 6], [-30, -23], [-67, -7], [18, 26], [15, 12], [12, -9], [3, 9], [-41, -14], [-55, 9], [12, 24], [-12, -15], [-54, -1], [13, -5], [-62, -12], [33, -21], [-23, 20], [-28, -4], [18, 18], [15, -21], [-45, -1], [-57, 19], [37, 17], [24, -12], [-12, -24], [-22, -23], [-18, 39], [-81, 5], [18, -20], [50, 1], [24, -25]];
    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_p();".
// 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_p(: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_p();".
// 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_p( : 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]>,<7,R![-4, -2, 1]>,<11,R![-4, -2, 1]>],Snew);
    return Vf;
end function;