
// Make newform 4650.2.a.p in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4650_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_4650_2_a_p();".
// 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_4650_2_a_p();".
// 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_4650_a();"
function MakeCharacter_4650_a()
    N := 4650;
    order := 1;
    char_gens := [3101, 2977, 1801];
    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_4650_a_Hecke(Kf)
    return MakeCharacter_4650_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], [1], [0], [-1], [-3], [4], [0], [-1], [-5], [2], [-1], [-4], [-10], [5], [8], [-5], [-6], [-2], [-2], [-5], [7], [3], [2], [1], [10], [-15], [0], [-19], [2], [15], [-8], [-2], [-2], [-20], [15], [-16], [-9], [4], [-21], [2], [0], [-5], [-16], [10], [-18], [-21], [7], [20], [9], [15], [21], [-14], [-12], [12], [-5], [-24], [2], [9], [-2], [-32], [-14], [18], [4], [16], [6], [-18], [8], [34], [22], [20], [-24], [3], [-14], [11], [-15], [-28], [-28], [31], [5], [4], [-18], [-14], [0], [-19], [20], [-29], [2], [-30], [16], [14], [-28], [-25], [4], [33], [22], [-14], [32], [0], [-19], [24], [-28], [-9], [-36], [-39], [26], [34], [-6], [-6], [13], [-44], [27], [-24], [-5], [-12], [25], [50], [7], [-7], [42], [6], [36], [30], [-13], [-35], [-41], [-35], [5], [10], [-49], [22], [-18], [-15], [22], [-20], [21], [45], [29], [-27], [22], [9], [35], [18], [6], [-14], [-25], [39], [55], [22], [-32], [29], [-42], [42], [35], [0], [36], [-46], [20], [-1], [2], [16], [-46], [28], [8], [-18], [-6], [36], [-53], [-6], [20], [27], [20], [-7], [24], [-38], [-32], [7], [-28], [37], [-7], [1], [24], [47], [-22], [-44], [26], [44], [23], [-4], [-50], [61], [3], [43], [36], [36], [-36], [54], [30], [-53], [9], [54], [33], [1], [63], [-35], [42], [66], [50], [44], [50], [-60], [57], [-56], [-52], [43], [68], [-61], [-8], [38], [-40], [-6], [1], [50], [-16], [31], [65], [-13], [10], [-8], [48], [-30], [-18], [28], [-10], [-15], [52], [21], [-11], [42], [-69], [-42], [-52], [46], [-66], [-50], [4], [-7], [-56], [3], [-10], [-64], [43], [28], [-31], [-33], [-8], [-28], [-10], [37], [-24], [78], [61], [6], [-53], [-17], [-64], [-23], [-27], [-6], [-38], [-22], [24], [4], [11], [39], [-24], [-24], [7], [-14], [56], [-27], [0], [36], [-72], [-56], [-32], [6], [79], [-18], [-20], [-34], [-13], [-11], [48], [12], [44], [-54], [56], [28], [11], [-37], [-66], [-3], [24], [29], [-22], [4], [24], [-16], [-12], [74], [-24], [36], [54], [-47], [-12], [-14], [-72], [72], [-44], [-85], [-66], [-3], [15], [-57], [-17], [-58], [-75], [-4], [52], [39], [-70], [38], [80], [-24], [-69], [-9], [-34], [69], [21], [50], [-36], [-60], [34], [-65], [-68], [-38], [-94], [-53], [16], [12], [56], [-3], [-15], [-42], [22], [-28], [40], [-87], [-92], [18], [-3], [49], [63], [68], [2], [11], [34], [24], [43], [2], [7], [42], [78], [-56], [-2], [3], [-85], [-4], [36], [55], [-52], [44], [50], [-2], [-22], [-30], [-70], [-70], [-43], [51], [-80], [31], [-54], [48], [-95], [-6], [75], [-58], [84], [-26], [53], [1], [-27], [42], [-92], [14], [-98], [-90], [-77], [-69], [-62], [68], [-42], [-62], [-46], [75], [60], [33], [39], [4], [76], [-34], [81], [-96], [-22], [85], [69], [85], [46], [-90]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4650_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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