
// Make newform 242.4.a.n in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_242_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_242_4_a_n();".
// 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_242_4_a_n();".
// 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 := [2420, 100, -99, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-54, 7, 1, 0], [146, 57, -2, -1], [400, -46, -9, 1]];
    Rf_basisdens := [1, 8, 8, 8];
    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_242_a();"
function MakeCharacter_242_a()
    N := 242;
    order := 1;
    char_gens := [123];
    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_242_a_Hecke(Kf)
    return MakeCharacter_242_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, 0, 0], [1, -1, 0, 0], [6, 0, -1, 0], [-1, 1, -2, 1], [0, 0, 0, 0], [10, -6, 1, -2], [-14, -3, 0, -4], [-7, -6, -7, -5], [76, 6, -2, -8], [-143, -9, -6, -5], [48, 4, 7, -14], [93, 27, 10, 5], [-3, -7, 22, -2], [188, 14, 12, 19], [93, 21, -20, -11], [-34, -8, -21, 18], [255, -26, -5, 9], [230, 28, -13, 14], [72, -51, 29, -30], [-300, 32, -23, 28], [297, 45, -54, 8], [144, 36, -41, 14], [-384, 92, -5, -17], [-298, 26, -76, -15], [566, 20, 69, 79], [-418, -28, -25, -8], [119, 41, -66, -77], [-7, 23, 68, -4], [426, -108, -100, -36], [-455, 196, -45, 35], [-20, -28, 77, -66], [-868, 55, -53, 90], [-1150, -108, -19, 39], [767, 47, 56, 131], [-450, 2, 1, 2], [453, -43, 36, -71], [1305, -39, -52, -77], [1108, -47, -276, -42], [184, -152, 179, 68], [-707, -103, 68, -3], [245, -60, 199, 55], [1142, -52, 91, 128], [391, -25, 112, 133], [486, 350, -99, -44], [-1170, 146, -78, 38], [832, -74, 270, 210], [1140, 184, -31, 115], [2337, 281, 140, 239], [-37, 96, 137, -171], [2570, -44, 123, -184], [-2122, 568, -39, 13], [1317, 177, -140, -229], [-1726, 683, -205, 12], [-1340, -550, 37, -244], [-671, -67, 326, 448], [-1456, -188, 396, 54], [-2606, 386, -23, -26], [1339, 137, 382, 441], [-630, -274, 265, 498], [-2618, 144, 685, 9], [-2775, -327, -44, 317], [785, -779, -348, -75], [4216, -234, -560, -305], [209, -567, 84, -133], [162, -155, -312, -372], [-638, -902, 213, 14], [852, -90, -200, 395], [5669, 500, -33, -205], [6068, -385, -16, 78], [-4311, -223, 548, 55], [22, 921, 197, 44], [4179, -429, 224, 229], [631, 309, -370, -17], [-3950, -648, 176, -674], [192, -236, 659, 63], [1776, 770, -15, 176], [-2819, 625, -776, 407], [5974, 22, 250, -128], [-2070, -948, -599, -297], [-6302, 154, 1009, 160], [-1160, -458, -772, -603], [105, -963, -24, 93], [6548, 54, -567, -344], [6625, -50, 509, -641], [6900, 470, 570, 670], [337, -269, -128, -116], [954, 45, -76, 628], [-1110, 204, 93, -429], [2286, -582, 1246, -24], [4392, -576, 508, 332], [-4220, 370, -317, 372], [660, 1066, 531, 196], [3569, -127, 1104, -177], [383, 1229, -296, -174], [1767, -1591, -20, -612], [6541, 613, -500, 179], [-4795, -157, -246, -647], [-2599, 826, 1417, 339], [-6952, 47, -32, -518], [-2646, -122, -183, 1142], [2357, 488, -185, 499], [14685, -1235, -148, -253], [-5916, 112, 589, 879], [1269, 1022, -339, -1005], [-8416, 724, -492, -32], [-4370, -805, 642, 812], [2935, -1076, 901, 61], [98, -451, -1527, -356], [-920, 1660, 273, 770], [8069, 1333, -1038, 452], [5436, 192, -821, -484], [7305, -2237, 1046, -183], [-10830, 1037, -559, 624], [-12923, 1766, -747, -25], [-11497, -567, 270, -1125], [8225, -611, 1090, 1170], [6980, 32, -1813, -139], [-1408, 1844, 275, 1050], [-4791, -1189, 1542, 273], [-1684, -1779, -463, -822], [-6126, 1540, 580, 224], [1198, -715, -2040, -1880], [-4882, 530, 505, 688], [-4932, -2112, 768, -1172], [-3960, 1915, 186, 726], [-1907, -491, 652, 241], [-12410, -278, -19, 832], [-16693, 407, -1660, -469], [-5848, -2712, -84, 114], [-1015, 1891, 1154, 495], [-17544, -2179, -456, 238], [10908, -558, 535, 622], [-2559, 87, -2190, -1745], [-11354, 1028, -1213, -588], [-2682, 2669, 908, 1176], [6086, -1252, 2320, 840], [10233, -985, 1942, 575], [-4112, -1083, -434, 1570], [14922, 328, 311, -376], [-7314, -1759, -1480, -132], [8833, 3183, 156, -280], [3581, -277, 362, 401], [-332, -770, 1301, -470], [9680, -1827, 2780, 1950], [-25787, 785, -596, -299], [7909, 1007, -618, -585], [17834, -1756, 1979, -794], [-1838, 1682, 464, 403], [-8984, 501, -567, -1402], [-9036, -3204, 681, -1232], [12873, 4029, 1180, 2407], [14418, 1522, -852, -1371], [1461, -1737, -320, -1906], [2823, -3863, -1950, -1575], [-372, -571, 3278, -238], [9836, -1584, -2393, 564], [12668, -2098, 1243, 48], [598, 3393, 1072, 2004], [-10562, -401, -1562, -1088], [2026, 4004, 851, -948], [28056, -1567, -1211, -158], [18693, 2853, -1058, 1138], [3048, -3230, 2282, 798], [14909, 545, -1140, -1291], [518, -2550, 189, -1192], [25931, -1375, 1618, 1761], [-6728, 2410, 770, 600], [-3779, 2055, -3694, -1611]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_242_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_242_4_a_n();".
// 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_242_4_a_n(:prec:=4)
    chi := MakeCharacter_242_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(997) - 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_242_4_a_n();".
// 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_242_4_a_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_242_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![2179, 242, -97, -4, 1]>,<5,R![-8284, 1710, 73, -25, 1]>,<7,R![295004, -1496, -1093, 3, 1]>],Snew);
    return Vf;
end function;