
// Make newform 5265.2.a.d in Magma, downloaded from the LMFDB on 18 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_d();".
// 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_d();".
// 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 := [[-1], [0], [-1], [-1], [2], [-1], [-4], [0], [-3], [-1], [8], [4], [9], [-8], [13], [-10], [6], [-1], [-1], [-6], [-12], [-6], [11], [5], [-2], [6], [-16], [13], [1], [4], [13], [14], [16], [-12], [-3], [10], [18], [-12], [-3], [-12], [6], [-7], [-12], [-2], [-24], [20], [6], [-17], [4], [-13], [4], [-24], [-23], [0], [-22], [-16], [9], [-4], [-20], [-27], [-13], [4], [-19], [-4], [14], [-18], [-18], [20], [-28], [-9], [0], [8], [-32], [-14], [-26], [-12], [-31], [18], [22], [30], [-10], [-14], [26], [-32], [0], [17], [6], [-16], [13], [-36], [-28], [6], [-16], [0], [-20], [39], [15], [3], [21], [5], [45], [22], [-19], [-38], [12], [-34], [37], [-4], [26], [10], [-23], [24], [-48], [40], [4], [41], [-19], [-7], [-36], [0], [26], [18], [-42], [4], [-50], [7], [43], [-26], [33], [-14], [6], [33], [-22], [2], [27], [17], [-8], [-28], [30], [34], [54], [-37], [-37], [-41], [-11], [-28], [42], [-42], [-14], [27], [-18], [-43], [-7], [-20], [-13], [30], [-10], [-26], [0], [-3], [3], [-6], [43], [8], [18], [-31], [30], [50], [-7], [4], [40], [5], [26], [16], [4], [58], [52], [-34], [-39], [-55], [16], [-32], [-64], [-38], [-9], [39], [12], [-25], [19], [-58], [-50], [33], [-52], [51], [-33], [10], [65], [44], [-48], [-39], [26], [-38], [28], [53], [24], [-34], [-70], [-5], [-3], [-28], [30], [22], [-17], [-12], [-30], [-11], [47], [38], [17], [-20], [55], [-50], [-55], [29], [59], [26], [16], [-6], [-15], [16], [62], [70], [-70], [33], [25], [-57], [-5], [34], [0], [6], [-65], [-22], [63], [7], [24], [16], [-53], [16], [12], [69], [-56], [71], [-48], [19], [24], [12], [-42], [-21], [-80], [60], [27], [-57], [-78], [14], [78], [-46], [26], [51], [45], [-18], [-46], [-47], [64], [-34], [-68], [-21], [-53], [-62], [-15], [-42], [8], [54], [-48], [33], [13], [44], [42], [8], [-60], [71], [-30], [-68], [-44], [48], [-64], [25], [-78], [-16], [-54], [44], [64], [-8], [28], [12], [-22], [30], [33], [-5], [40], [-68], [-24], [15], [-42], [55], [72], [-11], [46], [-18], [66], [-90], [88], [-66], [-78], [56], [14], [21], [24], [-37], [-3], [-70], [67], [0], [-48], [-12], [-78], [75], [-35], [28], [73], [64], [28], [84], [-3], [-46], [24], [-68], [6], [17], [42], [-68], [-46], [-22], [-13], [-55], [-53], [0], [-40], [48], [-12], [-81], [48], [-57], [-56], [6], [-36], [92], [-62], [15], [-38], [68], [4], [-3], [-83], [-20], [72], [90], [-8], [28], [-45], [-76], [45], [-38], [-55], [-42], [-16], [8], [-80], [96], [31], [-93], [-25], [34], [-30], [-100], [-18], [14], [-14], [18], [50], [18], [-35], [26], [-48], [36], [-27], [-14], [-68], [-45], [-57], [-42], [24], [60], [69], [56], [26], [-51], [30], [-32], [16], [-73], [105], [56], [-8], [2], [4], [0], [49], [14], [50], [14]];
    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_d();".
// 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_d(: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_d();".
// 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_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5265_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![1, 1]>,<7,R![1, 1]>,<11,R![-2, 1]>,<17,R![4, 1]>],Snew);
    return Vf;
end function;