
// Make newform 1859.2.a.e in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1859_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_1859_2_a_e();".
// 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_1859_2_a_e();".
// 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 := [-2, -6, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-4, 0, 1]];
    Rf_basisdens := [1, 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_1859_a();"
function MakeCharacter_1859_a()
    N := 1859;
    order := 1;
    char_gens := [508, 1354];
    v := [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_1859_a_Hecke(Kf)
    return MakeCharacter_1859_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 := [[-1, 0, 0], [0, 1, 0], [-1, 0, -1], [0, 1, -1], [-1, 0, 0], [0, 0, 0], [-1, 1, 1], [-2, 1, 1], [0, -2, 0], [-1, -1, 2], [-2, -4, 2], [-1, -4, 1], [-1, 3, -2], [-2, -2, 0], [2, 1, 2], [1, -2, 2], [6, -1, -2], [3, -3, 0], [-4, 1, 2], [-6, 0, 0], [-3, 1, -5], [-4, -3, -1], [0, 3, 1], [-6, 0, -3], [-6, 2, -1], [-7, -3, 1], [4, -4, 0], [-8, 1, -3], [-4, 6, -1], [7, -2, 1], [-2, -4, 4], [8, -1, 3], [5, -2, 5], [-4, 4, -2], [-1, 1, -1], [0, 2, 2], [-15, 4, 0], [2, -3, -2], [-2, -8, 2], [0, -6, 1], [-2, 1, -2], [-7, 4, -1], [18, 1, 2], [3, 1, 2], [-10, 0, -2], [2, -1, -4], [6, -3, -1], [12, 1, -2], [8, 2, 0], [-10, 2, 1], [-2, -2, -2], [6, -6, 2], [-3, 1, 7], [-2, 9, -4], [11, 4, 1], [0, -2, -2], [18, 2, -2], [2, -2, -2], [11, 7, 0], [3, 3, 0], [0, 5, -1], [-9, 3, -1], [-4, -5, 1], [-6, 9, -2], [10, -4, 3], [7, -10, -3], [6, -2, -2], [-17, -7, 1], [-12, -1, 1], [-30, 0, 0], [5, 2, 0], [8, 0, 2], [10, -6, -2], [-7, -11, 3], [-8, -4, 0], [20, -2, 4], [1, -2, 9], [-2, 8, 1], [5, -2, 0], [-11, 1, 3], [16, 2, -4], [-17, 8, 1], [-18, -3, 3], [-17, 0, 3], [-14, -3, -1], [-14, 3, -4], [18, -10, -3], [3, -3, -6], [9, -3, 6], [2, -9, 6], [6, 3, -6], [-12, -1, 1], [-8, 3, 6], [8, 1, -1], [-8, 9, -10], [-14, 9, -3], [19, 4, -2], [19, 4, -4], [4, -13, 7], [-21, 7, 5], [-2, 2, 8], [-7, -3, -2], [8, -11, 5], [10, -8, 10], [-2, 17, 1], [5, -4, 9], [-2, 5, -10], [7, 15, -6], [12, 2, -4], [13, 11, -6], [18, 13, -7], [7, 9, -5], [13, 4, -2], [4, 9, 0], [12, -5, -2], [-9, 0, 4], [10, 3, -2], [12, 22, -8], [-18, 2, -3], [14, -5, -7], [-25, 0, 3], [1, 11, 0], [-6, 0, -6], [-14, -18, 10], [-10, 4, -6], [-12, 10, 1], [15, -10, -6], [-2, -2, 6], [20, -4, 4], [-19, -7, 4], [-4, 10, -2], [-10, -15, 7], [0, 14, -4], [-14, 8, 9], [14, 20, -8], [-2, -6, 12], [-2, 4, 5], [-32, -1, -7], [2, -6, -7], [-17, -5, -1], [10, -10, 4], [-2, 2, 6], [6, 0, 8], [0, -1, -11], [-7, -2, -1], [-2, -7, 12], [-11, 3, -8], [5, 15, 2], [-26, -5, -6], [6, 0, -6], [-29, -17, 7], [3, -8, 6], [38, 10, -2], [-26, 8, -8], [-30, 9, -4], [-12, 3, -6], [4, 12, 8], [-27, -12, 4], [-3, 19, -8], [-2, -2, -2], [0, 0, 4], [-2, 18, 2], [-16, -9, -3], [-8, -13, 4], [13, -8, 11], [8, -4, 0], [-4, 13, -6], [-19, 15, -1], [10, -6, -10], [-23, -2, -7], [-20, -9, 7], [-41, 2, -8], [12, 19, -9], [3, -7, 0], [6, -2, -2], [11, 6, -2], [-26, 12, 8], [-1, 4, 8], [-12, -8, 2], [10, 0, -2], [-10, -2, -10], [8, -6, -12], [13, -20, -7], [-14, -2, -14], [-4, 2, 0], [17, -10, -3], [-2, -8, 16], [-14, -8, 4], [-7, -11, 5], [-18, -1, 7], [15, -8, -5], [28, 2, -8], [28, 10, -4], [-2, 22, -3], [-34, -13, 7], [30, 6, -9], [-3, -7, 1], [21, 20, -3], [39, -3, -6], [-6, 7, 15], [-7, 11, -6], [6, 6, 6], [-25, 14, 5], [-27, -13, 0], [-18, 2, -12], [17, 22, -2], [22, 7, 0], [2, -7, -11], [17, 17, -5], [24, -10, -10], [5, 11, 8], [18, 12, -1], [42, -9, -6], [32, 6, 8], [-26, 13, -11], [-3, 2, -6], [18, 10, -10], [-21, -21, 7], [20, -7, 14], [2, -22, 11], [23, 1, -12], [-4, 14, -2], [-55, 8, 0], [-22, 1, 6], [14, -19, 9], [-31, -15, 3], [-1, 2, 6], [-22, -1, 10], [14, -2, 14], [30, -11, -7], [17, 16, -5], [4, -3, 3], [26, -12, -2], [-13, -3, -3], [4, 0, 16], [26, -6, 6], [-6, 6, 10], [15, -21, 1], [-8, 12, -10], [8, -23, 14], [4, 2, 4], [-26, -4, 2], [-10, 2, -16], [-25, -4, -4], [11, 3, -12], [16, 1, 3], [0, 11, -2], [6, 8, 6], [24, -12, 10], [-4, 22, 0], [22, -4, 22], [-2, 6, -6], [36, 9, -8], [-29, -10, 1], [-6, 6, -10], [34, 8, 6], [-22, 4, -7], [22, -2, -8], [-22, 8, -13], [-17, -1, -6], [30, 2, -2], [-42, 0, 4], [-9, -19, 11], [9, -3, -1], [-55, -4, 5], [-14, -24, 16], [-6, 0, -7], [33, 0, -8], [-20, 18, -8], [-3, 3, 1], [-3, -14, 6], [28, 22, -10], [41, 0, -6], [-18, -11, 15], [24, -14, -1], [18, 20, 4], [22, -8, -10], [-41, 11, -5], [29, 9, -9], [26, -13, -7], [-8, -27, 3], [24, 17, -16], [0, 1, 5], [-17, -21, 3], [4, 5, 9], [-34, -13, 4], [-37, 8, -16], [-1, -1, -15], [-8, 5, -14], [-25, 1, -3], [46, -4, -3], [-28, -9, 4], [5, -7, 3], [-48, -9, 7], [20, 2, 17], [-13, 9, 12], [32, 10, 0], [-31, -2, -2], [-28, 17, -13], [0, -1, 3], [-27, 3, -20], [31, 1, 15], [2, -14, -12], [-54, -7, 0], [24, 13, -12], [25, -4, 20], [-2, -11, 8], [-23, -26, 16], [-34, -13, 6], [3, 9, -4], [-48, 6, -12], [-30, -14, 5], [15, 15, -3], [18, 7, -4], [24, 0, 22], [7, 35, -7], [-12, 11, 6], [40, -7, 5], [18, 4, -7], [-21, -17, 17], [26, 4, 10], [18, 10, 1], [-5, 17, 10], [-40, -8, -8], [-8, 26, -15], [-17, -10, 1], [-18, 9, -8], [-34, 10, 2], [-30, 0, 2], [-43, 1, 5], [31, -7, 9], [61, -14, 0], [20, 0, -12], [30, 16, 4], [2, 20, 2], [-22, 17, 4], [-25, 6, -11], [16, 14, 1], [-15, 28, -12], [18, 7, 13], [-6, -16, 21], [-1, 14, 0], [-5, 1, 13], [-42, -2, -6], [58, 12, -4], [-10, 7, 1], [29, 16, -2], [-14, -6, -4], [-6, 0, -2], [-2, 4, -13], [-34, 13, 9], [10, 16, 0], [-9, -30, 6], [-24, 12, -8], [25, -21, 5], [-17, -5, 5], [-2, -14, -4], [-50, 12, 0], [-3, -5, -14], [-32, -15, 8], [17, -17, 14], [-27, 3, -16], [-30, 21, -14], [6, -11, 15], [-32, 7, -14], [2, -8, 13], [-54, 12, 4], [20, -5, 15], [27, -21, 9], [66, 0, -4], [8, -4, 8], [24, 7, -11], [31, -15, 6], [42, -7, -5], [-27, -4, -14], [-34, 1, 4], [10, -10, 22], [-58, -8, 8], [-25, -19, 11], [-71, 7, 4], [-54, -8, 5], [1, 16, -4], [-14, 33, 5], [26, -26, 8], [-20, -1, -23], [44, -10, 12], [-26, 4, 2], [34, 32, -7], [20, 34, -4], [46, 20, -2], [21, 32, -12], [7, -4, -6], [-4, 7, 18], [-2, -6, 18], [6, -27, -8], [46, -12, 10], [40, 6, 6], [42, 18, -15], [58, -3, -8], [-13, -35, 17], [-10, -2, -4], [58, 6, -1], [-4, -1, 11], [-23, -20, 14], [21, 1, 12], [10, 34, -18], [10, 34, -22], [1, -1, -5], [-66, -10, 10], [-20, -24, 10], [-33, 9, 1], [-45, 5, 2], [-44, 12, 0], [-38, -7, 11], [23, -33, 1], [13, -14, 0], [-30, 11, 3], [-32, 4, 16], [-22, 28, 11], [-32, 19, -23], [-39, -2, 17], [39, 19, -5], [-4, 17, -26], [-12, 23, -31], [58, -16, -1], [-41, -8, -8], [-32, 7, -28], [-33, 1, 0], [6, 7, 10], [14, -20, -16]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1859_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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