
// Make newform 2646.2.a.bg 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_bg();".
// 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_bg();".
// 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
        poly := [-2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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, 1], [0, 0], [2, -3], [-3, 0], [3, 1], [-6, 1], [1, 0], [1, 3], [-3, 3], [-2, -6], [0, -2], [-1, 3], [0, -5], [-7, -3], [3, -2], [-6, -4], [9, 3], [-3, 6], [0, -7], [2, -9], [6, 8], [-9, 5], [-12, -2], [-6, -3], [-9, 5], [-8, 6], [-8, 6], [0, 9], [-8, 9], [-9, 6], [18, -3], [-6, -8], [5, 3], [2, 9], [9, 4], [-17, -3], [6, -7], [-12, -6], [-18, -3], [-15, 6], [10, 12], [5, 0], [-2, -6], [-9, -3], [15, 3], [0, 12], [-3, -12], [0, 0], [0, -6], [-6, -12], [-12, 9], [-6, -16], [-6, -12], [15, -6], [-12, 11], [-3, 7], [4, -6], [14, 6], [-12, -4], [-30, 2], [18, -4], [-12, 8], [0, 3], [-10, 0], [-11, 9], [17, 6], [6, -6], [3, 0], [3, -11], [5, 12], [15, -1], [4, 21], [-2, -12], [-6, 11], [22, 12], [-12, -2], [-6, -6], [-6, -6], [21, 4], [-36, -3], [-18, -12], [24, 6], [9, -15], [10, 12], [-12, 9], [-15, 0], [-6, 5], [-2, -15], [18, 4], [6, -17], [-18, -3], [6, 3], [8, 12], [18, -7], [6, -10], [9, 1], [24, -9], [-2, 9], [4, -6], [-15, 21], [27, 10], [20, -15], [1, -3], [-6, 6], [15, 18], [-6, -10], [7, 0], [30, 3], [3, 21], [24, 3], [2, 24], [-6, -2], [8, -3], [16, -18], [24, -5], [42, 0], [-3, -21], [0, 0], [12, -24], [-3, -18], [0, 21], [18, -3], [-12, -15], [-18, 12], [-2, 15], [12, 16], [3, 31], [3, 24], [6, 12], [-13, -6], [-22, 0], [2, 21], [-21, 23], [6, 15], [0, -13], [30, 2], [-42, 6], [4, 0], [-18, -24], [3, -15], [10, -12], [0, -21], [-24, 2], [18, -14], [-3, -30], [45, -1], [6, 26], [-11, 6], [-4, 6], [-21, 9], [-25, -9], [36, 2], [10, -24], [-26, -6], [10, 3], [30, -8], [30, -5], [-6, -20], [24, -9], [20, 21], [0, 39], [-9, -26], [-6, 33], [6, 11], [14, -18], [33, -10], [31, -12], [-24, -10], [-30, 6], [-27, 2], [-3, 24], [-5, 6], [-18, -28], [33, -5], [-9, 21], [33, 21], [-6, -12], [-9, -8], [-8, -24], [3, -16], [6, 9], [39, 11], [-1, 12], [-30, 2], [30, 9], [-36, 0], [5, -18], [-18, -10], [0, -18], [0, -9], [-1, -15], [-48, -14], [24, 27], [-3, -11], [-3, 24], [-20, 3], [-27, -27], [6, 7], [39, 21], [-3, -3], [0, -8], [-6, 22], [45, -2], [-36, -4], [-45, -13], [-6, -9], [-32, -3], [-6, 15], [-23, -24], [-30, -3], [34, -12], [-15, 16], [24, -7], [-18, 13], [-22, 3], [18, 20], [45, -12], [3, -39], [32, -15], [-18, -12], [12, -30], [-28, -9], [-6, 10], [-14, -12], [-3, 35], [8, -12], [33, -5], [18, 24], [-22, 9], [-60, -6], [46, -18], [-24, -12], [-12, 23], [12, -18], [-42, 0], [13, 3], [-14, -6], [-12, 43], [-36, 3], [30, 15], [9, 3], [-12, 6], [27, 1], [60, -5], [-9, -43], [-6, -46], [-7, 21], [43, 0], [20, 18], [-15, -9], [13, 6], [-18, -40], [-18, 11], [-64, 9], [4, 0], [36, -11], [0, 31], [0, -4], [10, 27], [-64, -12], [-18, 12], [-12, 28], [3, 33], [-24, -15], [30, -12], [-18, -18], [-37, 3], [32, -6], [9, -12], [-56, -18], [-48, -24], [16, 30], [54, -4], [-33, -7], [30, 6], [14, -12], [8, -6], [3, 46], [-12, 25], [-2, -3], [48, -22], [-33, 0], [-12, -23], [24, -12], [28, 18], [-59, -9], [-9, -1], [57, 19], [2, -18], [45, -26], [4, -9], [21, 26], [8, -15], [-6, -1], [12, -44], [-18, -47], [27, 16], [54, 22], [42, -12], [-9, 9], [-30, 3], [36, 15], [35, 33], [51, 6], [-30, -9], [-51, -14], [24, -12], [-38, 9], [-36, 2], [9, 21], [58, -15], [-11, -3], [27, 12], [-30, -17], [51, 4], [-22, -30], [0, -34], [12, 36], [6, -26], [-2, 24], [12, 2], [6, -18], [-36, -6], [-30, 29], [-37, 33], [54, 6], [-53, -18], [-20, -30], [36, 18], [26, 6], [60, 14], [-30, -2], [-25, -3], [-27, 22], [-6, -54], [33, 41], [12, 27], [24, 18], [-46, 0], [34, -3], [-24, 44], [42, -12], [-5, -15], [-38, 18], [-27, -24], [7, -3], [-18, -41], [-48, 30], [-12, -15], [-13, -36], [35, -15], [-21, -31], [-28, 0], [-18, 28], [-54, -1], [-42, 14], [-42, 12], [-46, -6], [-2, 9], [21, -41], [33, -36], [-6, 42], [66, 0], [40, 30], [-24, 11], [-8, 18], [35, 0], [12, 9], [12, 48], [47, -6], [-15, -51], [-15, -27], [-18, 30], [-33, 6], [-93, 0], [-18, -21], [-6, 36], [24, -45], [-18, -13], [-28, 18], [-24, 33], [-26, -30], [24, -5], [-60, -9], [18, -57], [33, 12], [-17, 21], [-12, 0], [11, -30], [-24, -39], [-62, -18], [6, 6], [5, -6], [-3, 6], [9, 35], [57, 23], [-9, -33], [-79, 15], [-69, 6], [12, -36], [-46, -30], [33, -25], [-24, -21], [84, 2], [-56, 24], [-4, 54], [48, 3], [3, -6], [-6, 42], [-9, 45], [30, 39], [-23, 15], [-17, 18], [-18, -27], [63, -6], [-36, -28], [33, -49], [-18, -8], [-33, -3], [51, -22], [30, 18], [-15, -14], [-6, 21], [-72, 23], [18, -51], [-44, -21], [-30, -52], [-6, -27]];
    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_bg();".
// 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_bg(:prec:=2)
    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_bg();".
// 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_bg( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2646_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-2, 0, 1]>,<11,R![-14, -4, 1]>,<13,R![3, 1]>,<17,R![7, -6, 1]>],Snew);
    return Vf;
end function;