
// Make newform 33.4.a.d in Magma, downloaded from the LMFDB on 17 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_4_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_4_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 := [-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 := 4;
    raw_aps := [[0, 1], [3, 0], [10, -4], [2, -2], [11, 0], [-42, 8], [-28, 30], [-18, -18], [112, 0], [88, 46], [-72, 104], [-46, 44], [-248, 2], [10, -86], [-8, -48], [22, -128], [196, 0], [-526, -52], [388, 152], [136, 184], [-170, -252], [-78, -74], [336, -324], [482, 8], [-802, 420], [16, -306], [684, 124], [1472, -28], [590, -80], [-1562, -20], [2298, -2], [-1660, 152], [1390, -36], [-1154, 198], [324, 602], [1514, -618], [814, -856], [208, 732], [-716, -140], [1920, -330], [-2436, 144], [-1906, 880], [1408, 216], [2410, 456], [472, 134], [576, -312], [-3050, -554], [1844, 76], [-3080, 244], [-1062, -1260], [-616, 914], [1308, 36], [-1586, 180], [-1108, -1608], [3490, 1376], [-732, -1132], [-218, 56], [3334, 1298], [534, 176], [4056, -1758], [1794, 98], [4196, -2230], [-5658, 1174], [5472, 432], [2334, -1748], [-598, 276], [2008, -756], [-2866, 1092], [96, 1828], [-10246, -748], [-518, -632], [-3096, -32], [1620, -2980], [-742, -2564], [-6832, -2204], [5072, 2488], [-1414, 140], [-6230, 1596], [9870, 692], [-3610, 3668], [3164, -4256], [-9442, -1376], [-1340, -1268], [2542, 1380], [-8322, 2498], [2148, 832], [-406, -3320], [3686, -2140], [7324, 1258], [2188, 2580], [-508, 3320], [6564, -1252], [5400, 400], [-2688, 2980], [76, 3056], [-1260, 6052], [1478, 4328], [-138, -5580], [8042, -3702], [11974, -2904], [-3814, 3570], [-16124, -2686], [-3884, -184], [-7532, -1754], [-10382, 2306], [3426, 864], [-19036, 1680], [7568, 2002], [-12776, 2072], [-4494, 8440], [-7198, -4842], [-4262, 1628], [-12826, 884], [-576, -1772], [-3536, -6792], [-15814, -3592], [-14512, -660], [16128, -2464], [-7986, 8224], [6276, 472], [7222, -888], [25162, 1256], [-5560, 3350], [1828, 8976], [-18520, 612], [2924, 7210], [21194, 2244], [-14808, -3696], [23512, -5792], [7454, 560], [-15014, -4126], [6152, -384], [-6256, 6112], [10990, -8512], [15532, -6962], [12578, 4368], [-4682, -4208], [14186, -8510], [29554, -132], [-10548, 2238], [23782, -5202], [21296, -1890], [-23620, -628], [21904, 1524], [-24566, 4380], [3408, 7552], [-45390, -524], [-25916, 9174], [-5048, -5300], [18848, -11368], [-23550, 4476], [-4722, -1948], [5892, 7688], [-14704, 9544], [10432, 52], [-30296, -3248], [22286, -10718], [14918, 1772], [-7318, 3120], [10408, 2830], [5596, -12992], [-7484, -11914], [41170, -6594], [-32324, -2472], [-21626, -260], [-7160, 808], [4584, 824], [-51686, -532]];
    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_4_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_4_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, 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_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_4_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_33_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-8, -1, 1]>],Snew);
    return Vf;
end function;