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