
// Make newform 33.4.d.b in Magma, downloaded from the LMFDB on 16 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_33_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_33_d_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_33_4_d_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_33_4_d_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 := [821047, 205270, 120841, 16258, 2614, 10, -35, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [-251237072504, -27353124741, -3509536790, 2191936298, 258375310, -37910704, -4621500, 654449], [204737740374, -23176332663, 1344980044, -2938713578, -310366016, 34074596, 7661354, -1117339], [-204737740374, 66274294521, -1344980044, 2938713578, 310366016, -34074596, -7661354, 1117339], [1611381064281, 378144323188, 104272839604, 13402791352, -533197318, -255666928, 18044843, 2849712], [-1902320865922, -1043874627293, -193283264130, -33192323398, 759114088, 480817032, -760108, -9352895], [-403613661909, -441525412450, -114008586182, -20257454786, -34001274, 295356518, 8072123, -5889566], [64671640283, 37980909510, 6517009006, 886340512, 12503088, -16270822, -399831, 345396]];
    Rf_basisdens := [1, 43097961858, 43097961858, 43097961858, 43097961858, 129293885574, 43097961858, 2268313782];
    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_33_d();"
function MakeCharacter_33_d()
    N := 33;
    order := 2;
    char_gens := [23, 13];
    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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_33_d_Hecke();"
