
// Make newform 1521.4.a.ba in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1521_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_1521_4_a_ba();".
// 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_1521_4_a_ba();".
// 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 := [81, -9, -40, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-9, -31, -1, 1], [9, 49, 1, -1], [-171, 22, 10, -1]];
    Rf_basisdens := [1, 9, 9, 9];
    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_1521_a();"
function MakeCharacter_1521_a()
    N := 1521;
    order := 1;
    char_gens := [677, 847];
    v := [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_1521_a_Hecke(Kf)
    return MakeCharacter_1521_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, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, -1], [-8, 0, -2, 0], [0, 2, 0, -1], [0, 0, 0, 0], [0, 8, 0, 0], [-28, 0, 14, 0], [0, 32, 0, 2], [0, 20, 0, -10], [152, 0, -2, 0], [-30, 0, -32, 0], [0, 4, 0, 9], [216, 0, 8, 0], [0, 54, 0, -5], [0, 52, 0, -8], [0, 46, 0, -15], [142, 0, 44, 0], [-780, 0, 34, 0], [0, -98, 0, -29], [10, 0, 136, 0], [-236, 0, 112, 0], [0, 2, 0, -5], [0, 204, 0, 7], [34, 0, 104, 0], [0, 92, 0, -54], [892, 0, -88, 0], [0, 72, 0, -86], [-890, 0, 88, 0], [0, -216, 0, 70], [1156, 0, -92, 0], [0, -176, 0, -86], [0, -196, 0, 25], [8, 0, -188, 0], [0, 336, 0, 13], [-720, 0, 90, 0], [686, 0, 84, 0], [-756, 0, -110, 0], [0, -218, 0, 23], [0, 36, 0, 38], [0, -12, 0, -104], [-2662, 0, 180, 0], [0, 172, 0, 108], [1726, 0, 352, 0], [0, -424, 0, -109], [-940, 0, 292, 0], [960, 0, -312, 0], [928, 0, 270, 0], [0, -930, 0, 149], [718, 0, 200, 0], [0, -1144, 0, 26], [0, -2, 0, -17], [514, 0, -200, 0], [0, -252, 0, -150], [0, -184, 0, -258], [0, 852, 0, 18], [0, -60, 0, 114], [64, 0, 302, 0], [-2462, 0, 0, 0], [0, 108, 0, 317], [4152, 0, -132, 0], [0, 912, 0, 257], [3244, 0, -466, 0], [0, -864, 0, 122], [-7350, 0, -336, 0], [0, -1248, 0, -117], [-8620, 0, -166, 0], [3534, 0, -832, 0], [0, -968, 0, -48], [3274, 0, 824, 0], [0, 2084, 0, -43], [0, 1302, 0, -201], [2796, 0, 948, 0], [1966, 0, 480, 0], [1988, 0, 558, 0], [0, -1070, 0, -83], [0, 1124, 0, 296], [6490, 0, 120, 0], [0, -708, 0, 47], [-3414, 0, -1104, 0], [0, 1216, 0, 482], [4574, 0, 208, 0], [0, 178, 0, 605], [5946, 0, -116, 0], [3516, 0, 1248, 0], [0, -1384, 0, -226], [0, -2484, 0, 119], [234, 0, 232, 0], [0, -2024, 0, -309], [-5072, 0, 1902, 0], [0, -644, 0, -590], [0, -2390, 0, 17], [344, 0, 566, 0], [0, 1436, 0, 462], [-7372, 0, 1082, 0], [0, -1412, 0, -730], [0, -2608, 0, 483], [0, 2048, 0, 718], [-10608, 0, 1684, 0], [9446, 0, -912, 0], [9296, 0, 1580, 0], [0, -1056, 0, -1231], [0, -1628, 0, 352], [0, -2768, 0, -758], [-5192, 0, -252, 0], [-7634, 0, -480, 0], [0, -1326, 0, -633], [0, 3012, 0, -117], [0, -1020, 0, -1130], [-14598, 0, 1712, 0], [-3676, 0, -1044, 0], [9202, 0, 1672, 0], [0, -844, 0, 445], [11868, 0, -914, 0], [21728, 0, -502, 0], [0, -664, 0, 364], [-14980, 0, 658, 0], [0, 768, 0, 836], [0, 2188, 0, 590], [0, 120, 0, -1580], [-318, 0, -24, 0], [3754, 0, 1012, 0], [0, -2692, 0, -504], [0, 1642, 0, -821], [-6164, 0, 2642, 0], [0, -2372, 0, 964], [-98, 0, 880, 0], [0, -3992, 0, 514], [8316, 0, 3388, 0], [-17994, 0, -1208, 0], [-3556, 0, 1318, 0], [0, -2978, 0, 271], [9844, 0, -192, 0], [-9846, 0, -1500, 0], [0, -3804, 0, 1077], [-5922, 0, -1344, 0], [0, -1640, 0, 597], [1620, 0, -2482, 0], [0, -300, 0, 488], [0, 384, 0, -782], [24932, 0, -1178, 0], [0, -296, 0, 1029], [1940, 0, -288, 0], [0, -3694, 0, -1597], [10018, 0, -4052, 0], [0, -2902, 0, -1887], [1042, 0, -5104, 0], [0, 7904, 0, -314], [6632, 0, -1600, 0], [0, 3322, 0, 1101], [-34246, 0, -1064, 0], [0, -1000, 0, 514], [28480, 0, 580, 0], [0, -4028, 0, -560], [28240, 0, -444, 0], [0, 4796, 0, -1348], [11172, 0, -1552, 0], [0, 2204, 0, -399], [15186, 0, -1292, 0], [0, -2072, 0, -2535], [0, -3130, 0, 917], [0, 5440, 0, -390], [10000, 0, -2626, 0], [0, 2768, 0, -864], [0, -2908, 0, -1793], [0, 5458, 0, -595], [-26644, 0, -980, 0], [19150, 0, 208, 0], [-42382, 0, 1608, 0], [0, -6188, 0, 918], [0, 4262, 0, 1361], [-28678, 0, 1520, 0], [0, 8064, 0, -1174], [-44134, 0, -1432, 0], [-24188, 0, -4840, 0], [0, -4352, 0, 1868], [-30388, 0, 1926, 0], [0, -8216, 0, 273], [-23956, 0, -1752, 0], [1174, 0, 880, 0], [27912, 0, 278, 0], [0, -1676, 0, -1752], [-16870, 0, 5780, 0], [0, 4988, 0, -385], [0, 1574, 0, 1575], [0, 4996, 0, 260], [1058, 0, 3524, 0], [-39956, 0, -42, 0], [-2006, 0, -3040, 0], [0, 4622, 0, -2465], [-45162, 0, 1892, 0], [0, 9258, 0, -345], [34536, 0, 1148, 0], [0, 7624, 0, -723], [0, 7468, 0, -1988], [0, -6616, 0, -1842], [39390, 0, 1952, 0], [3098, 0, -2560, 0], [0, -3324, 0, 91], [0, 1156, 0, -100], [0, -3832, 0, -2741], [-3500, 0, 7644, 0], [17726, 0, 3432, 0], [-19578, 0, 372, 0], [0, 4150, 0, 2537], [0, -1740, 0, -3292], [20160, 0, 3150, 0], [0, -9576, 0, -978], [0, -2100, 0, -1157], [-944, 0, 3620, 0], [17098, 0, 3628, 0], [0, 10764, 0, -748], [-27236, 0, -2664, 0], [0, 6490, 0, 2623], [0, -1102, 0, 1689], [50294, 0, -456, 0], [28628, 0, 1568, 0], [0, 11144, 0, -2392], [0, -12418, 0, -801], [0, -14344, 0, 209], [-6286, 0, -1328, 0], [46960, 0, -570, 0], [0, 14780, 0, -1505], [-39360, 0, -4474, 0], [0, 17260, 0, -1622], [73790, 0, -3264, 0], [0, -2432, 0, 244], [0, -3748, 0, 3218], [64292, 0, -6268, 0], [0, -4946, 0, 525], [-83950, 0, 1172, 0], [47440, 0, -2484, 0], [11784, 0, -2470, 0], [0, -3360, 0, 348], [34880, 0, 5732, 0], [0, -4058, 0, -2665], [-4894, 0, -512, 0], [0, 6952, 0, 33], [0, -740, 0, 100], [0, -18604, 0, 1588], [0, 1090, 0, 2395], [4776, 0, -572, 0], [-41100, 0, -1592, 0], [-11110, 0, 11960, 0], [0, 6300, 0, -2149], [0, 7456, 0, 1938], [-82200, 0, -4962, 0], [0, 12758, 0, -3507], [18300, 0, -7122, 0], [0, 10952, 0, -622], [-27322, 0, -4672, 0], [0, -10940, 0, 1321], [0, 4938, 0, 5669], [-10330, 0, 5168, 0], [0, 3684, 0, -154], [0, 9202, 0, 1767], [9874, 0, -6592, 0], [-35588, 0, -7322, 0], [0, 6572, 0, 1132], [-43354, 0, -472, 0], [4468, 0, 5572, 0], [0, -1464, 0, -3988], [-38994, 0, -10952, 0], [17782, 0, 3152, 0], [0, 14436, 0, -1425], [69088, 0, 2224, 0], [0, -8160, 0, -693], [0, 4260, 0, 3301], [-10124, 0, 4718, 0], [0, -6780, 0, -2658], [-35314, 0, 12356, 0], [9924, 0, -11974, 0], [126858, 0, -1480, 0], [12932, 0, -9144, 0], [-27714, 0, -9808, 0], [-54552, 0, 1714, 0], [0, -202, 0, -6843], [-6998, 0, 4648, 0], [7910, 0, -8864, 0], [0, 5156, 0, -4038], [0, 5108, 0, 2412], [-52848, 0, 10518, 0], [0, -12988, 0, -5548], [86882, 0, -128, 0], [-36524, 0, -9938, 0], [0, -1456, 0, 4592], [-2202, 0, 2924, 0], [0, 2496, 0, -3467], [-43920, 0, -9494, 0], [0, 10648, 0, -1710], [0, -1012, 0, 5212], [0, -23016, 0, 888], [0, 132, 0, -2053], [0, 1678, 0, 6433], [-123458, 0, -204, 0], [0, 4100, 0, -5112], [50292, 0, 2244, 0], [0, 8700, 0, 3528], [0, 30016, 0, -1604], [-48444, 0, -4778, 0], [-83862, 0, -9200, 0], [0, -4568, 0, -3533], [-28668, 0, 11608, 0], [0, 8528, 0, 7696], [18480, 0, 12164, 0], [-85950, 0, 6672, 0], [0, -4564, 0, 3712], [45402, 0, -12880, 0], [0, 130, 0, 4197], [87774, 0, 1760, 0], [0, 2588, 0, 4850], [0, -4976, 0, -5981], [0, -20416, 0, -1306], [52712, 0, -13240, 0], [0, 4758, 0, -1881], [-76986, 0, 9184, 0], [0, 4654, 0, -2243], [0, -21534, 0, 1285], [-84414, 0, 104, 0], [0, -6000, 0, 4740], [-50848, 0, -8232, 0], [-50346, 0, 248, 0], [0, -6844, 0, -6632], [-69472, 0, -4542, 0], [0, -9364, 0, -153], [32182, 0, -11316, 0], [90348, 0, 946, 0], [62412, 0, -13118, 0], [0, -5384, 0, -1080], [0, 21564, 0, -522], [43942, 0, -11304, 0], [0, -5556, 0, -5070], [121292, 0, 2992, 0], [0, -41278, 0, 2479], [5484, 0, 12206, 0], [0, 22770, 0, -3593], [41514, 0, 4248, 0], [0, -22364, 0, 5737], [-52166, 0, 15216, 0], [-13716, 0, -21696, 0], [51266, 0, -7224, 0], [0, 2640, 0, 3254], [0, 24864, 0, 309], [-25316, 0, -13064, 0], [0, 18464, 0, 575], [0, -10596, 0, 352], [-12446, 0, 22848, 0], [93220, 0, 7538, 0], [0, 4358, 0, -297], [0, -14708, 0, 36], [169084, 0, 3618, 0], [75626, 0, 15736, 0], [0, -6432, 0, -2983], [43444, 0, 7828, 0], [86246, 0, -1564, 0], [0, -2296, 0, 3358], [0, 4414, 0, -217], [0, -25786, 0, -2791], [0, -8384, 0, -7018], [0, 1754, 0, -7291], [-130318, 0, 5736, 0], [0, 8128, 0, 3276], [0, 6956, 0, -10110], [0, 18598, 0, -5915], [12544, 0, -8848, 0], [-3518, 0, -12084, 0], [0, -3384, 0, -501], [58128, 0, -502, 0], [5666, 0, -4860, 0], [0, 13152, 0, -5542], [-76072, 0, 6920, 0], [0, 11374, 0, 3379], [0, -25180, 0, 2604], [-44012, 0, -3496, 0], [-25526, 0, 5776, 0], [0, -29014, 0, -3605], [0, -20400, 0, -628], [95502, 0, 6616, 0], [0, -25832, 0, -820], [-594, 0, 13868, 0], [0, 15168, 0, 5933], [0, -10668, 0, -10887], [-106984, 0, 3726, 0], [0, -4052, 0, -6281], [-116004, 0, -4382, 0], [0, 1722, 0, 4437], [58544, 0, -28734, 0], [76346, 0, -10692, 0], [-122164, 0, 16158, 0], [0, -10724, 0, 10818], [-112750, 0, 10872, 0], [0, 4544, 0, -5791], [0, 29154, 0, -2953], [-66024, 0, -16464, 0], [0, -9898, 0, -6053], [-3634, 0, 17452, 0], [105224, 0, 9954, 0], [0, -7288, 0, -5776], [111784, 0, -5496, 0], [0, 20576, 0, -1389], [11594, 0, -9896, 0], [0, 8040, 0, -1700], [-153856, 0, 17926, 0], [0, 21092, 0, -6897], [0, 1920, 0, -6025], [-45532, 0, -18008, 0], [-101690, 0, 14792, 0], [0, 20548, 0, -3397], [76132, 0, -24514, 0], [0, 19578, 0, -2593], [33666, 0, -3616, 0], [0, 25564, 0, 2696], [0, -9496, 0, -11176], [-62648, 0, 16048, 0], [33222, 0, -5504, 0], [0, -24012, 0, 3544], [0, 278, 0, 5435], [229060, 0, -7064, 0], [0, -13444, 0, 7055], [0, 13640, 0, 13086], [0, 5012, 0, -4590], [-175134, 0, -13840, 0], [0, 49878, 0, 39], [0, 1032, 0, 8838], [68070, 0, -34088, 0], [0, -10232, 0, 9445], [0, -17556, 0, 782], [0, -6132, 0, -1553], [45244, 0, 962, 0], [0, -4564, 0, 9920]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1521_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1521_4_a_ba();".
// 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_1521_4_a_ba(:prec:=4)
    chi := MakeCharacter_1521_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_1521_4_a_ba();".
// 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_1521_4_a_ba( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1521_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![448, 0, -45, 0, 1]>,<5,R![75712, 0, -580, 0, 1]>,<7,R![-152, 18, 1]>],Snew);
    return Vf;
end function;