
// Make newform 33.6.a.d 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_d();".
// 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_d();".
// 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 := [-78, -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 := [[0, 1], [-9, 0], [-24, 10], [-10, 2], [121, 0], [20, -106], [-462, 4], [-1468, 4], [2610, 26], [-6234, -132], [4664, 608], [3158, -320], [12486, -728], [9560, 1240], [-2514, -778], [20088, 594], [10944, -3676], [-7072, 2746], [32300, 768], [32274, -3102], [26546, 320], [9626, -2130], [-5388, -3528], [-30582, 3024], [-92074, 1092], [-124698, -5932], [41156, -16164], [-35028, 10776], [94388, 5558], [144114, 12600], [88058, -11826], [-135660, 27312], [44142, 14156], [172964, 12332], [110106, -26080], [191858, 22926], [-137998, -2220], [248180, -7888], [198852, -9948], [-122586, -33156], [24120, -69396], [-291046, 32276], [386646, 5918], [15950, -49252], [8214, -45452], [60884, -60332], [-161608, -78728], [707840, -63184], [-388356, 49584], [213674, 45076], [-947250, 61200], [-713172, -15660], [-30970, -55780], [-407136, 64828], [566562, -68816], [1142880, 103280], [-356304, -173006], [185942, -119022], [-1086040, -55678], [-513294, -32268], [-1004056, 100856], [-853446, 59376], [-1275460, -8388], [-929334, -187662], [2334362, 56208], [1191204, -139346], [402164, -188920], [1218770, -196920], [-899628, 109696], [591284, 284014], [-1420998, 331560], [383532, -89612], [2998232, 201176], [-285172, -34258], [-2141140, -188560], [3195378, -42286], [3916632, -21174], [1898906, 209132], [761022, 34340], [2441678, -49220], [3026088, -430516], [3416678, 208592], [-3991536, 65544], [1645178, 151640], [162962, 256502], [1370232, -265220], [-3381882, -278524], [2582390, 142852], [-1600566, -60744], [-1335868, -393580], [-3690468, 119784], [4630344, 56864], [1509932, 465180], [-176316, 121216], [-320860, -123576], [2978772, -212684], [-6560112, -180366], [-3041154, 198492], [281360, 269856], [-541588, 625790], [1138604, 693828], [1443570, 682000], [472188, 427432], [-8913750, -156732], [1553660, 460044], [1492838, 931060], [-2252232, -91748], [-5044614, -1171036], [-2160006, -219758], [-4670950, 1001472], [1253978, 325134], [14584232, 230874], [366594, -264576], [-8452804, 1174560], [4707968, -384400], [2828838, 746580], [-6476932, 54168], [9903666, -144838], [1343316, -631946], [9044892, -1178888], [-4690150, -1413052], [-12986338, 597612], [-3599826, -357396], [3305640, 285332], [2633396, -2482848], [10624314, -108520], [-4963270, 759684], [7635546, 866130], [7610780, 680380], [1150772, 95062], [6381416, -1896768], [-16679076, 528436], [-5762104, 3003344], [-11791330, -1804392], [-2287542, -1485708], [17526590, -468804], [-21596472, -318366], [-18206224, -859240], [-6627816, 673442], [-11415114, 1538208], [8674088, -3332800], [22474782, -186308], [7962584, 2347360], [-9248460, -2323408], [-5159218, -2438560], [-7526538, 251710], [6083456, -169182], [12599574, 1832576], [-4847188, 3358768], [8386686, -2629602], [-20980300, 2061166], [-3523470, 547640], [25546820, 773480], [18304740, 508124], [-2832604, -1986488], [10185762, -3379782], [12597494, -718438], [-8441250, 2319396], [43885658, -202944], [-36177042, -1471692], [10471332, -2970600], [21310998, -3539936], [32686202, 1120838], [-10798224, 2297444], [-588666, -3739860], [10488666, -1262190], [-23836792, 63296], [-2571916, 939078]];
    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_d();".
// 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_d(: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_d();".
// 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_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_33_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<2,R![-78, -1, 1]>],Snew);
    return Vf;
end function;