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