
// Make newform 7938.2.a.by in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_7938_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_7938_2_a_by();".
// 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_7938_2_a_by();".
// 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, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-3, -1, 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_7938_a();"
function MakeCharacter_7938_a()
    N := 7938;
    order := 1;
    char_gens := [6077, 3727];
    v := [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_7938_a_Hecke(Kf)
    return MakeCharacter_7938_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 := [[1, 0, 0], [0, 0, 0], [-2, 1, 0], [0, 0, 0], [0, -1, -2], [-1, 1, 0], [0, -2, 2], [-1, -1, -1], [3, -1, 1], [1, 3, 1], [-5, 2, 1], [3, 2, 2], [-4, 1, 1], [-5, -2, 1], [2, -2, 1], [-2, -4, -1], [0, 5, 1], [2, -5, -3], [-3, 1, -3], [1, 1, -3], [-9, -1, -3], [0, -3, 4], [3, 1, 2], [-4, 1, -2], [-10, 0, -2], [-4, -3, -4], [-2, -3, -3], [6, 0, 3], [2, 2, 1], [2, -4, 3], [-4, 4, 5], [-4, -6, -4], [-5, 0, 1], [-10, 3, 1], [0, -3, -3], [-13, 5, 1], [-8, -6, 3], [7, 7, -2], [6, -7, 1], [-11, 4, -3], [0, 3, 3], [-17, -1, -5], [-11, 5, 2], [-9, 5, 2], [9, 2, -2], [-6, 1, 1], [-11, 4, 4], [-7, 2, 2], [-8, 8, 5], [-4, 6, -2], [-9, -1, -5], [1, 8, -4], [5, -4, 8], [17, 1, -2], [4, 5, 8], [-10, 1, 4], [6, 2, 10], [0, 9, 5], [-4, 3, 0], [0, 8, 1], [-7, -3, -5], [11, -8, -2], [-1, -3, 1], [3, -7, -2], [10, -8, 2], [6, -6, -9], [11, 6, 3], [17, -8, 2], [-16, 1, -8], [0, -1, -9], [-3, -9, -3], [-4, 2, 9], [-28, -1, 2], [21, -2, 3], [-3, -5, -12], [11, 2, -3], [-11, -3, -8], [-14, 2, -2], [4, 11, 5], [-10, 5, 2], [2, 10, 4], [-14, 8, -6], [-1, -8, -2], [-4, 5, 8], [11, -5, -6], [-14, 1, 0], [2, -13, 6], [19, -9, -1], [-3, -4, -5], [-7, -13, 1], [12, -4, 7], [16, 3, -5], [8, -9, 3], [-5, 5, -13], [-10, 2, 7], [-8, 11, 8], [-5, 14, 2], [-18, 0, -15], [10, -9, -6], [16, -7, 9], [-12, 15, -2], [1, 15, 7], [11, -10, 3], [3, 6, -6], [-9, 9, -2], [12, 11, 6], [14, 11, 0], [21, 6, -6], [29, -5, 4], [-8, 6, 3], [-14, -10, 1], [5, -11, -1], [0, 8, 1], [6, -4, 3], [-20, 10, -5], [-7, 12, -1], [-8, -9, 3], [33, 0, 3], [22, -10, -1], [-9, 2, -11], [2, -3, -8], [9, -4, -4], [9, 9, -3], [-11, -7, 5], [-5, 9, -3], [-30, 7, -1], [-8, 4, -5], [17, 1, -2], [14, -18, 1], [17, -14, -3], [-8, 17, -6], [9, 6, 0], [36, -9, -2], [8, -17, 8], [15, -17, -4], [-8, 13, 2], [22, -11, 6], [13, 2, -5], [-16, 4, -5], [-14, -9, 1], [4, 7, -13], [10, 4, 3], [8, -7, 13], [2, 7, 7], [-2, -5, -13], [10, -3, -8], [-21, 4, 7], [-1, -5, 4], [-23, 15, 0], [8, 11, 3], [-9, -2, 6], [-32, 8, 5], [-14, 10, -14], [32, -2, 1], [-16, 7, -7], [5, -14, -2], [-10, -4, -11], [-11, -4, 5], [-7, -7, 14], [10, 4, -6], [-5, -12, 10], [0, -10, -8], [1, 1, 10], [13, 2, -4], [10, -7, -4], [13, 14, -1], [1, -8, -5], [30, 13, -8], [10, 4, 19], [7, -10, 8], [-13, -13, -9], [-17, -11, -13], [0, -15, 9], [8, 16, 11], [17, 4, 15], [24, 8, 22], [1, 20, 12], [-2, -5, -21], [-7, 14, -7], [-14, -15, 3], [7, -13, 0], [-26, -6, 7], [28, -9, -10], [-29, 1, -3], [-48, 4, 2], [5, -2, 13], [-21, 0, -8], [-17, 3, -15], [8, -5, -13], [-3, -6, 0], [4, -5, 20], [0, 12, 18], [-10, -16, 1], [19, 12, -14], [-9, 30, 0], [-11, 9, -11], [12, 8, -1], [-18, 1, 6], [-9, 9, 12], [19, 5, 5], [-17, -10, -13], [1, -7, -8], [5, 11, 11], [27, 12, 8], [1, -25, -1], [-19, 22, 10], [18, -2, 10], [-35, 5, -4], [5, 16, -11], [30, 10, 28], [-10, -6, 12], [-43, 0, -13], [-5, -16, -6], [-7, 3, -1], [34, -1, 5], [7, -3, -9], [-8, 15, 8], [-43, -4, -18], [-18, 8, -11], [-12, -10, -5], [8, 14, 10], [-11, -2, -10], [-33, 12, 0], [18, -2, 15], [14, -8, -17], [35, 3, 6], [-14, 0, 16], [-28, 11, 9], [9, -18, -4], [3, 9, 12], [-23, -1, 15], [7, -2, -19], [22, -18, -13], [-4, -5, 22], [2, 20, 2], [25, -28, 2], [0, -1, -15], [24, 9, -12], [21, 3, 6], [18, 8, 7], [-10, 24, 2], [-19, -4, 8], [7, 3, -18], [-30, -6, 7], [29, -7, -9], [34, -13, -1], [5, 13, 18], [-6, -2, -4], [8, -15, 0], [-21, 30, 15], [-7, 32, -2], [-5, 8, -25], [-22, 1, -20], [1, -5, 11], [-32, 24, 4], [-38, -8, -21], [5, -12, -21], [28, -4, -2], [-47, -11, -6], [8, 14, -19], [21, 1, 24], [27, -2, 23], [29, -24, 4], [-4, 30, 4], [-14, -19, -10], [-8, -2, -19], [11, -2, 13], [31, -5, 15], [-26, -12, -10], [6, 6, 3], [25, -7, -8], [-20, -17, 4], [6, 15, 26], [-29, -2, 1], [38, 12, 4], [33, -15, -10], [-9, -7, -17], [1, 23, 18], [16, 0, 2], [-38, -19, 8], [39, 9, -9], [4, -1, -15], [-57, -6, -6], [48, -8, -11], [-4, -26, 9], [-18, -16, 13], [57, 0, 1], [-28, 21, 2], [-15, 19, -20], [-20, 8, 8], [27, -12, 0], [4, -10, -10], [12, 6, -9], [3, -19, 7], [18, 2, -13], [-1, -29, -2], [43, 0, -3], [-44, 14, 14], [3, -3, 9], [-43, 10, 6], [32, -21, 13], [18, -6, -9], [-13, 26, -11], [52, 2, -1], [-16, 0, -3], [50, -3, 15], [32, -25, 9], [39, -26, -5], [15, 11, 13], [-10, 9, -24], [27, 12, 30], [43, -21, 1], [-1, 11, -12], [7, -14, 4], [-27, 22, 11], [50, 2, -7], [43, 14, -1], [68, 5, 9], [-3, 10, -5], [52, -4, 5], [30, -32, -5], [-24, -11, -15], [17, 13, 28], [24, -35, -12], [-42, -8, -37], [-14, 3, -24], [-14, -8, -8], [-6, 11, 0], [-16, -21, -10], [38, 9, 14], [21, -4, -16], [-53, 7, -18], [-6, 21, -22], [46, -6, 1], [54, 1, 15], [-22, 24, -19], [7, 17, -21], [-27, -13, -8], [-46, -10, -16], [-41, 13, -4], [-16, 24, -3], [-27, -6, -18], [-15, -9, 3], [16, 17, 24], [7, -25, 5], [9, -36, 15], [-6, -5, -14], [-26, 7, 19], [-22, 19, 4], [-48, -12, 0], [20, -1, -10], [-20, 16, 13], [-16, -2, -14], [-16, 10, 15], [-44, -12, -19], [-28, -13, 6], [46, 22, -9], [22, 5, 32], [20, 18, 2], [13, 13, -6], [26, 14, -2], [2, -37, -6], [62, -1, -9], [-39, -13, -29], [22, -38, 5], [-25, 3, -24], [-10, -23, -14], [4, -22, -27], [0, -14, -18], [16, -26, 6], [18, 23, 27], [-30, 0, -3], [-41, 0, 4], [15, 8, 0], [-25, -10, -14], [-42, 0, -15], [24, -16, 4], [-33, 44, 9], [-47, 30, 16], [9, -14, 1], [-28, 16, 13], [-22, 11, -18], [14, -22, 4], [39, -15, 6], [-1, 9, 13], [40, 10, 0], [-2, 19, 16], [-40, 39, 13], [8, -13, -8], [-34, -13, -24], [17, -3, 18], [23, -41, -23], [10, -23, 0], [-30, 13, -29], [-12, -11, 2], [-34, 22, 5], [-30, 14, 3], [-43, -13, -27], [-48, -12, -5], [13, -20, -6], [23, 14, -19], [7, -10, 8], [34, -42, -1], [-11, 22, 3], [19, -19, -13], [23, -4, 8], [2, 8, 7], [-23, 11, 8], [-15, -23, -8], [-26, -3, 16], [-13, 9, -5], [-27, 5, 19], [6, 17, -2], [10, -10, 3], [22, -17, -14], [-70, -7, 0], [-22, -24, -1], [-43, 6, 7], [10, -16, -13], [-5, -11, -36], [-38, -17, -6], [-29, 9, 3], [-45, -13, -5], [35, -17, 13], [2, -34, -4], [10, -11, -12], [56, -16, 3], [-42, -18, 0], [-5, 8, 10], [-3, -17, 5], [12, 8, 21], [-45, 24, 21], [4, 19, 4], [78, -25, -8]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_7938_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_7938_2_a_by();".
// 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_7938_2_a_by(:prec:=3)
    chi := MakeCharacter_7938_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(3023) - 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_7938_2_a_by();".
// 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_7938_2_a_by( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_7938_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-3, 4, 5, 1]>,<11,R![-33, -26, -1, 1]>,<13,R![-3, -3, 2, 1]>,<17,R![-168, -44, 4, 1]>,<23,R![3, 4, -7, 1]>],Snew);
    return Vf;
end function;