
// Make newform 578.4.a.e in Magma, downloaded from the LMFDB on 18 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_578_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_578_4_a_e();".
// 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_578_4_a_e();".
// 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 := [-3, -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_578_a();"
function MakeCharacter_578_a()
    N := 578;
    order := 1;
    char_gens := [3];
    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_578_a_Hecke(Kf)
    return MakeCharacter_578_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], [-2, -3], [1, -1], [-3, -13], [-7, -14], [10, 25], [0, 0], [-56, 13], [57, -3], [-118, 145], [11, 36], [226, -14], [154, 56], [189, -28], [189, 6], [-438, -57], [-498, 276], [-121, 54], [198, -418], [714, -96], [-725, 166], [-386, 448], [358, -709], [-582, -108], [-1415, 199], [737, 214], [-686, 508], [-697, -14], [-1351, -384], [-769, 229], [795, 68], [-630, 306], [-494, -22], [-763, 933], [956, -206], [1119, -574], [-773, 1285], [-1193, 364], [1054, 29], [327, -894], [2091, 915], [671, -312], [-1720, 1333], [1245, -1780], [1013, 1723], [1527, 620], [-2149, 3], [1843, -2177], [3067, -364], [2296, -1856], [1340, -2678], [3369, -1398], [-2503, -2103], [-1298, 1415], [383, -1913], [-1100, 899], [4248, -2547], [-4883, 169], [1764, -1936], [-135, 1353], [-84, 1280], [-1478, 314], [-1929, -2629], [744, 1209], [-6002, 1390], [4725, 1866], [6901, 1399], [-1172, 1351], [6406, 89], [-5287, 4434], [4971, -1272], [4809, -1869], [642, -4426], [6411, -2320], [1811, -4368], [9170, -2003], [-2615, 4808], [-9185, 2506], [6787, -5461], [-10855, -738], [-6713, 698], [7945, 925], [-10421, -1543], [3892, -896], [-13292, 1534], [341, 3676], [10430, 2965], [937, -4079], [-3174, 7368], [-4036, -3666], [-14415, -717], [7635, -9114], [-15334, 1845], [-2709, -4659], [-17423, 1849], [-7449, 6654], [15183, -3102], [14312, -1088], [311, -1434], [-1768, -6771], [-9771, 5600], [404, -3404], [-6150, -7146], [-15963, -3042], [12196, 3775], [5951, -8946], [18160, -1855], [-6173, 9044], [12469, -8758], [-1722, -7711], [3021, 50], [254, 1134], [-3849, -5256], [14173, 46], [8706, 5867], [-3082, 11185], [-13570, -3882], [2195, -986], [-5871, 3042], [-11655, -9432], [5559, -4411], [11684, -4020], [1354, -11371], [-3956, -1153], [-14596, 5880], [4729, 3944], [16978, -13907], [-6377, -13096], [-17894, 16000], [17656, 7705], [21925, -1118], [3407, 6088], [-14647, 1542], [-4915, 7944], [-1275, -2196], [-17298, 1085], [4985, 1804], [24112, -4322], [-2167, -14324], [-1660, -6506], [11134, -7310], [10373, -230], [-33535, 3504], [-33071, 5660], [-1504, -8862], [-12300, -11826], [-31703, -308], [-1389, 18570], [3954, -10090], [9337, 11813], [17179, 10873], [22646, -17897], [-24594, 11387], [-11151, 18957], [-14426, 2203], [-11293, 13921], [-9690, 7607], [-3628, 17479], [17549, -15021], [39041, 1141], [-18343, -14726], [33753, 9615], [4993, -14183], [-39061, -1625], [-7858, 16711], [-30546, 20808], [-32032, -10419], [-4016, -3341], [-11213, 18574], [-25573, -1787], [10533, 17742], [-30515, -5381], [10247, -14204], [20637, -21742], [34196, 8712], [-56376, 2001], [35578, -7235], [-13073, -4156], [30852, -2830], [12189, -9562], [-8819, 3265], [32790, -18342], [-34688, 11986], [19692, -21702], [-24397, -15266], [20198, -2852], [-9569, 26950], [-8111, -12428], [-4831, -846], [-48298, 20077], [-22151, 18967], [7709, -17006], [15498, -30346], [-20084, -8590], [-12617, 18554], [-1965, -192], [34895, 5898], [-12120, 14654], [-40965, 19950], [32281, -22993], [-22506, 16179], [-33606, 8276], [-29858, 22597], [-17742, -27478], [1716, -29874], [-22137, 32526], [-5546, -25217], [-27690, 40908], [-31359, -2205], [43560, -28924], [32672, -210], [-51741, 19332], [-5612, 13855], [51298, 92], [-57088, -12254], [12407, 5346], [-41600, 22537], [-75978, 13611], [15086, -1430], [16682, 13696], [6448, 35998], [-44088, 36479], [23739, -31554], [41753, -12588], [7437, 4830], [52367, -1839], [24345, -43776], [1909, 7163], [20756, -35958], [45401, -7061], [-2125, 2925], [26087, 18897], [-72715, 888], [-22218, -38856], [8159, -8178], [-7477, -29540], [-16794, -10264], [-11694, 10986], [-61116, 1557], [-78560, -6694], [25310, -23810], [38230, 6565], [-25100, 52117], [22717, -9479], [-67405, 3013], [31764, 22404], [-19379, -33080], [-16543, 3400], [-47709, 21644], [64754, -7406], [-81023, -10349], [17504, -8963], [18651, 3780], [-21212, -23042], [8009, 3520], [-20949, -16407], [51329, -7602], [93007, -9116], [-13134, -16515], [-86742, -18820], [59724, -15844], [39109, -33433], [-672, 51212], [-63686, -6500], [-76735, 17629], [-62009, 9322], [5073, -18255], [54837, 22200], [82878, -32461], [-16401, 10830], [8921, 20493], [-34254, -17143], [3458, -11187], [19074, 22475], [94696, -4940], [-40451, 1384], [-24722, 69794], [51546, -14089], [1784, 28530], [-63666, 35667], [-28788, -36714], [-22588, 21600], [59990, -10400], [-49389, -17323], [-5831, 56248], [-53115, 47670], [-1915, 37773], [86565, -54582], [30129, -19522], [105835, -42802], [36613, -9658], [49345, -24586], [51232, 39971], [39112, -24907], [2179, -6833], [-91029, 6717], [45504, -17680], [-110420, -19165], [18569, -8018], [23282, -51537], [54182, -41157], [-51165, -13908], [-56600, 36658], [2685, -7005], [26821, 4477], [-113324, 29920], [-97569, 24822], [-52174, 82203], [51017, 5491], [15296, -33987], [-68994, -8991], [-95259, 59562], [38019, 32094], [-19143, 70466], [64541, 15244], [22776, -80368], [-10701, 12732], [59129, 7576], [-41426, 63061], [20268, 24765], [150498, -8671], [-38410, -15786], [45765, -72696], [56981, -10554], [-140805, -12102], [-110617, 19008], [-11905, -59202], [67803, -56116], [-161361, -7446], [66219, 33021], [-25244, -14309], [-32860, 26086], [-163537, -12069], [60947, -27905], [-39743, 44401], [-68112, 32208], [-62565, 40430], [-103056, 45972], [29839, -49208], [60927, 37214], [23790, 45065], [-46372, 8851], [-4093, 51829], [86027, -5502], [-59387, 29582], [146118, -17238], [-11637, 27419], [106545, -8176], [-176623, 13354], [-87898, 8218], [-22475, 36640], [-59779, 41214], [20633, -62345], [151186, -12815], [86011, -96170], [-85351, 80230], [-13891, 68035], [4927, -91351], [-48858, -48852], [-734, 14918], [147054, -31615], [19162, -9826], [-62060, 53318], [-58911, 24309], [-67233, -53803], [-117611, 59470], [18813, 7500], [67833, -28543], [-20944, -26364], [-11724, -36327], [204991, -1631], [13680, 39081], [-135324, -39339], [-90498, 25910], [33242, -48078], [71250, -90609], [-51291, 103938], [42598, -56036], [62098, 31973], [-134798, -3125], [30944, 2914], [102021, 57300], [-46257, -16120], [120699, -11757], [-90518, -42536], [-16985, -83215], [109262, -31350], [-37204, -10677], [102306, -84886], [-110861, 111578], [-114662, 106057], [10296, -31893], [100776, -17904], [-39439, -47643], [43626, 8043], [-202374, 13109], [-46937, -43418], [-26692, 27511], [83282, 16809], [23252, -87284], [14748, 470], [-87096, 79869], [-242083, -18402], [134716, -1864], [-3738, -35547], [-9530, 114235], [76354, 5494], [135466, 28670], [13487, -19497], [-140051, -3130], [-1813, -29595], [156011, -42788], [-137504, 24983], [224224, -26942], [-6435, -38257], [-48263, 57140], [-112158, 25506], [-21301, 109866], [-28260, 37671], [-50117, 81788], [139961, -83528], [53225, 86601], [-145472, 79034], [-212836, 13780], [-112699, -39165], [-23859, 84420], [151203, -69564], [-71691, 49572], [142493, -58020], [-24995, -36895]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_578_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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