
// Make newform 1078.2.a.l 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_l();".
// 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_l();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
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], [2], [2], [0], [1], [-2], [0], [2], [0], [6], [-4], [2], [-8], [12], [-12], [-2], [-10], [10], [-12], [4], [-12], [0], [18], [0], [12], [-6], [-12], [12], [-10], [-10], [4], [2], [22], [14], [-14], [-4], [14], [12], [-12], [6], [12], [-18], [12], [-2], [-6], [-4], [-4], [-8], [-6], [30], [-10], [-4], [-8], [18], [-16], [16], [6], [-16], [-14], [-10], [2], [26], [-14], [-16], [4], [30], [12], [34], [20], [14], [-24], [20], [8], [-30], [28], [-12], [10], [-34], [18], [-16], [18], [-34], [-36], [-4], [16], [12], [-30], [6], [6], [4], [22], [36], [8], [28], [20], [8], [30], [4], [34], [34], [28], [34], [22], [10], [4], [32], [42], [-20], [28], [-4], [-32], [-42], [18], [14], [-28], [18], [34], [44], [18], [-12], [-6], [6], [-42], [4], [-50], [-34], [-14], [28], [-12], [-6], [-4], [36], [0], [-30], [-8], [40], [-14], [-14], [-18], [38], [50], [18], [-20], [12], [-14], [36], [-42], [-48], [34], [-52], [-2], [-24], [-52], [-20], [4], [-32], [-8], [4], [20], [42], [12], [34], [-4], [6], [-2], [-44], [-52], [-62], [14], [38], [-20], [-2], [-48], [22], [28], [-48], [-52], [-38], [-40], [10], [-56], [-42], [-54], [52], [-40], [14], [-62], [14], [-50], [-4], [0], [36], [20], [-50], [28], [24], [2], [-30], [4], [-16], [-46], [28], [50], [-12], [6], [42], [4], [20], [6], [-30], [34], [-30], [-40], [-42], [-16], [28], [8], [-24], [-12], [34], [30], [-48], [-46], [-8], [18], [-2], [24], [8], [40], [-20], [26], [46], [64], [-54], [-18], [32], [-44], [18], [4], [24], [4], [-14], [8], [-34], [44], [-44], [-32], [-42], [60], [-32], [74], [12], [4], [-64], [74], [-44], [-62], [-66], [66], [56], [76], [60], [66], [-14], [-12], [-38], [78], [-24], [-44], [-18], [-26], [52], [16], [28], [52], [56], [-52], [-6], [38], [26], [24], [52], [-4], [-62], [-30], [-48], [34], [-30], [-28], [-12], [-50], [22], [-6], [26], [42], [-74], [48], [-66], [-74], [-42], [16], [-2], [-32], [-36], [-52], [-2], [-28], [10], [-72], [50], [24], [42], [-10], [76], [-48], [-76], [-30], [-56], [-56], [-34], [-18], [-42], [66], [-68], [54], [-60], [36], [-74], [20], [-26], [-30], [58], [44], [-2], [-60], [30], [-78], [4], [-40], [-52], [-58], [42], [30], [12], [-30], [-92], [-74], [4], [-8], [10], [62], [-38], [-26], [60], [-30], [-44], [12], [-46], [10], [-24], [-22], [12], [-32], [-60], [46], [-14], [22], [-4], [70], [4], [26], [-72], [-94], [32], [38], [62], [84], [-52], [-40], [56], [86], [54], [-72], [-30], [-34], [16], [100], [10], [-28], [36], [26], [-6], [52], [-86], [48], [46], [20], [12], [4], [-58], [18], [34], [-48], [20], [30], [12], [-10], [-2], [50], [-62], [-12], [78], [-12], [12], [38], [30], [-32], [-28], [68], [56], [-54], [-14], [68], [-66], [68], [66], [36], [-90], [-46], [52]];
    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_l();".
// 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_l(:prec:=1)
    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_l();".
// 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_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1078_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-2, 1]>,<5,R![-2, 1]>,<13,R![2, 1]>],Snew);
    return Vf;
end function;