
// Make newform 1274.2.a.c in Magma, downloaded from the LMFDB on 22 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_c();".
// 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_c();".
// 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_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], [-1], [2], [0], [1], [1], [-4], [-4], [-5], [6], [-11], [7], [-3], [2], [-3], [-2], [-4], [-5], [1], [-2], [-7], [1], [10], [-6], [-17], [9], [-6], [-10], [2], [5], [-17], [-8], [10], [-20], [-21], [-16], [-5], [12], [-12], [26], [14], [-7], [-12], [24], [19], [-26], [24], [15], [12], [22], [19], [-6], [14], [-9], [28], [-24], [19], [15], [-12], [-2], [-1], [10], [-10], [8], [10], [3], [-31], [-31], [0], [-16], [21], [26], [-10], [-30], [4], [23], [24], [28], [30], [-18], [-5], [-15], [6], [2], [-20], [6], [-20], [28], [-14], [16], [-32], [-24], [-44], [14], [23], [12], [-36], [40], [-17], [10], [26], [31], [19], [-27], [28], [2], [6], [-26], [37], [-28], [-8], [-11], [-24], [0], [-2], [-45], [44], [18], [-6], [44], [30], [-29], [-39], [25], [8], [-14], [45], [-34], [26], [-8], [36], [0], [-25], [-2], [17], [5], [42], [8], [9], [-42], [-56], [-34], [35], [-24], [-50], [15], [10], [28], [33], [56], [25], [-26], [-26], [-16], [-14], [24], [-39], [1], [-52], [12], [20], [30], [-22], [-27], [-6], [-56], [-23], [-41], [-12], [26], [12], [12], [-25], [-10], [10], [-52], [43], [-54], [36], [25], [-16], [-63], [34], [-21], [24], [42], [0], [40], [-20], [-3], [56], [5], [30], [-2], [-16], [-50], [4], [-42], [30], [56], [-11], [-28], [-18], [8], [-28], [-30], [40], [-54], [-22], [67], [-7], [-13], [-47], [-48], [35], [-23], [19], [32], [-38], [-15], [-24], [25], [2], [4], [-51], [-26], [-6], [-51], [54], [39], [-26], [-28], [-12], [-45], [33], [-63], [-49], [-9], [-26], [-60], [-33], [1], [44], [42], [-15], [76], [9], [44], [-67], [29], [1], [-70], [-12], [22], [-67], [0], [58], [-6], [-33], [-45], [-32], [-30], [-4], [51], [41], [-64], [-27], [33], [-41], [-52], [3], [-17], [9], [40], [44], [8], [36], [19], [-38], [-12], [-40], [72], [72], [8], [8], [15], [-39], [-46], [35], [30], [68], [-33], [-54], [-80], [-58], [-30], [-26], [85], [-47], [32], [12], [39], [0], [-6], [12], [-86], [-42], [-46], [-66], [-16], [72], [54], [21], [-2], [68], [-46], [24], [60], [82], [57], [57], [-78], [-17], [56], [-12], [22], [-59], [-66], [60], [36], [-23], [38], [72], [-12], [-73], [-54], [10], [-9], [-54], [-88], [-26], [-3], [54], [-83], [81], [50], [38], [-84], [-69], [-69], [-26], [-2], [9], [-46], [8], [-18], [71], [94], [-45], [-26], [26], [42], [-21], [-75], [47], [-9], [24], [-46], [-19], [-44], [-31], [38], [-34], [44], [26], [-18], [-39], [-35], [-28], [-96], [20], [-44], [-58], [-100], [24], [-52], [72], [77], [48], [12], [84], [10], [55], [52], [-66], [23], [83], [34], [-50], [-10], [8], [-78], [-76], [39], [82], [40], [-53], [-36], [58], [62], [-26], [-24], [30], [88], [-13], [30], [-48], [36], [-23], [6], [22], [-32], [16], [-56], [74], [-68], [-96], [58], [70], [-90]];
    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_c();".
// 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_c(:prec:=1)
    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_c();".
// 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_c( : 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, 1]>,<5,R![-2, 1]>,<11,R![-1, 1]>,<17,R![4, 1]>],Snew);
    return Vf;
end function;