
// Make newform 4005.2.a.b in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4005_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_4005_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_4005_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_4005_a();"
function MakeCharacter_4005_a()
    N := 4005;
    order := 1;
    char_gens := [3116, 802, 181];
    v := [1, 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_4005_a_Hecke(Kf)
    return MakeCharacter_4005_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 := [[0], [0], [-1], [-4], [-2], [4], [6], [0], [-5], [5], [-2], [-2], [-5], [2], [8], [-8], [9], [14], [15], [-12], [-15], [-5], [11], [-1], [-5], [14], [-20], [2], [1], [-17], [-12], [2], [-2], [-23], [-3], [-6], [-5], [-22], [24], [22], [-26], [-10], [-15], [-10], [18], [-11], [-12], [3], [0], [22], [14], [7], [-8], [-12], [-20], [6], [26], [-8], [27], [-18], [16], [9], [-13], [20], [-8], [-22], [20], [-20], [-20], [2], [11], [-4], [4], [17], [-20], [-24], [-18], [10], [30], [-3], [-11], [-2], [16], [-16], [-12], [-6], [6], [20], [0], [-24], [12], [-22], [7], [-17], [-18], [32], [-24], [2], [-12], [44], [16], [-7], [27], [-31], [-22], [20], [36], [39], [16], [-17], [-27], [-29], [-7], [31], [-13], [-46], [-43], [45], [-43], [-30], [30], [1], [-42], [-9], [15], [-10], [-34], [24], [-40], [-19], [18], [9], [-40], [-29], [-28], [49], [-15], [6], [48], [-50], [-41], [-36], [-47], [-49], [22], [-35], [22], [54], [-42], [-4], [38], [-34], [4], [17], [29], [12], [-18], [-16], [13], [13], [-18], [35], [32], [-6], [2], [-2], [-8], [5], [-8], [-24], [14], [-21], [-24], [-22], [-38], [-19], [-60], [-18], [7], [62], [-20], [-23], [6], [55], [-9], [5], [30], [23], [-20], [-19], [-21], [39], [20], [-35], [-21], [40], [-1], [0], [-9], [-56], [54], [32], [2], [-24], [-61], [3], [42], [57], [54], [20], [-10], [-48], [-55], [29], [-12], [-28], [-13], [35], [42], [-21], [26], [15], [-11], [16], [-4], [-50], [-16], [-56], [44], [4], [-38], [-18], [-67], [-58], [30], [35], [18], [0], [60], [-12], [28], [49], [14], [43], [-10], [-59], [-32], [-4], [-46], [-41], [-14], [40], [0], [16], [46], [-24], [22], [65], [-5], [3], [-26], [28], [7], [-50], [57], [-37], [24], [-65], [65], [-30], [69], [-18], [-48], [-49], [-2], [2], [-12], [53], [-15], [71], [51], [-14], [48], [-25], [-12], [20], [-17], [-78], [35], [42], [-58], [-33], [80], [-24], [27], [-26], [-12], [-51], [6], [38], [10], [54], [-34], [40], [13], [-76], [-18], [70], [6], [-4], [-84], [90], [-34], [19], [66], [67], [-48], [30], [-36], [79], [-41], [29], [-2], [-78], [-84], [-55], [-24], [-44], [-6], [-23], [-89], [-62], [28], [-60], [-86], [42], [-21], [-30], [2], [-64], [74], [-12], [-40], [38], [14], [-44], [20], [22], [67], [53], [45], [-52], [-6], [-4], [22], [-56], [-12], [20], [66], [-16], [26], [66], [32], [27], [56], [16], [-29], [40], [-34], [66], [25], [88], [14], [40], [12], [-56], [-34], [10], [-75], [-77], [12], [-19], [-22], [-26], [68], [-67], [33], [-14], [26], [-28], [-36], [-57], [45], [-48], [35], [-32], [83], [-23], [0], [-78], [14], [35], [72], [-56], [6], [49], [32], [74], [52], [-63], [80], [-4], [68], [44], [-3], [57], [72], [72], [-40], [90], [51], [62], [-64], [0], [-41], [-102], [-59], [-6], [108], [21]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4005_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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