
// Make newform 1617.4.a.m in Magma, downloaded from the LMFDB on 17 June 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_4_a_m();".
// 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_4_a_m();".
// 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, -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]];
    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 := [[1, 1], [3, 0], [-2, 3], [0, 0], [-11, 0], [6, 13], [54, 4], [24, 23], [-32, 8], [-38, 65], [168, -26], [-86, 107], [22, -86], [-76, 120], [-132, 163], [54, 58], [32, -53], [178, 80], [-16, -95], [496, -56], [322, -317], [-176, 124], [116, 30], [-130, 216], [1142, -18], [562, 476], [128, -54], [712, 315], [-778, -340], [-158, -12], [-488, -746], [-772, -308], [1754, -364], [308, 332], [-110, 525], [992, -838], [2282, 214], [-416, 1497], [1816, -1324], [578, -1630], [1700, 44], [-318, -750], [256, -1646], [-278, -1742], [-226, -1920], [4312, -812], [-204, -1138], [-24, 454], [-3404, 2014], [1330, -638], [-3878, -12], [-2356, 853], [3650, 1173], [-520, -195], [-366, 2775], [5148, -1509], [1914, -384], [-7124, -473], [1398, 818], [-1874, 329], [416, 767], [-4070, -1218], [-6100, 2696], [3576, -1992], [-6170, 184], [2926, -2718], [4644, -772], [6858, -1874], [9100, -212], [1078, 3209], [1674, -3119], [8496, -1612], [4632, -1986], [-642, -1836], [2992, 2647], [3072, -1520], [3094, -2626], [-2414, -1004], [506, -2378], [6998, -660], [2432, -5731], [-4174, -1087], [-3660, 3867], [5894, -5438], [-3580, -2809], [4236, -328], [-8438, -854], [4066, -342], [-8894, -1150], [612, -6035], [1992, 1439], [1400, -634], [-12856, 2560], [13560, -2181], [-17176, 647], [-2888, -2160], [13258, 3640], [4570, -2593], [-2648, 5145], [-3618, 8334], [-3236, -5796], [-1798, 8021], [-4276, 5196], [9562, -1448], [-13756, 7916], [-11362, 320], [-8072, -4935], [7998, -5934], [-7568, -4812], [-5806, -1785], [-180, -5873], [-20322, -2204], [5146, -11964], [-324, 7854], [360, 7644], [-14126, -3280], [-7812, 7608], [-12180, 8561], [10750, -2854], [24928, -3329], [6050, 594], [3514, -3666], [11346, -576], [-5612, -1538], [-5916, 6342], [-21050, -4888], [-7118, 11533], [1684, 4213], [-25896, -3796], [11394, 4760], [-11124, 12704], [3708, 8651], [-6780, -8419], [13562, -3317], [28630, -1690], [-3454, 1965], [14334, -8465], [17136, -9375], [14686, -7619], [-19714, -9691], [-8056, -12759], [15258, 5679], [-20740, 9445], [-4200, -2365], [-1462, -12546], [11732, 4931], [-17590, 1820], [-1890, 16640], [-7468, -9888], [-28616, 10828], [21742, -1614], [13714, 4335], [-248, 7231], [8464, 5254], [-16508, 2656], [6432, 13322], [3640, -5396], [19914, -21615], [-30090, -2224], [44882, 3286], [10652, -5094], [-10066, 23341], [8256, -16678], [28440, 441], [16394, 13066], [-23272, -8632], [11396, -25851], [-438, -11404], [-25438, -14572], [-4722, 5539], [-18408, 10891], [-40110, 9728], [36784, 1792], [-3782, 16088], [-5752, 3182], [21630, 2076], [55764, -6404], [-24738, -13704], [16124, -3627], [-30338, 8299], [31972, 11237], [3580, 1088], [16606, 2644], [16166, -25530], [-19400, 18020], [-61298, 2987], [49470, -5762], [-14476, -8650], [18874, -3960], [30904, -6230], [-38242, 12372], [24880, -9387], [-49424, -8255], [-25314, 27741], [24880, -7573], [22450, -8607], [38066, -18736], [38950, 604], [-21842, 32318], [-26264, -12140], [-9722, 14244], [28644, -851], [-50190, -1854], [-5430, -16673], [63108, -160], [14570, 16936], [37312, -8224], [22168, 1593], [-20838, -4496], [16692, -23444], [-26302, 26404], [41222, -567], [-45084, -12633], [-35356, 8188], [18512, 2286], [-4010, -18224], [-672, 1372], [77142, 2716], [-1512, 4898], [12438, 18052], [59534, -12976], [36012, -8323], [-53846, 8026], [-42964, 11063], [-19076, -8008], [-5602, 5792], [2770, 27977], [15152, -21866], [17052, 9293], [5312, -25367], [33318, -12792], [12960, 21333], [-11016, 13526], [36926, 645], [37716, -1816], [-22456, -650], [44622, 24536], [95810, -6879], [14788, 16118], [2660, 37111], [29828, 13312], [-10364, 25696], [37936, 8534], [21222, -35756], [4574, -38770], [42328, -16472], [58192, -27684], [45736, -11669], [-30892, 49100], [28596, 28435], [-42698, 24748], [31422, -18186], [-55272, 40242], [-46490, -900], [-37254, -31220], [-62076, 32840], [85834, -3145], [40760, -10479], [-89146, 11297], [51346, -21413], [97112, -2666], [47764, -9024], [25082, 32828], [-53298, 8163], [76426, 7893], [18420, 41954], [49702, -13796], [37242, -17793], [-44, 44372], [-6002, 10204], [-5286, -17146], [-57760, -29719], [-79394, 11886], [-7592, -4478], [-36294, -37209], [13664, -32846], [-96148, -2800], [84214, -18964], [60602, -4200], [15692, 51624], [52768, 30656], [36136, -56368], [51376, -40118], [39142, -67537], [110960, 387], [-17832, 54544], [-56730, 54865], [49950, -57068], [40720, -27070], [4150, 23576], [-11082, 12768], [22876, 19052], [55646, -32403], [59204, -32448], [27534, -48106], [10402, -1914], [77000, -17498], [62018, 5984], [-78300, -696], [45384, 22895], [13034, 7029], [55314, -13189], [-39296, -2008], [51396, -66716], [-77480, -8675], [-56810, -239], [-124948, 6912], [-38214, 55422], [-132832, 7708], [119206, -13622], [47176, -61338], [-57914, 48686], [-156298, 2813], [-38148, 4880], [-13088, -61080], [-133770, -12708], [93660, -34712], [-26168, 12656], [-32522, 12522], [15298, -56280], [63432, -57049], [-24530, -55339], [-113110, 8940], [81312, -34728], [-110394, 4895], [25798, 614], [-82740, -16296], [101332, -4012], [-37668, -35207], [-50654, 10653], [89038, 28702], [-129714, 32778], [-93508, 45995], [-107340, 46338], [63332, -67228], [-52752, 23199], [-33654, 68529], [-58162, 19398], [9622, 76464], [44144, -99712], [12254, 5060], [-40166, 208], [-83230, 8600], [-72252, 20151], [14918, -90744], [139072, 17587], [-34086, 30812], [-188380, 13876], [-34368, -52532], [-65230, 27100], [90252, -25248], [-41930, -30381], [62510, -27938], [-18628, -47357], [-208802, 1884], [-125714, 26004], [-49172, 85743], [-135836, 25620], [-25674, -29291], [18784, 3028], [-86650, -28222], [43814, -69062], [206680, -8400], [152632, -38789], [92252, 24928], [-32578, 41493], [56474, 8492], [18864, -22666], [-115702, 48272], [-97684, 35410], [54972, -16026], [-66212, 8009], [-14070, 65891], [-131800, 4208], [-117414, 51185], [-212320, 14009], [183740, 5037], [176266, -45525], [-7418, 85400], [74662, -5984], [59838, -68363], [-63726, 33166], [-96992, -36626], [-101690, -24931], [-27908, 22456], [-178956, -5269], [120692, -26643], [-51398, 27436], [-28244, 49210], [-115036, 793], [-156738, -40637], [-77198, -38884], [29868, 33944], [176812, 8576], [-76904, 43066], [-25654, -42376], [-171540, 31419], [89586, -13227], [47524, -79460], [-93902, -12211], [-148250, 47417], [70498, 69352], [5992, 35722], [-191778, 39268], [-72838, -13042], [64348, -102667], [-71574, 9801], [-165582, -1336], [-47660, -25506], [-40756, 42964], [182206, -4056], [-88258, 49444], [-676, 12144], [-36980, -47568], [-168918, -33972], [-118714, 26079], [110740, -17689], [-15888, -67546], [203478, -46536], [45296, -5958], [134742, -2636], [9854, -65515], [-25360, -77014], [61340, 37474], [58310, -77788], [225942, -23973], [135324, 41158], [-24370, 4777], [-85444, -84658], [-83304, 110040]];
    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_4_a_m();".
// 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_4_a_m(:prec:=2)
    chi := MakeCharacter_1617_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(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_4_a_m();".
// 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_4_a_m( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1617_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-2, -3, 1]>,<5,R![-38, 1, 1]>],Snew);
    return Vf;
end function;