
// Make newform 1143.2.a.c in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1143_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_1143_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_1143_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_1143_a();"
function MakeCharacter_1143_a()
    N := 1143;
    order := 1;
    char_gens := [128, 892];
    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_1143_a_Hecke(Kf)
    return MakeCharacter_1143_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 := [[0], [0], [1], [-2], [4], [-3], [0], [-4], [3], [-5], [-5], [5], [-4], [-4], [-12], [1], [-5], [-5], [-8], [6], [-1], [8], [3], [-7], [4], [-3], [-12], [-8], [4], [6], [1], [18], [11], [-20], [-4], [24], [-14], [-1], [-16], [18], [26], [-14], [26], [-14], [-10], [11], [3], [-12], [20], [0], [-19], [27], [4], [0], [-17], [24], [-2], [29], [20], [-10], [-14], [6], [14], [-1], [4], [-30], [-18], [-14], [-9], [16], [-20], [8], [-17], [20], [6], [-34], [-26], [17], [-19], [18], [36], [8], [26], [2], [-14], [-24], [-6], [39], [18], [-7], [33], [6], [20], [-8], [-36], [-12], [-12], [0], [23], [-38], [-8], [-10], [-36], [-20], [-36], [-47], [-8], [-1], [5], [-12], [7], [-26], [7], [-30], [-20], [6], [12], [-45], [-6], [17], [-5], [11], [-22], [8], [10], [31], [-30], [-34], [50], [-19], [-47], [-15], [24], [-11], [27], [42], [2], [25], [26], [18], [17], [17], [12], [13], [-4], [51], [10], [-37], [44], [33], [-46], [27], [-19], [-33], [-17], [42], [25], [30], [22], [40], [-32], [60], [-50], [-50], [-42], [42], [58], [2], [35], [-18], [9], [38], [20], [27], [-32], [9], [-23], [54], [40], [30], [3], [13], [4], [-36], [14], [21], [-8], [-65], [-6], [-6], [-34], [-44], [20], [-8], [60], [36], [-22], [-49], [52], [-27], [15], [-3], [-13], [64], [63], [50], [-5], [12], [-54], [60], [-72], [-10], [2], [-20], [-56], [-26], [-16], [-73], [-48], [6], [-26], [13], [-39], [-24], [14], [-37], [-56], [-46], [-8], [61], [4], [59], [64], [-28], [-68], [-43], [-34], [-22], [4], [-35], [44], [-10], [33], [59], [-51], [-72], [-10], [-38], [12], [-44], [29], [-66], [64], [-34], [63], [36], [-30], [-51], [-10], [30], [-26], [-4], [30], [47], [-15], [34], [49], [38], [44], [-72], [10], [10], [-68], [-48], [20], [-46], [18], [-50], [-48], [47], [-35], [26], [-60], [-42], [2], [40], [-72], [-44], [-42], [-34], [86], [-30], [-34], [48], [-24], [66], [-76], [-68], [50], [-19], [25], [9], [-53], [-14], [10], [-14], [72], [-14], [-51], [70], [-62], [-72], [-6], [58], [-53], [60], [-63], [-30], [9], [34], [-77], [-58], [-31], [24], [62], [-55], [-4], [-8], [73], [-27], [-77], [53], [-2], [-4], [16], [-12], [12], [34], [-29], [-81], [40], [-16], [31], [-49], [-76], [-75], [-60], [44], [-69], [94], [-24], [-88], [81], [70], [15], [24], [24], [-33], [-88], [84], [48], [17], [66], [-55], [36], [51], [58], [24], [76], [52], [-64], [-45], [70], [5], [25], [21], [-37], [-78], [-50], [-46], [-82], [50], [-20], [-32], [-14], [15], [-49], [38], [-19], [-65], [99], [28], [92], [-84], [30], [-56], [11], [-14], [-33], [-54], [-31], [46], [-25], [65], [48], [-27], [-88], [-82], [103], [48], [-88], [74], [-16], [34], [-32], [9], [25], [38], [-32], [-11], [-20], [-32], [21], [12], [-18], [-76], [-48], [11], [28], [5]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1143_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1143_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_1143_2_a_c(:prec:=1)
    chi := MakeCharacter_1143_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_1143_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_1143_2_a_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1143_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![0, 1]>,<5,R![-1, 1]>,<7,R![2, 1]>],Snew);
    return Vf;
end function;