
// Make newform 2006.2.a.h in Magma, downloaded from the LMFDB on 29 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2006_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_2006_2_a_h();".
// 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_2006_2_a_h();".
// 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_2006_a();"
function MakeCharacter_2006_a()
    N := 2006;
    order := 1;
    char_gens := [1771, 1123];
    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_2006_a_Hecke(Kf)
    return MakeCharacter_2006_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], [-1], [-3], [1], [4], [-2], [1], [1], [4], [-5], [-6], [-8], [-11], [2], [-8], [-9], [-1], [0], [-4], [8], [-10], [7], [12], [-10], [-10], [12], [18], [-17], [2], [-20], [9], [-14], [-1], [-16], [16], [24], [-20], [4], [-23], [22], [0], [-21], [4], [-11], [26], [5], [-10], [-12], [18], [-22], [8], [-1], [-5], [27], [5], [-9], [10], [9], [13], [-1], [0], [17], [-1], [-3], [-28], [18], [17], [14], [-8], [-30], [0], [-15], [-36], [22], [-11], [28], [10], [32], [22], [-6], [-22], [-30], [-8], [-25], [-40], [34], [-5], [20], [34], [-4], [38], [24], [9], [15], [-25], [28], [-24], [-6], [31], [20], [4], [-25], [18], [2], [-24], [-11], [-30], [29], [21], [-8], [-19], [-2], [-37], [25], [-20], [-18], [31], [-9], [13], [-36], [-49], [34], [-38], [-26], [28], [42], [39], [14], [32], [-14], [6], [0], [34], [-35], [17], [24], [18], [4], [-14], [-30], [-40], [-46], [4], [36], [37], [-50], [35], [-12], [-32], [-46], [-13], [16], [15], [18], [27], [9], [-44], [8], [-34], [-42], [23], [-22], [18], [3], [-60], [18], [20], [-13], [20], [-36], [0], [8], [45], [58], [33], [-57], [45], [54], [-32], [7], [53], [39], [32], [19], [36], [-50], [-26], [18], [-10], [18], [-36], [10], [0], [18], [-33], [-10], [-6], [-8], [24], [22], [-10], [-65], [38], [-22], [-5], [0], [-14], [6], [-34], [0], [-52], [-33], [-37], [-5], [1], [-32], [-8], [-34], [-62], [-2], [10], [24], [-36], [21], [56], [20], [-11], [0], [46], [-37], [0], [-36], [-36], [-12], [-50], [3], [-34], [-38], [0], [0], [-25], [28], [19], [-39], [47], [0], [28], [-60], [71], [-59], [13], [28], [-30], [7], [41], [48], [43], [66], [72], [-26], [-30], [57], [57], [-62], [-9], [-8], [-15], [24], [35], [-39], [-56], [-68], [-44], [-45], [67], [82], [32], [27], [-72], [-37], [40], [26], [78], [8], [76], [16], [30], [57], [-70], [-9], [-34], [-52], [66], [72], [-53], [-14], [12], [49], [36], [26], [9], [4], [-10], [-8], [15], [-46], [31], [-22], [36], [-6], [15], [-27], [51], [-58], [-1], [56], [-66], [77], [50], [18], [-20], [42], [-73], [9], [-61], [10], [20], [56], [-12], [74], [-28], [6], [16], [-57], [-32], [65], [-67], [14], [-58], [-23], [-46], [2], [-66], [-24], [-2], [36], [-58], [15], [-96], [-12], [10], [-13], [-39], [22], [14], [30], [-72], [39], [3], [-1], [-74], [-39], [-27], [-18], [-24], [-58], [42], [-61], [92], [-68], [-22], [-62], [6], [92], [85], [-30], [8], [50], [62], [33], [95], [-50], [44], [-28], [-35], [-66], [11], [-82], [-68], [-46], [34], [-6], [24], [86], [-92], [-74], [-61], [-73], [61], [25], [-62], [-22], [49], [-34], [-27], [20], [0], [-13], [-94], [32], [-95], [-6], [8], [17], [71], [3], [0], [8], [36], [81], [6], [-38], [-64], [37], [-47], [-47], [-92], [102], [-57], [-48]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2006_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2006_2_a_h();".
// 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_2006_2_a_h(:prec:=1)
    chi := MakeCharacter_2006_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_2006_2_a_h();".
// 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_2006_2_a_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2006_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 1]>,<5,R![3, 1]>,<11,R![-4, 1]>,<31,R![6, 1]>],Snew);
    return Vf;
end function;