
// Make newform 1840.4.a.i in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1840_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_1840_4_a_i();".
// 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_1840_4_a_i();".
// 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 := [-18, -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_1840_a();"
function MakeCharacter_1840_a()
    N := 1840;
    order := 1;
    char_gens := [1151, 1381, 737, 1201];
    v := [1, 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_1840_a_Hecke(Kf)
    return MakeCharacter_1840_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, 0], [8, 1], [-4, -10], [-9, 9], [-34, -11], [-12, -10], [23, 0], [-55, -2], [33, 26], [-242, -7], [-129, -74], [166, 22], [297, 5], [-156, 7], [-126, -105], [-92, 12], [286, -41], [-679, 104], [-183, 23], [16, 56], [-578, -117], [562, -18], [-1038, -164], [600, -87], [-220, 308], [66, -185], [-202, -12], [972, -17], [1647, 111], [-261, 391], [-1496, -50], [-161, 242], [838, -390], [627, 143], [-1768, 25], [-1793, -455], [-240, -680], [-366, 546], [-7, 505], [-324, 972], [-942, 602], [709, 753], [-507, -1033], [-88, -206], [962, 291], [964, 310], [-1446, 94], [-2510, -330], [223, -273], [-4051, 78], [-1142, -416], [-1020, 546], [-145, 1507], [-4174, -171], [-159, 1048], [514, 433], [773, 979], [542, -632], [-3104, 1039], [-1678, -613], [-3472, 502], [1751, -2073], [-8290, 23], [-4594, 286], [-1529, 1036], [2786, -1036], [-580, 524], [-4539, 1598], [2393, -575], [9648, -652], [-466, -711], [986, 1288], [5120, 1140], [-3660, 695], [-5060, 2354], [-2077, 123], [3774, -324], [-1195, 152], [6578, 1756], [-5238, -1172], [-2166, 82], [2064, 563], [397, -1819], [-595, -2735], [-6796, 1025], [-2130, -1661], [-2677, 2493], [-96, -586], [516, -3623], [4816, -724], [4779, -3269], [-13749, -1038], [2641, 248], [9728, 271], [-15677, -199], [10808, -1886], [11810, -2682], [-17137, 129], [-1089, -2729], [3656, 19], [94, -2375], [-6834, 1824], [-11484, -2540], [15461, 233], [19417, 1695], [-306, 4674], [-4200, 3736], [-6079, -1666], [-11536, 3104], [11060, -1182], [16282, -2821], [-10020, -2648], [-7202, 2418], [19450, 1608], [-14780, -1271], [273, 579], [-8789, 2637], [-10524, -2466], [5502, 5248], [6615, -1285], [21944, 2413], [-3033, 3283], [-1864, 3548], [-11850, -978], [6442, -2022], [22326, 871], [12520, -3309], [-6786, -3855], [7839, 2424], [3740, 1636], [12266, 3740], [6682, -6889], [9341, -3016], [41286, -30], [-14874, 3764], [-5966, -5281], [6446, 4411], [-4680, 609], [14897, -3118], [-15574, 24], [18757, 2301], [14962, 3865], [40768, 1055], [14978, -4256], [6806, -2640], [-16017, -1359], [-17739, 3698], [31143, 573], [-1454, 6892], [-26444, 2228], [25020, -5408], [48273, 955], [19242, 2587], [3372, -1904], [24302, 5926], [18603, 6816], [-27480, 4642], [12956, 6068], [22837, 1957], [16298, -8124], [-21193, 1095], [-41798, -3478], [18810, 2879], [4932, 3093], [-39838, 1783], [12694, -554], [16576, -8564], [-44889, 1463], [-25372, 3220], [-7411, -500], [-7020, 1334], [44734, -3807], [-13175, 4465], [39282, -2718], [2701, -2335], [-5869, -100], [19578, 9105], [20388, 5858], [46991, 1461], [-40866, -2758], [23993, 9107], [13374, -5472], [-33536, 8259], [2268, -15294], [-40558, 4284], [-48454, -4631], [18521, 2608], [-27738, -10551], [-26857, -2959], [28636, -6010], [-846, -11924], [-18316, -10941], [-24214, 1090], [-13096, -2891], [23244, 902], [-18428, -6974], [15888, -14242], [12675, -6503], [25396, 11136], [3160, -2212], [8353, 1939], [29048, -8902], [-21592, -12124], [16365, -3101], [8356, -4388], [45033, -1825], [-16021, -7854], [-30023, 2221], [-17445, -11127], [-35963, -2593], [-6610, 3470], [-10802, -142], [2358, 17339], [15018, 1886], [-13167, 9695], [34829, 5380], [6396, -14713], [-56669, -287], [-45497, 646], [-54852, 9706], [-64803, -1918], [42196, 2719], [2965, -16261], [-23041, -2500], [-16368, -15762], [26523, 620], [-7010, 5658], [-80398, 6405], [60394, 1616], [3090, -10416], [24754, -5212], [-33928, 10493], [5246, 663], [-36930, 12882], [-9092, 2028], [-40044, 1014], [26773, 3206], [-23697, 3638], [8010, -5001], [35769, 9018], [35565, 4979], [-79679, -2541], [2126, -1814], [-43297, 3459], [-7747, 5669], [-546, 14336], [13010, -7360], [-12736, 18121], [20544, -23570], [-4014, -4792], [-30036, 6103], [-28554, 5368], [-35370, -4024], [11991, -2016], [-59864, 18388], [-14850, 14110], [-68039, 1583], [86361, -8657], [-58958, -10347], [-24512, 16415], [-53540, -15115], [-17632, 23845], [79046, -12470], [-24680, -1920], [11520, -816], [-6758, -20244], [7510, -14139], [-58587, -9751], [-14587, -10848], [76984, -5487], [-24904, -2637], [38102, 8756], [35889, 19237], [-4315, 7531], [-98508, -2548], [8056, -30009], [-60882, 11170], [-129520, 2764], [30707, -25719], [66190, -19382], [-71164, -16652], [-130882, 830], [-4532, -16098], [-3642, -8683], [-41092, -12969], [61884, -13530], [61396, -8068], [27386, 8416], [-21198, 34664], [-12014, -4051], [29510, 16802], [-24004, 16800], [-33487, 32089], [2882, 23554], [26444, -29550], [-11157, -3595], [76491, 16290], [80563, 1301], [53404, -1255], [-66418, 6769], [84188, -20248], [56747, 14487], [91220, -4824], [-9513, 14083], [-130800, 6906], [-72612, 16166], [19450, 8258], [34526, -33126], [-59303, 12005], [-37320, -17042], [24496, 22368], [58955, 15131], [138966, -3699], [-40282, -25262], [-60549, -1216], [-176547, 246], [94474, -2339], [-41239, -29717], [124382, -17826], [-19968, -3591], [115653, -11845], [18812, 34814], [47748, 4639], [-93392, -120], [42486, -16510], [-79689, -16123], [-53040, -21624], [-166962, -2985], [93981, -19530], [-80589, 4981], [-40501, 9002], [-159861, -2959], [-31576, 21940], [81727, -8069], [88962, 17936], [48758, -424], [94257, -21961], [12488, -24543], [54811, -19415], [34334, -45560], [148746, -2184], [38030, -23474], [-18020, -26927], [-56910, -18959], [143739, 7522], [-67996, -23642], [-115194, -5400], [22448, 8605], [-158759, 7252], [41809, 11205], [-46140, 20297], [-41624, 13519], [125768, -10888], [-84874, 10436], [-141540, -18606], [-9114, -31776], [-44239, 24759], [27788, 4402], [-101480, -3773], [176601, -3604], [80643, 29657], [-107306, -6310], [67507, 17833], [85777, 4747], [5467, 231], [-88406, 7247], [-18466, -17390], [217255, -7190], [45550, 36823], [18441, 10355], [62280, -23652], [121048, -5468], [28235, -21915], [-3413, 4644], [139048, -898], [27484, 6219], [-203212, -4280], [79367, -15915], [116062, -4576], [-140616, 33958], [81624, -7648], [46111, 21837], [-132630, 27458], [30495, 1005], [154587, -14896], [80846, -2126], [22400, -26847], [90754, 3603], [33260, -20802], [-120446, -9056], [146349, 7680], [68636, -6268], [6054, 28244], [70704, 6535], [17986, -9512], [-7026, -22986], [-160116, -14480], [-44220, -40497], [236951, -1572], [-105379, -26629], [-172272, -24109], [-167934, 3459], [89399, -39764], [121285, 17152], [143370, -3977], [-12185, 16565], [72126, -27934], [43532, -13664], [15679, 55155], [-30069, 15877], [-25100, 48080], [122150, -13598], [-255686, 1047], [-60569, 38701], [158192, -18080], [-83176, -33800], [-27200, -42097], [31802, -29522], [10190, -25952], [98986, -19648], [24228, 7134], [-110740, 8080], [44849, 13953], [-26269, -41549], [14996, -22336], [-135492, 48641], [-131473, -31333], [-4603, 60808]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1840_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1840_4_a_i();".
// 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_1840_4_a_i(:prec:=2)
    chi := MakeCharacter_1840_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_1840_4_a_i();".
// 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_1840_4_a_i( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1840_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![-16, -3, 1]>,<7,R![54, -17, 1]>],Snew);
    return Vf;
end function;