
// Make newform 2240.2.a.d in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2240_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_2240_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_2240_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_2240_a();"
function MakeCharacter_2240_a()
    N := 2240;
    order := 1;
    char_gens := [1471, 1541, 897, 1921];
    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_2240_a_Hecke(Kf)
    return MakeCharacter_2240_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], [-3], [1], [1], [-1], [1], [-3], [-8], [4], [-3], [-6], [8], [10], [12], [3], [-12], [0], [-2], [-4], [-12], [-10], [-13], [0], [-6], [5], [-4], [-13], [6], [-1], [-16], [16], [16], [22], [-14], [-10], [-5], [-10], [14], [-21], [11], [0], [-12], [1], [-18], [-18], [4], [1], [-13], [-3], [-10], [-8], [27], [-28], [-10], [-14], [-18], [2], [-2], [22], [15], [-29], [-33], [7], [8], [-1], [-2], [-4], [6], [12], [-28], [25], [-24], [-33], [-10], [16], [-4], [19], [39], [13], [34], [-20], [35], [5], [-2], [34], [28], [-33], [-26], [-16], [2], [11], [24], [-2], [25], [-25], [21], [-42], [4], [-12], [-35], [-6], [-24], [20], [22], [-4], [27], [20], [43], [21], [14], [13], [-18], [30], [-28], [-19], [-18], [5], [28], [20], [-29], [-20], [-10], [-11], [14], [-32], [-3], [5], [-20], [8], [-19], [-41], [32], [-41], [-20], [0], [30], [-27], [29], [-5], [49], [50], [43], [-26], [-30], [-2], [4], [-30], [-2], [36], [-6], [20], [24], [-38], [8], [-52], [32], [-9], [16], [9], [22], [38], [-22], [22], [34], [32], [9], [-24], [47], [25], [-6], [36], [26], [-3], [46], [20], [36], [-37], [-5], [-8], [-6], [-30], [10], [12], [42], [-42], [48], [20], [21], [21], [-34], [2], [16], [-31], [34], [0], [-9], [19], [66], [45], [-29], [30], [2], [-14], [-36], [-12], [55], [26], [-6], [-30], [-38], [-22], [-42], [-46], [-7], [14], [50], [-52], [-26], [-60], [26], [-18], [-14], [-55], [-18], [9], [-46], [-31], [-39], [-37], [4], [-2], [54], [0], [9], [53], [-57], [22], [-36], [29], [60], [-24], [12], [-29], [-14], [-9], [-12], [-60], [-30], [-19], [64], [64], [-42], [-48], [-28], [-42], [-37], [-30], [20], [1], [39], [-44], [-54], [-32], [-75], [54], [16], [-17], [42], [36], [-28], [-22], [-42], [13], [-40], [-14], [11], [52], [-46], [17], [34], [71], [67], [48], [-10], [41], [-11], [40], [-12], [-20], [-60], [59], [-25], [-38], [-30], [32], [-10], [-26], [61], [-56], [17], [-3], [18], [13], [48], [29], [8], [-32], [-64], [-64], [-4], [-48], [-63], [50], [-86], [4], [78], [-90], [21], [-47], [37], [46], [22], [58], [22], [6], [-32], [45], [4], [14], [42], [2], [22], [42], [-33], [-12], [9], [7], [-26], [12], [23], [30], [-64], [96], [-91], [52], [4], [14], [34], [-84], [-69], [16], [-42], [27], [-25], [-41], [-30], [74], [-76], [64], [26], [86], [-48], [-42], [-21], [-28], [-88], [-21], [56], [41], [95], [90], [24], [49], [-10], [-18], [-84], [5], [47], [-42], [-49], [-26], [18], [52], [18], [-30], [-33], [-47], [33], [-56], [53], [37], [31], [5], [-52], [73], [68], [-22], [-42], [19], [-42], [-6], [-52], [68], [-27], [-60], [98], [-2], [47], [-44], [12], [-47], [-98], [-36], [-20], [-24], [0], [-23], [-8], [70], [35], [-21], [-78], [50], [-40], [-69], [-9], [36], [42], [2], [-14]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2240_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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