
// Make newform 4704.2.a.bu in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4704_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_4704_2_a_bu();".
// 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_4704_2_a_bu();".
// 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, -6, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 2, 0], [-4, -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_4704_a();"
function MakeCharacter_4704_a()
    N := 4704;
    order := 1;
    char_gens := [1471, 1765, 3137, 4609];
    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_4704_a_Hecke(Kf)
    return MakeCharacter_4704_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, 0, 1], [0, 0, 0], [0, -1, -1], [-1, -1, 1], [2, 0, 2], [1, 1, -1], [-2, 0, -2], [4, 0, -1], [1, -1, 0], [1, 1, 3], [-2, 2, 2], [5, 1, 1], [4, 0, 2], [2, 2, 1], [4, -1, -1], [-6, 2, 0], [3, 1, -1], [0, 0, -2], [11, -1, -1], [-9, 1, 2], [6, 1, -3], [-4, 0, -2], [0, -1, -1], [6, 2, 0], [5, -1, -1], [8, 1, -3], [3, -1, -5], [8, 0, 0], [5, -1, -2], [0, -1, -1], [-4, 0, 2], [1, -1, -3], [4, 0, -2], [-4, 0, 5], [6, -2, -2], [0, -2, 4], [-4, -2, -6], [-6, -2, 0], [8, 0, -4], [-9, 1, 1], [0, -4, 0], [3, 2, 6], [14, 2, 0], [-12, 0, -4], [0, -2, -2], [-20, 0, -1], [-4, -3, 5], [-5, -3, -7], [12, 2, 0], [-2, 0, -2], [8, 3, -5], [12, -1, 3], [-12, 0, -2], [-8, 0, -2], [6, -2, -3], [-12, 0, -3], [-1, 1, 1], [18, -2, 0], [-19, 1, 1], [8, 0, -1], [3, 3, 9], [6, -2, -6], [3, 2, 2], [0, 0, 3], [7, 1, -3], [1, -4, -4], [10, -2, 2], [-14, 4, 0], [-4, 4, 4], [-12, 6, 2], [-9, -3, 0], [5, -5, 3], [15, 1, 1], [-2, -4, -4], [4, 4, -2], [-3, -1, -1], [2, 6, -2], [1, -2, 6], [14, 2, 10], [7, 5, -1], [-8, 0, 4], [9, 1, 5], [4, 4, 5], [24, -1, 3], [8, 2, -8], [-11, -6, 2], [20, 0, 2], [11, 5, 1], [8, -4, 4], [18, -2, 0], [3, 5, -8], [10, -3, -3], [-15, 3, -3], [14, 0, 2], [-4, 4, -1], [-4, 2, 2], [-9, -5, -15], [11, 1, -5], [8, -2, -4], [18, 2, 7], [-6, 9, 9], [2, -4, 6], [13, 5, -7], [-17, 0, 4], [14, -1, -1], [-26, 4, -2], [-24, -2, -2], [3, 0, -4], [7, 1, 0], [-22, 0, 2], [-6, 4, 0], [-9, -1, -9], [0, 0, -13], [-2, 8, 12], [-3, -5, -3], [10, 2, -8], [-22, 2, 7], [-10, -6, 2], [-3, -1, -11], [-25, 6, 6], [32, 0, 1], [8, 5, 5], [3, 1, 13], [-18, -6, -3], [2, 4, 4], [10, 0, -4], [3, 5, -6], [-5, -3, -1], [9, -1, -11], [2, 4, 8], [-3, -1, -10], [2, -6, 8], [10, 4, -2], [11, 6, -2], [6, 6, 0], [-20, -2, -2], [10, 6, 7], [-24, 4, 8], [-20, 8, 2], [-8, -4, -13], [-16, 0, 12], [22, 1, 9], [-17, -1, -7], [8, -2, 12], [13, 1, 1], [-12, 6, 2], [0, 2, 2], [10, 4, -4], [-10, 2, 10], [-22, 2, -4], [-21, 5, 5], [-2, 8, -2], [11, -5, 9], [22, 2, 4], [-29, 5, 1], [8, -6, 2], [3, -8, -8], [-18, -2, -1], [-6, -2, 6], [-22, -4, 8], [-17, 1, -2], [12, 1, -7], [-2, -8, -4], [2, -10, 2], [5, -1, -8], [-23, -3, -11], [-27, -2, 2], [8, 0, -11], [-2, -1, -17], [-10, -6, -4], [8, 2, -8], [-19, -4, -8], [13, -9, -10], [4, -8, 0], [19, 3, 15], [16, -8, 1], [0, -4, 4], [-15, 3, 5], [5, 3, 4], [-14, 3, -9], [-25, -1, 7], [-2, -10, -4], [-10, 0, -4], [4, 0, -3], [-22, 2, 8], [-17, 5, -9], [-19, 2, -6], [18, 4, 6], [-11, 4, -12], [18, -1, -13], [-24, -2, -16], [6, -6, -4], [24, 3, 7], [16, -4, -2], [17, 1, -11], [10, -2, 0], [-22, 0, -12], [26, 2, 12], [42, 2, 3], [-5, -3, -15], [22, 8, 8], [34, 4, 16], [18, -10, -10], [-38, 2, 1], [0, 4, 4], [-16, -7, -15], [0, -6, -4], [4, 8, 6], [1, 5, 17], [20, 4, 5], [-44, 0, 1], [-4, -3, 17], [-46, 4, 6], [49, -3, 1], [28, 4, 3], [-32, 2, 12], [-30, -4, 8], [-8, 4, -2], [-13, -3, -3], [29, -1, 7], [-8, -6, -14], [20, -8, -3], [-28, -3, -7], [-10, 6, -6], [-12, 4, -6], [42, 2, -6], [23, -3, -6], [8, 4, 4], [-25, 1, 7], [4, -4, 12], [15, 5, -2], [6, 10, -12], [-23, -5, 9], [-12, 10, -4], [13, 2, -10], [2, 2, 11], [16, -4, 4], [24, 2, -4], [-12, 8, 0], [-19, -1, 11], [-43, -5, 2], [2, -4, -16], [-20, 2, 12], [-34, -6, -2], [23, 1, -6], [-24, 0, 12], [-4, 12, 12], [-8, -8, 8], [39, -5, 3], [10, 0, 8], [14, 0, 8], [15, 7, 3], [-24, 8, -5], [-58, 1, 5], [31, 1, 7], [31, 1, 5], [20, 8, 11], [-24, -7, 5], [19, 5, 17], [-18, 9, 9], [-37, 3, 3], [-23, -1, -21], [2, 4, -16], [29, -1, 9], [26, -10, -5], [-32, -6, 8], [16, -8, -12], [-26, 2, 7], [21, -1, -5], [8, 0, -6], [21, -12, -8], [27, 5, 0], [12, -7, -7], [-16, 8, -4], [18, 7, -1], [-7, -3, 3], [25, -7, -3], [6, 2, 10], [0, -8, -18], [27, -7, -10], [30, -4, -14], [-43, -5, -1], [4, 4, 2], [14, 4, 14], [-15, -6, 2], [0, -8, 13], [21, -1, -2], [-28, 0, -8], [48, -4, -6], [18, -9, 3], [28, 4, -8], [-76, 0, 0], [-27, -3, 3], [-10, -6, -1], [-31, -1, 3], [38, 6, -4], [8, 8, -4], [11, -9, -17], [-34, 0, -8], [-30, -2, -17], [-20, -4, -9], [26, -3, 13], [-11, 5, 5], [11, -5, 7], [-10, -7, 5], [31, 7, 7], [16, -14, -2], [5, -7, 9], [-30, 4, -12], [12, 8, 2], [-2, -4, 2], [-20, -12, -4], [-12, -6, 0], [19, -1, 3], [-22, -11, 17], [-6, 8, 2], [-11, 4, -12], [50, -2, -10], [-3, -5, 3], [13, -3, -19], [-50, 2, -7], [-5, -15, -7], [-30, 2, -18], [-7, -2, 10], [-13, -1, -13], [-3, 7, 9], [-4, 2, -2], [-16, -12, -11], [27, 1, 11], [20, 4, -3], [25, 11, 3], [6, 2, -14], [-53, 1, -1], [28, 8, 0], [34, 8, -2], [-40, -10, -2], [-43, 0, 0], [-15, -5, 18], [1, 11, 17], [-4, 2, 4], [-14, -6, -27], [41, 7, 18], [-8, 8, -13], [-36, 1, 9], [3, -9, 3], [12, 0, 2], [-34, -12, 12], [-30, -2, -11], [12, -6, -4], [48, -1, -5], [46, 6, 24], [40, 8, -17], [5, 3, 5], [24, 2, 18], [-8, -12, -2], [-36, 3, -9], [22, -2, 14], [2, 0, -20], [30, -14, -16], [-8, 0, -32], [18, 0, 8], [-26, -6, 6], [41, 9, 3], [-44, 3, 3], [-20, 0, 10], [49, 3, 11], [28, -3, -3], [-4, -3, 5], [41, -13, -9], [-12, 18, 18], [-26, 14, 1], [43, 9, -7], [35, 7, 9], [-34, 2, 2], [58, -4, 6], [4, 3, 23], [32, -12, -10], [11, -9, -13], [18, -10, 0], [-20, -10, -22], [-68, -4, 8], [34, -12, -2], [36, -6, -10], [-32, -4, -18], [-41, 13, 13], [26, 6, 18], [47, 7, 1], [-32, -14, 24], [69, 0, 4], [10, -2, 24], [50, 1, 5], [-13, -13, -5], [22, 4, 0], [-24, -13, -9], [4, 12, 3], [-10, -6, 8], [-33, -9, -11], [12, -4, 1], [-22, 8, -2], [-30, 4, 18], [16, -4, -24], [-2, -10, 16], [30, -2, 5], [5, 11, -6], [42, -4, 4], [68, -2, 6], [9, -11, 9], [-34, -3, -3], [-12, -9, 23], [40, 4, 5], [-18, 3, 15], [20, 2, 14], [19, -1, 15], [34, -2, 9], [-26, -14, 6], [-19, -5, -9], [-56, -2, -12], [-42, 6, -8], [10, -6, -1], [21, 9, 1], [-24, 4, 22], [-58, -7, -19], [14, -4, 12], [28, 4, 26], [14, 7, 11], [0, -2, 28], [12, 4, -10], [-12, 8, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4704_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4704_2_a_bu();".
// 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_4704_2_a_bu(:prec:=3)
    chi := MakeCharacter_4704_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_4704_2_a_bu();".
// 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_4704_2_a_bu( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4704_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![4, -9, 0, 1]>,<11,R![38, -27, 0, 1]>,<13,R![-112, -36, 3, 1]>,<19,R![112, -36, -3, 1]>,<31,R![47, -21, -3, 1]>],Snew);
    return Vf;
end function;