
// Make newform 1274.2.a.p in Magma, downloaded from the LMFDB on 29 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1274_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_1274_2_a_p();".
// 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_1274_2_a_p();".
// 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_1274_a();"
function MakeCharacter_1274_a()
    N := 1274;
    order := 1;
    char_gens := [885, 197];
    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_1274_a_Hecke(Kf)
    return MakeCharacter_1274_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, 1], [-2, -1], [0, 0], [1, 1], [1, 0], [-4, 0], [-2, -1], [1, -3], [-2, 1], [-7, -1], [-3, -2], [-7, 0], [-8, -1], [3, 3], [-2, 7], [6, 1], [-5, -2], [-13, 1], [4, 3], [3, 0], [-7, -1], [0, -7], [-4, 10], [3, 8], [7, -8], [-2, 6], [8, -3], [8, 8], [3, 12], [-3, -11], [10, -4], [-2, 0], [12, 4], [-7, -8], [4, -12], [-5, -12], [6, 8], [8, -2], [-8, 4], [2, 6], [-7, 0], [-8, -6], [4, -12], [-1, -8], [-12, 9], [-14, -3], [19, -3], [4, 12], [-6, 7], [13, -6], [2, -14], [-12, 2], [-1, 9], [0, -12], [-16, 0], [11, -2], [-3, 15], [0, -11], [-30, 1], [-9, -11], [14, 0], [20, -5], [-10, -9], [-6, 0], [7, -8], [3, 21], [25, -4], [12, 0], [-12, -11], [9, 2], [8, 11], [-12, -5], [-22, 1], [8, 2], [-19, -9], [12, 0], [-4, 24], [-10, 5], [-18, 0], [19, -3], [19, 8], [-14, 10], [-2, 4], [-2, -1], [-8, -13], [20, 13], [-12, 9], [-22, 9], [-8, 20], [-4, 10], [-8, -20], [22, -13], [-8, 13], [3, -7], [-12, -4], [12, 13], [-28, -1], [-9, -15], [24, -14], [20, 13], [-5, -8], [29, 11], [-5, -26], [26, -7], [-20, 4], [26, -10], [4, -6], [-13, 1], [-24, -1], [-30, -1], [15, -20], [-16, -13], [14, 13], [2, -14], [-13, -6], [4, 20], [8, -3], [-2, -11], [-22, -1], [-30, -11], [3, 26], [-33, -2], [19, -7], [-16, -8], [46, 3], [15, -10], [2, -30], [10, -6], [-12, 12], [0, 10], [28, 4], [-33, 9], [-22, -8], [-13, 0], [-41, -2], [-14, -8], [-8, -20], [1, 12], [20, -2], [-16, 4], [8, 18], [-43, -9], [-12, -14], [24, -14], [-25, -13], [-26, -11], [12, 3], [-15, 11], [-12, 16], [21, 6], [-22, -12], [10, 26], [14, 9], [6, -30], [-4, -2], [-5, 23], [11, -34], [-8, 16], [16, -5], [0, 6], [12, -6], [6, -22], [-3, 15], [34, -16], [54, -4], [-1, 33], [-9, 10], [20, -8], [10, 24], [-30, 0], [-20, -9], [15, -3], [-6, 1], [2, 30], [32, 20], [29, -19], [4, -12], [-12, -4], [-17, 8], [42, 13], [15, -23], [18, -4], [31, -4], [-44, 0], [20, -20], [-12, 20], [20, 8], [8, 4], [-27, -3], [24, -4], [-3, 45], [-34, -10], [22, 0], [-14, 31], [10, 0], [20, 12], [42, -8], [48, -12], [24, -4], [25, -12], [-28, 0], [-18, 3], [-8, -27], [34, 9], [-24, 12], [36, -14], [-34, 18], [-18, 12], [-27, 11], [-1, 8], [21, -14], [-5, 7], [24, -4], [15, 25], [3, -32], [-17, -5], [24, 28], [50, -10], [-27, 16], [-16, 17], [-27, -9], [-2, 31], [18, -9], [-21, -35], [-10, -25], [22, 21], [11, 37], [-8, -45], [-7, -43], [-50, 5], [-36, -20], [-16, 8], [-35, -18], [-5, 31], [-27, 11], [13, 4], [-35, 20], [-16, -13], [-16, 4], [9, -15], [-59, 1], [12, 24], [-38, -16], [-5, -18], [-34, 21], [17, -27], [42, -15], [1, -13], [-47, -7], [19, -6], [-28, 30], [2, 41], [50, -16], [11, -16], [22, 4], [-50, 12], [38, -14], [-49, -14], [9, -16], [6, 12], [18, 10], [-12, -5], [-13, 26], [-13, -24], [26, 20], [-25, -2], [-53, -6], [-11, 47], [-12, -8], [35, 2], [1, 29], [3, 28], [48, 8], [-24, 27], [32, -20], [-20, -24], [31, 18], [-2, -25], [12, 20], [26, 31], [18, -5], [-34, 19], [-8, 37], [0, -12], [-21, -29], [-17, 30], [-24, -18], [-53, 7], [-18, 1], [36, 15], [-55, 11], [6, 8], [-18, 15], [34, -8], [-36, 36], [-44, 27], [-21, -42], [37, -23], [42, -7], [-4, 17], [5, 38], [8, 36], [4, 45], [28, -36], [-2, 1], [-14, -18], [-8, 2], [-18, -14], [-4, 0], [2, 33], [-8, -44], [13, -20], [-18, 18], [-30, 1], [10, -7], [16, -32], [14, 45], [16, -32], [23, 14], [-25, 21], [42, -15], [-25, -6], [18, 3], [12, -35], [2, 12], [13, -27], [4, 29], [0, 6], [0, -3], [-67, 14], [-30, 7], [-38, -15], [56, -8], [-51, -21], [6, 30], [-64, 6], [-45, 22], [-8, -40], [42, -19], [-18, -24], [-27, -12], [-42, 28], [-37, -27], [29, 4], [12, 1], [-28, -36], [30, 16], [9, 3], [-3, 64], [-34, 14], [38, 11], [5, -6], [-26, 30], [-24, -9], [10, 43], [-35, -33], [38, -14], [-53, 28], [-40, 35], [-20, -12], [22, -28], [-11, 39], [45, 21], [47, -11], [15, 4], [-8, 44], [-18, 18], [3, -44], [-54, 27], [-13, -21], [-22, 22], [-22, 41], [80, 0], [-26, 51], [-40, 37], [29, 9], [9, -26], [-48, 0], [68, -24], [32, 12], [-64, -8], [4, -11], [0, -60], [-8, 44], [-48, -10], [26, -7], [11, 12], [-68, 2], [-48, -16], [-84, 11], [14, -12], [-43, -9], [36, 0], [14, -22], [-17, 58], [1, -45], [50, -9], [66, -2], [18, -40], [32, -20], [52, 0], [16, 44], [19, 34], [-38, 5], [0, 72], [-17, 44], [-4, 12], [44, 23], [48, 39], [18, -20], [-12, 16], [54, -34], [-8, -20], [37, -42], [50, 0], [14, 27], [24, -56], [-69, 12], [26, 34], [6, -44], [24, 3], [12, -20], [-20, -30], [64, -16], [-28, -36], [10, -7], [58, -7], [50, -10], [-10, -30]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1274_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1274_2_a_p();".
// 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_1274_2_a_p(:prec:=2)
    chi := MakeCharacter_1274_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_1274_2_a_p();".
// 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_1274_2_a_p( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1274_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, 2, 1]>,<5,R![2, 4, 1]>,<11,R![-1, -2, 1]>,<17,R![4, 1]>],Snew);
    return Vf;
end function;