
// Make newform 216.3.m.b in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_216_m();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_216_m_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_216_3_m_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_216_3_m_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 := [144, 144, 276, -84, 133, -2, 15, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [-256788, 140724, -29928, -75285, -7826, -5547, 198, -473], [78912, 156924, -132153, 82215, -6901, 10353, -1827, 677], [-177138, -98226, -29928, -75285, -7826, -5547, 198, -473], [-401472, 247356, -232782, 143085, -19444, 16707, -2688, 1013], [194364, 129078, -107691, 68730, -7447, 9966, -2019, 644], [209808, -1245834, 475248, -389565, 47066, -45123, 7182, -2407], [2015568, 544536, 364008, 657585, 95186, 67467, -4878, 5753]];
    Rf_basisdens := [1, 159300, 159300, 79650, 159300, 79650, 159300, 318600];
    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_216_m();"
function MakeCharacter_216_m()
    N := 216;
    order := 6;
    char_gens := [55, 109, 137];
    v := [6, 6, 5];
    // 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_216_m_Hecke();"
function MakeCharacter_216_m_Hecke(Kf)
    N := 216;
    order := 6;
    char_gens := [55, 109, 137];
    char_values := [[1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 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 := 3;
    raw_aps := [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [1, -1, -1, 0, 1, 0, 0, 0], [1, -1, -2, 0, 0, 0, 1, 0], [-2, -1, -4, 3, 0, -2, -1, 2], [1, -1, 3, 2, -3, -6, -2, 2], [0, -3, -4, 1, 1, 1, 4, -2], [-2, -5, 0, -1, 0, 0, 0, 4], [14, -2, -8, 0, 4, 0, 3, 3], [10, 2, 9, -1, 0, -3, 2, -4], [0, 0, -12, 0, -6, -12, -1, 1], [16, 2, 0, 0, -6, 6, 0, -2], [-42, 0, 21, 0, 0, 0, -2, -2], [-14, 7, 14, -7, 12, 6, 7, 0], [-15, -3, -10, -2, 0, 6, -3, 6], [10, 2, -8, 4, -4, -4, -12, 6], [21, 9, -6, 0, -14, 0, -3, -3], [2, 12, 5, -5, 18, 9, -2, 0], [-5, 5, 0, -10, 0, 0, 5, -5], [-42, 12, 84, -12, 4, 4, 0, 0], [-4, 11, 0, -7, -9, 9, 0, -18], [13, -5, -12, 6, 24, 12, -7, 0], [-19, 1, -22, 2, 0, 2, 1, -2], [-2, 14, -16, -24, 0, 0, 20, -10], [-63, 10, 67, -6, 0, 0, 2, 0], [36, -12, 21, 27, 0, 15, -12, 24], [-6, 6, 42, -12, -6, -12, 7, -7], [-30, -3, 64, 5, -18, -18, -4, 2], [-48, -20, 0, 18, 6, -6, 0, 38], [-93, -15, 39, 0, -3, 0, 8, 8], [78, -2, 0, -4, 6, -6, 0, -2], [-12, -24, -6, 0, 38, 0, 7, 7], [3, -6, 19, -10, 0, -42, -6, 12], [29, -29, -14, 58, 0, 0, -23, 23], [19, 17, -1, 0, 35, 0, 10, 10], [-9, -17, 0, 8, -24, -12, 1, 0], [-11, 11, -13, -22, 3, 6, -24, 24], [6, 0, 0, -6, 30, -30, 0, -6], [102, -6, -54, 0, -42, 0, 3, 3], [52, -16, 21, 47, 0, -15, -16, 32], [8, 4, -48, -20, 42, 42, 32, -16], [-36, -12, 0, 14, 12, -12, 0, 26], [-49, 1, -70, 20, 0, -12, 1, -2], [0, 0, -7, 0, 18, 36, 10, -10], [0, 24, 40, -4, -24, -24, -40, 20], [-64, -10, 0, -4, 30, -30, 0, 6], [-14, 14, -8, -28, 24, 48, 35, -35], [47, 27, -42, -22, -12, -6, 17, 0], [34, -25, 68, -9, 0, -12, -25, 50], [11, -11, -5, 22, 33, 66, 8, -8], [-122, 23, 228, -31, 77, 77, 16, -8], [162, 42, -60, 0, 32, 0, -29, -29], [-3, 36, 35, -4, -12, -6, -28, 0], [86, 25, -128, -3, 16, 16, -44, 22], [-10, -32, -11, 0, -28, 0, -16, -16], [-133, -11, -128, 6, 0, 82, -11, 22], [-38, -10, 48, -4, -44, -44, 28, -14], [30, 2, 0, 0, -12, 12, 0, -2], [-112, 66, 169, -9, -66, -33, -48, 0], [-40, 22, -27, -35, 0, -1, 22, -44], [-12, 12, -142, -24, -6, -12, -17, 17], [49, 47, -1, 0, 51, 0, -20, -20], [-298, 1, 0, 9, -30, 30, 0, 8], [32, -32, -32, 0, 54, 0, 55, 55], [169, -52, -177, 44, 0, 0, -36, 0], [12, 18, 29, -35, 0, 105, 18, -36], [205, -33, -228, 10, 96, 48, 13, 0], [10, -10, 185, 20, 6, 12, -28, 28], [-157, -53, 52, 0, 66, 0, -7, -7], [80, -86, -147, 19, 90, 45, 48, 0], [15, -6, -13, 34, 0, -130, -6, 12], [8, -26, -80, -6, 108, 108, 64, -32], [173, 39, -108, 26, -96, -48, -91, 0], [45, -45, -185, 90, -27, -54, 6, -6], [352, -35, 0, 25, 24, -24, 0, 60], [186, -42, -114, 0, -54, 0, 3, 3], [-240, -12, -295, 67, 0, -83, -12, 24], [-108, -22, 0, -16, -42, 42, 0, 6], [244, 38, -103, 0, 84, 0, -10, -10], [-34, 34, -155, -68, -78, -156, 16, -16], [-458, 14, 236, 0, 108, 0, -5, -5], [-12, -56, -19, 25, 114, 57, 6, 0], [-184, -2, 336, -14, -104, -104, 32, -16], [-56, -33, 0, 33, -21, 21, 0, 66], [203, -29, -216, 16, 12, 6, -3, 0], [212, 43, 138, 31, 0, 14, 43, -86], [40, 35, -100, -45, 179, 179, 20, -10], [185, 110, -129, -54, 36, 18, -2, 0], [-96, 30, -87, -39, 0, -35, 30, -60], [50, -50, 172, 100, 12, 24, -21, 21], [2, 73, -40, -91, -126, -126, 36, -18], [159, 21, 88, 50, 0, 146, 21, -42], [82, 36, 0, -98, -60, 60, 0, -134], [-381, 87, 234, 0, -168, 0, -59, -59], [25, -25, -466, 50, 48, 96, 77, -77], [-106, 70, 132, -110, 150, 150, 80, -40], [-205, -71, 67, 0, 105, 0, 70, 70], [66, -39, -36, 87, 1, 1, -96, 48], [-168, 34, 0, -4, 0, 0, 0, -38], [-258, 108, 0, 24, -42, 42, 0, -84], [-94, 119, 160, -53, -132, -66, -13, 0], [18, 84, 48, -42, -22, -22, -84, 42], [-561, -33, 264, 0, 40, 0, 65, 65], [-205, -20, -281, 96, 0, 28, -20, 40], [47, -47, 268, 94, -36, -72, -35, 35], [360, -25, 0, -15, -27, 27, 0, 10], [222, 3, 330, -111, 0, 166, 3, -6], [194, -26, -248, 96, -124, -124, -140, 70], [142, -94, -118, 0, -54, 0, 19, 19], [-203, 22, 183, -42, 216, 108, 62, 0], [-58, 58, -2, -116, 60, 120, -33, 33], [40, -62, 0, -36, 30, -30, 0, 26], [184, 2, -91, 0, 48, 0, -38, -38], [304, 19, -236, 49, -168, -84, -117, 0], [-666, -56, 0, -2, 6, -6, 0, 54], [257, -68, 175, 150, 0, 4, -68, 136], [-31, 31, 0, -62, -18, -36, 93, -93], [-120, 30, 256, -22, -260, -260, -16, 8], [-331, 91, 211, 0, -57, 0, -32, -32], [45, -15, -84, 144, 0, 24, -15, 30], [35, -35, 723, 70, -69, -138, -76, 76], [780, -52, -747, 85, -138, -69, -118, 0], [70, -4, 45, 29, 0, -161, -4, 8], [-50, 71, -32, -137, -10, -10, 132, -66], [-459, -65, 362, -32, -84, -42, 129, 0], [38, -26, -216, -44, -78, -78, 140, -70], [-512, -114, 409, 11, -78, -39, 92, 0], [-64, -182, 176, 206, 74, 74, -48, 24], [-705, -93, 576, -36, -96, -48, 165, 0], [127, -127, -393, 254, -3, -6, -166, 166], [-328, -93, 0, -81, 114, -114, 0, 12], [58, 62, 2, 0, 28, 0, 79, 79], [-24, 24, 182, -48, 6, 12, 11, -11], [-254, 88, 0, 56, 132, -132, 0, -32], [283, -79, -181, 0, -91, 0, 2, 2], [-29, 29, 655, -58, 9, 18, 120, -120], [-10, -158, 24, 160, 18, 18, -4, 2], [0, 0, 142, 0, 102, 204, -13, 13], [-1075, -29, 523, 0, 61, 0, 6, 6], [-108, -9, 148, -25, -71, -71, 68, -34], [306, -17, 0, 43, -60, 60, 0, 60], [34, 86, 57, -109, 0, 201, 86, -172], [-118, 118, -224, -236, 36, 72, 231, -231], [-84, -72, 296, 136, 94, 94, -128, 64], [-364, -22, 0, -44, 12, -12, 0, -22], [69, 9, 142, -82, 0, -60, 9, -18], [-118, 140, 209, -49, 246, 123, -42, 0], [52, 74, 209, -231, 0, 27, 74, -148], [57, -57, 412, 114, 108, 216, -83, 83], [-20, 122, 48, -118, 26, 26, -8, 4], [5, -5, 43, 10, 195, 390, 70, -70], [134, 130, -192, -92, -68, -68, -76, 38], [-238, -69, 0, -85, 78, -78, 0, -16], [156, 60, -48, 0, -172, 0, 35, 35], [-542, 211, 706, -47, -192, -96, -117, 0], [33, 117, 0, -84, 0, 286, 117, -234], [-394, 158, 0, 152, 102, -102, 0, -6], [76, -46, 209, -87, 0, -145, -46, 92], [-336, 114, 0, -96, 24, -24, 0, -210], [-465, 141, 303, 0, 9, 0, -68, -68], [-478, 115, -420, -173, 0, 246, 115, -230], [-168, -15, 348, 21, 39, 39, -12, 6], [50, -50, -208, 100, 138, 276, 55, -55], [-266, -118, 460, 82, -132, -132, 72, -36], [-808, 82, 445, 0, 0, 0, -16, -16], [-21, -75, 216, -162, 0, -48, -75, 150], [490, 78, 0, -44, -168, 168, 0, -122], [740, -78, -851, -33, 18, 9, 144, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_216_m_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_216_3_m_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_216_3_m_b(:prec:=8)
    chi := MakeCharacter_216_m();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    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_216_3_m_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_216_3_m_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_216_m();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![16, -528, 5612, 6468, 2661, 294, -37, -6, 1]>],Snew);
    return Vf;
end function;