
// Make newform 1610.2.a.d in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1610_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_1610_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_1610_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_1610_a();"
function MakeCharacter_1610_a()
    N := 1610;
    order := 1;
    char_gens := [967, 1151, 281];
    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_1610_a_Hecke(Kf)
    return MakeCharacter_1610_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], [-2], [1], [1], [-2], [-4], [-2], [-4], [1], [-2], [-4], [-8], [-2], [10], [-12], [-4], [10], [-14], [10], [-16], [6], [-4], [12], [-6], [-14], [-12], [-16], [2], [16], [-10], [0], [-6], [10], [-10], [8], [0], [-22], [20], [12], [16], [16], [22], [-12], [-2], [10], [-8], [4], [28], [24], [-10], [18], [-8], [10], [-8], [6], [12], [-28], [20], [-14], [10], [-8], [-2], [2], [-16], [-22], [2], [-8], [14], [20], [28], [14], [24], [-8], [4], [18], [0], [-24], [-28], [30], [-2], [40], [-40], [-16], [22], [-12], [-8], [-34], [-22], [-12], [-8], [-12], [-24], [24], [0], [-40], [-24], [36], [-10], [-36], [-14], [8], [-16], [44], [-2], [-30], [-18], [10], [-18], [-32], [26], [-28], [-12], [6], [32], [8], [-2], [-20], [24], [-6], [34], [-6], [-2], [18], [-44], [18], [0], [12], [36], [48], [14], [-36], [-8], [36], [20], [-2], [-50], [50], [-8], [22], [22], [-22], [-2], [-48], [-30], [0], [40], [-36], [10], [10], [40], [-10], [22], [-52], [-52], [22], [-48], [-20], [-18], [-22], [6], [-12], [-6], [-8], [0], [34], [40], [-24], [28], [-26], [28], [22], [12], [-48], [-2], [24], [26], [-48], [34], [-64], [14], [-8], [-40], [-34], [-34], [-36], [-18], [-42], [8], [-26], [-32], [14], [-36], [10], [-24], [42], [54], [58], [-64], [-30], [-36], [-60], [-56], [-44], [-30], [48], [-36], [48], [-8], [42], [26], [2], [-36], [-8], [-32], [20], [34], [-24], [-42], [4], [18], [42], [16], [42], [-24], [-22], [-6], [2], [-48], [32], [12], [70], [-20], [-8], [-58], [-64], [-16], [26], [24], [20], [36], [34], [-50], [76], [-50], [46], [-28], [-40], [-28], [-18], [76], [32], [14], [4], [-18], [24], [40], [-8], [-72], [-28], [-70], [-40], [22], [24], [50], [18], [52], [-60], [54], [14], [-66], [36], [2], [-10], [44], [14], [8], [32], [46], [-30], [0], [-12], [16], [48], [-70], [-78], [24], [-30], [-48], [-80], [-14], [-24], [-28], [-54], [56], [-22], [18], [8], [44], [10], [10], [14], [76], [20], [72], [2], [-62], [-12], [26], [36], [-50], [4], [44], [-58], [16], [48], [-22], [-22], [72], [-2], [78], [20], [-86], [-4], [-72], [-6], [14], [-2], [-58], [-80], [36], [-54], [58], [-48], [-62], [10], [42], [-20], [-54], [-46], [56], [38], [-14], [48], [20], [-28], [-60], [64], [28], [0], [-58], [-22], [-26], [-70], [0], [52], [-74], [40], [20], [-78], [-24], [8], [-70], [-32], [18], [70], [74], [12], [-44], [86], [48], [-30], [-64], [-20], [-8], [6], [-46], [-64], [14], [18], [50], [-50], [-14], [-8], [-66], [96], [52], [32], [60], [-14], [72], [82], [36], [-52], [26], [-56], [66], [-16], [26], [34], [34], [-20], [-66], [16], [-54], [96], [-100], [-32], [90], [-36], [-30], [102], [-6], [6], [46], [78], [36], [8], [-48], [18], [72], [24], [62], [32], [-42], [-86], [-32], [10], [-54], [-34], [0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1610_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1610_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_1610_2_a_d(:prec:=1)
    chi := MakeCharacter_1610_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_1610_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_1610_2_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1610_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![2, 1]>,<11,R![2, 1]>,<13,R![4, 1]>],Snew);
    return Vf;
end function;