
// Make newform 7728.2.a.x in Magma, downloaded from the LMFDB on 08 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_7728_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_7728_2_a_x();".
// 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_7728_2_a_x();".
// 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 := [-3, -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_7728_a();"
function MakeCharacter_7728_a()
    N := 7728;
    order := 1;
    char_gens := [4831, 5797, 5153, 6625, 6721];
    v := [1, 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_7728_a_Hecke(Kf)
    return MakeCharacter_7728_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], [-1, 0], [-2, -1], [1, 0], [5, 0], [1, -1], [-3, 2], [-1, -2], [-1, 0], [1, -4], [-3, 0], [-9, 0], [-7, 4], [1, 5], [6, -2], [-6, 5], [0, 3], [-5, -3], [8, -3], [3, 3], [3, 4], [1, 0], [-3, 2], [7, -3], [-15, 2], [8, -1], [4, 0], [-1, 5], [0, 3], [4, -1], [-3, -1], [-13, -2], [9, -6], [-12, 3], [6, -2], [-14, 2], [4, 6], [2, -9], [5, 6], [-13, 4], [4, 1], [1, 6], [2, -2], [-4, -10], [-11, -3], [-6, 5], [17, -8], [-16, 3], [6, -11], [-4, -5], [0, 11], [18, 3], [-1, -10], [-16, 6], [14, -10], [-13, -2], [-3, -3], [-1, 2], [-11, 1], [20, -6], [-16, 1], [-4, 16], [-13, 4], [3, -5], [-4, -4], [-24, 5], [-2, 12], [-17, -1], [1, 12], [-14, -3], [-11, 0], [16, -15], [-15, 5], [11, -4], [12, -8], [3, 6], [-21, 12], [-30, -2], [-3, 8], [11, -10], [-6, 7], [11, 1], [-3, -9], [-8, 10], [-15, 4], [12, -16], [-24, 9], [-20, -7], [-4, -9], [11, -6], [-21, 2], [13, -4], [-7, -6], [1, -13], [-28, 3], [-6, 9], [-10, 18], [6, -12], [-10, -8], [2, 6], [-4, -9], [-8, -10], [29, -11], [16, 4], [-5, 4], [-26, 8], [17, -17], [24, -10], [33, -5], [24, -13], [31, 5], [-5, -6], [3, -9], [-19, -7], [25, 10], [9, 15], [-11, 25], [-2, 11], [24, -5], [-27, 12], [13, -6], [-11, 12], [15, -17], [-9, -8], [25, -5], [-6, 15], [-33, 11], [-9, 4], [13, -26], [-24, 2], [33, -8], [-24, 5], [-10, -1], [-19, -12], [12, 0], [-4, 18], [11, -16], [-10, 17], [-26, 10], [9, -13], [13, 2], [-22, -2], [0, 5], [6, -3], [-7, 18], [-37, 1], [-40, 14], [-32, 14], [-38, 4], [24, 14], [-20, -4], [-36, 6], [25, 5], [-5, -13], [-14, -17], [6, -14], [13, 2], [26, -11], [-15, -8], [20, 16], [-2, -12], [-37, 11], [-10, 24], [39, 3], [9, -17], [15, -6], [-29, 5], [13, 10], [-20, 1], [-7, -7], [-9, -9], [18, 4], [33, -21], [5, -16], [-20, 0], [11, 8], [-1, 9], [16, -7], [-42, -5], [40, -9], [-20, 7], [-9, -15], [-22, -7], [-25, -1], [15, -12], [-31, 15], [-50, 4], [35, -4], [10, -23], [-25, -14], [46, -11], [-6, -13], [28, -14], [-21, 24], [5, 19], [-21, -15], [-19, -3], [-12, -10], [-6, 0], [-42, 15], [2, -4], [-8, 16], [30, -13], [7, 9], [-23, 0], [38, 0], [45, -8], [6, 7], [-12, -5], [-13, -10], [-18, -10], [-30, -9], [23, -28], [44, 8], [37, 1], [4, -1], [-23, 20], [-23, -17], [-25, 14], [-9, -10], [11, -7], [-10, 16], [23, -29], [-20, 12], [37, -4], [8, 9], [37, -24], [28, -4], [-35, 25], [63, 0], [1, -16], [11, -13], [4, -15], [16, 8], [57, -5], [-9, -17], [10, -2], [17, 6], [-45, 4], [-17, -22], [21, 4], [0, -9], [-60, 2], [49, -14], [39, -16], [23, -9], [-4, -20], [28, 16], [-13, 14], [33, 2], [-25, 32], [42, 0], [-49, 4], [-5, 4], [-41, -2], [-20, -10], [-19, 36], [-27, -14], [-21, 1], [47, 6], [13, 15], [-14, -16], [-22, 33], [-43, 0], [-37, -11], [13, -8], [-18, -16], [-17, 8], [35, 8], [28, -9], [-23, 18], [-17, 10], [13, -17], [-38, 22], [25, -26], [58, 0], [-67, 0], [45, -14], [-5, -20], [19, -22], [22, -10], [-33, -7], [7, -2], [31, 22], [-2, 25], [-15, 0], [55, -8], [30, -8], [-47, -5], [-12, 36], [25, -19], [47, -11], [51, -9], [-9, 12], [-20, 28], [55, 0], [-16, 16], [41, 1], [19, 20], [-29, 36], [3, -13], [54, -12], [23, 8], [14, -15], [48, 4], [39, -2], [18, 19], [-21, 40], [56, -14], [31, 7], [21, 0], [11, 10], [-24, 31], [45, 10], [-29, 6], [-16, -4], [-27, 20], [-15, -8], [-11, 36], [18, -6], [-2, -4], [-17, 7], [-6, 7], [43, -26], [69, 0], [16, 9], [-31, 0], [-50, 19], [-26, 50], [-24, -22], [-44, 0], [-15, -2], [19, -13], [-18, -33], [-34, -14], [-10, 16], [-16, -26], [18, 6], [38, -9], [-4, -13], [-47, 20], [14, 24], [27, -9], [47, 6], [2, 28], [-61, 21], [22, -37], [26, -27], [-48, 3], [-1, -10], [29, -33], [33, -20], [-11, -22], [18, -23], [37, -2], [-40, -20], [12, 14], [27, 24], [-37, 32], [-27, 8], [33, 10], [-23, 24], [-9, -8], [28, -24], [-38, -10], [-20, -28], [23, -15], [-46, -10], [51, 19], [63, -9], [15, -32], [24, -24], [-84, 6], [-8, 16], [-95, 0], [-4, -27], [-58, 17], [14, 10], [-6, 12], [-39, -14], [23, 30], [63, -9], [74, -18], [-73, -9], [78, -9], [-47, -3], [13, -13], [-38, 24], [61, -26], [12, 22], [43, -22], [-6, -33], [-6, 0], [-14, 10], [55, -3], [-8, 19], [-13, 6], [-3, 14], [-15, 35], [27, 6], [-13, 46], [-25, 11], [-76, 14], [-63, 15], [-39, -9], [71, -1], [0, -28], [-52, 30], [16, -8], [-24, -7], [-43, 39], [-82, 2], [4, 40], [17, 3], [3, 5], [-54, 12], [-21, -30], [-11, -12], [57, 0], [18, 14], [45, -18], [-17, 30], [-2, 7], [-41, 39], [23, 37], [18, 6], [5, 1], [24, 26], [-60, 27], [-45, -17], [21, -40], [12, 12], [0, 29], [53, -5], [-16, -2], [30, -28], [-44, 3], [56, -18], [27, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_7728_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_7728_2_a_x();".
// 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_7728_2_a_x(:prec:=2)
    chi := MakeCharacter_7728_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(3067) - 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_7728_2_a_x();".
// 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_7728_2_a_x( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_7728_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![3, 5, 1]>,<11,R![-5, 1]>,<13,R![-3, -1, 1]>,<17,R![-9, 4, 1]>],Snew);
    return Vf;
end function;