
// Make newform 5265.2.a.g in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5265_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_5265_2_a_g();".
// 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_5265_2_a_g();".
// 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_5265_a();"
function MakeCharacter_5265_a()
    N := 5265;
    order := 1;
    char_gens := [326, 2107, 2836];
    v := [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_5265_a_Hecke(Kf)
    return MakeCharacter_5265_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], [-1], [-4], [0], [1], [3], [2], [-3], [-6], [-4], [8], [12], [-1], [0], [3], [-6], [11], [-4], [-6], [8], [-7], [-6], [18], [14], [15], [-4], [3], [-10], [-3], [-16], [21], [-6], [5], [-18], [-22], [-13], [-16], [-12], [-15], [-9], [-25], [-3], [-4], [-12], [-7], [5], [-22], [12], [2], [-27], [18], [-10], [27], [-3], [3], [-6], [20], [-22], [12], [-13], [0], [-4], [24], [-10], [30], [20], [5], [-15], [-10], [24], [-18], [17], [35], [-16], [24], [9], [-4], [-18], [32], [-15], [2], [-24], [-37], [-13], [-9], [0], [8], [0], [26], [3], [-6], [38], [36], [-4], [33], [30], [-6], [-28], [20], [-40], [-6], [39], [-42], [-28], [-28], [-18], [-30], [33], [29], [-7], [26], [-6], [-10], [-16], [42], [-22], [-9], [-30], [0], [-22], [-7], [-42], [36], [44], [-27], [2], [-24], [-31], [-22], [-34], [-42], [-16], [-19], [-12], [32], [18], [-46], [-54], [-9], [-4], [30], [41], [-18], [14], [48], [-10], [-54], [-37], [6], [-10], [15], [8], [-27], [-1], [-39], [11], [54], [-31], [-18], [12], [-6], [-28], [12], [-24], [24], [-55], [53], [-16], [-42], [-54], [-4], [27], [-10], [5], [-30], [-10], [6], [56], [17], [-28], [45], [-19], [-6], [-36], [9], [-34], [44], [38], [36], [59], [18], [-1], [-18], [-24], [-54], [2], [-31], [12], [24], [36], [32], [20], [50], [24], [3], [8], [-12], [-30], [23], [11], [9], [8], [-54], [-66], [-34], [47], [66], [42], [-6], [-73], [8], [30], [-52], [0], [-19], [-33], [-57], [-64], [-30], [-31], [-25], [-46], [45], [-61], [24], [-34], [-66], [21], [-15], [-12], [-25], [11], [20], [-72], [57], [-22], [-66], [26], [-63], [-22], [-30], [72], [71], [-39], [30], [-19], [-10], [51], [-70], [-37], [-27], [20], [-10], [24], [-19], [48], [30], [-22], [-54], [-43], [38], [-46], [-4], [14], [62], [-18], [-4], [50], [60], [63], [-4], [63], [80], [-4], [-63], [-34], [-48], [32], [-33], [57], [-3], [30], [12], [-25], [33], [-73], [-30], [-33], [-16], [-61], [36], [-31], [48], [35], [-34], [-33], [-70], [48], [-7], [21], [66], [-30], [20], [-18], [17], [-78], [48], [38], [57], [35], [68], [57], [38], [-72], [-25], [80], [26], [0], [9], [26], [81], [-37], [-78], [26], [-48], [-4], [-18], [-46], [29], [26], [9], [-66], [-64], [36], [21], [-55], [38], [-6], [75], [14], [38], [-66], [-7], [-67], [-3], [54], [72], [33], [-96], [-10], [69], [51], [66], [-4], [14], [-42], [-40], [11], [0], [29], [-66], [57], [-13], [-43], [60], [-12], [56], [-54], [17], [12], [-36], [-64], [60], [-70], [60], [-82], [-58], [8], [-48], [-76], [0], [-48], [-73], [-12], [53], [56], [-75], [-55], [42], [-40], [-90], [-46], [90], [24], [-16], [-34], [-24], [-58], [0], [-34], [87], [12], [17], [-13], [57], [-54], [-25], [-66], [36], [-21], [20], [-48], [84], [-16], [60], [-99], [-24], [68], [-69]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5265_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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