
// Make newform 2646.2.a.y in Magma, downloaded from the LMFDB on 16 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_y();".
// 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_y();".
// 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], [0], [0], [0], [-5], [-3], [-2], [-9], [-3], [-5], [2], [6], [-1], [6], [3], [3], [10], [-13], [9], [-2], [-10], [12], [-15], [-8], [-18], [13], [-12], [-16], [12], [20], [9], [18], [-14], [9], [-10], [-23], [11], [-12], [24], [-24], [7], [12], [5], [18], [-11], [-13], [-8], [-9], [-14], [-12], [12], [10], [0], [-18], [9], [24], [7], [8], [6], [4], [6], [-2], [-24], [10], [30], [-19], [-13], [-30], [1], [27], [-3], [19], [14], [-16], [0], [18], [-2], [-18], [-14], [15], [-10], [-24], [16], [-35], [-6], [0], [35], [30], [-22], [-24], [-36], [38], [12], [-4], [-36], [18], [-3], [-20], [-16], [44], [21], [3], [-24], [5], [34], [-33], [-42], [-3], [-8], [-5], [38], [-42], [10], [-16], [36], [40], [-30], [-9], [-12], [-14], [-1], [-12], [12], [28], [-18], [-28], [-24], [-17], [-23], [20], [3], [14], [-16], [-9], [40], [24], [-14], [6], [-24], [-2], [39], [-22], [-18], [-2], [-42], [37], [-9], [-14], [33], [32], [27], [-7], [-36], [-28], [24], [2], [-30], [-56], [30], [-6], [-36], [-4], [57], [42], [0], [2], [19], [11], [-36], [-54], [-23], [-27], [-1], [-32], [15], [53], [3], [-56], [55], [8], [3], [32], [39], [-3], [54], [-52], [-8], [5], [42], [-56], [6], [-7], [-48], [-42], [39], [-67], [-10], [-51], [48], [-39], [-35], [22], [-62], [-9], [60], [43], [36], [-48], [-50], [47], [-30], [-10], [-57], [-6], [28], [-22], [-18], [15], [3], [14], [40], [0], [-52], [-12], [-10], [-39], [-24], [37], [-6], [-4], [16], [50], [60], [-8], [36], [58], [69], [-42], [18], [-54], [-2], [-59], [-52], [69], [-30], [61], [12], [-13], [9], [-4], [3], [-57], [10], [54], [42], [-16], [28], [-36], [-20], [-46], [-12], [-50], [-2], [-51], [-80], [18], [30], [-31], [6], [-59], [-52], [-44], [-4], [-50], [61], [-54], [26], [38], [-39], [-42], [50], [-24], [55], [76], [-48], [38], [-15], [-35], [9], [-18], [15], [-36], [15], [-46], [30], [-80], [-42], [75], [-62], [22], [51], [38], [-6], [41], [-7], [72], [49], [0], [-16], [-12], [-15], [-42], [77], [-45], [52], [3], [24], [-44], [12], [10], [-58], [72], [-34], [-72], [-20], [-7], [-62], [-69], [-54], [8], [-30], [28], [36], [11], [27], [38], [9], [58], [88], [-46], [6], [-48], [-10], [-15], [54], [7], [-1], [12], [48], [40], [-25], [39], [-53], [-76], [54], [36], [60], [18], [-48], [-28], [-39], [-33], [-6], [-38], [98], [12], [20], [29], [18], [-20], [45], [51], [13], [74], [-69], [27], [52], [-6], [82], [90], [-60], [-52], [-18], [-32], [6], [62], [49], [-7], [96], [-1], [60], [30], [-98], [57], [77], [-29], [21], [59], [51], [-41], [-12], [86], [-9], [24], [-56], [80], [-12], [4], [33], [82], [75], [36], [-55], [59], [30], [-57], [64], [-63], [-54], [-81], [7], [-48], [-3], [-92], [-24], [84], [72], [-38], [36]];
    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_y();".
// 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_y(: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_y();".
// 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_y( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2646_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![0, 1]>,<11,R![0, 1]>,<13,R![5, 1]>,<17,R![3, 1]>],Snew);
    return Vf;
end function;