
// Make newform 1856.4.a.l in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1856_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_1856_4_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_1856_4_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 := [-6, 0, 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_1856_a();"
function MakeCharacter_1856_a()
    N := 1856;
    order := 1;
    char_gens := [639, 581, 321];
    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_1856_a_Hecke(Kf)
    return MakeCharacter_1856_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, 0], [1, 1], [5, -6], [-8, -8], [45, 3], [25, -8], [-22, 16], [-54, 4], [-14, -78], [-29, 0], [33, 109], [-20, -4], [152, -80], [65, -53], [-257, -99], [479, -52], [90, -250], [-514, 12], [456, 20], [398, 34], [-428, 176], [-159, -361], [914, 38], [-472, 72], [184, 100], [56, 472], [890, 58], [500, 276], [997, 478], [-42, 680], [-274, 248], [-546, 604], [252, -548], [2390, -190], [861, 344], [152, 380], [-948, -596], [195, -513], [958, 146], [1614, -392], [-2942, 362], [-2737, -270], [4462, 160], [-1252, -1480], [2070, -424], [-2608, 588], [-2193, 571], [2182, 1206], [4734, 122], [2000, 1720], [-3049, -28], [3318, 1582], [-2503, 680], [1413, -2335], [2983, -1054], [2253, -2015], [774, 1616], [3131, 571], [-6610, -224], [207, -2440], [6446, -1074], [-890, -1848], [-155, -1949], [4606, -1424], [4467, -994], [2034, -1668], [4727, 2011], [3190, 444], [1466, 3270], [-1881, -444], [5674, -696], [-5323, 743], [-5030, -1316], [-5325, 1662], [3694, 1336], [-3316, -2656], [84, -1644], [-3949, -1950], [2331, 676], [30, -300], [-2066, -1894], [-11642, 1080], [-2950, 3402], [584, -3056], [2150, 4070], [2462, -3140], [1694, -4864], [5074, 2840], [6442, -3064], [1522, 5706], [-3327, -4037], [-18233, -337], [-1516, 420], [1875, 1873], [1682, 2558], [-2129, -73], [12675, 1560], [-15163, 1950], [6552, -2300], [6080, 2768], [-7568, 1264], [-5530, 1176], [-10693, -785], [-4938, 2268], [-2744, -2148], [6578, 2116], [-11590, 5226], [-7815, 3378], [-1271, 7331], [4640, 2852], [-13089, -3457], [14869, 1798], [-7572, 1756], [-6589, 2659], [3682, -3510], [10186, -2804], [23960, 1332], [6910, 7154], [2132, -3220], [-1683, 293], [13914, 3624], [-4215, 6788], [7198, -6612], [-2436, -4492], [14104, 864], [-255, 7828], [2793, 812], [8998, 1642], [-6250, -1016], [-4332, 6984], [-13699, 3389], [-25678, -5024], [4310, 9012], [-20078, 5384], [3126, 11616], [-10910, -540], [-6392, -1456], [-1118, 414], [-18034, -5140], [16728, -9988], [15600, -1072], [-24663, 8736], [31838, -3400], [-1561, -10433], [-922, -5908], [13431, -8711], [3598, 3384], [-7509, 2798], [23683, 10237], [4382, -6034], [18767, 5004], [-1050, -188], [18140, 5812], [-6831, 7065], [6230, 11596], [-22885, 9827], [-21426, 6426], [5214, -9944], [-22466, -12720], [5405, 1038], [2025, 15931], [4499, -6736], [-43097, -5019], [12346, 4732], [-43997, 6], [-16805, 7973], [-6950, -6858], [-14048, -4356], [-3793, 19078], [-15868, -11260], [11916, -2456], [-4795, 9418], [-44342, -2626], [-4486, -1216], [-22566, -1394], [3882, 3448], [-3880, 15028], [2202, -8004], [40553, 8575], [-32182, -8808], [-45851, 1155], [8319, 8819], [19426, 2656], [15743, 11946], [-1900, 368], [16127, -8662], [35922, -10836], [-25562, 9864], [-11178, -25092], [21780, 12392], [-28339, -13608], [27741, -15999], [-18735, 2263], [-6140, 13748], [-11094, -7280], [-38430, 12088], [-18732, 13504], [10770, 5112], [-6555, -22476], [27648, 12360], [9416, 8984], [3222, -15390], [-18924, -900], [14736, 22504], [-27902, -1088], [13031, -19070], [29409, -18289], [-14334, 25486], [-27627, -10498], [-34949, 1297], [61932, 212], [-19854, 9032], [7355, -11157], [20333, -20177], [-59514, -12736], [30327, -310], [-5696, -17100], [58676, -664], [10480, 14624], [-47738, 19880], [-51818, -924], [29740, 9260], [65630, 10100], [-40563, -18169], [2076, -11020], [31822, 19308], [-7872, 22980], [5154, -12024], [9559, 26485], [6056, 11992], [39108, -16816], [62008, 3912], [-27922, -7536], [-14288, 18420], [26802, 2370], [-21654, 3768], [-45694, -16356], [11360, -28328], [-66236, -3364], [6429, -21367], [34411, 23021], [-11486, 23994], [55538, 530], [-33964, 7224], [-20698, 18296], [37986, 13034], [36354, -14050], [6214, -7566], [-31514, -7866], [-25963, 11131], [-134, -10240], [27109, 998], [96639, 3155], [70096, -19856], [-45372, -12596], [77112, 3708], [-32444, 7392], [57525, 11501], [-33690, 12816], [22425, -31616], [26842, 1656], [69549, 6167], [-77366, -3344], [-8110, -15128], [19734, 11380], [-41039, 24699], [-67600, -360], [7830, 29452], [-9362, 84], [72957, 19230], [-71537, 7040], [-13594, -45270], [17765, 28368], [-20170, 50720], [24172, 5672], [61285, 8413], [-23646, 13952], [73211, 4054], [-1212, 27984], [37458, 10630], [-25266, 16854], [-60978, 30830], [-6652, 27276], [8030, 17928], [-42870, 27436], [-68982, 7784], [49414, -4956], [-41996, -35568], [-62816, -37484], [-11663, 4308], [3919, 36874], [-24962, 32498], [-42675, 1388], [97149, -21911], [40234, 11408], [-9503, 12388], [-31189, 55201], [10587, -11440], [45520, 4220], [-46810, 18436], [-55870, -13272], [-26414, 54600], [-23590, -4400], [35997, -45045], [83262, -29404], [56235, 25194], [11511, 50415], [-79267, 16372], [-18268, -32732], [16399, 45338], [-54710, 7158], [22228, 37772], [-28674, -31104], [45342, -1610], [87000, -21116], [64055, 680], [-107734, 9444], [91806, 17746], [70351, -44538], [39486, -3076], [21433, -27995], [-221, 44170], [-78150, -792], [16591, -41853], [-100521, 7384], [-15236, 9308], [122510, 5770], [-105296, -6892], [-68231, 44675], [-46527, -18866], [68212, 3736], [-108075, 29456], [83752, 19428], [-31682, 244], [16799, -25011], [27684, 392], [83922, 17368], [-105858, -10484], [-27990, 27452], [-120908, -13908], [-19574, -32304], [45418, -12104], [-43950, -23280], [-49428, 16220], [13910, 62032], [139077, -17437], [-54306, -10476], [64702, 34084], [109149, 13023], [-8802, -38232], [170878, -12930], [-38166, 67840], [-97698, 11956], [-101110, 19298], [44474, -59004], [-24638, -63784], [-25771, -71499], [80132, 46776], [-64326, 37660], [-18450, 73098], [78363, 24918], [13209, -27892], [-131283, 2707], [-31138, -53262], [-34215, 5351], [-23040, -42872], [177988, 22708], [217562, 7606], [-60494, 3836], [-46881, -78281], [168602, 2898], [-103152, 50180], [77654, -55704], [41874, -24982], [-65127, 5278], [32110, 3500], [104475, -8433], [-62802, 620], [160021, 36654], [174551, -30362], [53940, 21608], [128979, -6686], [-13045, 37049], [63210, 24944], [-109124, 4064], [-182556, 1364], [-12922, -8376], [-54631, -4068], [-51049, -14689], [-78202, 47496], [-24542, 46592], [54387, 59420], [-104503, 34713], [101455, 49081], [29361, -6879], [105371, 29042], [68032, 25008], [-139160, -18120], [105458, 9590], [-26026, -8896], [93442, -10952], [-5842, 26340], [146794, -18240], [58949, -13580], [74965, -37040], [25906, 37366], [124757, -54566], [-69934, -53224], [134533, -6061], [3868, 83992], [-143434, 22752], [-19689, -7660], [-29448, 31240], [-83710, 79678], [-97580, -2276], [-55702, -36860], [-91425, -44605], [99978, -70294], [-61248, -84008], [137329, 6999], [158238, -36088], [4034, 72492], [-57189, -53157], [-52905, -11855], [74431, 66492], [56949, 21742], [38238, -29106], [16660, -11160], [-50970, 35438], [19187, 50303]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1856_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1856_4_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_1856_4_a_l(:prec:=2)
    chi := MakeCharacter_1856_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_1856_4_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_1856_4_a_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1856_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![-5, -2, 1]>,<5,R![-191, -10, 1]>,<7,R![-320, 16, 1]>],Snew);
    return Vf;
end function;