
// Make newform 1062.2.a.k in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1062_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_1062_2_a_k();".
// 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_1062_2_a_k();".
// 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_1062_a();"
function MakeCharacter_1062_a()
    N := 1062;
    order := 1;
    char_gens := [119, 415];
    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_1062_a_Hecke(Kf)
    return MakeCharacter_1062_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], [-1], [5], [1], [-1], [0], [6], [10], [-8], [9], [5], [3], [0], [-4], [1], [6], [4], [-1], [0], [-3], [-7], [-16], [-12], [-17], [4], [6], [-10], [-6], [-8], [20], [5], [4], [-21], [-10], [-6], [14], [0], [9], [-5], [-22], [12], [13], [0], [-4], [-3], [-9], [-7], [-25], [-20], [-8], [15], [-4], [21], [-3], [-3], [-15], [-26], [7], [31], [12], [2], [-7], [14], [-32], [34], [2], [36], [-7], [24], [-11], [-32], [24], [14], [21], [22], [-22], [-16], [26], [35], [1], [-26], [-31], [-11], [-17], [31], [-4], [20], [20], [-13], [28], [-7], [-30], [24], [26], [10], [18], [28], [-11], [-30], [12], [-24], [-30], [-20], [-23], [3], [2], [33], [-48], [37], [37], [-3], [12], [-27], [14], [16], [24], [48], [-36], [2], [46], [-36], [-9], [25], [47], [-26], [4], [11], [-50], [37], [-24], [50], [-40], [-34], [10], [2], [-46], [-21], [12], [-21], [21], [28], [-26], [-28], [-54], [44], [-2], [-20], [16], [-44], [-46], [-40], [42], [44], [-1], [-34], [32], [2], [6], [-48], [-21], [-44], [18], [48], [14], [-20], [-20], [-20], [1], [-32], [-29], [-17], [-20], [-35], [-11], [10], [27], [-20], [-26], [0], [22], [-39], [-22], [57], [-14], [-10], [-53], [-2], [-54], [38], [-37], [25], [-34], [38], [-2], [-10], [-1], [56], [40], [-26], [-16], [62], [-10], [36], [-53], [-16], [19], [-48], [27], [64], [12], [1], [32], [-19], [10], [-45], [3], [-18], [-44], [61], [66], [18], [-4], [43], [1], [54], [-52], [22], [40], [41], [-51], [44], [-20], [52], [-24], [-58], [6], [24], [33], [76], [-43], [1], [2], [-19], [-33], [-60], [63], [-66], [-39], [30], [14], [8], [1], [6], [16], [-6], [32], [-38], [57], [16], [-60], [64], [78], [-18], [49], [72], [24], [-18], [18], [-50], [74], [48], [-7], [-9], [-34], [38], [50], [80], [0], [49], [-16], [32], [71], [44], [68], [-44], [6], [-30], [30], [35], [-50], [-30], [-33], [-8], [9], [-25], [-54], [27], [-68], [-34], [35], [68], [-15], [-52], [4], [42], [-63], [18], [-6], [-37], [-48], [-45], [-49], [-53], [4], [-52], [-73], [42], [5], [78], [46], [24], [80], [62], [40], [85], [-14], [24], [6], [-59], [10], [30], [-64], [-36], [-36], [-36], [-52], [32], [88], [64], [82], [-89], [80], [-13], [-57], [-77], [-58], [-8], [38], [-52], [67], [-26], [4], [-52], [12], [70], [-44], [-34], [-37], [53], [71], [-45], [63], [-68], [50], [-3], [53], [-34], [-20], [81], [-12], [28], [-12], [68], [-56], [-62], [-94], [12], [62], [-18], [-52], [31], [40], [30], [-14], [43], [16], [84], [48], [30], [85], [47], [29], [-72], [78], [-56], [90], [-44], [-12], [-8], [-32], [19], [-13], [-42], [-98], [26], [21], [-61], [-64], [-1], [66], [28], [-80], [-49], [68], [84], [52], [-78], [15], [-66], [-4], [24], [-52], [5], [40], [76], [-33], [14], [-24]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1062_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1062_2_a_k();".
// 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_1062_2_a_k(:prec:=1)
    chi := MakeCharacter_1062_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_1062_2_a_k();".
// 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_1062_2_a_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1062_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![0, 1]>,<7,R![1, 1]>,<11,R![-5, 1]>],Snew);
    return Vf;
end function;