
// Make newform 1850.2.a.w in Magma, downloaded from the LMFDB on 19 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_w();".
// 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_w();".
// 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 := [-6, 0, 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_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], [1, 1], [0, 0], [0, -1], [1, -1], [2, 1], [1, -1], [-5, 0], [2, 0], [4, 2], [2, -1], [-1, 0], [1, 0], [-6, -2], [0, -4], [6, 0], [-2, 0], [-4, 1], [7, 1], [-10, -1], [3, -4], [-2, -4], [1, -1], [7, -3], [14, 0], [6, 2], [4, -1], [-11, -1], [14, 0], [-7, -1], [-4, 5], [-6, -6], [-5, -6], [-15, -1], [-12, -3], [-14, 0], [14, -1], [15, 2], [-8, 1], [8, 6], [9, 4], [2, -2], [2, 4], [-5, -1], [-10, 3], [4, 8], [-7, -1], [12, -4], [-2, 6], [-4, -4], [-4, 10], [-4, -2], [-5, -1], [-21, 4], [-2, 2], [10, 4], [4, 0], [8, -9], [-4, -4], [4, -2], [3, 6], [-6, 9], [15, 3], [-6, 1], [4, -2], [-12, -1], [15, -2], [1, -6], [-9, 4], [-10, 1], [10, -2], [-2, 12], [-2, 5], [2, 9], [1, 9], [2, 3], [-4, -7], [-8, 3], [-11, -11], [-17, 7], [-19, 1], [28, -2], [-6, 6], [19, 0], [0, 8], [-15, 1], [21, -5], [3, -5], [24, 6], [12, 1], [-12, 0], [-6, -5], [6, 15], [-32, 4], [22, 0], [0, -4], [22, -3], [15, 2], [11, 12], [18, -3], [-11, -6], [-12, 4], [18, -8], [5, -9], [12, 0], [-1, -9], [-21, -6], [17, -12], [12, -11], [-7, 0], [8, 15], [-10, 10], [0, 2], [-28, 2], [2, 0], [2, -8], [-2, -14], [-28, 5], [12, 9], [9, 1], [16, -4], [-4, -4], [-24, 8], [-7, 0], [37, 3], [-22, 5], [12, -2], [-8, -3], [36, 0], [4, 8], [-12, 8], [-26, -8], [-16, 6], [-4, 4], [-29, -6], [41, -5], [0, -9], [16, -6], [-8, -5], [-10, 12], [-4, -4], [-18, -9], [2, -13], [27, 0], [14, -3], [6, 3], [-12, 3], [-15, 7], [15, 10], [6, -5], [12, 3], [0, -14], [31, 2], [36, -6], [-30, 2], [-4, 5], [-42, 4], [4, 4], [-5, -8], [-12, 12], [32, 0], [1, -10], [-44, 0], [-7, 19], [13, 15], [-6, -10], [-14, 1], [42, 1], [-41, -6], [-18, 9], [-23, 14], [-6, 4], [6, -15], [7, 16], [10, 21], [49, -3], [-3, 22], [-42, 5], [30, -13], [-10, -15], [24, -7], [9, 16], [44, 5], [-41, -3], [6, 11], [-36, -4], [-22, 0], [-1, -6], [31, -11], [2, 13], [17, 1], [-25, -11], [9, 24], [18, 4], [63, -1], [32, -8], [51, 3], [22, -4], [-31, -10], [6, -1], [4, -7], [-6, -13], [-32, -15], [-37, 4], [-12, 0], [54, 2], [22, -18], [-16, -10], [-6, 4], [10, 0], [28, 14], [-24, 19], [40, -9], [19, 7], [-6, 2], [21, -6], [-2, -15], [21, -5], [6, 6], [-18, -4], [38, -5], [4, -4], [-51, 0], [-6, -23], [-13, -3], [16, 15], [-19, 20], [40, 2], [12, -14], [27, -20], [-2, -1], [-11, 19], [6, -12], [-7, -10], [-16, 22], [-44, 1], [11, 2], [-36, -8], [-51, 10], [-28, -2], [-25, 12], [1, -10], [40, -11], [-14, 25], [-21, 10], [-52, 5], [-26, -5], [36, -4], [25, 11], [-12, -22], [10, -17], [21, 18], [-30, 12], [-47, -7], [-26, 7], [9, 23], [-16, 13], [-3, -1], [18, 10], [-31, 15], [18, 0], [-21, 4], [-44, 3], [-44, -13], [15, 7], [19, 11], [-6, -27], [-30, 10], [23, 7], [-4, 13], [0, -22], [17, -14], [41, -1], [-12, -1], [18, 0], [26, 15], [15, -9], [-14, 3], [-20, 18], [11, -12], [-30, 1], [6, -19], [-16, -12], [24, 2], [-47, 4], [34, 9], [-40, 6], [24, -22], [36, -16], [-40, 6], [-42, 12], [14, -2], [31, -4], [9, -5], [-20, -12], [-26, 5], [-32, -11], [36, -5], [-36, -12], [17, -7], [-1, -13], [6, -24], [26, 13], [-31, 18], [7, 12], [17, 9], [-14, 14], [-4, -19], [-14, 4], [58, 4], [10, 4], [-54, 0], [-3, -28], [-68, -6], [-20, 18], [-50, -14], [-23, -7], [12, 1], [-1, 28], [-21, -1], [2, 10], [-3, 16], [-18, 9], [-10, 0], [76, -4], [14, 26], [-5, -7], [2, 16], [30, -7], [-4, 23], [52, -3], [-36, -11], [22, 21], [-33, -22], [-47, -6], [-16, -6], [40, -6], [16, 20], [-9, 29], [-24, -17], [-8, 9], [-63, 4], [66, -8], [64, 6], [72, 3], [-12, -28], [-16, -18], [-3, -13], [0, -19], [30, 2], [35, -5], [-16, -18], [28, 5], [24, 0], [22, -27], [21, -26], [-24, -22], [29, -12], [-34, 16], [-52, 6], [-40, 7], [9, -4], [14, -13], [-38, -2], [-21, -25], [3, 11], [42, 9], [-24, 1], [-15, -5], [-6, 18], [-59, -10], [-30, -3], [24, -28], [20, -3], [62, -15], [35, -9], [-72, -9], [13, 2], [57, -7], [12, -14], [46, 10], [15, -29], [-20, 20], [55, -18], [-34, 0], [18, 8], [-66, -1], [-14, -31], [-16, 0], [-10, 20], [45, -14], [18, 11], [33, -20], [-27, -14], [42, -6], [-29, 26], [44, -6], [-21, -12], [29, -9], [-34, -7], [-24, -10], [-13, 3], [8, 5], [-3, -25], [66, -11], [8, -12], [0, -23], [0, 28], [56, 16], [-21, 17], [19, 10], [-24, -22], [-15, 6], [22, -4], [-37, -11], [-18, -18], [-8, -9], [-24, 5], [27, 2], [30, -14], [-36, 10], [28, -15], [-6, -20], [-39, 11], [37, 24], [-68, -10], [4, -10], [57, -14], [-45, -9], [-6, 18]];
    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_w();".
// 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_w(:prec:=2)
    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_w();".
// 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_w( : 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, -2, 1]>,<7,R![-6, 0, 1]>],Snew);
    return Vf;
end function;