
// Make newform 2646.2.a.n in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2646_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_2646_2_a_n();".
// 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_2646_2_a_n();".
// 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_2646_a();"
function MakeCharacter_2646_a()
    N := 2646;
    order := 1;
    char_gens := [785, 1081];
    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_2646_a_Hecke(Kf)
    return MakeCharacter_2646_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], [3], [0], [3], [4], [6], [7], [-3], [0], [-5], [-7], [9], [-10], [-6], [12], [6], [-8], [-4], [9], [-2], [-10], [0], [-15], [-8], [6], [-5], [-12], [11], [-18], [20], [-6], [12], [4], [6], [-10], [4], [-16], [18], [21], [-24], [-2], [15], [14], [-18], [7], [14], [1], [6], [4], [6], [0], [-26], [0], [21], [-15], [15], [16], [17], [18], [4], [-18], [-29], [-12], [28], [-30], [-28], [-13], [3], [10], [-21], [-12], [-17], [-13], [2], [30], [12], [-2], [24], [22], [-18], [17], [3], [16], [-8], [-3], [18], [-19], [-27], [14], [-6], [24], [2], [-9], [-22], [0], [30], [15], [-29], [-7], [-28], [-42], [24], [6], [14], [-2], [-18], [-33], [-33], [28], [4], [29], [18], [-35], [38], [-6], [-5], [6], [6], [-9], [40], [-46], [-15], [-15], [28], [0], [35], [30], [-8], [22], [-34], [9], [-22], [2], [-42], [4], [-39], [-32], [-27], [-24], [25], [-30], [14], [9], [34], [0], [-8], [-3], [31], [24], [14], [-9], [-16], [-24], [-46], [0], [-52], [-6], [-20], [15], [-39], [0], [-58], [48], [12], [6], [-16], [10], [-7], [-15], [12], [-14], [24], [-37], [-5], [-9], [44], [30], [-11], [28], [-10], [54], [23], [-42], [-33], [-39], [-34], [28], [-13], [24], [-20], [-36], [-34], [-42], [39], [15], [5], [26], [9], [-6], [12], [1], [-50], [-8], [36], [-30], [-20], [-39], [18], [4], [65], [-9], [26], [-42], [-12], [-8], [32], [51], [33], [0], [50], [-32], [24], [2], [54], [17], [21], [75], [55], [51], [59], [-11], [-40], [30], [-17], [-12], [-50], [60], [-36], [0], [75], [25], [49], [47], [-27], [-6], [7], [60], [-40], [27], [14], [66], [-24], [10], [33], [33], [47], [-35], [54], [-74], [62], [45], [-32], [34], [-21], [1], [36], [54], [14], [-60], [-50], [56], [28], [-40], [-14], [-11], [-3], [-46], [-70], [12], [78], [-58], [18], [-26], [-5], [-3], [65], [54], [1], [9], [-6], [-66], [-84], [-18], [-1], [-75], [1], [18], [-24], [-53], [-50], [-48], [-70], [9], [14], [47], [-15], [58], [-3], [83], [-6], [-6], [60], [32], [-81], [-56], [-72], [-87], [-8], [54], [28], [-31], [45], [74], [12], [16], [-34], [-35], [-84], [18], [17], [-42], [55], [-48], [-16], [36], [29], [-15], [94], [-47], [-37], [-72], [-54], [-64], [78], [36], [-74], [-28], [72], [-42], [85], [38], [-36], [-35], [23], [-9], [66], [-84], [42], [0], [26], [21], [-24], [51], [-92], [-91], [-81], [-34], [11], [-51], [43], [96], [-54], [-59], [-7], [42], [0], [-38], [39], [64], [69], [-18], [-52], [30], [-5], [42], [-10], [-32], [92], [-72], [-10], [39], [-51], [-8], [-48], [-13], [79], [-45], [50], [12], [-50], [-48], [14], [21], [51], [-83], [-91], [-72], [103], [48], [28], [-102], [36], [44], [14], [39], [15], [64], [18], [102], [12], [88], [63], [-18], [70], [78], [60], [72], [97], [-84]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2646_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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