
// Make newform 841.4.a.a in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_841_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_841_4_a_a();".
// 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_841_4_a_a();".
// 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;
    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_841_a();"
function MakeCharacter_841_a()
    N := 841;
    order := 1;
    char_gens := [2];
    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_841_a_Hecke(Kf)
    return MakeCharacter_841_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 := [[1, 1], [5, -3], [-5, -4], [-8, -10], [13, -37], [-13, 26], [-30, 18], [110, 32], [26, -48], [0, 0], [147, -63], [-156, -56], [-20, 138], [161, 171], [65, -207], [501, 122], [-450, -248], [474, -178], [160, -484], [-330, 34], [-324, -640], [-129, -341], [606, -64], [-380, 522], [-12, -578], [1176, -140], [-926, -206], [-444, -186], [-229, -896], [626, 312], [-410, 1124], [898, 1032], [-236, -344], [578, 580], [599, 1578], [792, 646], [1712, 678], [807, 951], [742, 182], [-2902, 260], [1494, 1454], [-2271, -540], [-3378, 816], [1784, -814], [2026, -464], [384, 730], [2923, -1433], [-26, -1586], [1234, -2342], [188, 2494], [-737, 2304], [1270, 1888], [-2007, 24], [217, 721], [-773, 2610], [2063, -1239], [-1342, 2250], [-4575, -433], [5442, 2280], [2647, 3684], [-3462, 288], [518, -2380], [1437, 731], [5246, 288], [-8417, 90], [-402, -1538], [-4237, -2429], [-5922, 1606], [-9826, 770], [2277, -3126], [-4438, -476], [-5549, 727], [-894, 976], [5669, 3100], [5714, 4164], [-4860, 494], [4700, 1516], [5701, -160], [2819, -1364], [-6654, -1028], [-1094, 3770], [-4542, 3886], [3274, -9456], [-1128, 9872], [-9106, -3768], [-486, 5308], [-9822, -2396], [2082, -3460], [12202, -1808], [-6014, 9148], [-6159, 4443], [-4243, 6155], [2092, -5482], [611, -4383], [-10082, 4152], [1893, -8973], [17945, -2094], [-12951, 1542], [2304, -6960], [10848, -8092], [-1368, 9950], [-6318, -6120], [-4697, 10703], [-1102, 7044], [-2872, -1560], [-6646, -9766], [-3010, 5208], [11637, -5158], [12503, 183], [-6396, 6506], [-939, -7845], [883, 5028], [-12140, 1568], [14311, 771], [-974, -1982], [11090, -8170], [15168, -5346], [966, 4816], [4656, 14022], [13473, -10171], [430, 3816], [12449, 10456], [-7666, -936], [-11716, 13188], [-3296, 6032], [-18245, -10938], [-14325, 1158], [4886, 18186], [-4090, 256], [5500, -4672], [16817, 8705], [-6670, -3884], [-1282, 18632], [342, 8258], [6806, 1732], [-3486, -17822], [-6852, 11198], [-19074, 9770], [-19014, 8054], [33064, -3412], [10864, 13426], [7635, -3954], [14726, -10500], [1163, -25101], [1714, 9186], [20585, 1145], [16014, 17028], [1199, -9958], [35563, -867], [25886, -10622], [3605, 858], [12354, 12616], [15452, 10806], [8771, 24493], [-1710, 6860], [7265, -22241], [-10522, 26830], [638, -7016], [-12458, 7472], [27971, -9056], [-2439, 23823], [4155, -11792], [-15447, 23897], [-12418, -10444], [11751, -4962], [46493, -1783], [-31446, 12314], [-23056, -14228], [-55373, -1866], [8596, -36848], [53660, 1786], [-749, -22764], [46530, -6270], [21406, -3336], [-29598, -1424], [1690, 3396], [-7248, -648], [25414, -14698], [-18761, 19539], [-18802, 18732], [-677, -3361], [6387, 10627], [-16458, -31256], [-64741, -46], [38556, -10888], [31481, -17308], [11006, -12734], [5274, 3228], [10126, -31302], [9852, -35650], [-6755, -3304], [-12639, 14809], [-14487, -31993], [-2788, -28248], [-26490, 24836], [43218, -15820], [-33148, -14600], [10502, -9660], [-49099, 8920], [-36320, 16164], [51764, 14978], [32078, -34924], [-512, 13358], [-22964, 19902], [-1522, -39572], [13649, 45124], [-25545, -17797], [26366, 17930], [-17415, -12766], [29475, -9243], [-45128, 23850], [28846, -3456], [-70039, -9313], [38613, 1615], [-12138, 23240], [-12229, 45570], [45264, -3668], [-29284, 19396], [-51472, -6056], [53646, 27748], [-38430, -17490], [62996, 11810], [-32686, 14380], [4435, -30161], [44756, 31728], [22822, 14412], [29792, 1736], [22274, -5240], [50617, 8337], [-18448, -46386], [46412, -36520], [12384, -19980], [-51474, -17260], [-17056, -50396], [16734, 41642], [-36222, -23596], [21770, -31318], [-36052, 32382], [31964, 2002], [9215, 16941], [4419, -4775], [-51746, -35178], [54258, 34326], [54676, 3400], [41006, 47320], [93874, 19046], [62154, 27452], [-43478, -29552], [-23622, -45194], [-7877, -8589], [-42058, 56406], [27233, 11782], [48069, 27247], [53072, 30804], [-37664, -63006], [80216, -7576], [-52224, 27962], [47425, 36113], [-15854, 27340], [54681, -23836], [-53670, 48256], [-5139, 36643], [-15282, 20428], [27182, -926], [-97746, -16650], [38137, -42205], [528, -31804], [-35658, 34986], [-20150, 32204], [42515, -51104], [5381, -53630], [-44822, 39178], [-24699, -53236], [-12770, -4004], [19276, -84752], [88175, 10669], [32366, 41860], [27749, -50272], [15068, 39216], [27430, 32332], [-87042, 34966], [-30202, 71342], [-45276, 38080], [-39270, -1120], [-75706, -57104], [29210, 35236], [-39834, 73854], [-23056, 75354], [-6120, 19394], [88825, 46196], [7193, -6080], [-38558, -89064], [-73467, 51276], [-43351, -45283], [-4150, -54088], [-23245, 27170], [-13427, 19441], [-91623, 22218], [-38152, 22558], [-65486, 44228], [-73342, -13798], [109590, 47328], [-28022, 16448], [21037, 28307], [90874, 37572], [-68449, -10434], [7019, -60445], [-52393, -65594], [-35404, 72282], [-150215, 5076], [-41814, 91972], [50352, 77806], [57590, 59824], [33810, 72692], [19384, 26518], [-52289, 45932], [-15874, 50956], [-22106, 41094], [4947, -51978], [67734, 50826], [56197, 31673], [-20377, -11686], [76278, -47920], [-63627, -12869], [-150449, 14588], [69912, 35330], [-18270, -22046], [-113936, 40552], [-36785, -116701], [83255, -77564], [94500, -46652], [-98321, 41526], [-47352, -27432], [-21022, -99048], [135695, -4911], [62620, 6556], [140702, 34832], [13226, 29520], [-4426, -18112], [52316, 14592], [23350, -37842], [15258, 4012], [-25594, 110758], [37332, -2252], [-135038, -9780], [46885, -31649], [164266, -12286], [24306, -13840], [-35941, 97131], [142886, 46560], [39746, -37184], [-75846, 5396], [-33106, 48344], [28946, -102608], [113166, 2662], [-116550, -36638], [-15185, -95259], [-38268, -128354], [-157806, -41238], [54910, -38150], [35653, 16128], [-9439, -85632], [-63133, -24833], [-90566, -14010], [62665, -95769], [-178004, 24302], [-109412, -30768], [46538, 20342], [-3458, 44432], [-86093, -29313], [51646, 44758], [24464, 13866], [-19498, -8496], [-53534, 28640], [-32065, 20608], [-64294, 16420], [1145, 30179], [-978, -2118], [-85903, -35870], [93731, 58378], [-1252, 20228], [-213721, -3194], [39117, 73193], [-52462, -63570], [143964, 40806], [99324, -3726], [-224970, 31672], [40611, -84778], [22851, 17959], [101718, 88380], [-97338, 912], [6201, -18814], [-25587, -6923], [99807, -11895], [238619, -9215], [207639, -42118], [104536, 35312], [30896, 75408], [-128786, 84018], [135350, -68180], [67894, -140100], [-72646, -136924], [175290, -43788], [72957, -82424], [178591, 40586], [58250, -20824], [-74397, -54300], [21458, 51790], [-40827, 36483], [62556, -59956], [-30562, -71120], [-29939, 31686], [-110296, -77562], [-44362, -12212], [164016, 48746], [138498, -22056], [134197, -51237], [-105806, 8234], [56264, 59924], [19151, 140051], [-22446, 25744], [24706, -165052], [17825, 24539], [503, -25835], [-51449, 101092], [138163, -73116], [231762, -2472], [77668, -60364], [17602, -8660], [-59287, -94597]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_841_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_841_4_a_a();".
// 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_841_4_a_a(:prec:=2)
    chi := MakeCharacter_841_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_841_4_a_a();".
// 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_841_4_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_841_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-1, -2, 1]>],Snew);
    return Vf;
end function;