
// Make newform 1323.2.a.y in Magma, downloaded from the LMFDB on 19 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_y();".
// 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_y();".
// 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 := [1, -4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-3, -1, 1]];
    Rf_basisdens := [1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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_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 := [[-1, 1, 0], [0, 0, 0], [0, 0, -1], [0, 0, 0], [-2, -1, 0], [-1, 2, 1], [1, -1, 2], [-1, -1, 1], [-2, -1, -1], [-4, 1, 2], [3, -2, -1], [-3, -1, -2], [2, -5, -1], [-4, 3, 0], [2, 4, 1], [-7, -2, 1], [-5, -1, -1], [-1, 2, -2], [4, 1, -1], [3, -3, 3], [0, -5, 2], [2, 3, 3], [0, 6, 3], [-3, 0, -4], [6, -2, 2], [-6, 6, 1], [1, 6, 0], [-9, 3, 1], [-6, 2, 4], [-7, -2, -4], [3, 5, -5], [3, -3, 2], [-4, -2, -2], [-9, -2, -7], [-11, -1, -1], [-7, 3, 0], [2, 2, 1], [-5, 7, 5], [7, 2, -5], [-8, -1, 0], [6, 0, 5], [-7, 2, -5], [0, -3, -8], [8, 3, 3], [-10, 4, -1], [-3, -2, -4], [-5, -5, 2], [5, 2, -5], [-17, 5, -1], [1, -3, -9], [-17, 8, 2], [18, -3, 3], [13, 0, 6], [13, -7, 4], [-17, -4, 0], [-5, 11, -3], [10, -1, 2], [-4, -7, -5], [4, -5, 2], [-5, 5, 1], [5, -1, -8], [-1, 7, 5], [14, -1, 4], [-2, -4, 2], [13, 0, 6], [-5, -10, 5], [8, -12, -3], [-5, -5, -10], [0, -6, -4], [14, 2, 1], [8, 4, -3], [2, -5, -6], [-18, -2, -4], [2, 0, 0], [-6, -4, -5], [-1, 13, 7], [-13, 7, 7], [0, 4, -1], [-8, 5, -7], [-20, 9, 3], [21, 0, 0], [14, -15, -3], [-7, 7, -8], [-7, -7, 1], [9, -2, 2], [10, -7, 2], [0, -9, 0], [4, -5, 5], [-28, 1, -7], [4, 4, 8], [-4, -5, -14], [-15, 6, 1], [13, -11, 2], [-2, 5, -8], [-17, -5, 2], [-6, -3, -6], [10, 5, 0], [22, -13, -4], [14, -7, -5], [23, 3, -6], [13, -11, -1], [-23, -1, 3], [-10, 10, 3], [-9, -3, 1], [-1, 3, 0], [2, 14, 1], [16, -4, 10], [-1, -8, 7], [5, -8, 10], [15, -2, -7], [1, -3, 12], [-5, 7, -13], [-6, -6, 6], [-10, 2, 1], [14, -12, -3], [11, 10, 13], [-17, -6, 3], [0, -6, 1], [-20, -1, -10], [15, -3, 3], [-4, 8, -5], [10, 10, 2], [17, -2, -2], [8, -17, -2], [1, -6, 9], [-15, -12, 3], [-24, -1, 4], [8, 10, 16], [10, -18, -3], [-36, 1, -1], [9, 5, 1], [12, 3, 15], [4, -8, 2], [-13, -12, 6], [-13, 7, -5], [-11, 15, 3], [6, -3, -11], [-13, -10, 10], [-8, -10, -2], [8, 10, 7], [13, -6, -3], [-4, 13, -12], [18, 8, 13], [0, 0, -9], [-8, 0, -15], [-14, 8, 7], [28, 9, 9], [32, 7, 12], [-20, -9, -3], [7, 2, -1], [-2, -20, 8], [-27, 0, 9], [-16, 9, 9], [-8, -4, 0], [-4, -18, -6], [21, -9, -9], [-1, 0, -18], [1, -1, 0], [-12, 4, -13], [48, 3, -2], [14, 13, 15], [-8, 14, 1], [-8, -2, -1], [17, 1, -11], [17, 7, 4], [10, -13, 8], [-10, 0, -9], [23, 5, -2], [-14, 1, -13], [7, -19, -3], [8, 4, -8], [-15, -5, -10], [37, -4, -7], [-16, -6, -3], [16, 9, -6], [2, -5, -19], [-17, 1, 5], [10, 2, -9], [-15, 25, 2], [20, -28, -8], [11, 3, 9], [-18, -3, 12], [-7, 12, 9], [-35, 11, 0], [5, 19, 1], [0, -18, -11], [1, 1, 2], [-28, -16, 4], [-20, 7, -10], [-7, 13, -15], [-7, -4, 4], [9, 12, 24], [-31, 12, -3], [-7, 4, 10], [10, 2, 0], [19, 11, 14], [23, -3, 6], [-9, 11, 10], [12, 12, -3], [20, -2, 1], [-27, 3, -22], [-35, -9, 6], [-21, 31, 14], [41, 5, 7], [5, 25, -4], [-12, 6, 16], [-12, 7, 14], [19, -13, -16], [27, 3, 6], [-22, 17, 10], [18, 14, 1], [-12, 15, 3], [-14, 16, -10], [0, -24, 8], [-21, -9, -17], [-10, 20, 7], [5, 6, -3], [44, 4, -3], [24, -3, 16], [-33, 12, -3], [11, 0, 3], [5, -13, -2], [2, 28, 3], [-25, -3, 9], [-10, -2, 5], [-34, 15, -3], [-51, 0, -7], [-9, 15, 17], [-41, 0, -12], [-8, 20, -7], [-8, 22, -16], [26, -10, 19], [-40, 0, -15], [31, -4, -9], [-6, 10, -19], [-14, -16, 11], [-4, -1, 7], [-25, 22, 12], [33, -9, 0], [-4, -2, 14], [11, 19, 13], [21, 19, 14], [9, -8, -10], [-2, 13, 17], [12, 9, 18], [45, -15, -1], [20, -7, 10], [-6, 3, -2], [10, 22, 2], [-6, -21, 12], [8, -3, 24], [-52, -2, 1], [13, -4, 0], [-10, -10, -5], [2, -17, 9], [23, -26, -12], [-10, -18, 9], [-36, 7, -7], [-27, 18, 9], [-38, -6, -18], [-13, -9, 9], [7, -25, -5], [19, -21, -3], [-23, -6, 12], [-24, 12, 16], [31, -18, 12], [-27, 9, 9], [-2, -31, 10], [-5, 4, -10], [-8, -28, 15], [17, 14, -17], [5, -15, -12], [62, 2, 10], [-1, 3, 9], [-28, 20, 4], [-20, -24, 15], [-33, -3, 4], [15, 23, 4], [16, -5, -1], [34, 20, -3], [21, 12, -11], [3, -31, -2], [8, -8, -16], [-16, 5, 16], [-11, 18, 6], [-2, 20, 17], [-5, -11, -13], [-18, -3, 3], [-50, 9, 6], [48, 6, 3], [-35, 5, -15], [-7, -5, 3], [13, -16, 23], [0, 3, 15], [34, -32, -7], [-19, 1, 3], [5, -13, 22], [-1, -8, 20], [1, 11, 0], [-38, 3, 12], [45, -17, -1], [-2, -25, 8], [-29, -5, -22], [11, 7, 5], [-30, 2, 16], [21, -13, -14], [-4, -20, 16], [-3, -8, -16], [21, 6, 13], [31, -2, 11], [-37, -2, -11], [-24, -6, 2], [-20, 5, -10], [-18, -19, -14], [-11, 8, 16], [-56, 12, 6], [9, -21, -15], [-4, -26, -17], [38, 5, -2], [18, -12, 3], [-17, -15, -21], [37, -23, -19], [-21, -6, -30], [44, 3, -9], [2, 19, 4], [23, -16, -5], [25, -5, 26], [-32, 9, -24], [-34, 16, 3], [-33, -12, -24], [-53, -2, -16], [-64, 16, 4], [-35, 36, 18], [61, -10, 5], [10, 19, 29], [43, -28, -11], [-37, 3, -9], [-37, 4, -20], [55, -6, -6], [-40, 11, -14], [37, -11, 17], [-11, 20, 13], [-18, -15, -21], [-44, 1, 11], [-4, -5, -3], [5, 25, -13], [-34, 14, 1], [28, 7, 8], [0, 6, 21], [31, -1, 30], [2, -16, -17], [-15, -16, -5], [-16, 16, 14], [-21, 19, -7], [40, -17, 5], [50, -11, 5], [5, -2, -23], [7, 20, 17], [-37, -5, -9], [-27, -6, -12], [24, -19, -8], [-12, 24, 14], [38, -38, -8], [-22, -14, 15], [34, 3, -12], [-23, -14, -22], [14, 13, -10], [-5, 25, 23], [-6, 32, 4], [-16, -8, -23], [57, -17, -7], [-47, 8, -10], [6, -21, -15], [28, 15, -18], [-11, 22, 8], [17, 1, 21], [-55, -5, -10], [-20, -6, 15], [-10, -8, 16], [9, -32, -1], [-12, 21, 4], [51, -27, -9], [52, -11, -1], [33, 12, 8], [0, 7, 2], [-3, -12, -11], [23, 27, -6], [-7, -25, 16], [-46, -9, -30], [63, -6, 15], [25, -5, 29], [41, -35, -5], [-10, 19, -29], [-41, 18, 12], [-7, 34, 30], [-59, 10, -1], [2, 14, 19], [21, 21, -3], [69, -22, -5], [-16, 40, 25], [28, -12, 21], [-13, 37, 24], [46, 4, -1], [-55, -14, 7], [-44, 17, -16], [-40, 29, 13], [12, -37, 10], [76, -4, 1], [-17, 42, 24], [-7, -5, 19], [31, -27, -21], [-31, -5, -30], [-41, 2, -5], [16, 1, 17], [3, -37, 10], [14, 13, 4], [-29, -7, 17], [-14, 0, 0], [3, 33, 3], [-7, -8, 10], [-34, -17, -5], [20, 14, 10], [10, 35, -20], [41, -35, 2], [-29, -9, 9], [-56, -4, 2], [18, -39, -8], [48, 6, 30], [75, -17, -4], [-8, 41, 8]];
    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_y();".
// 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_y(:prec:=3)
    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_y();".
// 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_y( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1323_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-3, -3, 2, 1]>,<5,R![-3, -6, -1, 1]>,<13,R![-47, -19, 2, 1]>],Snew);
    return Vf;
end function;