
// Make newform 7800.2.a.v in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_7800_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_7800_2_a_v();".
// 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_7800_2_a_v();".
// 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_7800_a();"
function MakeCharacter_7800_a()
    N := 7800;
    order := 1;
    char_gens := [1951, 3901, 5201, 7177, 4201];
    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_7800_a_Hecke(Kf)
    return MakeCharacter_7800_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], [1], [3], [-1], [-7], [0], [-7], [-4], [8], [-5], [-3], [-8], [6], [-11], [-4], [1], [12], [-9], [6], [-13], [-2], [3], [-19], [14], [10], [-15], [0], [-18], [6], [0], [6], [-11], [-1], [-2], [-16], [21], [14], [6], [10], [1], [6], [11], [-2], [8], [-4], [-16], [0], [22], [11], [27], [20], [-22], [26], [-24], [-6], [22], [-26], [-22], [-14], [24], [-11], [-20], [16], [-20], [-4], [-20], [-27], [2], [-18], [-32], [-18], [-2], [-8], [14], [28], [-33], [-34], [24], [-20], [18], [-36], [-30], [13], [9], [9], [-3], [-23], [29], [5], [5], [27], [2], [-18], [16], [11], [22], [34], [20], [8], [0], [-27], [-36], [19], [37], [-26], [-36], [-6], [23], [-8], [9], [-36], [-2], [20], [-12], [-19], [-9], [26], [16], [14], [-34], [5], [30], [2], [36], [-4], [-22], [-8], [-17], [16], [-24], [17], [-38], [14], [18], [34], [-24], [39], [-12], [-52], [-35], [14], [18], [50], [-5], [13], [23], [-23], [-8], [2], [-18], [-26], [-53], [-42], [38], [11], [15], [26], [-1], [18], [57], [-16], [-6], [34], [-40], [17], [14], [34], [-19], [-48], [48], [-48], [13], [-15], [-54], [58], [55], [-64], [-14], [61], [6], [34], [8], [24], [26], [20], [-17], [46], [-51], [8], [-10], [-21], [45], [-48], [21], [-40], [-4], [-26], [9], [-54], [-65], [17], [-35], [45], [-21], [-14], [40], [43], [1], [0], [-2], [42], [-30], [24], [-32], [42], [10], [-6], [14], [43], [44], [-18], [40], [33], [13], [47], [-18], [44], [-21], [44], [25], [2], [58], [42], [-52], [-58], [-36], [36], [4], [-44], [37], [16], [50], [56], [-20], [64], [-45], [12], [-24], [2], [-50], [60], [-75], [47], [37], [-22], [53], [6], [5], [-30], [59], [-4], [-42], [-42], [63], [19], [-53], [59], [-38], [6], [-25], [29], [-32], [-52], [-37], [34], [-50], [-60], [6], [-75], [-46], [-24], [26], [-56], [52], [28], [58], [46], [-76], [70], [36], [-8], [-48], [-22], [-60], [0], [-25], [-10], [1], [74], [72], [7], [-85], [-76], [35], [-21], [7], [8], [-54], [-43], [34], [-78], [44], [48], [-63], [21], [-21], [-42], [-50], [68], [-74], [68], [29], [-14], [67], [4], [69], [-63], [-70], [46], [71], [12], [20], [52], [12], [-54], [-16], [26], [8], [1], [7], [-1], [-32], [-18], [22], [77], [13], [60], [63], [-56], [3], [-51], [-28], [55], [-54], [23], [12], [-22], [26], [-6], [30], [0], [28], [-2], [-42], [60], [25], [-75], [-58], [92], [-8], [36], [93], [-42], [1], [-24], [-91], [-50], [-32], [-22], [54], [64], [22], [20], [-42], [54], [-16], [76], [29], [44], [-6], [-5], [-82], [31], [-68], [-86], [-30], [1], [58], [-84], [-13], [-69], [60], [33], [-51], [-46], [-38], [-49], [-79], [-18], [7], [33], [1], [-64], [-58], [81], [106], [-60], [-41], [100], [95], [54], [-56], [34], [-84], [-19], [-51], [-92], [-40], [-32], [75], [-1], [18], [-55], [-20], [-18], [-40], [28], [54], [60], [83], [20], [-76], [62], [42], [-84], [-82], [-86], [-10], [41], [12], [102], [-6], [-109], [14], [86], [54], [-82], [-40], [47], [-76], [14], [25], [-95], [-33], [-75], [4], [-60], [60], [32], [-78], [-29], [-58]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_7800_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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