
// Make newform 1005.2.a.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1005_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_1005_2_a_a();".
// 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_1005_2_a_a();".
// 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_1005_a();"
function MakeCharacter_1005_a()
    N := 1005;
    order := 1;
    char_gens := [671, 202, 136];
    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_1005_a_Hecke(Kf)
    return MakeCharacter_1005_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], [1], [-1], [2], [-6], [2], [-3], [-1], [-9], [3], [-4], [-7], [6], [-10], [9], [-12], [-15], [8], [1], [-12], [11], [-4], [-12], [15], [2], [0], [-4], [15], [-10], [-12], [17], [12], [18], [20], [-3], [-1], [-1], [5], [0], [21], [0], [-25], [-12], [-13], [-12], [11], [-4], [-7], [-3], [26], [-18], [12], [-7], [-12], [-15], [12], [-18], [20], [26], [0], [11], [30], [-7], [18], [14], [-6], [-22], [26], [0], [2], [18], [-33], [-10], [-28], [-16], [12], [-18], [-13], [-18], [-34], [3], [11], [-33], [-4], [-1], [6], [3], [11], [21], [-22], [-12], [15], [-16], [-33], [-40], [12], [9], [-30], [-25], [38], [-28], [-18], [-6], [-27], [5], [2], [12], [-6], [0], [35], [-16], [-7], [33], [-28], [-10], [-6], [-4], [12], [-6], [3], [14], [20], [36], [-18], [35], [-30], [14], [-15], [-10], [-22], [32], [48], [-13], [-52], [27], [32], [51], [-22], [-18], [-12], [2], [3], [5], [-33], [-31], [-39], [-37], [18], [-52], [39], [23], [-57], [-10], [15], [-1], [21], [20], [-6], [-10], [12], [3], [3], [32], [-27], [-15], [36], [2], [26], [11], [-36], [-33], [41], [63], [-34], [2], [42], [-64], [27], [8], [35], [-52], [-12], [-31], [6], [-6], [-42], [38], [2], [44], [-36], [50], [57], [-28], [66], [36], [-6], [5], [38], [36], [36], [-42], [-7], [14], [-10], [36], [3], [44], [21], [57], [-4], [-34], [-30], [8], [-12], [54], [-64], [-1], [-42], [-24], [-21], [14], [-31], [-60], [5], [0], [-37], [-18], [12], [11], [0], [2], [2], [-37], [-30], [5], [60], [5], [6], [39], [12], [-45], [2], [-58], [-52], [36], [-48], [-31], [12], [-34], [48], [41], [-54], [18], [-19], [-36], [30], [2], [-40], [18], [41], [71], [0], [-4], [-64], [-6], [-13], [-30], [-12], [-70], [0], [8], [32], [32], [-40], [47], [-16], [6], [-46], [-10], [-30], [-81], [20], [24], [-28], [-22], [-48], [-61], [-27], [-46], [72], [-9], [-36], [18], [0], [8], [42], [-40], [48], [-57], [-16], [-40], [33], [8], [-24], [-43], [80], [-3], [-49], [-69], [-10], [36], [21], [9], [-25], [15], [-4], [75], [-24], [11], [-12], [-73], [-43], [-15], [8], [21], [62], [-61], [-28], [-18], [-6], [-55], [-27], [20], [-60], [-16], [-87], [-70], [-3], [44], [83], [-10], [78], [12], [95], [27], [6], [56], [68], [36], [-48], [-52], [38], [-45], [-28], [32], [0], [-57], [-12], [-42], [24], [26], [6], [3], [-12], [-67], [56], [30], [47], [-52], [42], [80], [3], [-30], [-40], [44], [-75], [66], [-34], [24], [-73], [6], [-18], [56], [-54], [74], [54], [-28], [-19], [92], [0], [-49], [72], [-36], [-82], [66], [-31], [32], [-51], [-22], [-18], [14], [-18], [8], [-18], [-60], [-16], [-10], [51], [-49], [12], [5], [-57], [-15], [-1], [38], [51], [-75], [-37], [-69], [21], [-24], [-43], [96], [-42], [86], [87], [39], [-51], [92], [24]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1005_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1005_2_a_a();".
// 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_1005_2_a_a(:prec:=1)
    chi := MakeCharacter_1005_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_1005_2_a_a();".
// 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_1005_2_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1005_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![0, 1]>],Snew);
    return Vf;
end function;