
// Make newform 33.3.b.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_33_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_33_b_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_3_b_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_3_b_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 := [9, -3, -2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-9, 4, 2, 1], [6, 2, -2, -1], [3, 2, 0, -1]];
    Rf_basisdens := [1, 6, 3, 2];
    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_b();"
function MakeCharacter_33_b()
    N := 33;
    order := 2;
    char_gens := [23, 13];
    v := [1, 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_33_b_Hecke();"
function MakeCharacter_33_b_Hecke(Kf)
    N := 33;
    order := 2;
    char_gens := [23, 13];
    char_values := [[-1, 0, 0, 0], [1, 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, -1, -1, 1], [-1, 1, -1, 0], [0, -1, 0, 1], [0, -2, 0, -2], [0, 2, 1, -2], [-4, -4, 0, -4], [0, 2, 16, -2], [-6, 6, 0, 6], [0, 1, -16, -1], [0, 24, 4, -24], [14, 5, 0, 5], [-26, -7, 0, -7], [0, -4, 12, 4], [26, 4, 0, 4], [0, 22, 14, -22], [0, -30, -38, 30], [0, -23, 2, 23], [4, 20, 0, 20], [-6, 17, 0, 17], [0, -3, 4, 3], [-74, -6, 0, -6], [-32, -26, 0, -26], [0, 30, 24, -30], [0, -39, 26, 39], [38, -33, 0, -33], [0, 34, -4, -34], [-86, 28, 0, 28], [0, 6, -40, -6], [144, 10, 0, 10], [0, -75, -38, 75], [4, -56, 0, -56], [0, 6, -64, -6], [0, -41, -94, 41], [-74, -24, 0, -24], [0, -2, 40, 2], [8, -6, 0, -6], [30, 29, 0, 29], [94, 48, 0, 48], [0, 36, 16, -36], [0, -126, -72, 126], [0, -33, -66, 33], [114, -5, 0, -5], [0, 85, 164, -85], [58, 90, 0, 90], [0, -34, 12, 34], [-90, -84, 0, -84], [-154, -14, 0, -14], [-102, -53, 0, -53], [0, 114, -80, -114], [-66, -15, 0, -15], [0, 0, -100, 0], [0, 138, 108, -138], [-14, -18, 0, -18], [0, 83, 82, -83], [0, -16, -108, 16], [0, -14, 64, 14], [0, 2, -30, -2], [24, 44, 0, 44], [260, -10, 0, -10], [0, 18, 144, -18], [446, 14, 0, 14], [0, 108, 152, -108], [-122, 82, 0, 82], [0, -18, -246, 18], [250, -83, 0, -83], [0, 89, 172, -89], [-146, -9, 0, -9], [230, 84, 0, 84], [0, 152, -76, -152], [76, 86, 0, 86], [0, -273, -182, 273], [0, 20, 92, -20], [338, 59, 0, 59], [-60, -62, 0, -62], [294, 7, 0, 7], [0, -299, -228, 299], [0, 327, 116, -327], [-278, 84, 0, 84], [0, 184, -188, -184], [-242, -112, 0, -112], [0, -116, 24, 116], [-282, -68, 0, -68], [0, -90, 328, 90], [202, -93, 0, -93], [-160, -40, 0, -40], [0, 281, 374, -281], [0, -215, -234, 215], [-170, -58, 0, -58], [0, 72, -200, -72], [-110, 27, 0, 27], [0, -75, 98, 75], [0, -32, -380, 32], [-410, -83, 0, -83], [0, -472, -244, 472], [-90, -172, 0, -172], [0, -34, -148, 34], [0, -51, 164, 51], [0, -147, -150, 147], [50, -150, 0, -150], [568, -108, 0, -108], [-214, -282, 0, -282], [0, -94, -324, 94], [0, 92, 420, -92], [0, -180, 52, 180], [462, 134, 0, 134], [630, 213, 0, 213], [0, 416, 244, -416], [0, -488, -156, 488], [0, -418, -42, 418], [-530, 88, 0, 88], [-772, 114, 0, 114], [-588, 92, 0, 92], [0, 384, 236, -384], [226, 79, 0, 79], [334, 321, 0, 321], [0, 261, -190, -261], [-862, -69, 0, -69], [0, -89, 148, 89], [0, 173, -196, -173], [0, 314, 488, -314], [286, -219, 0, -219], [246, -4, 0, -4], [0, -54, 92, 54], [0, 496, 200, -496], [-46, -129, 0, -129], [0, -26, 684, 26], [470, -255, 0, -255], [0, 219, 384, -219], [158, 1, 0, 1], [472, 8, 0, 8], [-114, -52, 0, -52], [0, -292, -284, 292], [966, -95, 0, -95], [78, 104, 0, 104], [0, -348, -116, 348], [362, 114, 0, 114], [0, 426, 514, -426], [906, 10, 0, 10], [0, 289, -100, -289], [0, -492, -540, 492], [-1190, -48, 0, -48], [0, -142, 580, 142], [-14, 119, 0, 119], [0, 52, -292, -52], [-350, 159, 0, 159], [0, -355, 68, 355], [-944, -146, 0, -146], [0, -116, 188, 116], [422, 331, 0, 331], [0, 678, 350, -678], [336, 420, 0, 420], [0, -161, 442, 161], [26, 296, 0, 296], [0, 454, 96, -454], [410, 292, 0, 292], [0, -246, -626, 246], [-84, 118, 0, 118], [0, -372, -472, 372], [914, -246, 0, -246], [0, 522, 244, -522], [0, -429, 130, 429], [0, 98, 144, -98], [-236, -120, 0, -120], [0, 571, -214, -571], [0, 135, -50, -135], [0, -543, -212, 543], [758, 44, 0, 44], [564, -358, 0, -358]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_33_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_33_3_b_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_3_b_b(:prec:=4)
    chi := MakeCharacter_33_b();
    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_33_3_b_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_3_b_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_33_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<2,R![4, 0, 7, 0, 1]>],Snew);
    return Vf;
end function;