
// Make newform 1617.2.a.l in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1617_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_1617_2_a_l();".
// 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_1617_2_a_l();".
// 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, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_1617_a();"
function MakeCharacter_1617_a()
    N := 1617;
    order := 1;
    char_gens := [1079, 199, 442];
    v := [1, 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_1617_a_Hecke(Kf)
    return MakeCharacter_1617_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, 0], [-1, 2], [0, 0], [-1, 0], [3, -4], [-6, 0], [1, 2], [4, -4], [-7, -2], [-2, -4], [-7, 4], [-6, 0], [6, -4], [-5, -4], [0, -8], [-1, 8], [-6, 8], [-9, 6], [-2, 4], [1, 0], [4, -4], [-6, -4], [-6, 12], [-6, 8], [-10, 4], [-12, 12], [7, 8], [6, 0], [0, 4], [16, 0], [-2, 4], [6, 0], [-8, 0], [-7, 14], [6, -8], [16, -12], [11, -18], [-12, -4], [-12, 4], [-12, 4], [6, -4], [12, 0], [0, 16], [-2, -4], [-18, -4], [6, 8], [-18, 0], [12, -24], [4, -12], [-6, 12], [-15, 20], [19, 0], [9, -8], [11, -2], [15, -12], [10, 4], [-15, 6], [-6, -8], [-11, 10], [5, -6], [-16, 16], [-16, 0], [0, 8], [6, -20], [14, 8], [-16, 0], [-8, 12], [-12, 8], [-17, 12], [1, 2], [-8, -8], [6, 12], [-4, -8], [-3, -14], [4, -8], [-18, 24], [10, -12], [-2, -4], [-14, 0], [-3, 0], [23, 4], [15, -20], [32, -12], [21, -18], [22, -8], [-6, -12], [8, -12], [-10, -4], [7, -6], [15, -24], [4, -16], [4, -16], [15, -16], [-7, 18], [-30, 12], [-42, 4], [27, -2], [-13, 6], [2, 12], [6, -12], [21, -18], [-2, 4], [-6, -4], [-30, 12], [-22, -8], [-27, 8], [18, 12], [-20, 28], [33, -24], [13, 6], [0, 12], [18, -36], [-8, -12], [-4, 0], [38, -4], [14, -12], [-15, 12], [-20, 16], [7, 16], [4, 0], [-38, 12], [-24, -12], [16, -4], [-26, 12], [6, -12], [3, 12], [37, -20], [-24, 12], [-42, 8], [24, 12], [-33, 28], [-17, 18], [-31, 12], [-20, 4], [-1, 0], [43, -14], [-9, -18], [49, -2], [-31, 10], [-21, 30], [15, -30], [3, -30], [39, -24], [-4, 24], [27, -4], [-10, 40], [-14, 40], [4, -12], [-10, 28], [4, -16], [-13, 22], [1, 2], [-4, 20], [-40, 16], [-12, -12], [-24, 16], [9, -38], [22, -40], [20, -44], [26, -16], [33, -38], [-12, 36], [1, -16], [-20, 4], [0, -24], [23, -6], [-42, 0], [-24, 16], [7, 34], [31, 8], [18, -12], [-8, 32], [34, -24], [-2, -16], [42, 12], [32, 4], [-12, -24], [-33, 34], [1, 12], [-9, 34], [24, -28], [30, 8], [-2, -4], [-32, 16], [15, -22], [20, -24], [-8, -24], [16, -36], [-42, -4], [26, 16], [-51, 16], [-21, -10], [5, -34], [3, 0], [-3, -14], [66, -4], [42, -16], [4, 4], [4, -8], [2, -4], [11, -6], [-8, -12], [-23, 8], [44, -32], [-10, -28], [-42, -8], [-7, 32], [-2, -4], [8, -24], [16, 12], [-15, 14], [9, -26], [32, 16], [-28, 4], [10, -36], [-30, 0], [66, 4], [18, 0], [60, -12], [-12, 36], [39, -6], [-8, 8], [-19, -18], [-8, 4], [-8, 36], [33, 18], [-6, -12], [-9, -6], [-3, 0], [22, 0], [51, 6], [-44, 8], [-23, 10], [-44, 48], [-60, 12], [48, 0], [13, -2], [10, -20], [-21, 12], [12, -36], [44, -48], [-36, -20], [38, 24], [-44, 8], [-16, 28], [-48, 16], [31, -40], [22, -40], [23, -44], [16, 0], [-6, 44], [-14, 8], [26, -8], [8, -4], [-8, -16], [-7, -20], [-57, 30], [41, -26], [-43, 24], [-42, 4], [-52, -8], [30, -16], [-47, 20], [-7, 10], [70, -16], [-54, -4], [-21, 54], [16, -4], [-30, 28], [-20, -16], [-9, -18], [4, 0], [-44, 20], [9, -24], [0, -28], [-16, -28], [-54, 4], [0, -36], [12, -12], [32, 8], [24, 32], [-12, -40], [-27, 36], [41, -6], [30, 8], [-5, 24], [-42, 12], [-16, 0], [24, -24], [34, -28], [20, 16], [-51, 18], [-14, 20], [48, -60], [-22, -4], [-30, 20], [10, -44], [18, 24], [-7, -38], [19, 8], [-33, -30], [38, -48], [22, 28], [11, -18], [-13, 24], [50, -52], [14, -12], [-6, 4], [4, 32], [-32, 28], [-40, 8], [-19, 26], [48, -8], [36, 8], [-54, 8], [12, -32], [0, -48], [-32, -36], [30, -60], [33, 18], [15, 16], [6, -16], [56, 0], [15, -18], [-84, 12], [-8, -16], [-42, 40], [-47, 28], [13, 46], [50, -32], [-40, 4], [45, -10], [34, 8], [-34, -12], [15, -8], [43, -36], [-34, 16], [-22, 24], [-12, -16], [-18, 24], [66, 0], [36, -56], [5, -2], [38, 4], [-49, 16], [64, -28], [24, -24], [72, 4], [70, 4], [-24, 24], [-55, 24], [-22, -40], [-19, 6], [0, 12], [32, 28], [31, -20], [32, -44], [9, 18], [-24, 56], [-2, 60], [52, -20], [-30, 36], [35, -40], [-16, 28], [15, 0], [-50, 76], [-76, 0], [-24, -36], [8, 8], [-50, 8], [-71, 28], [-13, -22], [-52, 0], [87, -12], [-7, 56], [-15, 44], [41, -8], [-44, -32], [-38, 0], [45, 6], [14, 44], [-8, 20], [17, 26], [32, -32], [15, 28], [-45, 42], [-6, 0], [6, 36], [1, 4], [-75, 24], [-30, 12], [-58, 44], [-38, 68], [20, -64], [-12, 48], [-11, -18], [-43, 2], [24, 32], [-27, 6], [3, 36], [68, 8], [18, -12], [-22, 44], [-42, 4], [-9, 10], [-49, 4], [50, -28], [-6, 52], [-60, 32], [14, -48], [-30, 28], [-22, 32], [12, -60], [-76, 16], [39, -38], [3, -52], [32, -12], [14, -20], [-36, 24], [14, 16], [11, 4], [-32, 16], [-50, -32], [-22, 48], [7, 34], [-36, -44], [41, -62], [60, -8], [54, -24]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1617_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1617_2_a_l();".
// 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_1617_2_a_l(:prec:=2)
    chi := MakeCharacter_1617_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_1617_2_a_l();".
// 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_1617_2_a_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1617_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![1, 3, 1]>,<5,R![-5, 0, 1]>,<13,R![-19, -2, 1]>,<17,R![6, 1]>],Snew);
    return Vf;
end function;