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