
// Make newform 8450.2.a.bf in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8450_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_8450_2_a_bf();".
// 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_8450_2_a_bf();".
// 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_8450_a();"
function MakeCharacter_8450_a()
    N := 8450;
    order := 1;
    char_gens := [677, 3551];
    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_8450_a_Hecke(Kf)
    return MakeCharacter_8450_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], [3, 0], [3, 0], [0, 0], [3, -3], [3, 2], [6, -2], [-6, -2], [3, -1], [6, 3], [0, -6], [2, 0], [3, 0], [3, 2], [0, 6], [1, -3], [0, 0], [-6, 0], [3, -5], [1, 3], [3, -3], [12, 3], [3, 7], [-9, 1], [-4, 3], [-9, 5], [-12, -2], [0, 4], [16, 3], [-6, -7], [-3, -3], [-4, -3], [6, 0], [15, -5], [13, 0], [-9, 1], [-3, 0], [-15, 0], [6, -2], [1, 9], [-21, 1], [6, -6], [6, -9], [-4, -6], [-2, 9], [-3, 2], [-6, 6], [3, 1], [3, -1], [-3, -3], [0, -5], [-12, 3], [0, 8], [12, 9], [-15, 7], [6, -2], [-1, 0], [0, -6], [-20, -6], [6, -3], [-24, 2], [-3, -7], [-16, 6], [18, -3], [6, -4], [1, -3], [27, -1], [15, 11], [15, 3], [-12, -6], [16, 6], [-10, -6], [-9, 12], [-12, 6], [-21, 1], [-6, 3], [0, 3], [-18, -1], [0, 10], [15, 7], [3, -3], [-2, -6], [19, 9], [12, -6], [0, 9], [27, 5], [-15, 9], [6, -4], [-24, 8], [-15, 15], [-9, -14], [-12, 5], [12, -8], [18, -5], [12, -6], [3, -2], [-8, -6], [-3, 11], [-14, -12], [12, 3], [18, 2], [3, -14], [2, -21], [-3, -17], [30, 0], [0, 12], [-6, 8], [-17, 12], [22, -9], [6, -19], [-18, 0], [21, -6], [0, -12], [-39, -4], [9, -1], [-6, -3], [9, 6], [12, 10], [3, -19], [14, -6], [12, -8], [0, -18], [9, -16], [-3, 19], [15, 15], [-12, 8], [-2, -21], [18, 19], [-9, -24], [-24, -6], [10, -6], [17, -12], [-24, -3], [0, -22], [-18, -9], [-3, 23], [-6, 4], [24, 0], [33, -6], [-15, 3], [-22, 3], [-27, 9], [20, 0], [-27, -3], [0, -8], [-15, 1], [16, 21], [-12, 18], [24, -2], [-9, 10], [-11, -9], [-6, -27], [-11, -9], [-18, 2], [10, -12], [-24, -12], [20, 6], [24, 12], [21, 21], [9, 9], [3, -24], [0, -27], [6, -6], [3, -24], [20, 18], [1, 24], [-12, 9], [-6, -32], [24, -6], [6, 20], [27, -11], [-12, 2], [22, 0], [-51, -2], [-12, 10], [-21, 15], [-34, 6], [10, 0], [0, 2], [24, 15], [-7, 24], [30, 6], [9, 6], [3, 11], [11, 24], [36, 16], [12, 7], [15, -9], [7, -9], [24, 0], [-16, 9], [-33, 9], [-36, -18], [-9, -21], [-24, 20], [1, -24], [6, 18], [0, 6], [24, 6], [-31, 15], [6, -8], [17, -18], [27, 0], [-24, 8], [12, 4], [6, 14], [18, 21], [-34, -9], [19, 9], [21, 9], [8, -9], [18, 0], [-30, 0], [-6, -9], [-22, 18], [-9, 0], [-27, -12], [-36, 21], [-1, -15], [24, 10], [0, 9], [36, -2], [-3, -13], [16, 0], [-27, 13], [3, 21], [4, 0], [-21, -30], [5, 30], [2, -33], [21, 19], [-15, -24], [-14, -6], [-21, 12], [6, 4], [18, -24], [-66, 3], [33, -5], [48, 6], [56, 0], [-4, -39], [-18, -4], [9, -21], [45, 5], [-30, 12], [-27, -18], [39, 0], [6, -23], [-18, -4], [18, -21], [-27, 18], [-16, 0], [-27, 2], [-33, -24], [7, 27], [-36, -6], [-3, 22], [27, 9], [-44, -9], [-39, 1], [30, -2], [-14, -24], [12, 18], [-32, -18], [42, -12], [-18, -9], [-45, 21], [-42, -14], [-37, 3], [-33, 1], [-45, 15], [-5, -33], [-40, 12], [-12, -2], [-48, -18], [33, -13], [-60, 6], [-24, -8], [54, 13], [0, 36], [6, 30], [36, -8], [-45, -17], [9, 7], [2, 24], [6, -21], [42, -16], [-9, -38], [-3, -33], [-15, 33], [6, 6], [-45, 12], [28, -24], [-6, -36], [5, 15], [9, 12], [24, -6], [-15, -29], [-58, 6], [0, -24], [-38, -6], [39, -23], [-40, -12], [6, 12], [12, -36], [-31, -15], [-39, 3], [-41, 24], [-36, 25], [-75, 3], [-51, 4], [68, 0], [-45, 6], [-11, -18], [-24, 6], [-78, 0], [-21, -3], [-66, -2], [-26, -18], [-45, 19], [33, 3], [12, 20], [21, 27], [34, 6], [30, -24], [51, -1], [-30, 5], [54, 10], [45, -11], [-3, -36], [19, 15], [33, 3], [-30, 24], [-15, 3], [21, 1], [9, -3], [-6, -29], [-10, 9], [-12, -5], [12, 24], [45, 15], [-17, -9], [-24, 15], [66, 5], [-68, 0], [-48, -22], [-9, -21], [-15, 14], [15, 20], [-51, -3], [0, -6], [40, 21], [5, 39], [0, -2], [33, 3], [21, -42], [18, -26], [27, -30], [12, 29], [3, 30], [-12, 16], [27, 12], [2, 0], [-56, 12], [12, 12], [39, -14], [34, -18], [6, 14], [-2, 33], [-18, -36], [-9, 41], [-32, 0], [10, -6], [-39, -30], [18, 0], [36, -18], [45, 18], [-25, -27], [-15, -9], [36, 36], [21, 0], [-36, -24], [39, 20], [-3, 24], [-12, 14], [-14, 18], [-39, 1], [-6, -2], [42, 17], [-30, 27], [33, 30], [-19, 21], [-9, -27], [-4, -30], [-18, 32], [-42, -6], [4, -30], [-33, 9], [54, -8], [-39, 11], [30, -12], [-12, -48], [-87, -9], [61, 3], [6, -12], [-12, 15], [27, -1], [27, 6], [17, 45], [9, -34], [15, 23], [40, -9], [83, 3], [-30, -12], [-54, -24], [-34, -27], [60, -18], [24, -4], [-60, -6], [6, -9], [75, -6], [-66, 17], [-18, -14], [-66, 3], [3, -45], [36, -39], [51, 16], [30, 32]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8450_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8450_2_a_bf();".
// 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_8450_2_a_bf(:prec:=2)
    chi := MakeCharacter_8450_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_8450_2_a_bf();".
// 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_8450_2_a_bf( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8450_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-2, -2, 1]>,<7,R![-3, 1]>,<11,R![-3, 1]>,<17,R![-18, -6, 1]>,<31,R![6, -6, 1]>],Snew);
    return Vf;
end function;