
// Make newform 8512.2.a.f in Magma, downloaded from the LMFDB on 20 September 2025.
// 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_f();".
// 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_f();".
// 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, -1, 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_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 := 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], [-1, -1], [-1, 0], [1, 0], [0, 1], [1, 0], [2, -3], [1, 0], [-3, 4], [-2, -1], [4, -9], [5, 4], [2, 5], [-6, 4], [5, -4], [-3, 3], [-9, -2], [7, -8], [-2, -7], [5, -6], [-7, 7], [-2, 4], [-5, -3], [4, 2], [5, -4], [3, 10], [-9, 6], [-7, -2], [11, -2], [-11, 15], [0, 6], [0, -9], [2, -8], [7, -4], [13, 4], [-2, 13], [8, -7], [3, 11], [5, -4], [-20, 2], [-12, 19], [8, 5], [-7, 13], [14, -5], [5, 9], [17, -4], [-13, 7], [-17, 12], [-20, 9], [-10, 0], [-8, 9], [-25, 0], [14, -14], [-2, -15], [9, -7], [11, -9], [13, -21], [-20, 9], [6, -18], [-15, -6], [-19, -5], [-25, 2], [-20, 19], [-18, 25], [10, -22], [-14, 12], [-3, -3], [0, 11], [-5, -11], [-23, 21], [4, -15], [-8, -9], [-11, -2], [-17, 1], [15, 0], [-3, -6], [32, -9], [8, -2], [10, -21], [-9, 23], [-7, 4], [-11, -12], [-30, 4], [-5, 18], [-4, -12], [-19, 5], [-13, 1], [23, -5], [-31, 3], [7, -26], [17, -5], [-8, 11], [-11, -2], [-24, 4], [27, 1], [8, -8], [10, 0], [7, -10], [29, -16], [10, 16], [15, -1], [30, -11], [-15, 2], [-11, 12], [-19, 14], [-37, 15], [-38, 0], [-1, -10], [-23, 21], [29, -9], [-11, -2], [-17, 11], [-38, -3], [-3, 1], [7, 10], [30, -11], [3, -14], [9, -12], [29, -26], [-6, 7], [-2, 0], [5, -17], [-11, 31], [-41, -6], [-2, 30], [4, 8], [-39, 18], [-28, 16], [9, -22], [-20, 12], [-23, -4], [17, 14], [5, 9], [2, 0], [29, -4], [-16, 12], [6, -30], [28, -32], [-34, -3], [-9, -2], [38, 0], [-4, -16], [18, -18], [-37, -2], [-4, 8], [-20, -20], [-27, -9], [-32, 15], [-8, 11], [-7, 17], [-20, 34], [7, -5], [-11, 4], [-3, -8], [-1, -4], [22, 0], [3, -26], [-33, -9], [-34, 29], [-6, 28], [-35, -1], [6, -19], [-22, 25], [-7, -10], [2, 22], [-1, 30], [22, -10], [17, -25], [23, -16], [28, -24], [31, -17], [28, -5], [2, -15], [1, 16], [-12, -6], [24, -48], [-3, 27], [51, -6], [3, -28], [16, -37], [6, -11], [-5, 16], [15, -23], [-23, 27], [-29, -14], [-30, 20], [5, 4], [13, -29], [1, -37], [7, -35], [24, -40], [-4, 35], [58, 0], [27, 2], [-10, 4], [19, 15], [27, 0], [5, -38], [-21, 18], [14, 0], [40, 0], [7, 25], [19, 6], [-46, 2], [6, -27], [-14, 7], [46, 8], [-20, 42], [-38, 46], [29, 3], [-10, 6], [-9, 9], [16, -34], [41, -38], [30, -20], [7, 20], [-19, -6], [6, 32], [8, 20], [-19, -30], [19, -22], [-3, 1], [-46, 12], [-15, 28], [-50, 9], [-17, -1], [41, -14], [27, -19], [40, 16], [4, 3], [45, -3], [-60, 10], [-49, 32], [11, -8], [23, 31], [21, -41], [26, 3], [-22, -4], [19, -28], [-28, 25], [0, -18], [-41, -22], [-31, 0], [12, -4], [10, -17], [60, -25], [8, -10], [33, -65], [14, 12], [19, -20], [23, 18], [-2, -7], [-10, 21], [44, -13], [-24, 0], [47, -44], [33, -10], [30, -46], [29, -9], [-7, 40], [8, -48], [-34, -8], [-13, 36], [26, -30], [-28, 32], [46, -22], [-65, 10], [-9, -18], [0, 12], [-41, -21], [-52, -15], [-52, 18], [11, -49], [15, -35], [39, -67], [40, -52], [-38, 35], [54, -48], [24, -34], [-27, -10], [-23, 49], [-45, 44], [-4, 24], [-52, 30], [18, 38], [12, 0], [67, -26], [-13, -30], [-1, -18], [5, 20], [15, -44], [-27, 3], [-10, 3], [10, 36], [-25, 7], [-43, 41], [-3, -10], [10, -28], [40, 15], [59, -29], [-23, 4], [57, -10], [0, 50], [39, -41], [-20, -9], [-25, 26], [-23, 30], [27, -9], [-32, 44], [54, -51], [-47, 32], [-22, 9], [-23, -20], [9, 39], [22, -48], [-72, 24], [11, 13], [-3, -50], [-29, 1], [58, -16], [-3, 52], [-46, 53], [-24, 29], [-3, -11], [30, -72], [37, 15], [11, -27], [26, -55], [17, 27], [-25, 47], [-40, 26], [-8, -40], [34, -3], [-15, 2], [27, -58], [-16, 21], [-43, 6], [-12, -3], [-27, -22], [61, -1], [2, 53], [52, -3], [29, -8], [-1, 56], [-1, -16], [-28, 11], [-54, 24], [14, -34], [65, -36], [-57, -7], [-7, 40], [47, -18], [43, -65], [24, -25], [54, -3], [-50, 3], [-30, 30], [47, -48], [12, 12], [-38, 39], [-27, -12], [7, -55], [-33, -8], [-34, -2], [-67, -2], [4, 0], [1, -18], [44, -35], [20, 24], [6, 39], [50, 10], [32, -16], [20, 30], [-19, 17], [-1, 51], [20, 10], [33, 18], [18, 12], [3, -36], [5, 46], [41, -16], [-20, 18], [11, 4], [33, 20], [-46, 32], [8, 32], [-36, 21], [-57, -7], [9, -56], [37, 27], [-4, -7], [75, -33], [61, -52], [0, 29], [7, -40], [-47, 32], [13, 44], [-44, 8], [14, 8], [63, -15], [65, -40], [-33, -6], [-5, 36], [-63, 22], [-52, -11], [35, -1], [-14, 47], [42, -20], [-30, 12], [-42, 19], [-15, -2], [63, 18], [-34, 25], [13, -35], [18, -70], [32, -43], [21, -52], [-27, -25], [11, 24], [-5, -7], [17, -24], [67, -30], [-27, -20], [-59, 3], [-32, 27], [-43, 80], [-82, 1], [15, 10], [-1, -32], [28, -26]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8512_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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