
// Make newform 1020.2.a.d in Magma, downloaded from the LMFDB on 23 December 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_1020_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_1020_2_a_d();".
// 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_1020_2_a_d();".
// 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_1020_a();"
function MakeCharacter_1020_a()
    N := 1020;
    order := 1;
    char_gens := [511, 341, 817, 241];
    v := [1, 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_1020_a_Hecke( : Kf := Kf)
    return MakeCharacter_1020_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]];
    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], [3], [1], [-2], [1], [1], [6], [7], [-10], [-3], [-9], [8], [3], [5], [8], [10], [16], [12], [-9], [2], [12], [0], [-2], [-10], [-8], [4], [-10], [10], [-6], [-8], [-17], [2], [-6], [-19], [-12], [9], [6], [8], [-18], [-14], [0], [6], [0], [14], [-24], [-6], [18], [-9], [26], [-2], [-18], [-24], [2], [-27], [-3], [8], [26], [24], [7], [9], [-16], [-11], [-1], [-18], [5], [-21], [-8], [31], [7], [0], [8], [-36], [28], [1], [4], [-15], [-15], [25], [-33], [21], [3], [-18], [10], [-36], [-30], [-28], [0], [18], [33], [-16], [-16], [-2], [28], [18], [18], [-39], [-34], [-2], [-29], [14], [-41], [-24], [34], [-24], [-27], [15], [26], [-10], [35], [4], [26], [10], [13], [33], [19], [0], [26], [-50], [11], [-10], [-6], [16], [-12], [-12], [34], [27], [14], [14], [-28], [-6], [42], [44], [-18], [35], [-13], [15], [-5], [6], [-40], [30], [-23], [-32], [-39], [-41], [-46], [0], [31], [-27], [5], [-45], [-20], [36], [39], [-31], [5], [51], [-2], [-2], [28], [-41], [48], [-8], [-46], [6], [-50], [-37], [0], [50], [20], [-18], [-24], [6], [24], [-14], [-38], [6], [-58], [50], [-18], [29], [-41], [-29], [-28], [32], [65], [-24], [40], [9], [-43], [46], [15], [46], [32], [12], [-22], [-31], [28], [24], [19], [-26], [-10], [43], [-20], [21], [-64], [-20], [19], [-37], [-43], [60], [-1], [-33], [25], [28], [22], [-10], [38], [-34], [50], [22], [-12], [72], [43], [-43], [36], [40], [62], [-11], [34], [22], [57], [-48], [0], [-24], [-34], [26], [9], [64], [-12], [9], [10], [-14], [76], [37], [7], [-45], [25], [23], [-26], [65], [3], [-40], [-57], [-6], [32], [33], [36], [-74], [-48], [9], [2], [-49], [-70], [-16], [-60], [38], [-60], [-37], [-32], [82], [38], [-73], [-18], [26], [59], [71], [-65], [-10], [24], [68], [2], [-50], [56], [-58], [19], [66], [-41], [-84], [-59], [-76], [63], [-23], [-11], [-1], [-19], [59], [31], [-8], [20], [39], [52], [70], [-60], [-67], [27], [-58], [62], [12], [22], [87], [-30], [-76], [-84], [53], [44], [-1], [-14], [60], [14], [-46], [-62], [20], [-48], [17], [-80], [59], [30], [-82], [60], [72], [-42], [-43], [24], [2], [9], [-54], [-72], [-90], [82], [-54], [-9], [44], [-66], [75], [-34], [40], [-41], [12], [-28], [-25], [-80], [63], [-54], [21], [80], [81], [-48], [51], [18], [-62], [-75], [8], [10], [46], [-48], [-56], [-60], [12], [40], [-12], [-54], [-80], [38], [-92], [45], [34], [-32], [-14], [62], [-1], [35], [98], [44], [84], [25], [-28], [44], [73], [4], [86], [-40], [30], [86], [90], [17], [-30], [54], [-102], [-48], [83], [-42], [76], [20], [-74], [10], [-42], [-28], [20], [1], [-35], [-55], [18], [68], [-45], [24], [97], [28], [8], [-52], [21], [-12], [76], [35], [38], [96], [81], [28], [60]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1020_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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