
// Make newform 8512.2.a.bi in Magma, downloaded from the LMFDB on 01 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8512_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_8512_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_8512_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 := [-1, -4, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-3, 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_8512_a();"
function MakeCharacter_8512_a()
    N := 8512;
    order := 1;
    char_gens := [5055, 6917, 7297, 3137];
    v := [1, 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_8512_a_Hecke(Kf)
    return MakeCharacter_8512_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]];
    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, 0, 0], [-1, 1, 0], [1, -1, 1], [-1, 0, 0], [-2, 0, 1], [1, -1, 1], [2, -2, -1], [-1, 0, 0], [5, 1, 1], [0, 4, -3], [-4, 2, -1], [-1, 1, -3], [-2, 6, 1], [2, -2, 2], [3, 3, 1], [1, 1, 2], [3, 3, -1], [1, -1, -3], [0, -2, -3], [-1, -1, -3], [-1, -1, -4], [-2, -2, -2], [-11, -1, -2], [-8, -6, 4], [-11, 3, -3], [1, 3, -5], [-5, -9, 3], [-13, -1, 1], [-3, 3, -3], [1, 9, -6], [-4, 0, -6], [-4, 2, 1], [6, -4, 4], [1, -3, 7], [1, -5, 7], [4, -2, 1], [0, 4, 5], [-7, -5, -2], [9, -7, -1], [8, -6, 0], [-2, -8, 5], [0, 0, -5], [-1, 1, 6], [-8, -4, 5], [-9, -1, -8], [-9, -1, 5], [3, 1, 6], [3, 3, 1], [-4, 2, -7], [-6, 0, -8], [2, 6, -1], [3, -1, -1], [18, -4, 2], [-10, -8, 1], [-11, 5, -10], [-7, -1, 2], [5, 1, 4], [-12, 10, -1], [-6, -12, 2], [7, 5, -5], [-9, 5, -14], [7, 9, 1], [8, 2, -1], [-2, -4, -11], [18, 4, -2], [6, -4, 0], [-7, 3, 2], [22, 2, 3], [-27, 3, -4], [3, -1, 10], [20, 0, 5], [-10, 0, 3], [-1, -5, 11], [15, 3, 6], [1, 9, 1], [1, -3, 1], [-10, 6, 9], [8, 18, -8], [-12, 0, -3], [7, -5, 10], [7, -5, 1], [-5, -11, 5], [14, -10, -6], [7, 9, -3], [0, -8, 0], [-9, 13, 6], [-1, 3, -6], [-15, 5, -12], [15, -1, 0], [-1, 7, 9], [-25, -3, 2], [-16, 10, 5], [-9, 7, -11], [12, 8, -8], [-3, 11, 0], [-4, -12, 4], [14, -4, 16], [-17, -7, -3], [-11, -7, -1], [-6, 8, 0], [7, 13, -10], [-16, -4, -7], [-27, 5, 1], [-5, 5, 13], [5, 9, 7], [9, 9, -10], [14, -14, 10], [-7, -5, -1], [-11, 11, 2], [-7, 13, -8], [-15, 5, -11], [1, -19, 8], [4, -8, 13], [-1, -7, 8], [-9, 15, -3], [-8, 16, -9], [9, -7, -11], [-9, -9, -7], [9, -1, 9], [-28, -2, -1], [-22, -16, 4], [1, 9, 10], [-11, 1, -8], [-11, 1, 7], [-6, 2, -4], [12, -14, 18], [9, -9, 1], [-4, 4, 0], [31, 3, 3], [-12, 18, 6], [-3, -3, -15], [3, 3, 1], [-15, 7, 2], [-18, 12, 0], [7, -3, 7], [16, -10, -2], [22, 0, 2], [-4, 0, -8], [18, 2, 5], [-13, -7, 3], [-18, 2, -14], [0, 14, -2], [-6, -18, -4], [-3, 17, 11], [24, 14, -6], [0, 16, -12], [-9, 15, -8], [8, -4, 13], [-28, -6, -11], [-15, 7, 2], [24, -6, 12], [13, 13, 2], [17, 1, -3], [45, -3, 11], [-15, -7, -7], [-26, 6, -2], [15, -17, -7], [-11, -3, -2], [-14, -10, -9], [6, -4, 4], [-41, 1, -4], [-20, 12, -13], [12, -22, 7], [9, -23, -7], [-14, -20, 14], [-5, -9, 11], [-10, -6, -8], [-17, -25, 16], [-7, 19, -5], [-12, 10, 6], [3, 1, -6], [4, -16, -7], [-4, 2, 19], [-7, 11, -21], [24, -12, 6], [-8, -2, -6], [3, 13, -12], [-5, 1, 3], [31, -17, 1], [-8, 8, -13], [-20, 18, -17], [-9, -21, 9], [-5, 15, 10], [-31, -11, 0], [-1, -1, -3], [42, 0, -8], [7, -7, 1], [33, -5, 2], [-39, 1, -2], [29, 3, 2], [0, -2, 14], [-26, 4, -1], [22, -6, 18], [27, -11, -3], [-14, 22, 2], [-33, 3, 10], [-9, -23, 1], [-7, -17, 1], [5, -17, 15], [6, 14, 10], [-12, -6, -22], [-17, -7, -14], [7, 9, 1], [-2, 0, -6], [26, 4, -7], [42, 2, 13], [14, -2, -6], [-36, -4, -6], [2, -12, 6], [21, -9, 22], [-6, -12, -2], [-7, 13, -20], [-16, 4, -10], [17, 17, 9], [6, 14, 2], [-33, 5, -29], [-23, 9, -21], [34, 0, -4], [8, -16, -4], [13, 27, -23], [9, -9, 17], [29, 15, 4], [38, -12, 20], [41, -11, 1], [-34, 4, -1], [25, 5, -8], [-7, -13, -5], [43, -7, 12], [20, -4, -12], [34, -16, 3], [19, -5, 0], [-16, 0, 2], [9, 37, -11], [25, -5, 19], [1, 7, -12], [-1, 1, -2], [6, 6, 13], [-30, -8, 8], [-29, 11, -13], [0, -6, -7], [-16, 0, 14], [33, 1, 1], [-9, 15, 13], [-40, -2, 6], [-2, 18, -21], [20, -10, 25], [8, -4, 18], [-7, 19, -6], [18, -34, -10], [-17, 27, -13], [-1, -35, 7], [6, -6, 7], [24, -10, 11], [-24, 4, 17], [8, 38, -6], [23, -9, 15], [-3, -37, 13], [-22, 26, 12], [-7, -3, 16], [-1, -11, 15], [16, -24, 32], [-10, -6, 2], [-19, 11, -27], [-18, 0, -14], [20, -14, 22], [6, 22, -20], [-11, 3, -15], [13, -33, 15], [28, -8, -4], [-31, -11, -4], [40, 8, -9], [-4, 12, -30], [-27, 5, 6], [25, 27, -20], [-21, 19, 4], [-12, -28, 16], [24, -2, 25], [-6, -8, 0], [20, -18, 0], [-37, 11, 11], [-47, 3, 4], [-37, -17, 7], [16, 8, 0], [-24, -6, 4], [10, 18, 8], [-48, 0, -16], [-37, -15, -1], [25, 7, 17], [13, -7, -7], [9, 7, -7], [-9, 33, 13], [19, 9, -18], [-32, -4, -11], [14, 2, 18], [-41, -9, 0], [-33, -5, 6], [-35, 1, 9], [-6, -32, 8], [-36, -10, -9], [9, -25, 20], [-33, 21, -9], [-17, 21, 3], [16, 12, 14], [7, 5, 18], [42, 0, -9], [-3, -13, -27], [3, -33, 25], [19, 15, 22], [-4, 4, 4], [-36, 0, -11], [-9, -9, -11], [-32, -4, -11], [15, -7, 21], [17, -17, -8], [-38, -6, -22], [-16, -18, 2], [29, -1, 2], [15, -1, -15], [21, 1, 32], [-14, 24, 12], [17, -19, -13], [8, -12, 29], [-12, 36, -13], [5, -29, 18], [-26, 20, 0], [-11, 9, -16], [-5, -35, 8], [26, -16, 23], [9, -13, 20], [5, 17, 4], [-4, -10, -12], [-48, 6, 6], [66, 8, -7], [-13, -13, -21], [19, 23, -15], [-20, 22, -33], [3, 33, 3], [-24, 16, 3], [43, -19, 25], [7, 17, 18], [-20, 28, 11], [18, 30, -5], [33, 7, 17], [65, -11, 5], [5, -9, 19], [-50, 8, 5], [70, -12, 8], [46, -18, 4], [-11, -35, 19], [-63, -3, 0], [21, 9, 19], [9, -33, -3], [-35, -7, -8], [36, 10, -17], [16, -8, -13], [50, 6, 3], [22, -22, 20], [-11, -31, 3], [24, 22, -22], [4, -6, -1], [41, -13, 31], [-3, 41, -20], [37, 9, 13], [-22, 26, -8], [31, -9, 11], [40, 22, -2], [-17, 1, -15], [-34, 16, 3], [-52, -14, 6], [-36, 10, -11], [-18, 38, -12], [-40, 8, -8], [-24, -6, 16], [-13, 33, 20], [-3, 33, -28], [4, 36, -2], [21, 5, -25], [-6, -8, 8], [-19, -5, 17], [1, 19, 15], [17, 7, -19], [-44, -2, -8], [15, 35, -5], [-19, -33, -5], [18, 6, -14], [-4, -20, 8], [26, -24, -3], [17, 5, -4], [-7, 9, -15], [-55, 15, -2], [-50, 22, -15], [-17, -5, -2], [-7, -23, 1], [-4, 6, -13], [-35, 1, -23], [-53, 21, -3], [-23, -7, -29], [-4, -6, 6], [-26, 10, -2], [5, 5, -26], [13, -37, 29], [27, 17, -9], [-19, -15, -3], [5, -1, 31], [36, 12, -13], [41, -21, 0], [-4, -36, 23], [-2, 4, 0], [-2, -22, 2], [6, -12, 19], [41, -5, 11], [-25, 5, -5], [-12, 2, 5], [-21, -23, 10], [26, -36, 22], [0, -12, 21], [-13, -1, 3], [-3, -13, 14], [53, 27, -19], [-33, 33, -22], [-57, -15, 5], [35, -11, -3], [-23, 13, -11], [-7, -21, 18], [-8, 36, -23], [45, -13, 9], [32, -34, -3], [47, 13, 11], [-23, 37, -1], [40, -20, -2]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8512_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8512_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_8512_2_a_bi(:prec:=3)
    chi := MakeCharacter_8512_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_8512_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_8512_2_a_bi( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8512_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-4, -1, 3, 1]>,<5,R![2, -5, -2, 1]>,<11,R![4, 11, 7, 1]>,<23,R![-56, 53, -14, 1]>],Snew);
    return Vf;
end function;