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