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