
// Make newform 8512.2.a.s in Magma, downloaded from the LMFDB on 13 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_s();".
// 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_s();".
// 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)
    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, 2], [-1, 0], [1, 1], [3, -4], [-1, -3], [-1, 0], [-5, -2], [3, 1], [1, -3], [5, 2], [-3, 9], [2, -4], [1, 4], [2, -5], [-11, 0], [1, 2], [-7, 9], [-7, 2], [2, 1], [2, -4], [4, -9], [-6, -6], [3, -4], [-5, 12], [13, -4], [1, -10], [-7, 4], [-2, -9], [-6, -6], [-13, 5], [-14, 12], [-9, 4], [9, -12], [-7, 5], [-3, 9], [8, -3], [-3, -2], [2, -6], [-11, 11], [7, -11], [-8, -5], [15, -15], [18, -5], [-7, 0], [2, -1], [-25, 2], [-13, 3], [-18, -4], [-19, -5], [-7, 14], [-8, -2], [3, 3], [-18, 5], [-20, 9], [-4, 7], [-3, 15], [-8, 10], [3, -12], [0, 5], [3, -2], [-15, -3], [-17, 19], [-4, -2], [18, -4], [-10, 13], [-3, 1], [14, -21], [16, -9], [-23, 21], [3, 15], [-13, -6], [-6, -11], [13, 0], [-5, 12], [-17, 9], [-6, 6], [27, -15], [10, 3], [-27, 4], [-19, 18], [-10, 0], [-1, -22], [16, 0], [4, -9], [18, -15], [-10, -11], [6, -23], [-17, 8], [0, 25], [-1, -7], [17, 10], [4, 12], [-34, -1], [0, -8], [6, -24], [23, 10], [1, -22], [-6, 20], [18, -33], [7, 7], [-1, 24], [17, -34], [-7, 4], [20, -27], [-30, 12], [-33, 4], [-2, 9], [-8, 3], [3, 0], [6, 5], [31, -9], [38, -1], [-13, 16], [-35, 7], [-7, -10], [-15, 12], [11, -18], [-17, -1], [38, -8], [2, -9], [32, -1], [7, 2], [24, -6], [12, -20], [-45, 6], [-4, -8], [-5, 26], [32, 0], [-13, 6], [-7, 30], [2, 5], [-14, 4], [11, 22], [12, 8], [20, 6], [-12, -12], [-11, 21], [-31, 26], [10, 24], [-12, -20], [-12, 2], [-17, 6], [32, -16], [28, 4], [30, 1], [-3, -29], [-21, 25], [22, -31], [-6, -30], [32, -23], [3, 26], [13, -18], [25, -44], [-26, 12], [35, -20], [-24, 25], [-1, -23], [18, 4], [-14, 25], [9, -25], [19, -27], [-25, -16], [-44, 2], [53, -12], [-4, 2], [34, -35], [-5, 2], [32, 4], [-14, 19], [-23, -1], [31, 13], [-7, 0], [18, 2], [-12, 40], [34, -43], [-43, 30], [-29, 6], [-5, -5], [19, -51], [-37, 18], [-30, 5], [-24, 3], [23, -8], [-30, 4], [-7, 38], [-22, -13], [-30, 11], [-16, 15], [-36, 24], [-25, 23], [-10, -8], [7, -26], [10, -32], [18, 19], [47, 0], [15, -2], [27, 20], [18, -48], [12, -4], [-10, 9], [-17, 18], [32, -38], [11, 31], [21, -9], [26, -12], [18, 6], [4, 34], [-22, 55], [-4, 22], [-38, 31], [50, -6], [-13, 40], [46, -40], [-27, 0], [57, -16], [-6, -12], [20, 8], [-13, -10], [-15, 36], [32, -7], [-30, -24], [-29, 50], [37, 7], [34, -11], [-11, 6], [-44, 5], [-8, -40], [15, 23], [-22, 11], [50, -42], [-55, 12], [-53, 6], [50, -7], [44, -47], [41, 7], [42, 12], [37, -42], [23, -25], [26, -26], [-27, 10], [25, 20], [24, -32], [-25, -5], [-53, 25], [14, -22], [-46, 7], [34, 8], [-15, -22], [9, 22], [47, -23], [11, -11], [-39, -17], [40, -12], [5, -26], [-49, 26], [20, 2], [24, -57], [-25, 10], [24, 24], [42, -20], [-3, 30], [72, -2], [-16, -4], [28, 26], [1, -20], [75, 4], [-40, 0], [22, -31], [-1, -19], [14, 18], [52, -19], [2, -1], [28, -7], [-52, 44], [29, 3], [-30, -12], [-18, 18], [35, -22], [64, -19], [1, 22], [-36, -24], [42, -30], [-52, 34], [-32, 8], [69, -6], [-31, 56], [-7, -34], [-25, 52], [-25, 36], [-48, 25], [-13, 9], [18, -40], [36, -29], [60, -25], [31, 20], [-58, 32], [41, -19], [-14, 9], [27, 22], [11, -16], [10, 2], [-46, -5], [3, 27], [1, 24], [-47, -2], [-38, -9], [-72, -4], [23, -37], [21, 0], [35, 19], [-35, 34], [-40, 11], [-22, -24], [36, -52], [52, -37], [-51, 2], [50, -9], [14, -28], [65, 2], [-29, -21], [-41, -3], [74, -15], [-38, 8], [44, -13], [12, 1], [57, -5], [-76, 7], [-46, -7], [10, -18], [20, 32], [-9, -1], [-17, 10], [-5, 28], [-3, -13], [67, -4], [-57, 17], [25, -24], [68, -19], [-71, 35], [-27, 35], [-25, -24], [-31, -40], [35, -74], [81, -5], [-58, 36], [-28, 50], [-43, 54], [-2, -17], [53, -10], [-67, 8], [34, 25], [-7, -35], [-79, 11], [67, -1], [-40, 58], [27, -20], [4, -60], [39, -13], [65, 4], [34, -13], [49, -40], [40, -26], [31, 42], [-4, -28], [-27, 16], [7, -59], [-32, 40], [17, -45], [-4, -46], [-40, 4], [58, -2], [70, -45], [18, -11], [-46, -26], [-95, 0], [-10, 0], [-75, 26], [-23, 14], [11, 12], [26, 10], [61, -38], [-79, 12], [30, -8], [-24, 40], [5, -39], [10, -29], [11, -18], [-30, 3], [7, -5], [-22, 15], [-13, 38], [25, -37], [57, -44], [-35, 6], [49, -6], [12, 0], [42, -88], [-28, -21], [-61, 32], [-3, 50], [61, -8], [-27, 2], [59, 21], [-22, 49], [17, -7], [30, -40], [26, -76], [13, 25], [-35, 58], [-3, -6], [-55, 41], [4, -45], [-4, -10], [-15, 1], [-73, 44], [46, 15], [53, -50], [-66, 41], [33, -86], [-33, -10], [3, 2], [-18, 55], [-31, -45], [-21, 24], [-75, 37], [1, 14], [-53, 28], [-18, 38]];
    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_s();".
// 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_s(: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_s();".
// 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_s( : 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, 1, 1]>,<5,R![-1, -4, 1]>,<11,R![1, -3, 1]>,<23,R![31, 12, 1]>],Snew);
    return Vf;
end function;