
// Make newform 4600.2.a.d in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4600_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_4600_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_4600_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_4600_a();"
function MakeCharacter_4600_a()
    N := 4600;
    order := 1;
    char_gens := [1151, 2301, 2577, 1201];
    v := [1, 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_4600_a_Hecke(Kf)
    return MakeCharacter_4600_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], [-2], [0], [3], [5], [5], [4], [1], [1], [9], [-2], [2], [3], [7], [12], [-12], [-6], [-10], [8], [2], [-1], [-11], [-9], [-14], [16], [-14], [9], [-16], [-10], [-8], [10], [6], [-6], [12], [14], [-18], [-8], [-16], [14], [-5], [12], [-20], [-1], [-2], [23], [5], [-2], [8], [-20], [-30], [25], [12], [-4], [20], [2], [16], [5], [12], [-13], [-8], [4], [-28], [-16], [18], [10], [-17], [26], [-32], [-32], [-7], [-21], [-3], [15], [4], [-4], [23], [-4], [6], [6], [-35], [3], [-2], [16], [-6], [0], [-14], [6], [28], [41], [-24], [21], [-17], [32], [-2], [24], [5], [30], [18], [-7], [-23], [-44], [-42], [39], [-4], [-44], [23], [-30], [17], [-14], [-22], [-40], [-18], [32], [-24], [-25], [0], [-29], [32], [29], [-45], [8], [-15], [18], [34], [-16], [8], [-6], [-34], [-16], [26], [-26], [-21], [19], [-2], [1], [2], [18], [25], [-18], [-23], [40], [51], [-12], [-7], [-31], [15], [-19], [38], [-2], [-36], [-22], [2], [-16], [-48], [-15], [5], [0], [-13], [44], [-24], [-18], [2], [-12], [-41], [20], [1], [40], [-5], [52], [-42], [-33], [-23], [35], [14], [-4], [-24], [50], [-9], [-1], [8], [-22], [35], [-41], [39], [21], [18], [-42], [-15], [65], [18], [21], [-46], [7], [30], [43], [-10], [-14], [34], [-38], [12], [-24], [-26], [-37], [-6], [-11], [39], [-60], [66], [-29], [68], [57], [19], [-39], [23], [0], [-34], [-42], [42], [15], [66], [-61], [31], [-22], [-15], [50], [63], [54], [-48], [32], [4], [66], [44], [65], [21], [44], [-27], [-18], [38], [-68], [30], [11], [50], [66], [-14], [-54], [18], [-36], [72], [-33], [-7], [30], [-42], [43], [44], [-15], [40], [-74], [49], [-65], [38], [0], [-57], [-2], [-2], [-46], [-21], [36], [4], [-41], [-57], [17], [44], [80], [41], [71], [16], [50], [-33], [-40], [84], [-66], [-56], [-51], [-38], [4], [30], [-26], [60], [-8], [21], [-76], [-23], [-29], [12], [-6], [68], [-15], [45], [-32], [68], [-58], [-16], [-31], [42], [52], [33], [4], [-60], [-56], [-27], [-76], [50], [-38], [-16], [0], [62], [-60], [66], [-14], [62], [-28], [-28], [-33], [14], [54], [-52], [-68], [-14], [-25], [40], [38], [-31], [46], [-14], [-92], [72], [-70], [64], [15], [-52], [-26], [12], [35], [-24], [60], [62], [-14], [-4], [19], [-4], [-2], [-2], [-35], [72], [-80], [51], [-19], [-67], [-57], [42], [-48], [45], [-22], [-61], [68], [86], [78], [40], [-52], [48], [64], [-58], [-82], [21], [-11], [-18], [-35], [62], [72], [89], [18], [30], [46], [62], [75], [-42], [-68], [-94], [28], [16], [60], [99], [6], [-8], [-88], [-84], [-89], [-96], [33], [-30], [-41], [-2], [79], [-6], [-70], [74], [-20], [78], [-11], [0], [98], [-87], [47], [25], [-28], [-51], [-54], [-14], [96], [-15], [-8], [94], [-22], [86], [-65], [-51], [61], [15], [28], [4]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4600_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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