
// Make newform 1859.2.a.f in Magma, downloaded from the LMFDB on 24 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_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_1859_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
        poly := [7, -6, -1, 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 := [[0, -1, 0], [0, 0, 1], [-1, 1, 1], [1, 1, 1], [1, 0, 0], [0, 0, 0], [-2, 0, -1], [1, -2, 1], [0, 2, 1], [-2, -1, 3], [-1, 2, 2], [4, 1, -1], [2, -4, -1], [-4, 5, 3], [-5, 3, 4], [-3, -1, 1], [10, 1, -1], [-7, 3, 0], [4, 1, 2], [-4, 1, 0], [-2, 0, 0], [4, -2, 2], [-4, 1, -3], [-5, -1, -1], [5, 0, -4], [6, -5, -6], [-1, 3, 8], [-7, -1, 3], [9, 2, -3], [-7, 4, 5], [2, -3, -1], [-6, -1, -3], [-7, 1, 2], [6, -6, -2], [2, -3, -5], [10, -7, -4], [5, 5, -2], [-7, 1, 5], [-3, 8, 7], [-10, -1, 4], [8, 3, 3], [10, -8, -8], [7, -4, -1], [-3, 6, 4], [-7, -4, -4], [3, -8, -6], [3, 6, 3], [-3, -7, 1], [-14, 6, 5], [20, 0, 3], [-18, 0, -4], [-5, 5, 8], [13, -4, -3], [-1, 4, 0], [10, -9, -6], [0, -2, -9], [-5, 1, 1], [28, -1, -2], [-2, -1, -8], [1, 2, 5], [13, -4, -6], [0, 6, -6], [4, 1, -1], [0, -3, 8], [-6, 9, 1], [-2, -7, -2], [-10, -8, 0], [10, -13, -10], [6, -4, 1], [3, -3, 3], [0, -8, 6], [-2, 2, 1], [9, -9, -11], [2, -2, -5], [17, -1, 0], [19, -8, -5], [-13, 0, -7], [16, -12, -5], [-7, -7, -11], [4, 5, 11], [1, 2, -8], [2, 4, 2], [7, -13, -6], [16, -3, -4], [-16, 0, 9], [1, 5, 13], [10, -1, -11], [-5, 1, -8], [-6, -1, -4], [5, -3, -7], [-14, 11, 10], [7, 0, 1], [-24, 10, 4], [-17, 6, -4], [7, -4, 1], [-7, -7, 4], [5, 3, 7], [22, -9, -11], [18, 1, -2], [-6, -2, -7], [-26, -9, 2], [-7, -2, -8], [-12, -4, 1], [19, 12, 0], [10, -9, 4], [11, -6, -3], [-3, 2, -2], [-14, 9, 10], [4, 5, -3], [2, 10, 3], [-1, 4, 6], [-24, 3, -2], [-2, -5, -13], [8, -3, -4], [-1, -3, -12], [4, 4, 11], [-1, 1, 0], [1, -7, 1], [9, 5, -4], [-8, 6, 12], [19, -2, -11], [21, 3, 2], [5, 9, -4], [18, -7, -2], [-1, 6, 6], [1, -7, -18], [5, 11, 3], [6, 7, -8], [1, 15, 0], [18, -2, 3], [-3, -16, -3], [-3, -5, 13], [-6, 4, 10], [28, 6, -6], [6, -7, 5], [28, 5, 0], [-11, 0, -11], [-6, -6, -14], [6, -4, 12], [-42, 5, 5], [17, 3, -12], [1, 6, 13], [9, -7, -10], [-7, 15, 1], [-7, -2, 4], [-8, 3, 8], [9, -13, 2], [27, -7, -8], [-1, 6, -7], [-29, -1, 1], [22, 12, 4], [-3, 13, 5], [-19, -9, 1], [13, 5, -4], [1, 13, 7], [18, -9, -7], [-1, 0, 8], [-15, 16, 3], [17, 2, -13], [-14, 4, 14], [-7, -9, 0], [-7, 9, -1], [-27, 14, 7], [6, 1, -8], [-10, -10, -6], [3, 3, 14], [11, -13, -19], [-3, -17, 2], [1, 15, 10], [7, -14, -5], [-47, 5, 4], [20, 9, 15], [11, 18, 2], [-4, -3, -18], [29, -26, -19], [-36, 2, -7], [6, -3, -8], [37, -10, -12], [-8, 2, 15], [-15, -2, 14], [0, -24, 1], [-10, -13, -6], [-5, -14, 5], [-2, -2, -4], [11, -15, -2], [22, -1, -3], [18, -12, -13], [-24, 11, 13], [-7, -9, 3], [10, 2, -9], [-16, -2, -13], [-10, 11, 20], [-26, 13, 9], [29, 2, 7], [0, 8, 6], [-12, 13, 10], [-1, -19, -1], [4, -18, -15], [27, 13, 7], [-8, -2, -7], [15, -3, -10], [-13, -18, 3], [-8, -15, -19], [-19, -9, -22], [-26, -7, -5], [-3, -12, -14], [6, 12, -9], [-14, 16, -3], [59, -9, -8], [-24, 2, 1], [5, -5, 14], [11, -17, -5], [31, -7, 7], [3, -22, -3], [-28, 9, -1], [-5, -8, -1], [-50, 5, 6], [0, -7, 6], [-25, 0, -8], [9, 15, -5], [-30, -4, 1], [31, 7, 15], [-5, -1, 9], [-44, 3, 8], [6, -12, -7], [-23, -6, 14], [-6, 1, -10], [30, 9, 4], [8, 4, -3], [5, -5, -24], [-52, -5, 3], [16, 1, 14], [2, -16, -17], [-14, 10, 13], [-16, 9, 26], [-43, -12, 7], [-28, 4, 9], [-1, -1, 2], [46, 1, 6], [12, -8, -13], [-13, 19, 21], [-47, 15, 10], [-25, 3, -3], [-14, 8, 11], [13, 1, 25], [-14, 26, 3], [22, 19, 5], [6, 1, 2], [-18, -3, -27], [-4, 0, 0], [1, 25, 7], [2, -17, -3], [-21, 10, -6], [5, 3, -16], [29, 0, 5], [-43, 9, 7], [5, 6, 8], [26, 13, 4], [-28, -7, 8], [15, -11, 5], [6, -3, -20], [21, -20, -1], [22, 16, 7], [-28, 21, 5], [-6, 22, 25], [-14, 29, 13], [4, 12, 2], [5, -7, 9], [-29, -21, 7], [10, 6, 22], [-23, -18, 12], [-6, -5, -15], [12, -26, -24], [-38, 16, 7], [-36, 10, 12], [-11, 11, 11], [30, 1, 1], [-13, 4, 9], [12, 12, -7], [-39, 10, 3], [-26, 24, 9], [-8, 18, -4], [16, 7, 36], [6, 9, 23], [14, 7, -12], [42, 13, 5], [-49, 5, 7], [11, -20, -3], [-9, -10, -27], [16, 17, -8], [-14, 20, 0], [38, -27, -25], [-14, 24, 1], [-26, 1, -19], [5, -15, -30], [-35, 12, -4], [32, -6, -10], [1, 6, -1], [-42, 31, 20], [-24, -6, 0], [-6, 1, 10], [-18, 16, -10], [9, -10, -14], [49, -7, -18], [-12, -11, -12], [5, 10, 33], [-34, 27, 22], [18, 0, -6], [-36, 2, -1], [9, 9, -11], [27, -1, 0], [-13, -14, -13], [-52, 14, 4], [75, -7, -9], [-27, 19, 22], [1, -8, -26], [-35, 17, 7], [26, 3, -15], [4, -16, -14], [12, 30, 7], [15, -25, -8], [-18, 20, 7], [-20, 33, 21], [-13, 14, 31], [14, 23, 13], [21, 12, -14], [-16, -1, -4], [-3, 15, 20], [8, -10, -13], [-1, 2, -5], [1, 0, 7], [40, 0, 1], [26, -15, -17], [25, 0, -2], [0, -9, -35], [23, -24, -24], [-7, 18, 3], [-39, 7, -4], [14, -5, -21], [-13, 8, -2], [39, -34, -22], [20, 3, -13], [15, 24, -17], [-10, -6, 17], [2, -24, 2], [-48, 19, 1], [-2, -17, 0], [36, 8, 26], [5, -1, -29], [42, -30, -23], [0, -5, -4], [13, -8, -13], [-18, -26, -8], [15, -14, -12], [-54, -1, -4], [-17, 0, -6], [-1, 0, -1], [5, -8, 5], [-2, -15, -2], [29, -22, -13], [10, 9, -5], [-30, -21, 10], [-18, 10, 12], [52, 10, -3], [34, 11, -10], [2, -30, 6], [-3, -5, -19], [27, 1, -17], [-14, 5, 22], [-5, -14, -27], [-3, 17, 10], [-27, 29, 8], [-16, 4, -20], [-2, -15, -12], [2, 14, -16], [48, -6, -17], [0, -12, -2], [34, -16, 4], [44, -6, 17], [-48, -2, -17], [19, 6, 32], [-44, 18, 23], [47, -41, -22], [-14, -13, 11], [38, 8, -1], [7, 7, 30], [51, -8, -8], [41, 14, 12], [54, 16, 8], [41, -3, -25], [59, -22, -7], [10, 12, -19], [-40, 36, 33], [-41, 0, -16], [-1, -13, -28], [-18, 11, 27], [36, 13, -8], [1, -26, 5], [44, -3, -8], [59, -16, -4], [-22, 0, 3], [39, 15, 11], [-13, -32, -5], [-20, 11, -15], [22, -3, 16], [-2, 15, 22], [-16, 27, 43], [-8, -23, 9], [37, 11, 6], [21, 10, 12], [15, -24, -31], [10, 9, 6], [31, -10, 11], [28, -19, -30], [2, 13, 9], [37, 6, 14], [5, -6, -38], [32, 7, 11], [-39, -21, -1], [18, 9, 16], [15, -23, -23], [57, -10, -14], [20, -23, -40], [-37, 13, 7], [13, 1, -10], [-29, 2, -11], [2, -2, -4], [63, -9, -10], [8, 30, 8], [45, -4, 0]];
    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_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_1859_2_a_f(: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_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_1859_2_a_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1859_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-7, -6, 1, 1]>,<7,R![1, 2, -5, 1]>],Snew);
    return Vf;
end function;