
// Make newform 1617.2.a.a in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1617_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_1617_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_1617_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_1617_a();"
function MakeCharacter_1617_a()
    N := 1617;
    order := 1;
    char_gens := [1079, 199, 442];
    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_1617_a_Hecke(Kf)
    return MakeCharacter_1617_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 := [[-2], [-1], [0], [0], [-1], [-5], [6], [7], [-4], [-2], [-7], [7], [4], [-9], [6], [-2], [12], [-2], [7], [8], [-5], [-11], [-4], [6], [2], [-2], [-19], [-20], [-1], [-6], [-13], [-4], [-10], [11], [-6], [-16], [-14], [-8], [-6], [-10], [-16], [5], [22], [-7], [-4], [4], [-4], [0], [14], [5], [-20], [-24], [-10], [-18], [-8], [-18], [-14], [-20], [-1], [24], [23], [12], [-3], [-16], [3], [6], [-33], [-17], [2], [6], [30], [36], [-17], [21], [-5], [-38], [22], [27], [36], [11], [-8], [9], [10], [19], [16], [22], [0], [3], [-36], [-29], [8], [-38], [-29], [-30], [13], [-44], [6], [22], [-31], [15], [32], [-36], [18], [10], [35], [-17], [24], [-26], [10], [-15], [13], [-34], [-24], [-21], [-16], [24], [21], [6], [0], [18], [11], [1], [12], [-40], [-5], [22], [30], [-6], [-5], [-13], [-33], [-36], [-31], [30], [-30], [-3], [6], [4], [-4], [6], [44], [52], [16], [22], [-31], [30], [17], [-32], [0], [-44], [18], [50], [-9], [22], [39], [0], [-31], [0], [-47], [8], [-54], [14], [17], [-8], [52], [34], [29], [-11], [-45], [28], [30], [34], [0], [51], [-7], [-24], [-27], [16], [24], [-19], [-43], [-28], [1], [24], [12], [56], [-22], [-5], [-29], [-14], [21], [34], [12], [60], [-48], [-16], [-43], [-50], [-46], [60], [54], [37], [-26], [-66], [-22], [-68], [16], [6], [-2], [20], [-7], [10], [4], [-14], [46], [55], [-8], [4], [-4], [34], [19], [-49], [24], [-8], [54], [30], [-18], [34], [21], [-2], [9], [28], [1], [-54], [-27], [28], [-35], [42], [-18], [30], [-18], [-5], [-39], [50], [42], [0], [43], [-6], [-32], [-34], [43], [-60], [58], [-43], [30], [0], [9], [61], [38], [34], [-31], [2], [-41], [-79], [70], [-75], [42], [6], [-36], [-16], [-41], [-24], [33], [-39], [-82], [-16], [-38], [-35], [49], [-52], [-6], [15], [12], [33], [32], [-52], [-39], [26], [-71], [0], [-78], [82], [44], [-36], [29], [-50], [-43], [-42], [-58], [-83], [-14], [-72], [40], [62], [-59], [85], [14], [21], [-60], [5], [66], [42], [-34], [36], [16], [55], [42], [30], [61], [32], [-29], [75], [0], [-5], [30], [57], [11], [85], [24], [-62], [-37], [-2], [35], [-76], [-47], [-18], [66], [58], [-43], [-1], [19], [-4], [0], [-95], [14], [-40], [-39], [-48], [38], [38], [56], [10], [-40], [48], [-71], [12], [12], [-60], [42], [-68], [22], [66], [82], [80], [-7], [46], [50], [-53], [22], [-12], [97], [-76], [-6], [-59], [-59], [74], [-8], [94], [-24], [27], [18], [-90], [68], [-102], [-28], [-58], [43], [-74], [-47], [-12], [-59], [-50], [44], [43], [46], [46], [-36], [6], [-77], [28], [-34], [-6], [-56], [68], [36], [51], [86], [-80], [21], [54], [-50], [68], [46], [-60], [-41], [-38], [68], [73], [64], [6], [-20], [-23], [-30], [42], [10], [-6], [70], [-84], [68], [-44]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1617_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1617_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_1617_2_a_a(:prec:=1)
    chi := MakeCharacter_1617_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_1617_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_1617_2_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1617_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![2, 1]>,<5,R![0, 1]>,<13,R![5, 1]>,<17,R![-6, 1]>],Snew);
    return Vf;
end function;