
// Make newform 6566.2.a.o in Magma, downloaded from the LMFDB on 16 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6566_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_6566_2_a_o();".
// 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_6566_2_a_o();".
// 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_6566_a();"
function MakeCharacter_6566_a()
    N := 6566;
    order := 1;
    char_gens := [4021, 1275];
    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_6566_a_Hecke(Kf)
    return MakeCharacter_6566_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]];
    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], [-4], [2], [4], [-6], [0], [2], [-4], [2], [-8], [-4], [-4], [-6], [10], [-2], [-1], [-12], [8], [-16], [2], [0], [8], [6], [-4], [-20], [-2], [14], [-8], [-6], [14], [10], [14], [-8], [-14], [-4], [-12], [-6], [-12], [10], [-24], [14], [2], [4], [-20], [8], [10], [2], [-10], [-20], [20], [-6], [0], [28], [-6], [32], [-2], [-10], [18], [6], [-22], [-16], [0], [-2], [-20], [2], [12], [2], [-4], [24], [24], [10], [-4], [4], [10], [-14], [18], [-32], [-26], [-10], [16], [-24], [32], [-4], [18], [-26], [18], [24], [34], [-12], [-28], [28], [12], [8], [18], [8], [-30], [10], [4], [30], [42], [-6], [-20], [-4], [42], [-20], [40], [-16], [-32], [-22], [2], [10], [0], [2], [-42], [28], [22], [-12], [14], [38], [42], [-36], [42], [30], [-6], [36], [4], [-42], [44], [-24], [0], [-34], [-12], [16], [-18], [-18], [-6], [-26], [-2], [38], [-44], [-28], [-26], [-20], [-14], [0], [-10], [-12], [2], [48], [-36], [-36], [12], [24], [0], [-32], [4], [54], [28], [-54], [-8], [-30], [-18], [36], [0], [6], [62], [-6], [36], [2], [-44], [-10], [-4], [52], [20], [42], [-8], [14], [-32], [-18], [34], [0], [-12], [26], [-30], [-26], [-2], [-12], [-56], [4], [28], [-30], [4], [-40], [-54], [46], [-68], [-64], [-66], [-4], [-42], [-52], [-58], [-2], [60], [-36], [-66], [-38], [-62], [70], [44], [-34], [0], [-40], [-12], [-60], [48], [10], [22], [16], [42], [40], [-58], [26], [-24], [12], [8], [4], [-26], [-30], [-36], [-54], [26], [0], [-36], [-10], [60], [-56], [28], [-34], [-56], [-30], [-36], [60], [64], [54], [-12], [24], [62], [16], [48], [40], [6], [20], [-42], [-34], [-34], [-20], [-56], [4], [70], [-26], [60], [-10], [-14], [-4], [4], [-58], [-6], [44], [44], [-32], [72], [8], [20], [-58], [-18], [-2], [24], [-20], [4], [30], [-50], [48], [2], [-18], [84], [4], [-58], [-2], [2], [-30], [66], [-22], [48], [-70], [-18], [26], [-48], [58], [-44], [-20], [4], [38], [12], [-34], [-80], [74], [-40], [10], [30], [-84], [52], [-36], [14], [8], [56], [-66], [-50], [38], [30], [-56], [6], [28], [-20], [74], [-68], [-10], [-66], [18], [52], [66], [52], [-6], [38], [24], [-12], [60], [-50], [42], [50], [-20], [-38], [60], [50], [-76], [56], [70], [-50], [-78], [6], [-44], [-6], [-36], [84], [30], [10], [-20], [38], [0], [-64], [44], [10], [42], [-38], [-24], [-74], [60], [-26], [-84], [90], [48], [-78], [-46], [20], [-40], [48], [-28], [2], [54], [24], [34], [74], [-16], [56], [-26], [-36], [-60], [-54], [-34], [68], [26], [76], [38], [4], [-100], [76], [30], [-58], [-46], [-88], [72], [-70], [-60], [-62], [-98], [38], [66], [-96], [-78], [20], [36], [70], [-102], [-28], [-52], [36], [-88], [102], [70], [-8], [58], [-80], [62], [-52], [-26], [-82], [-84]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6566_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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