
// Make newform 1078.2.a.w in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1078_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_1078_2_a_w();".
// 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_1078_2_a_w();".
// 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;
    Rf_num := [[1, 0], [-1, 2]];
    Rf_basisdens := [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_1078_a();"
function MakeCharacter_1078_a()
    N := 1078;
    order := 1;
    char_gens := [199, 981];
    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_1078_a_Hecke(Kf)
    return MakeCharacter_1078_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], [1, 1], [-1, -1], [0, 0], [1, 0], [1, -1], [2, 2], [5, -1], [4, 0], [0, -2], [-2, 0], [-2, -4], [-2, -2], [-6, 2], [2, 0], [4, -2], [-5, -1], [3, 1], [-2, -6], [2, -2], [-4, 4], [0, 0], [1, -5], [-10, 0], [-8, 2], [3, 5], [6, -4], [-2, -2], [-10, 0], [4, 2], [-12, 0], [-7, -1], [-2, 8], [15, -3], [0, 10], [12, 0], [-3, -7], [-6, 6], [2, 6], [1, -1], [0, 4], [-7, 1], [2, 2], [-6, 4], [18, 0], [-10, 4], [-18, -2], [-4, -2], [17, -1], [-15, 1], [-6, 4], [20, 0], [-2, 6], [-27, 1], [2, 4], [4, 4], [25, 1], [8, 4], [8, 2], [2, -12], [1, 7], [11, -7], [-3, -13], [8, -6], [-24, -2], [-22, 4], [-8, 4], [18, 0], [-2, -2], [-15, 3], [26, 4], [0, -12], [-8, -6], [-6, 0], [10, -2], [6, -8], [20, -6], [17, -3], [-8, -10], [10, 6], [-15, 1], [12, -2], [32, 4], [-24, 2], [0, 4], [-16, 4], [10, -4], [18, -4], [-27, -1], [-26, 2], [7, -9], [10, 10], [8, -4], [-8, 4], [-10, 10], [-4, 0], [5, 13], [-12, -10], [21, 3], [-18, 4], [8, 4], [-2, -12], [-29, 1], [-10, 12], [-28, -8], [-18, 4], [17, 5], [-24, 0], [-10, 10], [8, -12], [32, 0], [24, -2], [8, -6], [15, 15], [-18, 6], [32, 2], [21, 1], [2, -12], [24, -10], [30, 6], [-17, -9], [14, 8], [-3, -13], [24, -4], [3, 7], [2, 20], [0, -2], [10, 12], [-18, 0], [-29, 9], [20, 0], [4, -8], [-8, 4], [8, 14], [-2, -6], [30, -6], [-9, 3], [17, -5], [17, -11], [-30, -4], [-7, -1], [12, 6], [-6, 18], [8, -4], [-5, 3], [10, 12], [1, 15], [42, -2], [5, 9], [14, 6], [18, 4], [-2, 12], [24, 12], [22, -10], [-2, 10], [-28, 0], [-40, -8], [0, 18], [12, 4], [23, 5], [8, -4], [14, -4], [28, -8], [-17, 15], [-2, 12], [-14, 0], [22, 10], [-13, 5], [-30, -4], [-19, -3], [10, 18], [-27, 9], [-28, -4], [-26, -8], [-10, 4], [30, -12], [32, 4], [-18, -12], [-44, 8], [-5, -19], [-12, -20], [-17, 13], [-16, -18], [12, 8], [-36, 4], [-15, 17], [18, 8], [31, 15], [30, 0], [-2, 10], [-14, 8], [14, 6], [-18, -6], [13, -23], [-12, -24], [-34, 0], [-18, -4], [-26, 4], [52, -4], [-24, 4], [0, -6], [-2, 10], [-13, -25], [-20, -12], [15, 7], [7, -1], [-50, 4], [-16, -4], [-10, 0], [13, 5], [-2, 4], [3, 15], [24, 0], [-13, 7], [0, -12], [28, 2], [-12, -4], [-2, 2], [-2, -14], [-36, -14], [-48, -10], [0, -12], [10, -12], [-36, -4], [37, 3], [10, -4], [6, -20], [0, -8], [12, 24], [22, -2], [-6, 8], [-5, -19], [12, 20], [2, 0], [51, 3], [-8, -4], [0, 2], [44, -10], [-10, -6], [48, -10], [14, 18], [33, -13], [56, 6], [0, 10], [16, -4], [-30, -10], [-28, 2], [-57, -5], [-20, -16], [-36, -12], [18, 16], [8, 0], [-2, 18], [10, -4], [-9, 13], [-10, -10], [-18, -20], [7, -11], [7, -1], [22, -2], [44, 8], [8, -20], [-25, 5], [-29, 13], [-8, 22], [25, 21], [20, -18], [-22, 16], [24, 12], [44, 10], [-7, -19], [-2, 18], [26, -8], [60, 8], [-28, -12], [56, 2], [28, -16], [-50, -4], [-18, -16], [33, -5], [-4, -4], [62, -6], [-18, -18], [3, -27], [37, 7], [-28, 16], [-6, 20], [-72, -6], [50, 0], [-20, 24], [32, -22], [27, -5], [-66, -4], [-27, -25], [4, 22], [5, 15], [-32, 14], [1, 9], [-45, 1], [-53, -7], [-14, 6], [-62, 4], [0, 8], [34, -10], [22, 10], [-2, 16], [-12, -8], [-5, 27], [60, 4], [4, -2], [-4, 4], [-20, 18], [2, -4], [4, 0], [48, 8], [-20, -4], [-15, -11], [-28, 4], [46, -20], [30, 16], [3, -35], [38, 12], [-57, 5], [34, -14], [54, 0], [48, -10], [-10, 10], [-29, -21], [-32, 20], [-6, -28], [-18, -28], [-2, -12], [30, -6], [-9, -27], [12, -32], [27, 19], [10, 16], [36, 8], [18, -28], [-18, -10], [-66, -8], [8, 18], [-55, -1], [2, -22], [44, -14], [-20, 16], [13, -19], [-42, -6], [28, -28], [-13, -1], [-77, 1], [18, -4], [-58, 0], [6, 6], [20, 6], [36, 0], [-70, 0], [-67, 3], [18, 0], [-56, -8], [-32, 26], [8, -4], [-12, 20], [60, 0], [-33, -17], [-16, -14], [-43, -21], [4, 0], [-18, 0], [-38, -10], [5, 5], [44, 12], [30, -12], [-32, 0], [-52, 6], [-5, 11], [32, -4], [16, 4], [-90, -2], [22, 22], [13, -15], [24, -14], [48, -16], [-22, -8], [55, 13], [36, 30], [-18, 6], [27, 3], [-6, -22], [-18, 20], [0, 42], [51, -9], [-50, -6], [57, -11], [-8, 16], [14, 16], [-30, 2], [0, -18], [22, -6], [-38, -12], [-25, 21], [34, -16], [28, -20], [-8, -2], [45, -5], [38, -6], [18, 20], [-18, -16], [41, -23], [45, 13], [-4, -20], [28, 2], [-56, -12], [92, 0], [-22, -12], [-47, 13], [-20, 12], [22, 16], [92, 2], [36, -12], [40, -2], [47, 21], [-22, 30], [35, -27], [-64, 14], [7, -21], [34, 22], [10, 36], [-7, -7], [10, 10]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1078_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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