
// Make newform 6498.2.a.m in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6498_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_6498_2_a_m();".
// 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_6498_2_a_m();".
// 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_6498_a();"
function MakeCharacter_6498_a()
    N := 6498;
    order := 1;
    char_gens := [3611, 6139];
    v := [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_6498_a_Hecke(Kf)
    return MakeCharacter_6498_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 := [[1], [0], [-2], [-3], [2], [3], [1], [0], [-5], [-3], [6], [-6], [12], [-10], [8], [-3], [3], [0], [-15], [0], [-11], [12], [-2], [6], [-12], [-10], [-6], [-3], [-3], [12], [-12], [-14], [-19], [6], [8], [18], [0], [-6], [-12], [-18], [-12], [-18], [11], [-6], [-4], [-7], [-3], [-18], [3], [-12], [-14], [-1], [-24], [20], [-18], [8], [-6], [-11], [30], [12], [-14], [-9], [-12], [11], [21], [-33], [-9], [-18], [-16], [28], [31], [-19], [8], [21], [-3], [18], [-26], [2], [36], [-6], [14], [27], [24], [-30], [0], [22], [6], [1], [-4], [32], [-8], [40], [18], [-8], [18], [-1], [18], [-12], [-9], [2], [36], [-22], [-24], [-24], [-32], [15], [28], [2], [-36], [6], [12], [18], [-10], [-24], [0], [18], [32], [23], [10], [-15], [-15], [48], [3], [-36], [-8], [40], [-8], [43], [-35], [-22], [-12], [6], [-12], [12], [13], [-15], [15], [9], [33], [11], [-33], [-2], [3], [15], [-51], [-18], [46], [24], [54], [48], [-27], [-34], [-24], [-24], [15], [-30], [15], [-41], [-47], [-27], [46], [-30], [-24], [-48], [-30], [-24], [-6], [6], [6], [-62], [-3], [39], [-16], [-9], [0], [-26], [30], [10], [-42], [8], [35], [45], [54], [-18], [-29], [2], [45], [-12], [24], [11], [6], [28], [12], [15], [-50], [54], [13], [-44], [-11], [61], [-66], [18], [27], [-24], [-38], [-68], [-7], [51], [-5], [-3], [-27], [38], [44], [45], [-66], [42], [49], [-18], [-48], [58], [-18], [30], [-24], [45], [33], [-42], [0], [-42], [-36], [-32], [-72], [57], [36], [-36], [-12], [-7], [-58], [4], [-26], [60], [36], [-16], [-49], [15], [36], [-32], [24], [-60], [60], [31], [42], [-55], [-25], [30], [-64], [26], [-20], [-48], [54], [-42], [6], [24], [-40], [-21], [38], [45], [-45], [17], [-33], [58], [3], [15], [9], [-21], [-48], [3], [-70], [-69], [-18], [10], [72], [69], [-61], [-81], [22], [28], [-3], [78], [51], [18], [52], [52], [-54], [36], [42], [-14], [-48], [-56], [-39], [36], [-42], [-81], [-79], [-9], [60], [48], [75], [-15], [52], [-2], [16], [44], [36], [-36], [-40], [48], [-20], [30], [55], [70], [-33], [-77], [45], [6], [49], [0], [-15], [-21], [60], [-56], [78], [15], [-15], [16], [-70], [62], [-51], [-30], [3], [79], [-15], [41], [21], [84], [45], [-60], [12], [-9], [90], [-76], [-60], [-18], [-80], [-30], [-18], [-96], [-25], [74], [-83], [-6], [-8], [38], [18], [-45], [72], [24], [-8], [-84], [-42], [62], [15], [95], [57], [40], [-52], [21], [56], [-2], [-53], [-12], [30], [-50], [43], [-70], [-75], [-6], [-1], [-30], [-6], [-36], [-90], [-9], [-4], [-24], [-84], [90], [-66], [66], [-48], [40], [30], [38], [12], [-84], [-78], [61], [-10], [-72], [-69], [2], [6], [-64], [75], [8], [-23], [-46], [90], [-30], [37], [36], [-66], [-39], [5], [-15], [-12], [63], [15], [25], [-48], [59]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6498_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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