
// Make newform 18.4.c.b in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_18_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_18_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_18_4_c_b();".
// 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_18_4_c_b();".
// 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, -8, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-81, -8, 8, 1], [0, 17, 1, -1], [-17, 8, 0, 1]];
    Rf_basisdens := [1, 72, 9, 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_18_c();"
function MakeCharacter_18_c()
    N := 18;
    order := 3;
    char_gens := [11];
    v := [2];
    // 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_18_c_Hecke();"
function MakeCharacter_18_c_Hecke(Kf)
    N := 18;
    order := 3;
    char_gens := [11];
    char_values := [[-1, -1, 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 := 4;
    raw_aps := [[0, -2, 0, 0], [0, -1, 0, -1], [6, 6, 0, 3], [0, 8, 3, -3], [0, -9, -6, 6], [-32, -32, 0, -3], [3, 0, 3, 0], [59, 0, -15, 0], [-36, -36, 0, -3], [0, 108, 21, -21], [-110, -110, 0, -9], [152, 0, 42, 0], [-237, -237, 0, -6], [0, -79, 72, -72], [0, 264, -45, 45], [96, 0, 42, 0], [-81, -81, 0, 6], [0, -502, -45, 45], [565, 565, 0, -36], [-204, 0, -96, 0], [-187, 0, -63, 0], [0, 194, -39, 39], [0, 642, -63, 63], [-186, 0, 120, 0], [0, -31, -66, 66], [0, -606, -129, 129], [-848, -848, 0, -243], [1455, 0, -3, 0], [-340, 0, 54, 0], [444, 444, 0, 261], [1082, 0, 198, 0], [942, 942, 0, -177], [0, 585, 102, -102], [-827, -827, 0, 264], [504, 504, 0, -435], [0, 956, -477, 477], [-1334, -1334, 0, -9], [764, 0, -48, 0], [-1590, -1590, 0, -333], [0, -2976, -99, 99], [-3108, 0, 168, 0], [-874, 0, 348, 0], [0, 4020, 87, -87], [-929, -929, 0, -738], [-96, 0, -690, 0], [-1138, 0, 318, 0], [3010, 3010, 0, 591], [0, -1672, 171, -171], [0, 1551, -270, 270], [1708, 1708, 0, -675], [-1947, 0, 861, 0], [480, 480, 0, 9], [0, 1061, -1050, 1050], [1473, 0, -165, 0], [-4803, -4803, 0, -324], [0, -192, 1023, -1023], [-156, 0, -1146, 0], [4628, 0, -396, 0], [0, -772, 297, -297], [0, -2916, 561, -561], [-3134, -3134, 0, -189], [6156, 6156, 0, -75], [2405, 0, 1359, 0], [-3360, -3360, 0, 417], [0, 5921, -150, 150], [0, 2076, 141, -141], [0, 2858, -147, 147], [-3455, -3455, 0, 1128], [2937, 2937, 0, -288], [0, -2134, 975, -975], [0, -837, 480, -480], [1422, 0, -846, 0], [0, 4394, 441, -441], [940, 940, 0, 393], [1259, 0, -219, 0], [-498, -498, 0, -33], [0, -1272, 1941, -1941], [-6208, 0, -702, 0], [-4737, -4737, 0, 966], [6787, 6787, 0, 1038], [14178, 14178, 0, 423], [0, -154, -1113, 1113], [-942, 0, 1974, 0], [8039, 0, 135, 0], [0, -10330, 1197, -1197], [0, -6891, -1404, 1404], [3303, 0, 567, 0], [0, -1483, 1386, -1386], [0, -7992, -1023, 1023], [-9086, -9086, 0, 1167], [-8931, 0, -273, 0], [0, -2364, -693, 693], [-6502, 0, 678, 0], [11601, 11601, 0, 1572], [12319, 12319, 0, 486], [-5544, 0, -2556, 0], [-9786, -9786, 0, -897], [1515, 0, 759, 0], [16508, 0, -108, 0], [2528, 0, -2730, 0], [0, -9223, -684, 684], [11802, 0, 408, 0], [-3039, -3039, 0, -4176], [0, 5649, -534, 534], [-4625, -4625, 0, -510], [4277, 0, 2097, 0], [0, -3063, -336, 336], [-4074, 0, -3768, 0], [-9342, -9342, 0, -165], [0, 6635, 3816, -3816], [-8702, -8702, 0, -1293], [6644, 0, -1386, 0], [-21531, -21531, 0, 696], [0, 2189, -1008, 1008], [17360, 0, 1104, 0], [0, 9873, -1950, 1950], [-18029, -18029, 0, 2430], [-3294, 0, 1854, 0], [3096, 3096, 0, 2913], [0, -2982, 69, -69], [34, 34, 0, -2817], [0, 19832, 285, -285], [0, -12522, -1389, 1389], [-24669, 0, 441, 0], [0, 22862, -27, 27], [5766, 0, 4596, 0], [0, -23602, -957, 957], [20772, 0, 1116, 0], [0, 5456, -81, 81], [4804, 4804, 0, 3969], [-23965, 0, -675, 0], [-1104, -1104, 0, -6723], [16534, 16534, 0, -2985], [8498, 0, -3204, 0], [-14712, -14712, 0, -3627], [15160, 15160, 0, 2625], [9378, 0, 3168, 0], [-1550, -1550, 0, 2619], [12450, 12450, 0, 3123], [-16161, 0, -537, 0], [-21499, 0, -513, 0], [0, 2868, -2523, 2523], [-9770, -9770, 0, 195], [7860, 0, 3720, 0], [-21946, 0, -132, 0], [0, 5106, -3483, 3483], [0, 12512, -3375, 3375], [0, -3444, 1305, -1305], [775, 775, 0, -6558], [26688, 0, -3624, 0], [18190, 18190, 0, 1911], [-13242, 0, 96, 0], [20345, 0, 1443, 0], [11814, 11814, 0, 1803], [0, 12803, 762, -762], [0, 18606, 477, -477], [-11122, 0, -126, 0], [0, 5880, -2259, 2259], [14462, 0, -4728, 0], [-5166, -5166, 0, -489], [0, -13731, 2016, -2016], [-35721, 0, 2655, 0], [-2108, -2108, 0, 4077], [-1260, 0, -9036, 0], [25695, 25695, 0, -1686], [0, -10362, 7773, -7773], [12944, 0, -1800, 0], [0, -14218, -7389, 7389]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_18_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_18_4_c_b();".
// 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_18_4_c_b(:prec:=4)
    chi := MakeCharacter_18_c();
    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_18_4_c_b();".
// 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_18_4_c_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_18_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![46656, 1944, 297, -9, 1]>],Snew);
    return Vf;
end function;