function MakeCharacter_33_d_Hecke(Kf)
    N := 33;
    order := 2;
    char_gens := [23, 13];
    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 := 4;
    raw_aps := [[0, 0, 1, 0, 0, 0, 0, 0], [1, 0, 0, -1, 0, 0, 0, 0], [0, -1, 0, 1, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, -3, -3, 3, 1, -1, 1, 0], [0, 0, 0, 0, 0, 0, 0, -1], [0, 0, 8, 0, -4, 0, -4, 0], [0, 0, 0, 0, -4, 0, 4, 0], [0, 3, 0, -3, 0, 1, 0, 0], [0, 0, 18, 0, 2, 0, 2, 0], [104, -3, 0, -3, 0, 0, 0, 0], [-98, -25, 0, -25, 0, 0, 0, 0], [0, 0, -58, 0, -6, 0, -6, 0], [0, 0, 0, 0, 8, 0, -8, -2], [0, -34, 0, 34, 0, 25, 0, 0], [0, -8, 0, 8, 0, -47, 0, 0], [0, 23, 0, -23, 0, -26, 0, 0], [0, 0, 0, 0, 4, 0, -4, 1], [8, 27, 0, 27, 0, 0, 0, 0], [0, -23, 0, 23, 0, 67, 0, 0], [0, 0, 0, 0, -20, 0, 20, -24], [0, 0, 0, 0, 12, 0, -12, 25], [0, 0, 4, 0, 12, 0, 12, 0], [0, 41, 0, -41, 0, -122, 0, 0], [-532, 105, 0, 105, 0, 0, 0, 0], [0, 0, 162, 0, 38, 0, 38, 0], [1004, 72, 0, 72, 0, 0, 0, 0], [0, 0, -270, 0, 6, 0, 6, 0], [0, 0, 0, 0, 28, 0, -28, -21], [0, -109, 0, 109, 0, 22, 0, 0], [0, 0, 0, 0, -40, 0, 40, 45], [0, 0, -20, 0, 80, 0, 80, 0], [0, 29, 0, -29, 0, -44, 0, 0], [0, 0, 0, 0, -4, 0, 4, -4], [0, 0, -146, 0, -70, 0, -70, 0], [0, 0, 0, 0, -52, 0, 52, -71], [254, -99, 0, -99, 0, 0, 0, 0], [-2774, 158, 0, 158, 0, 0, 0, 0], [0, 0, -412, 0, 24, 0, 24, 0], [0, 0, -206, 0, -30, 0, -30, 0], [0, 85, 0, -85, 0, -170, 0, 0], [-410, -187, 0, -187, 0, 0, 0, 0], [0, 389, 0, -389, 0, -113, 0, 0], [0, 0, 0, 0, 0, 0, 0, -70], [0, 0, 694, 0, -26, 0, -26, 0], [2512, 284, 0, 284, 0, 0, 0, 0], [0, 0, 0, 0, 40, 0, -40, -94], [-132, -725, 0, -725, 0, 0, 0, 0], [0, 0, -1014, 0, 38, 0, 38, 0], [-246, 655, 0, 655, 0, 0, 0, 0], [0, 0, -686, 0, -134, 0, -134, 0], [0, 0, 116, 0, 156, 0, 156, 0], [0, 0, 0, 0, -152, 0, 152, -26], [0, 245, 0, -245, 0, 118, 0, 0], [0, -264, 0, 264, 0, 520, 0, 0], [0, 0, 988, 0, -168, 0, -168, 0], [0, 280, 0, -280, 0, 289, 0, 0], [0, 0, 0, 0, 56, 0, -56, 27], [0, 0, 0, 0, -36, 0, 36, 163], [0, 0, 164, 0, 76, 0, 76, 0], [0, 0, 0, 0, 136, 0, -136, 98], [0, 0, 942, 0, -86, 0, -86, 0], [0, 0, 0, 0, -108, 0, 108, -48], [0, -574, 0, 574, 0, 373, 0, 0], [1386, 679, 0, 679, 0, 0, 0, 0], [0, -183, 0, 183, 0, -593, 0, 0], [-3262, 393, 0, 393, 0, 0, 0, 0], [0, 0, 0, 0, 236, 0, -236, 32], [0, 0, -1636, 0, -72, 0, -72, 0], [0, 0, 0, 0, -184, 0, 184, -137], [0, -585, 0, 585, 0, -602, 0, 0], [0, 0, 1168, 0, 220, 0, 220, 0], [1688, 675, 0, 675, 0, 0, 0, 0], [0, 0, 0, 0, 192, 0, -192, -223], [5486, -759, 0, -759, 0, 0, 0, 0], [0, -829, 0, 829, 0, 673, 0, 0], [0, -1237, 0, 1237, 0, 115, 0, 0], [-650, -844, 0, -844, 0, 0, 0, 0], [0, 512, 0, -512, 0, -422, 0, 0], [0, 0, 0, 0, -80, 0, 80, 218], [0, 662, 0, -662, 0, -212, 0, 0], [-8338, 672, 0, 672, 0, 0, 0, 0], [0, 0, 292, 0, 376, 0, 376, 0], [-6250, -165, 0, -165, 0, 0, 0, 0], [0, 0, 0, 0, -132, 0, 132, 571], [0, -517, 0, 517, 0, -422, 0, 0], [0, 1153, 0, -1153, 0, 340, 0, 0], [0, 0, 0, 0, 192, 0, -192, -314], [0, 0, -426, 0, -298, 0, -298, 0], [5772, -83, 0, -83, 0, 0, 0, 0], [0, -725, 0, 725, 0, 1120, 0, 0], [0, 0, 3788, 0, -32, 0, -32, 0], [4624, 965, 0, 965, 0, 0, 0, 0], [0, 0, 2918, 0, 414, 0, 414, 0], [134, -246, 0, -246, 0, 0, 0, 0], [0, 0, -2128, 0, 728, 0, 728, 0], [0, 1983, 0, -1983, 0, 481, 0, 0], [0, 679, 0, -679, 0, 244, 0, 0], [0, 0, 0, 0, 92, 0, -92, 606], [0, 0, 0, 0, 512, 0, -512, 243], [0, 0, 0, 0, 296, 0, -296, -32], [0, 0, 2022, 0, -6, 0, -6, 0], [0, 0, 1010, 0, -582, 0, -582, 0], [0, 0, -3968, 0, -96, 0, -96, 0], [0, 0, 0, 0, 56, 0, -56, -448], [12016, 1643, 0, 1643, 0, 0, 0, 0], [0, 482, 0, -482, 0, -1952, 0, 0], [0, 0, -1660, 0, 788, 0, 788, 0], [0, -1134, 0, 1134, 0, -791, 0, 0], [0, 0, 0, 0, 36, 0, -36, -416], [0, 0, 0, 0, 452, 0, -452, 47], [0, 0, 0, 0, 356, 0, -356, 219], [0, -1048, 0, 1048, 0, 1396, 0, 0], [-7720, -1761, 0, -1761, 0, 0, 0, 0], [-5216, 1265, 0, 1265, 0, 0, 0, 0], [0, 2445, 0, -2445, 0, 976, 0, 0], [-240, -335, 0, -335, 0, 0, 0, 0], [0, 263, 0, -263, 0, -2417, 0, 0], [0, -3145, 0, 3145, 0, 1327, 0, 0], [0, 0, -1490, 0, 966, 0, 966, 0], [13886, -783, 0, -783, 0, 0, 0, 0], [0, 0, 0, 0, -544, 0, 544, -494], [0, 0, 4550, 0, 154, 0, 154, 0], [0, 3214, 0, -3214, 0, -1136, 0, 0], [2168, -2463, 0, -2463, 0, 0, 0, 0], [0, 0, 5702, 0, -430, 0, -430, 0], [-238, 1599, 0, 1599, 0, 0, 0, 0], [0, -911, 0, 911, 0, 2101, 0, 0], [-20056, 1677, 0, 1677, 0, 0, 0, 0], [0, 0, 0, 0, -780, 0, 780, 71], [0, 0, 0, 0, 40, 0, -40, 522], [0, 0, -3768, 0, -1136, 0, -1136, 0], [-16276, -861, 0, -861, 0, 0, 0, 0], [-4406, 932, 0, 932, 0, 0, 0, 0], [0, 0, -5136, 0, 748, 0, 748, 0], [0, 0, 0, 0, -140, 0, 140, 902], [0, 2384, 0, -2384, 0, -401, 0, 0], [0, 0, 0, 0, 48, 0, -48, -538], [0, 1577, 0, -1577, 0, 373, 0, 0], [0, 0, 4138, 0, -266, 0, -266, 0], [0, 0, 0, 0, -504, 0, 504, 1014], [0, 0, -9178, 0, -482, 0, -482, 0], [-10116, -3269, 0, -3269, 0, 0, 0, 0], [0, 0, -3060, 0, -384, 0, -384, 0], [21310, 83, 0, 83, 0, 0, 0, 0], [0, -3513, 0, 3513, 0, 277, 0, 0], [0, 0, 0, 0, 724, 0, -724, -221], [0, 0, -3398, 0, -1038, 0, -1038, 0], [16910, -573, 0, -573, 0, 0, 0, 0], [0, -1750, 0, 1750, 0, -1715, 0, 0], [0, 0, 0, 0, 272, 0, -272, -271], [0, -2327, 0, 2327, 0, 1846, 0, 0], [1756, -5572, 0, -5572, 0, 0, 0, 0], [0, 0, 3936, 0, -512, 0, -512, 0], [18084, 3748, 0, 3748, 0, 0, 0, 0], [0, 3230, 0, -3230, 0, -4871, 0, 0], [0, 0, 0, 0, 844, 0, -844, -687], [0, 4280, 0, -4280, 0, 658, 0, 0], [0, 0, 0, 0, 348, 0, -348, -1128], [0, 0, -2750, 0, -214, 0, -214, 0], [0, 2345, 0, -2345, 0, -3248, 0, 0], [0, 0, -3318, 0, 1950, 0, 1950, 0], [0, 0, 0, 0, -612, 0, 612, -337], [0, -1597, 0, 1597, 0, 3046, 0, 0], [0, 501, 0, -501, 0, 4024, 0, 0], [0, -769, 0, 769, 0, 4627, 0, 0], [-8716, 2184, 0, 2184, 0, 0, 0, 0], [0, 0, 0, 0, 1048, 0, -1048, 1399]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_33_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_33_4_d_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_33_4_d_b(:prec:=8)
    chi := MakeCharacter_33_d();
    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_33_4_d_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_33_4_d_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_33_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![138, 0, -27, 0, 1]>],Snew);
    return Vf;
end function;