
// Make newform 1323.2.a.c in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1323_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_1323_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_1323_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_1323_a();"
function MakeCharacter_1323_a()
    N := 1323;
    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_1323_a_Hecke(Kf)
    return MakeCharacter_1323_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 := [[-2], [0], [3], [0], [-2], [-6], [3], [6], [8], [2], [-6], [9], [9], [-9], [-3], [-4], [-3], [6], [4], [4], [12], [-1], [15], [-6], [6], [6], [12], [4], [-9], [-10], [-11], [0], [2], [6], [10], [9], [18], [-11], [9], [18], [-14], [12], [-16], [-9], [22], [0], [0], [-18], [12], [0], [22], [10], [-18], [3], [-6], [2], [15], [-12], [-19], [-20], [6], [21], [-12], [-27], [24], [20], [-9], [27], [-26], [-12], [3], [-4], [-36], [-5], [9], [-3], [34], [-12], [-28], [-12], [-21], [-26], [-4], [-18], [-12], [-2], [-22], [18], [-3], [5], [12], [15], [0], [-10], [9], [33], [3], [15], [6], [-7], [-37], [-32], [-36], [-20], [-41], [12], [36], [39], [-46], [30], [-42], [-2], [-8], [6], [25], [-38], [-12], [-48], [-10], [20], [-30], [18], [30], [14], [-18], [14], [27], [-51], [-18], [48], [20], [-10], [0], [-45], [21], [36], [-27], [-48], [30], [4], [-42], [-20], [-31], [-22], [-18], [-3], [12], [9], [0], [32], [-31], [-42], [45], [-15], [-27], [-2], [-27], [9], [-18], [-9], [-56], [40], [32], [27], [-8], [33], [-9], [18], [-43], [45], [-52], [-18], [10], [-1], [18], [-21], [8], [4], [24], [24], [19], [-9], [-14], [-33], [26], [-39], [-18], [6], [-50], [-57], [42], [16], [45], [21], [40], [39], [18], [19], [-18], [-45], [28], [12], [-12], [0], [9], [9], [30], [-44], [16], [66], [-7], [-42], [-45], [-57], [-69], [-6], [63], [30], [-14], [-64], [27], [30], [10], [63], [15], [9], [9], [32], [0], [74], [-18], [18], [27], [8], [72], [36], [0], [-14], [64], [-39], [-16], [36], [18], [18], [-30], [45], [-48], [3], [-23], [68], [-27], [6], [-8], [-48], [57], [14], [47], [48], [30], [-12], [64], [20], [-60], [-54], [-18], [-36], [-76], [-54], [-23], [-44], [-6], [36], [-42], [63], [-12], [30], [-82], [-9], [-9], [-57], [39], [23], [-27], [12], [-72], [-32], [-63], [-10], [-12], [9], [16], [27], [-62], [51], [71], [-63], [-24], [63], [15], [-42], [-30], [32], [16], [-26], [-67], [-18], [16], [-12], [62], [-54], [48], [50], [56], [9], [-68], [-72], [57], [28], [54], [44], [36], [9], [45], [-72], [34], [-12], [11], [-84], [-2], [22], [54], [-10], [78], [-81], [-7], [-3], [-34], [-27], [-72], [-84], [-1], [38], [63], [-36], [-56], [22], [60], [-81], [3], [78], [78], [-43], [80], [-6], [67], [-81], [-84], [57], [44], [74], [55], [-39], [58], [-16], [84], [9], [75], [-29], [-45], [88], [6], [-10], [74], [-30], [2], [36], [58], [-42], [-42], [36], [21], [-62], [72], [-16], [48], [-12], [72], [78], [-79], [-48], [-1], [-54], [68], [42], [-38], [-18], [-60], [51], [-68], [80], [-36], [40], [13], [-15], [-78], [30], [1], [-88], [-36], [-84], [-36], [-74], [22], [-63], [-85], [-30], [-50], [42], [99], [-48], [2], [78], [-34], [93], [6], [-99], [-52], [-86], [12], [36]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1323_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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