
// Make newform 2400.2.a.n in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2400_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_2400_2_a_n();".
// 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_2400_2_a_n();".
// 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_2400_a();"
function MakeCharacter_2400_a()
    N := 2400;
    order := 1;
    char_gens := [1951, 901, 1601, 577];
    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_2400_a_Hecke(Kf)
    return MakeCharacter_2400_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], [-1], [0], [3], [4], [7], [-4], [-1], [8], [0], [-3], [-2], [-6], [11], [-6], [-6], [-6], [-1], [15], [6], [-2], [-8], [-2], [-16], [-13], [-2], [16], [18], [17], [12], [-8], [10], [14], [4], [6], [-13], [-7], [-7], [6], [-16], [-18], [13], [-20], [13], [-26], [-17], [19], [15], [10], [9], [-18], [10], [5], [24], [24], [-26], [-20], [20], [3], [8], [-23], [-14], [17], [-18], [17], [12], [-24], [-5], [-14], [-14], [16], [-10], [-15], [25], [17], [8], [6], [-27], [-38], [29], [-6], [-2], [-8], [-23], [13], [-20], [-8], [-26], [10], [-4], [-12], [2], [25], [24], [11], [-26], [10], [24], [1], [-3], [8], [6], [-36], [-32], [23], [1], [-30], [42], [-8], [19], [-32], [26], [-42], [-1], [-35], [32], [-12], [28], [-10], [-10], [-26], [18], [12], [24], [-20], [-20], [-35], [8], [11], [-22], [-20], [-12], [8], [47], [12], [7], [-46], [-11], [-40], [14], [-21], [6], [-37], [18], [-18], [12], [21], [-8], [28], [44], [5], [-36], [7], [38], [-36], [58], [-53], [36], [-27], [-50], [40], [24], [8], [34], [-30], [52], [13], [30], [-11], [42], [-40], [31], [-18], [-9], [-1], [-12], [-21], [0], [19], [-13], [59], [16], [-13], [6], [18], [-50], [27], [1], [1], [12], [-65], [34], [9], [8], [-8], [50], [-42], [-30], [42], [-68], [-38], [48], [-30], [-38], [12], [48], [-65], [20], [8], [25], [-7], [-30], [-1], [36], [-34], [-26], [35], [0], [18], [-20], [5], [35], [-6], [20], [62], [-1], [4], [-54], [29], [0], [-45], [16], [-37], [-34], [-16], [32], [-54], [2], [-2], [-42], [52], [-47], [-7], [-57], [58], [-70], [-47], [56], [-3], [-26], [30], [-28], [-48], [-19], [-24], [36], [5], [-8], [-18], [-46], [7], [0], [-70], [-11], [-18], [-44], [38], [12], [20], [50], [27], [-44], [-38], [57], [51], [-29], [-10], [19], [27], [60], [-42], [-16], [24], [27], [45], [-2], [-25], [-50], [-8], [34], [54], [-8], [-72], [-52], [-47], [-46], [-67], [46], [0], [-12], [-30], [20], [65], [-66], [4], [-37], [-40], [-47], [-6], [-66], [-32], [-48], [52], [-61], [-20], [1], [40], [-84], [-73], [52], [-44], [-23], [6], [-9], [-70], [50], [79], [-31], [0], [30], [-35], [26], [-31], [16], [19], [92], [31], [54], [17], [-56], [-31], [18], [0], [-69], [12], [14], [-31], [29], [-18], [-48], [75], [-70], [-80], [-19], [3], [-90], [-14], [-12], [48], [16], [-55], [34], [52], [-4], [72], [81], [48], [-24], [-17], [36], [25], [-68], [56], [0], [-37], [-78], [-58], [-7], [76], [23], [-46], [36], [80], [22], [84], [-18], [17], [-10], [-48], [-12], [-15], [-66], [26], [-29], [-98], [59], [-23], [-26], [-55], [-8], [2], [6], [85], [-46], [-46], [8], [19], [66], [69], [-40], [57], [92], [-78], [-73], [39], [100], [-18], [-43], [18], [34], [20], [73], [22], [-6], [26], [102], [78], [6], [5], [48]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2400_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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