
// Make newform 4033.2.a.b in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4033_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_4033_2_a_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_4033_2_a_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_4033_a();"
function MakeCharacter_4033_a()
    N := 4033;
    order := 1;
    char_gens := [1963, 2295];
    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_4033_a_Hecke(Kf)
    return MakeCharacter_4033_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 := 2;
    raw_aps := [[1], [0], [-2], [2], [1], [-5], [-3], [0], [2], [0], [-4], [-1], [12], [11], [9], [-8], [2], [10], [8], [-6], [-10], [-2], [-12], [-14], [16], [12], [-6], [-12], [1], [12], [13], [15], [-9], [1], [16], [16], [17], [12], [-12], [-9], [6], [20], [-13], [22], [3], [-4], [-22], [-2], [11], [-14], [9], [9], [5], [-2], [6], [4], [-6], [20], [-33], [12], [22], [19], [-8], [-15], [13], [-14], [-35], [8], [-4], [-3], [14], [24], [-7], [-25], [-21], [2], [19], [-2], [6], [-28], [-5], [-18], [27], [9], [19], [-20], [-9], [18], [18], [-1], [13], [-16], [-6], [5], [-21], [4], [14], [18], [-28], [44], [2], [-30], [0], [27], [-32], [9], [-8], [13], [40], [-16], [40], [-24], [20], [-22], [-34], [2], [-46], [25], [36], [0], [-10], [30], [22], [12], [28], [30], [-17], [-48], [13], [-2], [-45], [-18], [46], [13], [-2], [50], [6], [19], [-8], [-10], [2], [40], [-40], [1], [-4], [47], [35], [18], [46], [54], [-31], [31], [-6], [28], [49], [2], [-10], [12], [10], [-52], [60], [-38], [52], [-8], [-2], [0], [54], [-6], [-23], [-15], [52], [-59], [8], [60], [-5], [41], [-10], [37], [34], [21], [-51], [0], [-6], [-48], [-29], [-20], [-57], [20], [14], [2], [-6], [36], [20], [54], [30], [-2], [41], [-39], [28], [-48], [-33], [-43], [3], [14], [-4], [-30], [-8], [6], [69], [-7], [-11], [24], [44], [-18], [62], [-4], [22], [54], [-4], [-14], [-6], [65], [-70], [-16], [-1], [18], [33], [34], [-57], [13], [-19], [35], [5], [-35], [-54], [-33], [-22], [-27], [-12], [3], [19], [-68], [-49], [49], [50], [12], [56], [-3], [49], [-74], [58], [-49], [18], [32], [30], [12], [44], [-44], [20], [16], [-13], [-28], [15], [-26], [-57], [-64], [-45], [-31], [40], [-25], [62], [52], [22], [-41], [28], [-15], [8], [44], [42], [52], [-12], [63], [60], [-60], [14], [-77], [-82], [-38], [59], [42], [-78], [38], [51], [-18], [13], [52], [-19], [-78], [-4], [10], [56], [59], [75], [24], [-59], [-62], [-58], [18], [-63], [-17], [13], [20], [22], [48], [72], [-57], [8], [26], [20], [-66], [-47], [-63], [65], [24], [-61], [-6], [-40], [64], [-9], [66], [-88], [58], [-10], [-75], [-65], [-22], [34], [69], [74], [39], [54], [-40], [54], [-56], [25], [-24], [16], [37], [84], [-46], [-62], [-5], [90], [-67], [-7], [-21], [-3], [48], [-4], [89], [-2], [33], [0], [-33], [-73], [-62], [-18], [19], [-57], [-92], [70], [22], [42], [24], [-2], [-37], [66], [58], [64], [-35], [24], [-35], [47], [8], [-5], [87], [-53], [-30], [26], [36], [-74], [13], [75], [72], [81], [77], [-82], [-6], [29], [-53], [-24], [26], [-22], [-78], [44], [-10], [-1], [-12], [-92], [62], [-51], [78], [52], [4], [-50], [74], [6], [67], [81], [10], [-26], [-78], [0], [22], [54], [-30], [76], [-63], [4], [42]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4033_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4033_2_a_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_4033_2_a_b(:prec:=1)
    chi := MakeCharacter_4033_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 2));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_4033_2_a_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_4033_2_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4033_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-1, 1]>],Snew);
    return Vf;
end function;