
// Make newform 2850.2.a.bi in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2850_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_2850_2_a_bi();".
// 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_2850_2_a_bi();".
// 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 := [-3, 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_2850_a();"
function MakeCharacter_2850_a()
    N := 2850;
    order := 1;
    char_gens := [1901, 1027, 1351];
    v := [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_2850_a_Hecke(Kf)
    return MakeCharacter_2850_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, 0], [0, 0], [1, 1], [2, -1], [1, -1], [1, -3], [-1, 0], [1, 4], [0, 1], [1, 2], [2, 0], [4, 4], [3, -3], [0, 2], [0, -1], [-3, 3], [2, -5], [2, 1], [7, -3], [-1, -2], [-9, -2], [-2, 1], [-3, -8], [3, -7], [11, 1], [7, 2], [3, 3], [-8, -6], [3, -6], [5, 0], [12, 1], [2, -6], [-9, 7], [-8, 8], [-8, 2], [0, 8], [2, 0], [2, -4], [2, 9], [-12, 0], [-1, 9], [3, 0], [-3, 11], [-9, 9], [-14, 0], [-12, -1], [7, 2], [6, 0], [-8, -1], [9, -7], [-14, 0], [-25, 3], [8, -2], [-9, 0], [-15, 0], [-24, -2], [-21, -3], [-6, 3], [-3, -12], [-4, 0], [26, -1], [22, 1], [-12, 10], [-3, -4], [0, -1], [-24, -1], [-14, -2], [0, -12], [2, 11], [3, 11], [-10, 4], [15, 1], [9, -13], [10, 6], [-3, -13], [-6, -16], [-10, 1], [3, -8], [12, 6], [0, -8], [-14, -12], [-12, 10], [-19, 3], [5, -12], [-12, -5], [21, -2], [-12, -4], [-4, 8], [-18, 10], [2, -3], [17, 8], [16, 8], [4, 4], [5, -21], [-4, 8], [26, 7], [-7, -18], [4, 0], [12, -3], [-14, 17], [-9, -3], [1, 5], [18, 0], [-1, 3], [-7, -14], [14, 1], [38, 6], [5, 5], [9, 11], [-9, -12], [-18, 12], [-36, 6], [5, 3], [-4, -4], [14, 2], [-34, -4], [1, 6], [14, -2], [23, 9], [12, 20], [19, 15], [0, -5], [-21, 15], [-17, 3], [14, 6], [4, -25], [-19, -18], [0, -22], [-2, -13], [-22, 18], [12, 4], [4, -2], [-2, -9], [3, 1], [-7, 16], [28, -10], [10, 7], [-20, 0], [15, -13], [-18, -21], [25, 13], [-14, -12], [-5, 1], [1, -3], [-31, 9], [8, 2], [36, -4], [50, -4], [38, -10], [0, 18], [-30, -2], [-29, -1], [18, 6], [-32, 10], [-4, -22], [-27, -13], [36, -4], [34, 12], [-14, -1], [8, 8], [19, 14], [-20, -22], [9, 27], [2, 2], [-39, -5], [-23, 10], [-44, 11], [14, 16], [1, 5], [42, -10], [19, -7], [-8, -6], [-3, 8], [-1, 6], [36, -6], [-8, 28], [27, -9], [-28, 10], [-20, 5], [21, 11], [-31, 9], [15, -9], [-7, -24], [-37, 2], [-9, -7], [10, -2], [-8, 27], [36, 0], [-29, -4], [50, -2], [-20, -13], [18, 21], [-14, 7], [8, -26], [-2, -2], [29, -4], [-44, 3], [42, 0], [-7, -24], [30, 11], [8, -26], [-15, -5], [-25, -13], [10, 19], [66, 2], [-9, 17], [5, 11], [-8, 10], [4, -30], [11, 12], [47, -1], [49, -11], [18, 16], [-38, 10], [40, -14], [4, 30], [-11, 8], [-42, 8], [4, 26], [-16, 6], [21, -4], [-18, 22], [33, -1], [57, -3], [56, 0], [29, 12], [-63, -1], [6, -28], [-2, -23], [-56, -6], [-12, -25], [-31, 6], [11, 6], [-7, -7], [60, -4], [-20, -16], [45, 1], [10, -38], [-39, -5], [-53, 9], [-14, -6], [-4, 30], [-7, 17], [-16, -28], [9, 0], [0, 20], [30, -20], [56, 1], [-19, 18], [-52, 7], [-13, 5], [-7, 26], [31, -27], [-14, 18], [-24, 11], [66, 0], [28, 9], [12, 18], [42, 10], [23, 14], [11, 29], [4, -35], [17, -35], [0, 22], [54, 13], [-8, -2], [9, 17], [44, -5], [27, -23], [-4, -32], [-2, 3], [11, -36], [4, 38], [8, 36], [3, -17], [6, -43], [-13, -23], [28, 8], [32, -9], [-29, 19], [25, 33], [38, -24], [53, 13], [4, 32], [24, 4], [51, -8], [22, -9], [-10, 2], [5, -12], [15, -13], [-22, -21], [70, 2], [-47, -7], [-4, 24], [-1, -25], [-12, 34], [-53, -15], [-18, -16], [16, -22], [55, -2], [-24, -21], [45, 15], [36, 8], [30, 16], [14, 6], [5, -5], [34, 10], [-57, 2], [32, 23], [53, -4], [-5, 35], [-59, -2], [38, 11], [-24, -18], [-1, -9], [22, 23], [20, -38], [10, -2], [17, -9], [-2, -14], [-36, -12], [2, -16], [44, -12], [-59, -3], [33, 7], [56, 2], [16, -26], [-9, 15], [21, -11], [-6, -35], [-8, 37], [18, 38], [-66, -3], [50, -8], [-76, 1], [60, -14], [25, 12], [-27, 14], [22, 42], [81, -5], [-21, 29], [12, -31], [13, -6], [-24, 1], [-48, 2], [-10, 27], [14, -23], [18, -6], [-15, 23], [10, 3], [-41, -17], [-6, 10], [13, -36], [-68, 16], [37, -4], [39, -22], [6, -3], [9, 23], [-17, -21], [-48, -25], [23, -17], [-81, 1], [0, -22], [52, -12], [-11, -31], [26, -28], [32, -18], [-46, 2], [10, -1], [26, -21], [-72, 8], [48, 19], [-54, -10], [-16, 10], [-31, 1], [-33, -2], [-55, 26], [-18, 14], [-2, 24], [22, 16], [30, 0], [-22, 2], [-21, -21], [64, 7], [-51, 1], [16, 0], [-44, 10], [-34, -38], [-11, -13], [-11, 1], [-38, 23], [38, 33], [16, -18], [45, 19], [-11, -17], [4, -34], [79, 0], [-22, 41], [-7, -21], [16, 18], [-32, -30], [-12, 2], [-9, -2], [26, 19], [-38, 0], [-10, -11], [-51, 0], [-38, -21], [28, 31], [-20, 44], [-30, -4], [5, -21], [-30, -30], [19, 48], [-8, -42], [-22, -10], [-7, 42], [-36, -24], [35, 21], [54, 4], [66, -16], [-29, -4], [16, 2], [-27, 41], [-18, -32], [-6, -26], [-6, -38], [21, -17], [-17, -46]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2850_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2850_2_a_bi();".
// 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_2850_2_a_bi(:prec:=2)
    chi := MakeCharacter_2850_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_2850_2_a_bi();".
// 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_2850_2_a_bi( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2850_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-2, -2, 1]>,<11,R![1, -4, 1]>,<13,R![-2, -2, 1]>,<23,R![-47, -2, 1]>],Snew);
    return Vf;
end function;