
// Make newform 1339.2.a.c in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1339_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_1339_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_1339_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
        poly := [1, -3, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, -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_1339_a();"
function MakeCharacter_1339_a()
    N := 1339;
    order := 1;
    char_gens := [1237, 417];
    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_1339_a_Hecke(Kf)
    return MakeCharacter_1339_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, -1, 0], [0, -1, -1], [-2, -1, 0], [1, -1, -1], [-3, -1, 1], [1, 0, 0], [1, -2, -2], [-1, -2, 1], [0, 4, -2], [-6, 1, 1], [-4, 4, 2], [0, 3, 2], [-2, -2, 2], [2, 2, 0], [-3, 5, 1], [-5, 3, 1], [-2, -5, 2], [2, 3, 3], [4, -4, 0], [2, 4, -4], [6, -9, -4], [0, 6, 0], [-4, 1, 4], [0, -1, 6], [8, -2, 2], [-8, -2, -4], [1, 0, 0], [6, 0, 2], [7, -1, -3], [-4, 2, 0], [-1, -6, -2], [2, -2, 4], [-15, 3, 1], [0, 4, 2], [-3, -7, -3], [0, 8, 6], [5, -1, -5], [-14, -1, -4], [1, 6, 1], [2, 6, 4], [5, -7, -5], [-5, 1, 3], [2, 3, -3], [7, 0, -7], [5, 0, 7], [-13, -4, 2], [19, -10, -4], [15, -8, -5], [-8, 10, 6], [8, -4, -2], [5, 9, -1], [-8, -3, 4], [-10, 5, 4], [2, 9, -9], [5, -7, -11], [-18, 9, 7], [8, 6, 8], [1, 7, -5], [2, 0, 2], [7, 2, -5], [6, -3, -1], [15, -1, -3], [0, -4, 12], [9, -7, 1], [-6, -11, 1], [-22, 8, 6], [-5, -1, 7], [12, 9, -3], [-18, 6, -2], [-9, 12, -5], [7, -8, -11], [19, -3, -3], [15, 1, 1], [-24, 2, -4], [21, 5, 5], [6, 4, 12], [-5, -9, -1], [-10, -9, -12], [8, -6, -4], [-7, 11, 5], [-13, -1, -3], [-8, 2, -6], [5, 3, 17], [-8, 0, -10], [0, 4, -6], [14, 9, 7], [13, 5, -9], [-25, -1, 1], [9, 1, 7], [-1, 7, -13], [-9, -5, -1], [-24, 14, 8], [-10, -16, 10], [0, -4, -16], [10, -14, -4], [-17, 1, -13], [-15, -15, 7], [-16, 4, -2], [17, 9, -9], [-22, 8, 4], [-21, -1, 3], [-29, 11, 1], [2, 3, -1], [-10, -8, 10], [25, -9, -3], [7, -6, 1], [15, 1, 3], [3, 12, 5], [19, 4, 18], [2, -10, -10], [-23, -7, 1], [13, -1, 1], [-20, -1, 0], [-11, -1, 3], [-1, -3, -15], [-16, 14, -2], [8, -3, 10], [12, -6, 8], [26, 0, -2], [1, 3, 5], [-15, -7, 7], [18, -3, 5], [-25, 14, 2], [-4, 4, -8], [2, 16, -2], [-23, 26, 14], [-10, -2, -10], [-14, 2, -18], [21, -4, 8], [1, 5, 7], [-6, -1, 12], [-2, -14, 0], [0, -8, 0], [-7, 14, 2], [-9, 19, 13], [1, -14, -1], [-3, 7, -19], [24, 9, 8], [-4, -13, 3], [7, -25, -7], [6, -12, 0], [-12, -8, -14], [10, 2, 16], [25, -13, -5], [22, 10, 6], [-27, 16, 11], [-33, 3, 1], [-24, 29, 11], [-32, -4, -10], [21, 3, -15], [-4, -9, 14], [-22, 12, -6], [-12, -4, -24], [30, 0, -2], [-13, 21, 13], [6, -19, -25], [13, -2, -14], [-10, -14, -20], [-11, 3, 13], [-22, 0, -2], [8, -8, -4], [-20, 18, 6], [12, -2, 26], [-6, 22, 0], [-3, -9, -9], [-3, -10, 1], [-2, -6, 16], [3, 11, 29], [-5, 7, 1], [15, -9, -1], [-30, -5, 8], [2, -19, 2], [-10, 4, 2], [37, -7, -5], [-25, -13, 7], [-5, 10, 22], [-10, 16, -8], [9, -3, -21], [4, -22, -20], [31, -13, -7], [7, -9, 19], [17, 1, 5], [-40, 10, 10], [-1, -10, -19], [0, 2, 8], [-21, -16, 14], [-11, 9, -1], [21, -1, 19], [-5, -10, -9], [-27, 10, 3], [28, -26, -10], [-11, 31, -5], [-18, -4, 10], [21, 20, -1], [5, 12, 30], [21, -8, 14], [-3, -7, 11], [22, 8, -12], [8, 5, -24], [2, -1, 23], [-5, 19, 3], [48, -4, -4], [-5, -17, -15], [-13, -22, -10], [-11, -22, -11], [-14, 0, -12], [11, 13, -15], [15, 16, -8], [21, -10, 3], [-15, -13, 5], [-10, -5, -3], [13, -11, 9], [-22, 18, 16], [9, -7, -25], [6, 7, -14], [33, 1, 11], [29, -23, -19], [13, 17, 35], [39, 6, -9], [8, 22, 0], [-33, 17, 9], [-11, 8, 23], [-31, 5, 19], [10, 12, 8], [37, -30, -14], [20, -20, -24], [-27, -7, -31], [-11, -5, 19], [-5, -6, -6], [31, -32, -15], [15, 9, 37], [0, 8, 16], [-10, -1, -6], [-20, -28, 14], [-8, 2, -2], [-24, 32, 6], [31, -24, -11], [-7, 17, -7], [20, -21, -13], [24, -20, -20], [6, -21, -6], [-6, -2, -28], [2, -14, 14], [-27, 17, -7], [-39, -13, 7], [-2, 8, 28], [-14, 10, 8], [29, 4, -7], [-5, 17, -27], [-12, -8, 0], [9, 15, 1], [-20, 22, -4], [-34, -16, -8], [33, -24, -8], [7, 22, -26], [-12, 2, 4], [26, -28, -26], [5, -8, -1], [8, -38, -8], [-11, 5, 11], [-4, 6, -2], [5, -27, -29], [-1, -4, -9], [18, -20, 20], [-18, 0, -8], [-6, -27, -11], [-6, 18, 28], [-5, 39, -3], [51, -27, -13], [-34, -16, 18], [-39, -8, 2], [3, -35, -3], [19, 13, -17], [-7, -5, -13], [47, -26, -4], [-6, -35, 8], [-7, -5, -17], [-29, -13, 7], [24, -16, 6], [-38, 2, -4], [9, -33, -9], [-26, 8, -12], [-28, -12, -8], [2, -22, 30], [-20, 19, 0], [-35, 1, -15], [10, 16, -6], [22, 16, -20], [1, 24, 25], [-28, 32, 8], [23, -17, -3], [25, -38, -10], [18, -4, -6], [50, 4, 4], [15, 18, 14], [48, -2, 18], [21, 9, -25], [26, -5, -5], [28, 16, 8], [0, 11, -4], [-16, 10, -28], [-13, -17, -7], [-31, 20, 22], [10, -8, 4], [-69, 8, -4], [-34, 8, 10], [-17, 10, -4], [-32, 15, 15], [15, -11, 7], [-12, 38, 4], [-24, 9, 19], [-66, 22, 8], [-57, 15, 1], [-5, -3, 7], [35, -3, -11], [-8, -19, -15], [-11, 33, 27], [24, -14, -8], [15, -4, 35], [16, -24, -14], [-36, 20, 0], [-38, 7, -6], [-38, 20, 0], [-35, 35, 15], [0, 10, 0], [-15, 17, -7], [50, -21, -20], [-63, -1, 5], [-1, -18, 36], [36, -14, 0], [-2, -6, -8], [-13, 33, 11], [-19, -9, -21], [2, 30, 12], [-13, -3, -31], [19, -39, -3], [11, 2, 47], [-26, 34, 26], [-65, 17, 11], [-42, 27, 11], [57, -7, 1], [2, 8, -30], [-3, 17, -17], [-22, 17, -13], [-27, 42, 25], [-8, -8, -22], [15, 3, 23], [14, -29, -2], [32, -42, -12], [-11, -34, 10], [-2, 13, -8], [11, -13, -17], [23, 4, -3], [40, -4, -18], [-20, 8, -32], [-9, -21, 3], [21, -22, -13], [-21, -13, -33], [-25, 13, -9], [-17, 27, 31], [-8, -4, 8], [24, -6, 24], [-24, -24, -4], [5, 25, -11], [18, -44, -34], [-49, 10, 20], [-8, -3, 18], [8, -42, -6], [23, -2, 18], [21, 1, -25], [-44, -9, 23], [27, 7, 31], [-9, 43, 19], [-37, 39, 29], [-4, 6, 30], [7, 17, -27], [-59, 7, 9], [10, 2, 18], [-19, 6, -12], [-21, 31, 9], [-14, -8, -22], [-12, 10, 34], [3, 14, -15], [-8, -24, 16], [-62, 22, 16], [7, 21, 35], [-4, 18, -14], [-6, 12, 4], [41, -3, 3], [13, -1, 39], [-3, 29, -17], [-6, 18, 22], [53, 13, 9], [43, 17, -11], [29, 3, 31], [-9, -29, 35], [58, -1, -8], [4, -30, 18], [4, 16, 38], [37, 17, 11], [15, -22, -35], [7, -17, -3], [24, 26, -2], [23, 8, 31], [-11, 13, 7], [7, -36, 0], [-38, 18, -22], [-27, 2, -9], [-43, -7, -31], [-45, 9, -17], [-2, -40, 12], [26, -25, -9], [23, -52, -12], [-46, -17, 1], [58, 1, -1], [-40, -14, 24], [46, -17, -20], [17, -6, 38], [41, 7, -23], [-8, -14, -40], [-4, 27, -13], [32, 20, 32], [4, -10, 2], [8, 24, 20], [-17, 27, 1], [-31, -34, 9], [66, -14, 2], [3, -20, -27], [24, -34, -28], [12, -39, 15]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1339_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1339_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_1339_2_a_c(:prec:=3)
    chi := MakeCharacter_1339_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_1339_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_1339_2_a_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1339_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-1, -3, 1, 1]>,<3,R![-1, -5, 1, 1]>],Snew);
    return Vf;
end function;