
// Make newform 2738.2.a.k in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2738_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_2738_2_a_k();".
// 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_2738_2_a_k();".
// 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 := [-5, -1, 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_2738_a();"
function MakeCharacter_2738_a()
    N := 2738;
    order := 1;
    char_gens := [1371];
    v := [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_2738_a_Hecke(Kf)
    return MakeCharacter_2738_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, 1], [-1, -1], [-2, 0], [-1, -1], [-2, 1], [4, -2], [-2, -2], [-2, 1], [-2, 1], [3, -3], [0, 0], [-8, 1], [-6, 0], [-4, 2], [2, 2], [-2, -2], [-11, 1], [-1, 3], [-2, 4], [-1, -3], [10, 1], [8, -4], [-6, 0], [-10, 2], [-4, 2], [-5, 1], [0, 3], [6, 0], [14, 2], [8, 0], [-6, 6], [-9, -3], [8, -3], [-8, -2], [4, -6], [-2, 0], [-16, 2], [-13, 5], [-8, 4], [2, 2], [-2, -6], [3, -3], [-4, 8], [-2, 10], [8, -4], [5, -3], [14, 0], [-8, 10], [-4, -6], [0, -9], [11, 5], [8, 2], [-8, -2], [6, 6], [20, -4], [6, -6], [22, 0], [15, 3], [0, 0], [-24, 0], [-6, 0], [4, 3], [18, -3], [-22, -4], [18, -6], [-22, 2], [-17, 9], [-4, 2], [10, 0], [-2, -2], [-16, -4], [-16, -6], [2, -6], [4, -9], [-4, 8], [6, -15], [-4, 6], [6, -6], [-20, 4], [4, 1], [10, -5], [16, 4], [16, 3], [-18, -3], [-19, 5], [-10, -4], [0, 12], [-2, -8], [10, -11], [20, 2], [2, -1], [8, -16], [0, -3], [0, -6], [21, 3], [-8, -2], [2, -4], [8, -4], [-11, 1], [8, -10], [-11, -11], [0, -6], [38, -4], [-20, -3], [-10, 2], [-2, 10], [-17, 1], [16, 4], [-17, 9], [-14, 7], [28, -12], [32, 5], [19, -9], [-10, 5], [-16, -7], [18, -12], [2, 11], [-6, -3], [-20, -5], [-34, -1], [-20, 9], [-2, 4], [-28, 2], [-4, 12], [-5, -5], [25, 1], [-14, 4], [-3, -9], [-26, 12], [19, -9], [-8, 4], [20, -12], [-37, -1], [-16, -1], [26, 8], [14, -10], [8, 0], [-30, 3], [28, -14], [7, 15], [-4, -4], [-16, 0], [26, -4], [-26, -5], [18, 12], [-25, 11], [26, -4], [10, 16], [-30, -6], [22, 6], [-3, -9], [-14, 10], [22, -20], [-16, 8], [32, 8], [-36, 0], [-3, 3], [8, -15], [0, 12], [-12, -6], [-1, 17], [-13, 5], [45, -3], [-16, 14], [4, -8], [-10, 5], [-18, 0], [11, 9], [-13, -1], [-20, 4], [-18, 3], [25, -5], [8, -9], [8, 12], [4, -8], [-12, 12], [48, 0], [-2, 18], [-4, -6], [8, 20], [-30, 0], [9, -3], [14, -16], [6, 0], [-46, -4], [2, 6], [-16, -10], [-4, -10], [40, -2], [-2, 16], [-36, 0], [-52, 2], [-20, -8], [-59, 1], [-50, 4], [52, -2], [-22, 8], [16, 7], [39, -3], [-9, 15], [-22, -6], [-22, 0], [-16, -9], [24, -9], [-33, 15], [2, 18], [-9, 15], [-4, -22], [-8, 0], [-8, -14], [-42, 12], [10, -11], [9, 9], [-45, 9], [13, -21], [-3, -9], [10, 10], [9, 3], [36, 0], [16, 6], [-38, -12], [10, -20], [30, 9], [13, -11], [-26, 7], [36, -15], [40, -20], [38, -6], [-18, -12], [40, -6], [-19, 9], [10, 18], [-7, -1], [-29, 15], [38, -10], [20, 15], [18, -9], [-52, 8], [47, -7], [46, -2], [10, 10], [-44, 18], [-1, -25], [-36, -3], [-18, -21], [26, -19], [58, -8], [44, -9], [8, 8], [-15, -3], [-3, 15], [18, 0], [32, 2], [-14, 13], [-28, -7], [-4, 12], [8, 24], [-8, -14], [-18, 6], [22, 7], [-10, 8], [-2, 12], [40, 6], [22, 16], [-56, -9], [-16, -4], [12, 18], [35, -9], [-22, 23], [23, -13], [-22, 14], [36, 0], [14, 5], [-4, -3], [20, -6], [-12, -21], [3, 3], [-8, -9], [-16, -4], [-56, 10], [24, -24], [42, 6], [-68, 6], [-52, 14], [-34, 2], [-10, -16], [12, 0], [-11, -5], [14, 20], [18, 12], [12, 6], [10, 4], [8, -16], [34, 0], [16, -26], [-16, -6], [36, -12], [22, -8], [-8, -9], [-26, -8], [38, 14], [52, -12], [-16, 20], [-68, -2], [18, 24], [42, 6], [2, 17], [-26, -14], [26, -25], [14, 2], [4, 16], [-1, -7], [17, 9], [-38, 1], [-6, 12], [16, 4], [14, -19], [16, -15], [-52, -4], [2, -22], [1, -9], [-33, 3], [4, 0], [-17, 1], [22, -2], [-52, -3], [-48, -6], [29, -1], [18, 18], [-14, 6], [-11, 13], [-5, 1], [16, -2], [18, 12], [-57, 15], [-50, 0], [10, 4], [6, 24], [-58, 0], [-26, 12], [4, 13], [46, 13], [-35, -5], [-74, -5], [14, 2], [-50, 12], [4, 24], [62, -7], [16, 22], [4, 9], [-7, 15], [-62, -8], [48, -12], [40, 0], [0, 21], [-18, -3], [-10, 20], [41, -13], [-35, 31], [-14, -23], [-35, -17], [17, -25], [-20, -14], [-56, 3], [-20, 16], [18, 9], [13, -17], [52, -2], [42, -12], [56, -10], [18, 18], [48, -18], [14, 11], [-28, 30], [72, -12], [-52, 14], [44, -3], [78, -12], [-32, -21], [22, -17], [-46, -10], [-53, 1], [21, -9], [-8, 22], [16, 10], [8, -6], [35, -13], [-4, -22], [9, 3], [-22, 12], [-35, 1], [-14, 10], [-62, 4], [-26, -2], [46, -12], [-23, 7], [47, -7], [7, -9], [0, 18], [20, 0], [24, -18], [18, -15], [24, -6], [-34, 23], [38, -6], [47, -25], [21, -9], [32, 9], [16, 19], [-10, -24], [-4, 26], [6, 0], [4, -14], [-28, -10], [54, -12], [36, -6], [-26, -6], [54, -12], [-26, -5], [19, 7], [-33, -9], [18, -6], [4, 16], [-14, 0], [-28, -4], [3, 3], [-43, 23], [-47, -15], [-4, 8]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2738_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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