
// Make newform 33.6.a.e in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_33_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_33_6_a_e();".
// 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_6_a_e();".
// 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 := [-8, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_a();"
function MakeCharacter_33_a()
    N := 33;
    order := 1;
    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;

function MakeCharacter_33_a_Hecke(Kf)
    return MakeCharacter_33_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 := 6;
    raw_aps := [[7, -1], [9, 0], [24, 10], [42, 62], [-121, 0], [-28, -74], [-550, 372], [12, -852], [46, -330], [1094, -1492], [-6040, 1600], [454, -2816], [18246, -8], [6440, -3112], [22066, -390], [-2536, -7102], [-2384, 1980], [-13664, 2026], [-13908, -12704], [17870, -4354], [-26174, 5568], [-14138, 11426], [28740, 21960], [-40454, 26704], [-125746, 9924], [21958, -17052], [-77588, 5252], [107572, -5432], [-44508, 19958], [-85966, -23752], [224902, -19486], [-35612, 79408], [-176258, 72492], [319084, -6204], [216458, -39536], [37070, 38706], [103938, -49580], [-34052, 80592], [311132, 52412], [-284346, 180972], [654600, -56844], [33546, -2860], [-155222, -121806], [-233474, 96444], [256998, 67684], [-349524, 309036], [331880, -229192], [1047216, 24944], [-299404, -154480], [-802902, -59340], [810190, -240032], [-190620, -258996], [-1064570, 119484], [-299632, 488772], [-359454, -382736], [172928, -308720], [15840, 75826], [270090, -303314], [761960, 14674], [-934470, -234108], [-295480, 168568], [-137078, -170912], [-55772, -777388], [-315978, -286098], [-106502, 437264], [2014772, -599730], [1049884, -27144], [362498, 1104648], [-652364, 218432], [1176388, -17138], [-1374694, -700120], [1299172, 45740], [1836872, -537176], [1391580, -924754], [-670668, -1074448], [144942, 340430], [-2343176, 1155226], [34138, 232908], [1234478, 45988], [393358, -1163684], [-2296344, -1305772], [-3158106, -549520], [970304, -627976], [1364602, -740904], [-501394, 2356954], [-1880648, 2551940], [3605718, -423388], [-6266138, 753892], [3851066, 264200], [-1232948, -1066068], [1867348, 1688824], [-3512056, 531712], [3414340, -1926172], [-2609588, 459776], [-3204420, 1621624], [-7723268, 21068], [2618224, -631358], [-940962, 683964], [-4496656, -339024], [-2074148, 3061278], [-1191724, -1053780], [5323074, -689792], [2443284, -1879496], [2804114, -214348], [13774740, -260636], [2468574, -357900], [-3952696, -2554524], [-1478334, -844492], [1872102, 3752398], [-809750, 127232], [6945158, -3539886], [4045320, -311894], [12520258, -1851104], [-3446372, 2197664], [1332208, -3249456], [-8018666, 1725172], [9602732, 3099624], [4874030, -1407098], [-5505116, 2531702], [-17099420, 351752], [-2378486, 2635428], [-13660194, 2372236], [-2425250, -3835652], [-3993416, 1620300], [-13593340, 2946720], [-7367478, 639080], [-2410918, -3685308], [7961286, 1716462], [10366868, -5507356], [15429796, -998186], [-2370648, -2331152], [7426964, 4973196], [14676552, -1086640], [-17841906, -899336], [14740370, 4101668], [771086, -1139748], [1014776, -9258526], [-16856736, -1165000], [-9641768, 500082], [-25936074, -1343280], [-2576216, 4560816], [-5718994, 5065548], [-9780824, 5343040], [-2463644, 7285008], [13062142, -5527680], [-11817782, -3421966], [20726656, -3492622], [-21979562, 5915184], [-20548300, 389168], [16678594, 996178], [21410292, -463074], [-7193374, -6571592], [22286324, 4813816], [7900332, -3139804], [1441028, -2735368], [-12331426, 13008278], [4596618, 11126534], [-3933842, 3964612], [16845674, 1465248], [22469390, -178396], [11962796, 3638696], [50464950, 1525456], [-30114938, 2728938], [16464016, 105084], [-15148282, -5752468], [7828070, -12410386], [-37264360, -2218496], [-35247564, 8712982]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_33_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_33_6_a_e();".
// 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_6_a_e(:prec:=2)
    chi := MakeCharacter_33_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    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_6_a_e();".
// 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_6_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_33_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<2,R![34, -13, 1]>],Snew);
    return Vf;
end function;