
// Make newform 6240.2.a.j in Magma, downloaded from the LMFDB on 10 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6240_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_6240_2_a_j();".
// 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_6240_2_a_j();".
// 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_6240_a();"
function MakeCharacter_6240_a()
    N := 6240;
    order := 1;
    char_gens := [1951, 2341, 2081, 2497, 5761];
    v := [1, 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_6240_a_Hecke(Kf)
    return MakeCharacter_6240_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], [1], [-1], [3], [-1], [-5], [2], [3], [0], [4], [1], [9], [-2], [-8], [-1], [-4], [-3], [16], [15], [-14], [15], [8], [-9], [-5], [-4], [-4], [-5], [20], [18], [8], [-12], [6], [1], [-5], [16], [-10], [-5], [8], [-18], [6], [1], [6], [17], [-10], [-16], [-8], [16], [-2], [4], [21], [7], [20], [0], [22], [0], [-4], [14], [-16], [18], [28], [24], [-21], [14], [-6], [-2], [18], [-18], [15], [-2], [-12], [0], [22], [14], [-24], [30], [36], [33], [18], [-8], [26], [-28], [40], [-14], [17], [-9], [-3], [23], [33], [-9], [-9], [-3], [-3], [-30], [-34], [24], [35], [30], [-2], [-38], [-32], [12], [19], [-2], [15], [-5], [-30], [8], [16], [-45], [2], [43], [-6], [-8], [12], [-2], [-49], [-27], [18], [-48], [-26], [12], [3], [10], [18], [-24], [36], [28], [14], [-15], [10], [42], [25], [-16], [-6], [-6], [0], [24], [21], [-2], [30], [-19], [24], [48], [-6], [-37], [39], [41], [-51], [-30], [6], [8], [-44], [-39], [-38], [30], [-29], [35], [0], [31], [-8], [-9], [-48], [34], [-42], [-24], [37], [-32], [-20], [-33], [-60], [2], [6], [-41], [21], [42], [40], [-17], [-16], [-26], [-49], [22], [-36], [-12], [-46], [-20], [18], [-19], [14], [5], [18], [26], [11], [-31], [-12], [-21], [-26], [44], [-60], [-41], [-18], [-25], [-17], [-31], [-27], [-3], [-2], [-20], [39], [41], [-52], [-48], [-8], [-60], [-32], [6], [-46], [28], [18], [30], [15], [-6], [-42], [-12], [-33], [-67], [41], [24], [10], [-9], [-2], [-11], [30], [0], [-26], [24], [-14], [-22], [46], [10], [-24], [-67], [64], [18], [10], [18], [-48], [23], [-48], [48], [-22], [22], [-68], [-31], [49], [-3], [10], [67], [10], [35], [-2], [53], [-40], [10], [72], [-29], [51], [7], [-7], [-50], [-62], [-43], [-37], [-44], [32], [29], [4], [32], [-38], [66], [19], [-56], [-72], [84], [20], [48], [26], [16], [-44], [-40], [-2], [-12], [-84], [12], [54], [46], [20], [-3], [6], [63], [-76], [18], [75], [-87], [12], [-19], [61], [19], [80], [40], [15], [-6], [84], [14], [-74], [53], [-15], [67], [90], [-12], [32], [38], [-30], [-1], [68], [-5], [4], [23], [63], [-70], [10], [-43], [40], [6], [56], [-56], [-20], [-64], [-72], [-2], [-5], [-23], [-77], [40], [46], [-86], [-79], [-33], [-24], [5], [32], [73], [-51], [-62], [11], [-46], [-21], [68], [54], [-70], [2], [-30], [-40], [44], [-30], [56], [88], [63], [-39], [16], [76], [22], [42], [53], [10], [-23], [52], [11], [-80], [-58], [-94], [-6], [-16], [-2], [-76], [-12], [-46], [-38], [-76], [3], [26], [-64], [7], [14], [-65], [22], [86], [12], [-15], [54], [-70], [-43], [45], [-2], [-11], [-43], [54], [34], [-3], [29], [6], [-43], [-41], [-43], [-32], [-60], [-51], [24], [76], [21], [-32], [-55], [78], [52], [-10], [-16], [43], [93], [-52], [74]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6240_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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