
// Make newform 2400.2.a.w in Magma, downloaded from the LMFDB on 25 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_w();".
// 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_w();".
// 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], [0], [5], [0], [-5], [4], [4], [5], [10], [-10], [1], [-2], [10], [10], [-5], [-3], [10], [10], [0], [-14], [16], [5], [2], [16], [18], [5], [20], [-8], [-10], [10], [-20], [-10], [-5], [-5], [11], [-18], [-20], [-10], [-7], [0], [-5], [10], [-25], [-25], [9], [18], [21], [30], [30], [5], [0], [0], [6], [0], [20], [-15], [20], [-21], [-10], [-13], [30], [-25], [-20], [0], [5], [-18], [-14], [-20], [10], [-17], [-5], [5], [-16], [-30], [15], [-2], [-35], [-10], [-10], [20], [-25], [-35], [-4], [0], [10], [38], [36], [-8], [10], [-17], [40], [-25], [6], [-6], [-28], [19], [33], [-32], [-30], [24], [-20], [-5], [-25], [18], [10], [0], [35], [8], [-10], [30], [35], [-35], [-40], [4], [-12], [-30], [-30], [-10], [-10], [0], [-24], [20], [0], [-31], [20], [37], [-10], [-20], [44], [40], [5], [12], [-1], [10], [-17], [0], [-6], [15], [30], [-11], [2], [30], [-40], [15], [0], [20], [-36], [15], [20], [-11], [2], [-28], [-30], [-5], [40], [-45], [-42], [-28], [40], [-32], [10], [-10], [-4], [-35], [-30], [-19], [-10], [-20], [-53], [30], [-15], [55], [-36], [55], [12], [29], [55], [53], [0], [25], [10], [54], [6], [25], [59], [25], [-40], [25], [-54], [45], [-60], [-52], [30], [30], [30], [30], [-24], [46], [0], [-50], [-14], [60], [-60], [55], [24], [16], [5], [55], [10], [-39], [28], [-10], [-50], [37], [-12], [2], [40], [25], [-5], [-6], [-4], [-42], [35], [40], [10], [43], [-60], [-15], [40], [-5], [30], [-56], [32], [-30], [-30], [-50], [-30], [56], [45], [-41], [-61], [-10], [-30], [47], [60], [-15], [66], [-30], [-48], [-72], [-75], [-20], [-60], [-47], [32], [30], [-50], [-31], [28], [26], [15], [-30], [-20], [6], [-28], [-4], [30], [-33], [52], [-10], [-15], [45], [29], [-38], [-9], [75], [-60], [-26], [40], [28], [63], [63], [70], [-55], [-10], [40], [-50], [-10], [48], [0], [20], [-5], [-10], [-35], [70], [-60], [12], [-50], [60], [65], [-6], [20], [-35], [32], [29], [30], [-30], [4], [-24], [68], [-31], [72], [41], [-20], [60], [-15], [-40], [-20], [55], [-70], [-39], [-50], [-70], [35], [11], [-12], [-10], [-15], [30], [-55], [-84], [-65], [-12], [-85], [-50], [25], [-48], [-85], [50], [-60], [75], [40], [-10], [-35], [47], [90], [-40], [15], [-90], [60], [91], [-9], [-30], [30], [0], [-60], [-36], [-5], [42], [8], [-60], [48], [15], [0], [-24], [-65], [-60], [-75], [96], [-80], [-40], [25], [70], [-50], [-65], [-20], [25], [-78], [0], [8], [-90], [-60], [34], [25], [-70], [-24], [-72], [-15], [70], [-90], [-23], [-90], [85], [-55], [-46], [5], [-80], [-70], [30], [-77], [90], [30], [80], [25], [-98], [-49], [80], [55], [100], [14], [-45], [105], [-12], [-10], [-13], [-30], [-6], [60], [-5], [-62], [50], [-10], [-50], [-106], [-30], [-95], [20]];
    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_w();".
// 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_w(: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_w();".
// 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_w( : 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![0, 1]>,<13,R![-5, 1]>,<19,R![5, 1]>],Snew);
    return Vf;
end function;