
// Make newform 1617.2.a.y in Magma, downloaded from the LMFDB on 23 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_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_1617_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, 2, -3, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [-1, -2, 1, 0], [1, -2, -2, 1]];
    Rf_basisdens := [1, 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_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, 0, 0], [1, 0, 0, 0], [-1, 1, 1, -1], [0, 0, 0, 0], [-1, 0, 0, 0], [3, -2, 0, 2], [4, -3, -1, 2], [1, 2, 0, 0], [2, 1, -1, 0], [3, 1, 1, 1], [2, -1, 1, -2], [-1, 2, -2, -2], [4, 0, -2, -1], [-2, 3, 1, -1], [7, -3, -1, 3], [2, 1, -1, 4], [5, -3, -3, 3], [-6, 4, -4, -6], [-3, -4, -4, 4], [4, -4, 2, 3], [3, 2, 2, -2], [0, -2, 2, -4], [8, 3, -3, 0], [-2, 6, 0, -2], [-2, 3, 5, -3], [0, -1, -3, 8], [8, -4, -4, -1], [-7, 7, 7, -5], [-10, 7, 1, 0], [-2, 0, -2, -7], [0, -1, 1, -3], [0, 6, 4, -9], [0, -1, -3, -2], [8, -4, -2, 8], [5, 3, -5, 1], [-2, -9, -3, 10], [-4, 6, -2, -6], [-9, 0, 4, 0], [-2, 5, 7, 0], [-12, 10, 6, 2], [0, 4, 8, -4], [-6, 3, 1, 1], [12, 1, -7, 7], [-8, -1, 9, 0], [10, -2, -4, 0], [-6, 11, 5, -5], [2, 0, 4, 0], [6, 4, 4, 4], [2, 6, 0, -5], [0, -7, 3, 6], [2, 0, 6, 2], [1, 1, -3, -9], [9, 2, 0, 4], [-3, 1, -3, -1], [-11, 1, 1, -7], [1, -5, -7, -1], [-18, -2, 10, -6], [9, -14, -2, 2], [6, -8, -8, 3], [7, -1, -3, -11], [15, 4, -8, 2], [0, -8, 4, -6], [-2, 0, 8, 6], [16, -2, 0, -2], [-6, -5, -7, 8], [-2, -7, 1, 5], [-4, 6, -2, 12], [-8, 5, -1, -16], [2, 0, -10, -2], [11, -8, 0, 0], [11, -1, -11, 3], [4, -16, 2, 12], [6, -12, -4, 12], [0, -1, 5, 7], [-13, 10, 8, 8], [18, -12, 2, 6], [12, 10, 4, -7], [-14, 13, -1, -2], [14, -15, -11, 3], [-20, -2, 4, 0], [-3, -15, -1, 11], [3, -6, -2, -14], [-9, 3, 3, -13], [8, -12, 0, -10], [21, -18, -6, 2], [-12, -4, -2, 9], [-8, -4, 2, -3], [-12, -7, 3, 4], [4, -2, -8, 3], [-17, 20, -2, -10], [-19, 11, 7, -9], [8, 9, 5, -21], [-22, 8, 0, -6], [9, -5, 3, -11], [-21, 4, 0, 2], [18, 5, -3, -1], [-4, -6, -4, -8], [-1, -7, 1, -11], [7, 16, 6, -16], [-10, 12, -8, -11], [10, -2, -6, -11], [-11, 5, 17, -1], [-26, 17, 13, -5], [-8, -2, 4, -10], [2, -6, -2, -9], [-22, 13, 7, 0], [-3, -5, 13, -1], [16, -16, 2, 3], [4, 12, 0, -18], [9, 14, 0, -16], [9, 12, 8, -8], [-8, 7, 5, 19], [14, 8, 4, 0], [16, -21, -7, 17], [4, -14, -14, 16], [4, 3, 17, 10], [-2, -7, -1, 1], [-11, 1, 3, -1], [40, -18, -6, -4], [-13, -15, 1, 15], [-12, 11, 17, -6], [14, -21, 5, 17], [-6, 0, 6, 13], [-16, 11, 15, -13], [10, -16, -20, 12], [2, 2, -6, -18], [-17, 2, -10, -18], [23, -1, 7, 7], [-4, 25, 3, -18], [12, -12, -8, 8], [-4, 15, 5, -28], [13, -9, -19, 11], [-7, -8, -4, -12], [-11, 8, 2, -2], [-4, 2, -6, -18], [3, -4, 0, -18], [3, 9, -5, -17], [25, 0, -4, 14], [-23, 19, 5, 11], [-15, 21, -1, -17], [15, -2, -18, 8], [7, -1, 3, -7], [17, -2, 4, 8], [-27, 15, 9, 7], [0, -18, -2, 24], [-7, 19, -3, -15], [10, 2, 6, -6], [-14, 6, 6, 4], [0, -6, -6, -10], [6, 1, -7, -15], [24, -4, 4, -4], [7, -11, 11, 13], [5, -4, 2, -12], [4, 15, -9, -15], [0, 2, 0, -18], [18, -10, 4, 15], [8, -21, -11, 22], [17, -21, -3, 13], [-4, -12, -20, 2], [12, -2, -14, 0], [-18, -8, -4, 6], [-29, 9, 3, -15], [4, 0, 4, 17], [1, 5, 1, 1], [-38, 13, 3, 6], [4, 0, -2, -26], [-11, 22, -4, -8], [-22, 10, 0, -22], [20, 6, 2, 1], [1, 9, 7, -7], [13, -13, 5, 1], [10, 4, -4, 2], [14, -2, -6, 8], [-2, -7, 3, -4], [-30, 2, 14, 6], [-10, -12, 10, 16], [20, 11, -3, -8], [22, -22, 0, 15], [17, -8, -4, -18], [-7, -4, 18, 4], [19, 2, -16, -2], [-14, 1, -1, -16], [22, -6, -2, 16], [4, -16, -10, 3], [-12, 0, -4, 8], [-21, -23, -3, 17], [-12, -8, -8, 4], [20, 1, 7, 13], [-20, 11, -3, -23], [16, -1, -3, 28], [-34, -10, 10, -10], [-7, -3, -9, -11], [-9, 2, 0, 2], [-23, 11, 19, 1], [-25, -1, -5, -13], [-3, -27, -5, 31], [18, -13, -3, 10], [-2, -14, 6, 6], [26, -11, -13, 4], [26, -9, -7, -6], [-14, 18, 14, -14], [15, 4, -6, -6], [-28, 7, -3, 8], [-13, -8, -8, 28], [6, -12, 14, 20], [2, -8, -18, -14], [-10, 20, 8, -31], [-7, 11, 3, 25], [-2, 2, 8, 26], [-24, 13, 23, 6], [-24, 18, -2, -3], [-33, 11, 5, 9], [19, -2, 6, 0], [-10, 20, -4, 0], [2, -6, 4, 1], [14, -36, -8, 26], [10, -14, -14, -1], [-16, 5, -13, -10], [-24, 17, 3, -4], [36, -7, 1, -15], [-16, -17, -3, 9], [-29, -8, -10, 6], [42, -17, -11, 12], [11, 0, 16, -4], [-36, 6, 18, -18], [-36, 2, 10, 3], [-25, 1, -5, 9], [-30, 8, -8, 0], [13, -2, 10, 2], [-11, -7, 17, 13], [-26, 30, 12, -18], [-39, 10, -4, 6], [-12, 20, -12, -11], [-15, 13, -5, 11], [-12, -5, 17, -2], [10, -20, -2, 31], [8, 13, -13, 7], [-15, 15, 9, -5], [-4, 17, -9, 4], [9, -23, -9, 19], [6, 29, 7, -26], [-12, 32, 8, -8], [12, 5, 3, 19], [-18, 32, 26, -20], [4, 18, -6, -8], [-20, -28, -4, 28], [12, -4, -4, 28], [-11, 9, 13, 5], [34, -10, -6, -13], [-23, 1, -5, 13], [4, -26, -18, 19], [-14, 11, -9, 13], [26, -14, 2, -14], [-18, 12, 4, -8], [-2, 8, 10, -11], [-36, -2, -4, 16], [-23, -10, -12, 18], [-5, 14, -14, -14], [-61, 23, 5, -7], [-37, 32, 12, 4], [6, 14, -2, -24], [-14, -6, 18, -4], [12, -4, 12, 6], [21, -32, -4, 30], [-17, 5, 9, 3], [-30, 19, -15, -12], [22, -2, -14, 38], [-11, 9, -7, 5], [28, -24, -8, 6], [32, -14, 4, 14], [-52, 26, 10, 4], [-15, 2, 0, -2], [16, -1, -3, 32], [-4, 27, 17, -27], [5, -4, -16, 4], [40, 1, -5, 1], [8, 16, -8, 12], [-18, -2, 10, -6], [-28, -1, -3, -17], [-18, 5, 11, 10], [10, -25, 1, -2], [-16, -4, 24, 4], [40, -11, 1, -7], [-19, 12, 14, 12], [-29, 2, 14, -32], [-6, -19, -15, 5], [-17, -4, 4, 6], [10, 10, -8, 18], [-16, -13, 1, 17], [12, -15, -3, 15], [4, -9, 1, 26], [18, 24, -2, -14], [-37, 23, 27, -7], [-50, 21, -3, -5], [-44, 8, -16, -20], [10, 3, 31, 15], [-38, 16, 0, -18], [12, 8, -12, -10], [38, -9, 3, 5], [-29, 16, 18, 0], [15, 28, 18, -18], [21, 7, -17, 15], [-14, 25, 3, -5], [-6, 1, -19, -11], [15, -26, 0, -6], [-25, -10, 6, -10], [14, -31, -15, 15], [-6, 9, -5, -1], [54, 13, -7, -5], [44, -11, -5, 8], [14, -8, 2, 41], [44, -16, -8, -16], [1, -35, -3, 19], [6, -46, 0, 28], [8, -38, -4, 36], [12, -34, 4, 12], [6, -4, -4, -28], [-4, -4, 16, 0], [20, -21, -15, 10], [6, 8, 24, 20], [-13, -26, 8, 12], [-7, 20, 6, -22], [54, -22, -2, 4], [8, 16, 14, 4], [-31, -1, 21, -5], [28, -26, -2, 36], [-42, 2, -8, 0], [50, -13, -15, 1], [-59, 3, 11, -15], [11, 15, -3, -7], [-22, 8, 16, 5], [24, -24, 4, 6], [25, 14, -16, 0], [52, 1, -1, 18], [26, 13, -17, 5], [25, 21, -27, 3], [-37, 10, 24, -6], [-6, 11, -5, -31], [6, 23, 9, -16], [50, 16, 4, -14], [-62, 34, 6, 4], [-28, 23, 1, -10], [20, -19, 17, -1], [7, -4, 32, 14], [36, 11, -11, 4], [-11, 11, -5, 1], [12, -8, -28, -17], [16, 8, 2, 12], [-16, 2, 2, -50], [-2, -20, -6, 16], [8, -2, -2, -10], [-13, 22, 26, -8], [12, 12, 2, -37], [17, 8, -8, 8], [-12, -19, -5, -5], [24, -35, 13, 23], [59, -43, -31, 9], [20, -10, -2, 22], [3, -17, -11, -7], [-38, 15, 13, 8], [-2, -14, 26, 21], [-4, 4, -16, 0], [-16, 6, 32, 11], [39, 11, -15, 27], [-16, 0, -12, -18], [15, -10, -18, -2], [52, -11, 3, -10], [4, -28, 0, 19], [8, 0, -28, 7], [32, 3, -13, 9], [-38, -19, 15, -8], [17, -19, 9, 25], [-31, 7, 21, 1], [28, -8, -8, -12], [19, -38, -20, 40], [-17, 5, -9, -31], [15, 3, -9, 21], [-17, 4, -24, 2], [-10, 32, -2, -25], [-36, 28, 8, 0], [-33, -3, 31, -13], [38, -19, -15, -19], [0, 24, 4, -17], [43, -9, -5, 1], [-68, 8, -4, 6], [23, -13, -21, 1], [-15, 14, 2, 24], [14, -10, -10, -32], [2, 29, 3, -18], [35, 9, -13, -13], [-23, 4, -14, -12], [-2, -10, 16, 2], [18, -1, -33, -13], [14, 13, 15, -35], [46, -50, -4, 15], [-8, -11, 19, -7], [-13, 20, -4, -6], [-21, 31, -9, -29], [-16, -2, 18, -28], [13, -19, 5, 5], [-17, 6, 28, -18], [8, -33, -13, 19], [70, -15, -21, 6], [22, 10, -6, -34], [-26, 49, 5, -33], [-7, -28, 22, 6], [-19, 24, 14, -32], [34, -50, -18, 18], [38, -26, 10, 12], [-48, 16, 26, 12], [-38, -2, 10, -34], [-12, -10, -14, 38], [44, 0, -6, -21], [16, -13, 1, 48], [-28, 2, 14, 11], [-19, 23, 29, -7], [23, -17, -23, -3], [4, 19, 9, -45], [-18, 34, 4, -4], [48, -15, -7, -11], [4, 15, -3, 14], [45, -14, -22, 30], [-20, 9, -15, 7], [-44, -9, -3, 14], [6, 16, -12, -24], [7, 15, 35, -1], [24, -1, 19, 15], [29, -19, -11, 37], [8, 2, 34, 15], [-44, 14, 28, 1]];
    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_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_1617_2_a_y(:prec:=4)
    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_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_1617_2_a_y( : 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, 6, -3, -2, 1]>,<5,R![7, 4, -8, 0, 1]>,<13,R![-7, 24, 6, -8, 1]>,<17,R![-28, 48, -6, -8, 1]>],Snew);
    return Vf;
end function;