
// Make newform 1850.2.a.ba in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1850_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_1850_2_a_ba();".
// 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_1850_2_a_ba();".
// 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, -3, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, -1, 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_1850_a();"
function MakeCharacter_1850_a()
    N := 1850;
    order := 1;
    char_gens := [1777, 1001];
    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_1850_a_Hecke(Kf)
    return MakeCharacter_1850_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], [1, 0, 1], [0, 0, 0], [2, -2, -1], [0, -1, 2], [4, 0, 1], [-1, 2, -1], [2, -3, -1], [2, 2, 2], [-2, 4, 0], [-3, 3, 0], [1, 0, 0], [-7, 0, -2], [1, 5, -3], [0, 4, 0], [4, -6, -2], [1, 5, -3], [-4, 4, 3], [3, 4, 3], [-4, 4, 3], [10, 1, 1], [8, -2, -2], [2, 1, 8], [0, -3, 0], [2, 0, 2], [-4, -4, -6], [-3, -3, -2], [3, -2, -5], [2, -2, -4], [-4, 3, 2], [0, 4, -1], [11, -5, -3], [9, -6, -4], [0, 7, -4], [1, -1, -6], [4, 0, -2], [7, 7, 0], [0, -5, -7], [11, -7, -4], [2, 0, -6], [5, 0, -6], [0, 2, 4], [6, 8, -4], [6, -1, 8], [-1, -9, -8], [-2, 0, -12], [-7, 12, 5], [8, 2, 8], [-15, 1, 3], [0, -6, 10], [7, -3, -1], [-6, -6, 8], [3, -8, -1], [3, 0, 6], [14, 0, 4], [-10, -2, -8], [-2, 8, 12], [6, 8, 3], [-16, 6, 4], [13, -7, 1], [20, -1, -5], [-7, -9, 2], [-4, -7, -16], [-1, -1, 6], [9, 5, 5], [-5, -5, -16], [16, -9, 3], [-5, -6, 10], [9, -6, -8], [1, 3, -10], [-16, 18, 10], [-6, 2, 8], [10, -18, -5], [11, 7, -2], [9, 4, 9], [-5, -3, -2], [4, -2, -1], [5, -13, -12], [-14, 1, -4], [-17, -4, 7], [2, -15, -8], [-8, 0, 6], [20, -6, -8], [15, 4, 16], [-20, 0, -4], [12, 11, 6], [-6, 17, 0], [-8, 1, 10], [-6, 4, -4], [-1, -11, -8], [-10, 6, 12], [-17, 7, -8], [7, 5, -14], [22, -14, -4], [-35, 1, 3], [-8, -12, -4], [-13, -9, -2], [6, -3, 11], [-1, -12, -8], [-4, -4, 5], [14, 7, 7], [0, -10, 0], [-7, 1, 7], [-10, -11, -14], [16, 4, -6], [-5, 12, -7], [-8, -1, 1], [5, -4, 12], [-10, 16, 1], [-2, 15, 9], [35, 1, 2], [-16, -10, -16], [-5, 13, 1], [26, -14, -4], [12, 4, 12], [6, 8, 16], [3, 7, 15], [1, 13, 4], [-2, -10, -3], [-3, -12, -17], [16, -4, 12], [-7, 7, -7], [2, -18, -2], [-8, -7, 1], [8, -9, -8], [-36, 14, 5], [-16, 0, 14], [-4, -4, -5], [6, -12, 8], [-6, 12, -8], [32, 8, -8], [-18, 4, 0], [4, 4, -2], [8, 12, 8], [-22, -11, 1], [-22, -3, -6], [1, -17, -4], [0, -4, -12], [22, 8, 7], [16, 10, -8], [12, 0, -2], [-9, -5, -24], [18, -6, 9], [10, 1, 9], [8, -2, 17], [14, -12, 1], [-12, 22, 9], [31, 2, 5], [-48, 3, 1], [12, 4, 23], [7, 13, -18], [1, 7, -1], [3, 20, 18], [-2, -16, -20], [11, -1, 13], [-1, 1, 20], [2, -16, -4], [25, -33, -11], [10, -11, 1], [20, -6, 14], [-16, -4, 0], [-14, -13, 3], [-26, 10, 6], [12, -5, 20], [3, -6, -17], [-12, -10, 20], [3, -21, -18], [6, 22, -9], [8, 7, -13], [-32, 12, -3], [-22, 23, 9], [-32, 0, -14], [13, 13, 8], [22, -19, -1], [-8, 2, 21], [31, -18, -1], [-14, -21, 13], [-29, 17, 16], [4, 6, -25], [-21, 15, 10], [-23, 25, 8], [3, -6, -2], [8, -2, 3], [-21, 12, -3], [-44, 18, 7], [10, 8, 8], [6, -6, -14], [45, -6, 4], [7, 20, -9], [-22, 14, 3], [-22, 5, -10], [-29, -8, -27], [14, -21, -9], [14, 2, -22], [10, -1, 12], [19, -3, -13], [-2, -29, 6], [-6, 6, -4], [25, 12, 10], [-1, 19, 0], [-12, -16, 7], [12, -4, -13], [-33, 19, 10], [-15, -14, -12], [20, -8, -14], [-8, -2, -8], [-34, 0, 14], [8, 20, 12], [-12, -10, -2], [-1, 13, 15], [-15, -11, 17], [4, -24, 1], [-29, 15, 8], [-22, -1, 8], [48, -4, -10], [1, -8, -6], [-13, -9, 18], [-20, 31, 18], [38, 6, 0], [16, 2, 14], [1, -17, -28], [-20, -8, -28], [3, 14, -22], [5, -21, -22], [-36, 9, 18], [6, 14, -1], [31, -2, -14], [-20, -22, 2], [-6, -8, -28], [35, -8, -8], [39, -17, 0], [49, -4, 7], [-22, 22, 16], [46, 3, 7], [22, 10, -12], [12, 2, -17], [6, -23, -19], [16, 10, 8], [36, 5, -7], [-12, -10, 0], [19, 20, 10], [-8, 9, 25], [-24, 14, -13], [22, -32, -21], [-6, -23, 9], [-23, -9, 2], [69, -1, 4], [26, -6, -16], [39, 6, 1], [-16, 2, -12], [-26, -16, 21], [-32, 21, 11], [-36, 0, 10], [-35, -4, 1], [18, 28, -15], [15, 14, -9], [-27, 17, 4], [-20, 3, 16], [48, 10, 0], [15, 18, -13], [16, -16, 0], [-34, -15, 11], [1, 23, 36], [-51, 5, -2], [-24, -7, 2], [-19, 26, 5], [-15, -25, -10], [22, 28, -16], [-48, -7, -8], [-24, 22, 19], [-8, 8, 6], [17, 0, -6], [-32, 23, 10], [-39, -17, 16], [18, -12, -22], [-2, -4, 29], [-56, 33, 16], [32, 12, 7], [5, -9, -35], [36, -25, -11], [18, 6, -13], [-11, -7, 8], [-42, 0, -2], [20, -14, -24], [23, -26, -8], [6, -10, -13], [33, -11, 21], [-18, -22, 4], [0, -18, -18], [-15, 5, 13], [24, -26, -18], [17, -19, -21], [12, 7, 15], [29, -8, -9], [-10, 28, 4], [31, -7, -4], [6, -14, -3], [-3, -7, -14], [26, -18, 8], [20, 11, 28], [-2, 15, 2], [-14, 42, -2], [-16, 22, -19], [-28, -23, -9], [-49, 24, 16], [15, 22, 29], [-13, -21, 1], [28, 14, -5], [58, -2, -4], [-48, 2, 10], [20, -6, -10], [22, -4, 0], [9, -28, 6], [12, 36, -4], [28, 14, 8], [-12, -30, -22], [38, 1, -2], [29, 7, 10], [40, -9, 1], [50, -45, -16], [-11, -15, -15], [30, 15, 23], [36, -28, 1], [-4, 0, -10], [1, -29, -7], [-14, -12, -10], [-18, 23, -6], [37, -39, -11], [7, -17, 14], [-9, 7, 16], [-37, 7, -6], [70, -2, 1], [-37, 13, 24], [3, 8, -24], [-33, 6, -6], [2, -6, -18], [0, 14, -8], [-3, 23, -25], [-48, 25, 6], [8, -38, -23], [-35, 27, 10], [10, -1, 11], [-54, -6, 0], [-28, 38, 0], [8, -38, -11], [-16, -28, -4], [-8, 28, -18], [-42, 25, -4], [-19, 37, 0], [42, -12, -2], [-6, 27, 2], [31, -27, -9], [16, 28, -9], [30, -40, -16], [-33, -7, -32], [-33, -2, 20], [-24, 14, 28], [7, -18, 24], [-14, 16, 18], [-8, -2, -8], [-24, 10, 29], [8, 9, -23], [-3, -41, -12], [-47, -3, -7], [29, -18, 15], [13, -30, -29], [8, 2, -15], [15, -7, 16], [-26, 1, 0], [-17, 23, 29], [1, 0, 14], [56, -24, 5], [-32, 36, 0], [-65, 3, -14], [13, -35, 0], [-25, 16, 33], [-4, -30, -15], [-5, -30, -48], [22, 5, 22], [-1, 37, -27], [50, -36, -22], [62, -37, -14], [-32, 8, -8], [-28, -13, -3], [17, -23, -31], [50, 16, -12], [-76, -8, -17], [34, -16, -17], [-38, 6, -8], [-2, -10, 2], [6, 23, 39], [-28, -32, 19], [20, 1, -13], [7, 22, -8], [-22, 22, -2], [-33, -12, -4], [-30, 2, 16], [-44, 11, 11], [-74, 1, -12], [-21, -31, 8], [-38, -18, -32], [-6, -1, -8], [15, 29, -36], [43, 18, -3], [12, 4, 13], [-22, 20, 18], [40, -46, -21], [-21, -23, -7], [-30, 34, 24], [14, -13, 14], [-22, 29, 3], [0, 6, 16], [7, -26, -14], [-59, 41, 19], [1, -2, 7], [-8, 0, -14], [16, 10, 1], [-6, 14, 1], [47, 24, 6], [34, 8, 6], [-4, 42, 34], [-34, 34, 1], [-58, 16, 12], [19, -38, -7], [-12, -35, 31], [-36, -14, -36], [60, 16, -4], [-42, 43, 9], [53, 14, 15], [-50, -6, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1850_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1850_2_a_ba();".
// 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_1850_2_a_ba(:prec:=3)
    chi := MakeCharacter_1850_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_1850_2_a_ba();".
// 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_1850_2_a_ba( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1850_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![5, -1, -3, 1]>,<7,R![34, -8, -4, 1]>],Snew);
    return Vf;
end function;