
// Make newform 33.4.a.c in Magma, downloaded from the LMFDB on 19 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_c();".
// 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_c();".
// 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 := [-24, -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], [-6, -2], [14, -4], [-11, 0], [14, 2], [60, -14], [26, -2], [72, -10], [-96, -6], [176, 8], [-190, 52], [-384, -14], [206, -26], [96, 74], [-234, -54], [-36, -100], [-406, 34], [-340, -96], [288, 54], [662, -28], [254, 144], [-240, 156], [-414, 72], [-322, 192], [168, 314], [92, -96], [432, 132], [374, -406], [150, 180], [-250, 168], [492, 24], [-354, -280], [-1030, 14], [-756, -238], [-826, 240], [-322, -60], [464, 116], [1644, -96], [-312, 66], [-1548, 132], [-1138, -124], [-984, 266], [-22, 332], [2256, 562], [-2296, -524], [-2494, -326], [1508, -532], [-1032, -36], [2762, 208], [720, -726], [2436, -528], [2750, -280], [-1740, 844], [-3390, 688], [6828, -172], [-858, -302], [-1078, -216], [6350, -418], [3360, -810], [-2938, -898], [-4260, 426], [-142, 462], [-5016, 558], [-5506, 300], [1818, 202], [-1768, 1820], [2990, 540], [-5568, -548], [818, 22], [570, -144], [4200, -1028], [-3244, 788], [4082, 974], [3056, -1876], [5400, -1210], [-11382, 78], [-1174, -184], [1278, 1208], [-12250, 280], [-7164, -28], [-562, -2200], [3276, 516], [4622, 188], [2066, 1628], [2412, -1868], [2442, -1828], [-2746, 2560], [6420, 1458], [14972, -592], [12636, -144], [780, -388], [6464, 2244], [-8880, 1660], [10100, 24], [-6132, 2080], [-4026, 738], [18918, 480], [-10258, 2430], [302, 3866], [830, -3606], [1500, -3758], [5772, 832], [11532, 1194], [182, -1326], [-16030, -68], [14364, -284], [7320, 1766], [-13632, -266], [1730, -2424], [-10402, -1392], [8786, 690], [4758, -2460], [4160, 2628], [248, 392], [11802, -132], [992, 3492], [-5064, -4714], [-6162, -674], [13788, 496], [3110, -28], [-16486, -2484], [16608, -3894], [-17700, 764], [1352, -2364], [12324, 1826], [-7270, -1440], [2592, -1842], [10016, -1532], [21446, 2890], [24590, 1782], [1416, 28], [-8680, 1424], [-4642, -1728], [-27516, -1614], [-14974, -588], [7830, -2982], [6350, -5410], [16578, -3562], [18036, 4278], [194, 2330], [13320, 562], [19340, -2852], [22512, -2788], [14378, 1772], [-6696, -2126], [-4390, -3198], [-15924, -514], [18008, 2308], [8856, 378], [19178, 478], [3486, 8060], [-1156, -904], [-33984, -580], [7280, -1124], [-9744, 8742], [6962, 320], [-27402, 1872], [-45718, 720], [3840, 6234], [11172, -5976], [-26004, 5038], [24494, 5120], [-6252, -8524], [-138, 1560], [-33504, -2010], [-42304, -520], [-1582, 1302]];
    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_c();".
// 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_c(: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_c();".
// 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_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_33_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-24, -1, 1]>],Snew);
    return Vf;
end function;