
// Make newform 1014.2.a.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1014_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_1014_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_1014_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_1014_a();"
function MakeCharacter_1014_a()
    N := 1014;
    order := 1;
    char_gens := [677, 847];
    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_1014_a_Hecke(Kf)
    return MakeCharacter_1014_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], [-1], [-2], [2], [0], [5], [-2], [6], [-9], [-4], [-11], [5], [10], [2], [-1], [-8], [-11], [2], [-14], [-13], [-4], [6], [2], [-2], [-5], [10], [-18], [-2], [-3], [-12], [-8], [17], [-12], [3], [-6], [-7], [20], [24], [-22], [-6], [5], [4], [-17], [6], [10], [24], [-16], [14], [10], [-6], [-6], [7], [4], [-3], [14], [-14], [8], [-11], [25], [26], [-1], [-14], [6], [6], [-33], [-28], [-9], [6], [6], [17], [-30], [-2], [9], [12], [24], [19], [-18], [-27], [23], [-32], [-23], [-2], [-21], [10], [20], [-30], [3], [3], [-14], [-22], [32], [-26], [-30], [0], [-14], [15], [25], [-38], [-7], [2], [-9], [40], [6], [-2], [27], [-32], [-39], [0], [11], [32], [13], [-15], [32], [12], [5], [8], [0], [-22], [24], [25], [43], [-46], [-40], [-2], [34], [-15], [24], [2], [13], [16], [-12], [26], [-18], [34], [-34], [18], [4], [-2], [5], [-36], [-30], [-16], [-8], [-35], [44], [49], [45], [-50], [46], [37], [17], [-8], [24], [-44], [-32], [-16], [-23], [-1], [-22], [8], [54], [-50], [20], [21], [60], [18], [-23], [-2], [11], [18], [-3], [-44], [-21], [16], [45], [-22], [-25], [24], [-26], [10], [32], [41], [54], [-18], [-17], [-2], [4], [19], [-42], [-29], [54], [-10], [14], [-2], [66], [3], [-39], [-3], [-4], [-9], [0], [-23], [-61], [-38], [-49], [26], [20], [9], [32], [-5], [-6], [-50], [-18], [32], [-57], [24], [45], [-68], [26], [-63], [-44], [-11], [20], [30], [37], [73], [12], [52], [12], [9], [36], [20], [21], [40], [60], [-50], [27], [-8], [-42], [74], [-2], [-54], [-35], [17], [-36], [-60], [-12], [54], [42], [62], [62], [-32], [-13], [30], [-66], [-42], [-10], [-6], [-1], [-20], [60], [37], [-43], [-15], [-78], [-66], [9], [-34], [63], [61], [-36], [-41], [-60], [22], [66], [12], [1], [47], [-42], [18], [-16], [-44], [-23], [-58], [-6], [51], [-17], [2], [-39], [34], [22], [-71], [0], [14], [63], [-32], [39], [36], [-10], [-49], [-33], [-58], [36], [26], [-73], [-6], [5], [-12], [29], [-46], [50], [9], [38], [54], [43], [18], [42], [62], [13], [82], [42], [-33], [-10], [18], [43], [22], [-10], [-58], [-21], [-3], [-53], [58], [-62], [-50], [-10], [5], [26], [-89], [-44], [-26], [-15], [27], [26], [66], [-22], [-19], [16], [-2], [50], [-6], [22], [3], [2], [21], [-51], [-18], [60], [-27], [-54], [13], [5], [-48], [-68], [40], [-74], [18], [32], [43], [-44], [42], [-26], [7], [14], [53], [-30], [6], [-14], [33], [7], [-30], [-35], [70], [34], [60], [58], [-102], [-90], [-96], [-92], [-29], [-33], [-14], [-74], [64], [14], [26], [38], [44], [11], [18], [10], [0], [-23], [-57], [90], [18], [-15], [-34], [-80], [-69], [-81], [-34], [-12], [51], [-77], [-44], [-54], [-50], [12], [-65], [41], [-22], [-10], [-90], [11], [20], [-63], [-10], [-84]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1014_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1014_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_1014_2_a_a(:prec:=1)
    chi := MakeCharacter_1014_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_1014_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_1014_2_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1014_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![1, 1]>,<7,R![2, 1]>],Snew);
    return Vf;
end function;