
// Make newform 324.5.c.a in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_324_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_324_c_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_324_5_c_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_324_5_c_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 := [440076, 85254, 6529, -1647, 1104, 121, 6, -3, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [-386178, 464580, -159143, 17923, -1115, 635, -142, 17], [-42441945, -1050579, 106240, 113944, -70703, 5765, -367, 53], [60657, 25974, -223, 197, 116, 34, -5, 1], [2470650, 172422, 166664, 118754, 11717, -1775, 49, 118], [178665, 18954, -18433, 428, 410, 73, -38, 4], [445521, 96174, 15841, 4441, 661, 143, 2, 5], [-71046885, 16720785, 281948, -608323, -237610, 40423, -1433, -1052]];
    Rf_basisdens := [1, 104247, 104247, 729, 34749, 729, 729, 104247];
    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_324_c();"
function MakeCharacter_324_c()
    N := 324;
    order := 2;
    char_gens := [163, 245];
    v := [2, 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_324_c_Hecke();"
function MakeCharacter_324_c_Hecke(Kf)
    N := 324;
    order := 2;
    char_gens := [163, 245];
    char_values := [[1, 0, 0, 0, 0, 0, 0, 0], [-1, 0, 0, 0, 0, 0, 0, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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 := 5;
    raw_aps := [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [-3, 0, 0, 1, 0, 0, 0, 0], [0, 0, -1, 0, 0, 0, 0, 0], [2, 0, 0, 1, 0, 1, 0, 0], [0, -1, 1, 0, -1, 0, 0, 0], [70, 0, 0, 0, 0, 0, 1, 0], [0, 2, 3, 0, 2, 0, 0, 1], [0, -8, -5, 0, 1, 0, 0, 1], [-46, 0, 0, -4, 0, 3, -1, 0], [-5, 0, 0, -19, 0, -5, -1, 0], [0, 26, -1, 0, -3, 0, 0, 2], [15, 0, 0, -3, 0, -2, 1, 0], [0, 11, 4, 0, -3, 0, 0, 4], [0, 61, -25, 0, 5, 0, 0, 5], [0, -63, -20, 0, -1, 0, 0, 7], [480, 0, 0, -53, 0, 19, 2, 0], [-16, 0, 0, 96, 0, -3, 0, 0], [0, 35, 17, 0, 9, 0, 0, 13], [-953, 0, 0, 83, 0, -1, -8, 0], [568, 0, 0, 90, 0, -3, 7, 0], [0, -229, -12, 0, -7, 0, 0, 12], [0, -241, -19, 0, 3, 0, 0, 7], [-1822, 0, 0, -17, 0, 1, -9, 0], [0, 163, -36, 0, 16, 0, 0, 4], [-2181, 0, 0, -41, 0, 34, -16, 0], [0, 368, 60, 0, -36, 0, 0, 7], [-929, 0, 0, 1, 0, -49, 9, 0], [0, -197, 54, 0, 10, 0, 0, 0], [792, 0, 0, -36, 0, -2, 22, 0], [0, -85, -18, 0, 33, 0, 0, 10], [0, -688, 39, 0, 17, 0, 0, -22], [-907, 0, 0, -93, 0, 8, -11, 0], [0, 600, 45, 0, -33, 0, 0, -47], [-4392, 0, 0, 106, 0, 37, 29, 0], [-3473, 0, 0, 166, 0, 32, 39, 0], [-7748, 0, 0, 92, 0, 10, -28, 0], [0, -701, -2, 0, -39, 0, 0, -24], [0, 565, 66, 0, 6, 0, 0, -62], [0, 260, -180, 0, 48, 0, 0, -8], [-5335, 0, 0, 183, 0, 53, -17, 0], [0, -43, 234, 0, -17, 0, 0, -16], [-3740, 0, 0, 293, 0, -121, -29, 0], [0, 152, -168, 0, -52, 0, 0, -104], [10875, 0, 0, 461, 0, -37, -37, 0], [-16448, 0, 0, -282, 0, -147, 7, 0], [6091, 0, 0, 679, 0, 98, -12, 0], [0, -372, -45, 0, 136, 0, 0, 56], [23285, 0, 0, -916, 0, 186, -37, 0], [0, -670, 506, 0, 18, 0, 0, -49], [0, 956, 337, 0, -32, 0, 0, 27], [21231, 0, 0, -248, 0, -288, 72, 0], [0, -366, 162, 0, -14, 0, 0, 73], [0, -705, -2, 0, 222, 0, 0, -13], [0, -672, -117, 0, 124, 0, 0, 99], [0, -1651, 39, 0, -247, 0, 0, -79], [-25408, 0, 0, 840, 0, 222, 78, 0], [7597, 0, 0, 1060, 0, -70, -159, 0], [0, -1626, 173, 0, -257, 0, 0, -79], [54421, 0, 0, -227, 0, 128, 48, 0], [0, 335, 426, 0, 98, 0, 0, 46], [56982, 0, 0, 880, 0, -152, -13, 0], [0, -2194, -545, 0, -126, 0, 0, 99], [-58555, 0, 0, -906, 0, -142, 142, 0], [0, -112, -181, 0, 277, 0, 0, 75], [42302, 0, 0, -584, 0, 31, 189, 0], [74827, 0, 0, -580, 0, -404, 50, 0], [0, 1886, 323, 0, -178, 0, 0, 220], [45127, 0, 0, 758, 0, 328, -97, 0], [0, 84, 1027, 0, -107, 0, 0, 114], [0, 2034, -210, 0, 98, 0, 0, 160], [-115162, 0, 0, -120, 0, 135, -99, 0], [-79956, 0, 0, -49, 0, 167, 218, 0], [-88099, 0, 0, 1317, 0, -127, -8, 0], [0, -1839, 40, 0, 227, 0, 0, 206], [0, 141, -150, 0, 506, 0, 0, 98], [-45775, 0, 0, 2951, 0, -651, 185, 0], [0, -1942, -1657, 0, 25, 0, 0, -80], [6723, 0, 0, 928, 0, 156, 126, 0], [0, 228, 201, 0, 328, 0, 0, 215], [-160106, 0, 0, 1157, 0, 613, 14, 0], [0, 1338, 1670, 0, 2, 0, 0, -96], [-111831, 0, 0, 1509, 0, 469, 166, 0], [47430, 0, 0, 436, 0, -665, -133, 0], [0, -5163, -1594, 0, -53, 0, 0, 111], [0, -855, -969, 0, -235, 0, 0, -200], [85798, 0, 0, 5947, 0, 461, -33, 0], [0, -1904, -341, 0, -55, 0, 0, -237], [-25614, 0, 0, -1804, 0, 635, -43, 0], [0, -6888, 964, 0, -464, 0, 0, -69], [0, -6320, -917, 0, 308, 0, 0, -281], [-140694, 0, 0, -1534, 0, -988, 62, 0], [0, -5289, -970, 0, 313, 0, 0, -161], [222137, 0, 0, 4059, 0, 588, 21, 0], [0, 2077, 967, 0, -81, 0, 0, -327], [0, -8704, 1909, 0, 159, 0, 0, -75], [0, 5802, 234, 0, 902, 0, 0, -115], [239587, 0, 0, -1723, 0, 275, 199, 0], [-224608, 0, 0, 1050, 0, 1250, -170, 0], [86030, 0, 0, 654, 0, 1695, -72, 0], [0, -8233, -1795, 0, -105, 0, 0, -311], [0, -600, -767, 0, -688, 0, 0, -90], [0, -13491, 886, 0, 42, 0, 0, -51], [-60658, 0, 0, 5082, 0, -817, 22, 0], [320769, 0, 0, -143, 0, -971, -406, 0], [0, 5547, -1232, 0, 177, 0, 0, -225], [0, 9105, 195, 0, 329, 0, 0, 293], [0, -6853, 198, 0, -903, 0, 0, -888], [245798, 0, 0, 1871, 0, -459, 935, 0], [390965, 0, 0, -5271, 0, 560, -2, 0], [-126041, 0, 0, -3331, 0, -361, -257, 0], [0, -3098, 2523, 0, -679, 0, 0, -116], [258894, 0, 0, 1450, 0, 1471, -250, 0], [246304, 0, 0, 8132, 0, 862, -508, 0], [0, -3009, 1348, 0, -788, 0, 0, -51], [-301234, 0, 0, 6318, 0, 927, 252, 0], [0, -2422, -3434, 0, -382, 0, 0, -736], [0, -1209, -654, 0, 934, 0, 0, 918], [0, 7990, 2533, 0, 1570, 0, 0, 471], [211671, 0, 0, -2870, 0, -1160, -619, 0], [150204, 0, 0, -6457, 0, -481, -592, 0], [0, -16899, -3374, 0, -38, 0, 0, 248], [0, -8084, -952, 0, -1112, 0, 0, 361], [-351345, 0, 0, -5277, 0, -1226, 226, 0], [0, 10923, 977, 0, 207, 0, 0, 435], [185291, 0, 0, 4534, 0, -2228, -261, 0], [0, -9822, 2350, 0, -850, 0, 0, -492], [250634, 0, 0, -84, 0, -69, -513, 0], [-43436, 0, 0, 4363, 0, 479, -1140, 0], [271957, 0, 0, 193, 0, -1735, -450, 0], [0, 13257, 1696, 0, -1893, 0, 0, -382], [-420195, 0, 0, 2665, 0, 384, -324, 0], [-323186, 0, 0, 1820, 0, -1804, -332, 0], [0, 17806, -3217, 0, 1021, 0, 0, 523], [276819, 0, 0, -4594, 0, 1628, -259, 0], [0, 10335, -2243, 0, 299, 0, 0, 1539], [-60567, 0, 0, -2339, 0, -1052, 1088, 0], [0, 2642, -2717, 0, 521, 0, 0, 677], [0, -17309, -1531, 0, -153, 0, 0, 546], [-191004, 0, 0, -3990, 0, 2674, 247, 0], [0, 5886, 2333, 0, -2729, 0, 0, -101], [496826, 0, 0, -3536, 0, -2597, 549, 0], [0, -862, -5530, 0, -278, 0, 0, 78], [-269223, 0, 0, 8639, 0, 1613, -223, 0], [0, 3061, 5186, 0, -189, 0, 0, -754], [-211454, 0, 0, -4267, 0, -103, 1096, 0], [0, 4706, 3393, 0, 2479, 0, 0, 1045], [-402100, 0, 0, -4068, 0, -871, -686, 0], [0, -6372, 2924, 0, 88, 0, 0, 82], [108894, 0, 0, 17185, 0, 189, 156, 0], [0, -10257, 797, 0, 675, 0, 0, -93], [-148740, 0, 0, 10634, 0, -2086, -1693, 0], [0, 3744, -3707, 0, 264, 0, 0, -1271], [-412573, 0, 0, -2735, 0, -1622, -807, 0], [0, 1006, 1165, 0, 914, 0, 0, -1865], [-67788, 0, 0, 4276, 0, 1690, -274, 0], [0, -31530, -4387, 0, 1355, 0, 0, 233], [-80425, 0, 0, -12315, 0, -2233, 439, 0], [0, 21345, -3736, 0, -1664, 0, 0, -998], [0, 32306, -2507, 0, -1342, 0, 0, -310], [0, 351, -7887, 0, -821, 0, 0, -196], [-127454, 0, 0, 4652, 0, -853, 1273, 0], [0, -25959, 6779, 0, -2365, 0, 0, -679], [0, 2681, 7012, 0, -896, 0, 0, -917], [0, 10996, -2777, 0, -404, 0, 0, -513], [141938, 0, 0, 9150, 0, -132, 2064, 0], [-148461, 0, 0, -17678, 0, -1796, -865, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_324_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_324_5_c_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_324_5_c_a(:prec:=8)
    chi := MakeCharacter_324_c();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 5));
    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_324_5_c_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_324_5_c_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_324_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,5,sign)));
    Vf := Kernel([<5,R![19274879556, 0, 403999407, 0, 2238759, 0, 2853, 0, 1]>],Snew);
    return Vf;
end function;