
// Make newform 8112.2.a.cm in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8112_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_8112_2_a_cm();".
// 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_8112_2_a_cm();".
// 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, -2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, 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_8112_a();"
function MakeCharacter_8112_a()
    N := 8112;
    order := 1;
    char_gens := [5071, 6085, 2705, 3889];
    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_8112_a_Hecke(Kf)
    return MakeCharacter_8112_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 := [[0, 0, 0], [1, 0, 0], [0, -1, -2], [-2, -2, 1], [-3, 3, -1], [0, 0, 0], [-2, 0, 2], [4, -4, 4], [-2, 2, -4], [5, -1, 3], [-5, 5, -5], [0, -2, 6], [-2, 6, -2], [-6, 2, -4], [-4, 4, -4], [4, -5, 2], [4, -2, 5], [8, 0, 2], [-2, 2, 2], [8, -2, 4], [-8, 7, -4], [-10, 2, 3], [0, -10, 3], [0, -6, 8], [-8, -3, -4], [-2, -4, 3], [2, -5, 0], [4, -6, 7], [-16, 4, -8], [-4, 8, 0], [2, -3, -4], [2, 5, 2], [-4, 0, 0], [2, 10, -4], [-3, -1, 7], [-7, 3, 9], [-16, 2, -4], [-10, 0, -10], [0, -6, 8], [-5, -3, 5], [-10, -3, -6], [8, 2, 0], [-2, -4, -6], [-2, 12, -8], [-6, 8, 9], [9, -1, 9], [-4, -8, 2], [-12, 4, 8], [-2, -3, -6], [6, 0, 14], [-8, 2, -6], [14, -2, 12], [8, 5, -4], [8, 2, 3], [-10, 0, 2], [20, -14, 14], [12, -1, -6], [-2, -3, -4], [-10, 6, -20], [12, 6, -8], [6, -12, 8], [15, -11, 21], [2, 8, 10], [-2, 2, -4], [12, -7, 12], [-22, 4, -3], [-4, 2, -6], [9, -5, -11], [0, 2, 1], [-12, 2, -16], [-6, 0, -18], [14, -14, 0], [-2, -2, -9], [12, -20, -2], [-12, 0, -8], [2, 2, -4], [3, -11, -15], [-28, 8, -2], [6, 8, 2], [0, 0, -1], [-17, 9, -23], [4, -16, 6], [-24, 2, -4], [25, -9, 9], [-14, 22, -21], [-1, 21, -15], [0, 8, 12], [-15, 3, 1], [10, -4, 15], [-4, -4, -12], [18, -17, 18], [6, -6, -10], [-2, 27, -12], [-17, 13, -7], [0, 0, 0], [-4, 26, -6], [2, 12, 4], [10, -4, -16], [4, -10, -2], [-14, -2, -6], [-4, -4, -16], [-6, 0, 8], [22, -25, 18], [0, 4, -4], [-10, -8, 0], [16, -28, 11], [-5, 25, -23], [14, -4, 24], [-6, 20, 14], [-5, 5, 3], [-6, 2, -1], [-26, 10, -18], [4, 12, -14], [-16, 10, -8], [14, -18, 27], [26, -14, -12], [-6, 20, -6], [-8, 18, 10], [-2, 16, -23], [-4, -10, 3], [8, 6, -18], [-16, 20, -21], [-14, 8, -15], [14, -21, 22], [14, -16, -4], [18, 4, -16], [-20, -2, -2], [-22, -16, 0], [-14, 6, 19], [2, 4, -10], [6, -34, 18], [10, 18, -6], [-17, 13, 3], [-8, -8, 16], [-4, -16, -8], [6, 8, 4], [6, 4, -7], [-10, 10, -14], [-5, -23, 17], [30, -6, -6], [16, -12, -20], [-8, -15, 10], [10, 6, -5], [-9, -7, 1], [-10, 18, 8], [0, 18, -2], [-38, 2, -6], [0, -4, 26], [24, -8, 30], [6, 14, 10], [-14, 2, -2], [-18, -8, 14], [6, 4, 6], [-2, -22, 14], [6, -24, 8], [6, -16, -18], [6, -17, 16], [36, -14, -6], [15, -15, 15], [1, 19, 3], [30, -1, 10], [2, -26, 0], [-13, 1, 27], [13, -17, -13], [-2, 4, -26], [-18, -28, 14], [-18, 29, -8], [-16, 26, -28], [-11, -9, -15], [-30, 16, 0], [20, 0, -8], [26, -24, -2], [-36, 18, -14], [34, -12, 32], [-4, 12, -28], [-2, 18, -16], [-38, 6, -4], [26, -8, -13], [0, 4, 32], [16, -18, 36], [30, -15, 12], [-9, 17, -3], [38, -20, 22], [6, -18, 16], [56, -6, 6], [-22, 20, -3], [34, -36, 30], [22, -6, -4], [-8, 8, -9], [10, -18, -4], [-23, -5, -15], [-20, 2, 9], [18, -24, 6], [-24, 15, -26], [-34, -7, -2], [-40, 4, -20], [-11, 3, -15], [28, -18, 40], [20, -24, 22], [14, 16, -22], [-22, 36, -24], [-37, 9, 7], [20, -28, 0], [16, -35, 28], [-20, 18, -29], [9, -45, 31], [-10, 29, -20], [-12, 14, 13], [20, -32, 30], [-28, 22, -14], [36, 13, -8], [-13, 21, -15], [-24, 4, 8], [44, 0, 12], [14, 12, 14], [12, -48, 9], [-45, -7, 3], [-12, 8, 16], [26, -30, 16], [-28, 23, -10], [-6, -8, -16], [-34, -1, -20], [32, -2, -18], [13, 3, -19], [-4, 4, 24], [-8, 0, -2], [-34, 4, -16], [4, -2, 28], [26, 11, -12], [18, -5, -2], [26, -34, 14], [-6, 52, -16], [-62, 10, -15], [-16, 22, -32], [-28, 2, 16], [10, 18, -4], [4, 4, -13], [22, -40, 19], [-4, 4, 20], [-10, 18, 30], [-25, -35, 17], [10, 6, 4], [-28, -4, -8], [4, -44, 16], [32, 8, 8], [18, -18, -10], [-15, -1, 9], [-25, -31, 9], [36, 10, 2], [10, -10, 18], [-12, -14, 2], [6, 36, -30], [-14, 26, -20], [-16, 16, -9], [-46, 28, -61], [-4, -34, 29], [2, 34, -24], [-38, 14, -38], [6, 28, 3], [-5, 41, -29], [27, 21, 7], [-10, -13, -18], [14, -16, -14], [2, 28, -12], [0, -24, -6], [36, 4, -16], [0, -1, -2], [-22, 12, 20], [-32, 6, -24], [-12, -3, 22], [-32, 20, -28], [-6, 18, -38], [4, -33, 28], [6, -11, 20], [32, 8, -5], [-5, -7, -5], [-18, 33, -46], [-18, 40, -4], [-16, 40, -1], [32, -10, 45], [4, 24, -22], [-10, -19, -8], [0, -32, -16], [-50, 12, 0], [26, -32, -8], [8, -44, 40], [12, -37, 12], [-40, 35, -30], [-1, 21, -5], [-14, -6, -6], [34, -40, 33], [-3, -37, 27], [40, 16, -14], [30, -19, -26], [48, -30, 48], [-26, -28, -7], [-14, 5, -20], [-42, 12, 19], [15, -27, -15], [34, -12, 38], [-7, 27, -19], [-28, -17, -14], [74, -10, 19], [34, -17, 34], [-26, 16, -60], [36, -3, 0], [16, 48, -12], [2, -24, 24], [-14, 14, 40], [14, -20, -8], [-10, -16, -28], [38, -40, 45], [2, -2, -10], [0, -18, -26], [34, -8, 6], [13, 11, 17], [-38, 11, -22], [48, -16, 10], [2, 40, -16], [2, 52, -34], [14, 0, 6], [-21, 37, -5], [14, 32, -9], [13, -1, 45], [42, -28, -18], [-5, -15, -41], [-30, 52, -26], [26, 0, 12], [-66, 18, 6], [-34, 0, 0], [32, -34, -34], [16, -55, 18], [-12, -20, 8], [-74, 41, -70], [10, 0, 20], [6, -21, 18], [10, -38, -2], [-50, 22, -6], [59, -31, 51], [-21, 37, -25], [-38, 12, -10], [-24, -22, -18], [-29, 1, 1], [17, -9, -31], [-25, 37, -7], [-48, 2, -33], [18, 0, -18], [-22, -34, 26], [-8, 6, -44], [-48, 19, -14], [-8, 8, -48], [24, -4, -13], [39, 29, -7], [8, -12, 44], [2, -12, 4], [10, 10, 24], [26, -18, 2], [0, -26, 19], [-66, 16, -34], [6, -14, -38], [-18, -14, 14], [28, 12, -20], [2, -58, 18], [-16, 22, -11], [62, -36, 52], [-28, 63, -40], [-30, 8, 5], [10, 11, -28], [-43, 7, 9], [66, -19, 6], [4, -24, 36], [2, -4, -6], [5, 23, 3], [-70, 23, -28], [6, -22, -22], [-38, -7, -10], [-20, 18, -18], [-12, 4, 11], [18, -8, 38], [24, -36, 41], [29, 3, 35], [40, -28, -6], [11, 37, 3], [-12, -6, 6], [12, 24, -28], [2, 14, 22], [46, 14, -1], [0, -34, 8], [12, 2, 48], [6, 36, 14], [35, -7, 51], [60, -1, -10], [8, -38, 35], [8, 16, 22], [-32, 14, 0], [-1, 33, 11], [-22, -30, -1], [38, -22, -28], [-48, 18, -32], [17, -45, 47], [-20, -2, -30], [60, 0, 8], [-22, -9, 8], [-30, -2, 18], [31, 13, 13], [-2, 1, 44], [-12, 26, 28], [-82, -12, 4], [22, 30, -18], [12, 0, -4], [-16, 28, -31], [47, 1, 1], [27, -23, 33], [24, -50, 16], [16, -11, 48], [-38, -4, -24], [-64, 42, -58], [-59, 51, -35], [6, -54, 8], [36, 32, 0], [34, -16, 41], [-4, 6, -34], [60, -18, 32], [-4, -12, -16], [-69, -19, 15], [-10, -4, -28], [-78, -3, 6], [34, -28, 0], [24, -34, 24], [-52, 10, -20]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8112_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8112_2_a_cm();".
// 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_8112_2_a_cm(:prec:=3)
    chi := MakeCharacter_8112_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_8112_2_a_cm();".
// 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_8112_2_a_cm( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8112_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![29, -16, -1, 1]>,<7,R![-1, 20, 9, 1]>,<11,R![1, -8, 5, 1]>],Snew);
    return Vf;
end function;