
// Make newform 17.4.a.b in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_17_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_17_4_a_b();".
// 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_17_4_a_b();".
// 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 := [-4, -14, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-10, 0, 1]];
    Rf_basisdens := [1, 1, 2];
    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_17_a();"
function MakeCharacter_17_a()
    N := 17;
    order := 1;
    char_gens := [3];
    v := [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_17_a_Hecke(Kf)
    return MakeCharacter_17_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 := 4;
    raw_aps := [[0, 1, -1], [2, -1, 2], [-2, 0, 2], [6, -1, -4], [-10, 11, -2], [12, 8, 6], [-17, 0, 0], [24, 22, -8], [46, -39, -4], [-142, -16, 30], [82, 39, 16], [102, -28, -50], [-118, -52, -60], [204, 2, 56], [228, -48, 44], [98, 116, -8], [212, -130, 0], [-2, -64, 78], [292, 24, -132], [-110, 185, 72], [274, 16, -16], [-138, -267, 180], [-756, 82, 128], [-20, -276, 110], [-50, 140, 120], [388, 32, -98], [792, 76, -224], [-534, -53, -98], [342, 420, -62], [-498, 68, -188], [1804, 62, 240], [-814, -185, -178], [812, 392, 322], [666, -163, 570], [-34, 288, -544], [884, -354, -72], [-550, 76, 388], [-202, 167, -446], [-566, -235, -540], [-110, 420, -674], [648, -426, -8], [-2874, -220, 110], [-796, 372, -452], [-510, -776, -12], [258, 372, -134], [130, 1203, -420], [-3194, -271, -406], [1776, -606, 508], [-666, -21, -558], [168, -528, -810], [-2698, -916, 420], [3412, -568, -348], [-3486, 708, -468], [-564, 1536, 916], [2880, 240, 606], [2964, -326, 888], [2422, -480, 1138], [2044, -736, 540], [-942, 652, 1438], [-190, -4, -708], [-858, -533, 1462], [-202, -2008, 312], [-1248, 1272, -500], [-4614, 257, -2356], [242, 528, -676], [4842, 376, 538], [3196, 138, -1184], [2258, -1412, 496], [-1218, 1579, -366], [-918, 228, 608], [850, 604, 1308], [-4952, 10, -12], [-3810, 1607, -968], [-3356, 496, 1334], [-294, -2353, -70], [-4612, -790, -560], [528, 816, -1922], [2726, 2132, -154], [6170, 316, 2540], [-1450, -3168, 1680], [1070, 2025, 302], [-5988, -1336, -474], [414, 309, 540], [-5024, 2876, 2], [1026, -703, -336], [28, 1152, 1604], [8454, 1064, -1480], [-1784, -3380, 3074], [-4966, 1972, -3952], [4488, -2816, 2696], [5640, -714, -552], [-12030, 1323, -1228], [5366, -1473, -312], [8112, 2662, 136], [11278, 2525, -1486], [-10098, -3045, 424], [6974, -776, 3128], [9546, -1100, 956], [4948, 2896, -1868], [3774, -324, -1218], [8410, 1083, -1674], [4716, -216, -3482], [11252, -978, -104], [-11090, -5172, 2860], [-6330, 1505, 830], [11580, -352, 1082], [-3360, 374, -4368], [-586, 68, 1860], [13072, -968, 1752], [-2942, 4812, -5240], [14154, -2165, -2216], [-10514, 1680, -5448], [-3970, 2972, -5460], [-3370, 515, 50], [1216, 3126, 4260], [-122, -5324, -208], [3402, -4481, 4842], [-372, -2936, 2932], [9354, -3672, -1106], [-2968, -1376, 1428], [-7146, -4760, -2252], [7014, -104, 2032], [-13518, 2388, 1578], [170, -837, 1906], [-11754, -2857, 2406], [4380, 4896, 246], [-9446, 3320, -4426], [-1094, -4167, 2096], [-4988, -2568, -1452], [302, -3128, -4668], [-16528, 5422, 2248], [18650, 1991, 552], [21914, -3651, -3408], [16808, -6752, -1538], [5524, -4120, -230], [-5724, 1568, -7662], [2832, 5216, 6230], [-23286, 77, 954], [-9986, 504, -3116], [29262, 3016, -512], [-9774, 605, 6134], [5370, -3036, -2734], [3198, 9013, -3660], [7878, -3999, -2406], [5018, -1860, 7436], [-5370, -1973, 5140], [6686, -3384, 5802], [-18462, 7040, 3932], [-39732, -1762, 56], [2704, 10248, -5296], [398, 12380, -3670], [-14478, -6044, 1128], [-4404, -7668, -5820], [9378, 1335, -500], [-10210, -1773, 2150], [6966, -461, 5316], [3780, 4692, 4764], [-8190, 2508, -10028], [4502, -4904, 8944], [-8430, 9636, -1806], [-20214, 2101, 4330], [11620, -8596, -4706], [7752, 3654, 1132], [32628, 3534, -1592], [-7370, -5948, 7596], [-9086, 2089, -4104], [5754, -8103, -1488], [218, -8540, -8750]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_17_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_17_4_a_b();".
// 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_17_4_a_b(:prec:=3)
    chi := MakeCharacter_17_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 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_17_4_a_b();".
// 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_17_4_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_17_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![32, -24, -1, 1]>],Snew);
    return Vf;
end function;