
// Make newform 1386.4.a.z in Magma, downloaded from the LMFDB on 04 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1386_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_1386_4_a_z();".
// 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_1386_4_a_z();".
// 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 := [-2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [0, 2]];
    Rf_basisdens := [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_1386_a();"
function MakeCharacter_1386_a()
    N := 1386;
    order := 1;
    char_gens := [155, 199, 1135];
    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_1386_a_Hecke(Kf)
    return MakeCharacter_1386_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 := [[2, 0], [0, 0], [-1, 2], [7, 0], [11, 0], [-31, 7], [-46, -19], [-23, -35], [-100, 15], [-93, -67], [-28, 3], [-59, 8], [94, 40], [-138, 11], [41, 30], [170, 5], [49, -196], [-150, 236], [-443, 54], [-422, 218], [187, 137], [-246, -222], [-206, 121], [-226, 156], [170, -119], [300, -19], [-396, -326], [-289, 347], [-904, 297], [-124, -632], [-1200, 243], [206, -22], [-538, 337], [-60, 348], [-765, 227], [-846, 735], [-438, 184], [-1755, 144], [176, 429], [2098, 290], [-558, -22], [-80, 53], [568, 793], [6, -1499], [1782, 456], [-1562, -733], [-1560, 1216], [1312, -226], [2630, -1000], [-4824, -301], [270, -562], [3517, -543], [1229, -123], [-1305, -50], [3035, -986], [1571, -751], [-170, 1654], [-539, 65], [-700, -2176], [2299, 369], [-3475, 785], [3176, 1616], [-2592, -494], [5240, -1614], [-2970, -1225], [1252, -109], [1828, -122], [-3264, 633], [2444, -526], [-9603, 387], [3209, -2366], [2016, 2220], [1010, -336], [-3144, -3297], [-3533, -2660], [-1260, 472], [1510, -504], [4666, 1557], [-3454, -395], [1166, 2340], [4143, 2200], [-5085, -320], [-7937, 755], [8850, -2636], [213, 5993], [11068, 1400], [-1924, 394], [684, -1057], [912, -1366], [11773, -136], [7405, -1824], [5668, 1927], [6140, 2792], [2963, -4231], [-5509, -2288], [-400, -1569], [7430, -824], [-16629, -328], [13755, -2445], [5478, -1610], [-4348, -3890], [-4813, -3807], [-4724, 493], [-10286, -3408], [-6904, -2912], [22188, 791], [3995, 2804], [2100, 78], [-14810, -1516], [10435, -1341], [13669, 1761], [6018, 5707], [-6002, -5364], [13602, -5073], [-9396, 2748], [-12644, -995], [17394, 4565], [-21625, -3516], [-23820, -2240], [22455, 1491], [18374, -1497], [1616, 3707], [15480, -6244], [1374, 2875], [-7782, -9328], [-86, 12376], [7035, 1342], [5887, -6858], [-20316, 4539], [16034, -816], [-29680, -633], [-1057, 11309], [19657, 2812], [-15667, -1936], [4486, 10104], [-22875, 4185], [-11349, 186], [18823, -2671], [-21931, -5900], [2859, -839], [-24773, -5177], [17229, -7935], [-14583, -1716], [13603, -6557], [7748, 4824], [-2243, -2904], [-16702, -7584], [-9696, 9448], [42190, 774], [-7868, 6789], [29268, 1498], [-14225, 2274], [-8805, -1374], [-134, 3875], [-3572, -12854], [18316, 1170], [32126, 3541], [14157, 3780], [-13254, 1964], [34982, 5666], [14854, 5050], [-16157, -10463], [-14624, -1190], [10915, 13446], [-7156, 12453], [29164, -8916], [-14147, 1378], [13834, -3026], [-32466, -3382], [15623, -1008], [6395, -3513], [-10428, 990], [4276, -8150], [-36380, -1685], [29416, 5858], [-30354, 1470], [-30280, 7207], [-47742, 3450], [-31969, -2513], [-6901, -11117], [-7239, -10088], [30556, 6661], [-31714, -8982], [-47688, 20], [-17220, 2936], [-37653, -4750], [1044, 15074], [13412, 19203], [10566, -9507], [-16886, 13783], [1790, 20698], [-11567, -3099], [15729, -5520], [32905, -2138], [-16173, 5727], [-47695, 4242], [-22398, -13231], [-22946, 11236], [-45248, 6365], [24906, -20711], [-22362, -13080], [-65917, -2593], [24460, -14507], [5891, -4371], [-39708, 9364], [-46958, -7144], [26114, -5938], [13705, 13981], [20518, 14490], [16562, -4883], [-1172, 334], [-40875, 834], [9519, -23340], [-29164, 3358], [34416, 2010], [-6592, 4774], [-30978, 6050], [-72308, -7201], [8432, -8851], [-54222, -547], [35310, 10193], [-54345, 7371], [-12622, 19803], [4623, 15590], [-77714, 5850], [-53840, -3706], [-41115, 724], [-58212, 5180], [-45417, -513], [32981, -10579], [1458, 25448], [36995, -7607], [-32598, 4112], [-17273, -23781], [-486, -21405], [12100, 25262], [-8856, 6373], [-26993, 25837], [-13574, -13281], [-40395, -8710], [30208, -14241], [-29688, -21650], [80590, -6693], [10926, -19418], [25468, -14512], [-63594, 3122], [5872, 12814], [-51027, 4072], [38554, 6416], [21135, 21069], [36138, -21002], [9722, -17769], [70982, 5832], [3950, 15040], [14256, 18772], [82192, -6566], [-75303, 1936], [90815, -9283], [-105947, -6916], [108551, -4609], [-16906, -7840], [-18680, -26842], [88402, -7796], [-5053, -16495], [-15251, -14156], [3182, 38037], [37846, 11412], [59939, 4564], [9066, 8010], [48394, -5476], [23008, 10792], [-41927, 1464], [37450, -17743], [66786, 17495], [77887, -16947], [41902, -2027], [23222, 7234], [-33236, 8702], [-65010, -559], [45900, -8243], [17426, 2543], [87440, 19182], [19740, -27897], [-23385, -3219], [10193, 29705], [11362, -30523], [-85617, -13756], [-21378, 30234], [-39896, 24663], [-54236, 13101], [-48632, -11525], [54036, 9928], [101463, 18979], [40776, 26045], [-86462, 1642], [49526, -28539], [-27298, -33902], [-58646, 16392], [35346, -5105], [34145, 16135], [48173, 3177], [26693, 47933], [31176, -21109], [70004, 20521], [-49847, -3512], [12067, -28466], [42588, 33887], [37244, -34389], [69516, -9985], [84682, -2291], [36836, 42142], [-23284, 17178], [140803, 8461], [72916, 18264], [-60192, -22004], [26522, 27556], [-46920, 9580], [171736, -3640], [12212, -6643], [5778, -34960], [-5655, -18727], [83099, 7584], [-66624, -13342], [14344, 41878], [-89787, -37221], [18162, 45724], [-33700, -7622], [-53680, 17973], [78293, -27629], [-57801, -43901], [21768, -23734], [43502, 35484], [9069, 2319], [174732, 4551], [-66270, -1043], [1641, -50468], [8275, -43892], [-92328, -31387], [27996, 17959], [100044, -362], [75306, 5104], [-75638, 14043], [66452, 22013], [-41065, 45704], [100624, -30991], [17135, 32993], [-45340, 112], [-3576, 15048], [-144338, -19354], [-28894, 35582], [-7168, -65170], [-4107, -50488], [41854, -54690], [-74775, -10143], [27266, 54327], [-29022, -14757], [-3475, -35094], [169022, -2532], [-31365, -47949], [-92986, 31037], [-92078, 11018], [25490, 33002], [-62046, -10238], [176583, -987], [52590, -51922], [2639, 646], [-162508, -525], [-100358, -30294], [21936, 29754], [83396, -22697], [-51950, -57965], [14173, -11493], [-99831, 25401], [-16, -270], [1079, 17862], [-157205, 2504], [67069, 27619], [40355, 35915], [16134, 23470], [31298, 45704], [107377, 3438], [66618, 11057], [165048, -18922], [-102185, 4787], [80132, 34766], [9213, 56442], [-31419, 21990], [39738, -58260], [29472, -44473], [-215193, 10478], [-91827, -54942], [-165282, 5760], [9908, -57801], [-2082, -25279], [20666, 39464], [-79108, -13865], [-163935, -35441], [-10347, -20118], [-15512, -6720], [35411, -14955], [30517, 65751], [45068, -73253], [-101672, -56639], [-108158, 38392], [48222, -95], [-31889, -11355], [-119289, 30890], [98514, 39814], [-49250, 74426], [-9156, -64064], [-229070, 15686], [-90538, -44726], [-77656, -816], [-118552, 28659], [28274, -7536], [125971, 844], [-131113, 741], [-79558, -24379], [-12718, -55354], [93060, -32385], [-93010, -30835], [119233, 39639], [114184, 25605], [5120, 8439], [-135170, 2986], [-85085, 138], [217250, -21637], [190847, -19269], [-21518, -31130], [-58280, -59198]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1386_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1386_4_a_z();".
// 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_1386_4_a_z(:prec:=2)
    chi := MakeCharacter_1386_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_1386_4_a_z();".
// 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_1386_4_a_z( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1386_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![-31, 2, 1]>,<13,R![569, 62, 1]>],Snew);
    return Vf;
end function;