
// Make newform 1620.2.a.d in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1620_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_1620_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_1620_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_1620_a();"
function MakeCharacter_1620_a()
    N := 1620;
    order := 1;
    char_gens := [811, 1541, 1297];
    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_1620_a_Hecke(Kf)
    return MakeCharacter_1620_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], [0], [1], [-4], [3], [-4], [0], [5], [-6], [-9], [5], [2], [-9], [-10], [-6], [-12], [9], [-10], [2], [3], [-4], [-4], [6], [-9], [2], [-15], [-10], [6], [11], [12], [20], [-15], [12], [-13], [-18], [17], [14], [-4], [0], [-6], [9], [-13], [3], [14], [-12], [-16], [5], [8], [-24], [14], [-30], [-24], [29], [12], [-6], [6], [21], [32], [26], [-18], [-22], [30], [2], [-3], [20], [18], [5], [8], [24], [17], [-6], [15], [14], [-28], [-4], [36], [-18], [2], [-18], [-10], [0], [17], [33], [-34], [17], [6], [-39], [-10], [-3], [-34], [0], [3], [-16], [-3], [41], [-36], [18], [18], [-16], [-1], [20], [-24], [-18], [-27], [11], [20], [18], [42], [-3], [-19], [-22], [-4], [-48], [-28], [5], [3], [14], [-30], [48], [-12], [-19], [-22], [6], [-24], [-28], [-9], [38], [-15], [44], [-22], [11], [-48], [-40], [-52], [-3], [5], [6], [32], [-12], [-9], [29], [15], [14], [12], [-1], [15], [-4], [0], [-31], [-24], [-16], [-33], [-40], [-42], [-22], [-51], [-25], [-15], [26], [6], [-18], [-24], [44], [3], [-12], [-12], [-43], [-28], [-7], [48], [39], [-13], [51], [14], [-40], [3], [-28], [-27], [20], [-1], [2], [0], [32], [-30], [-30], [27], [-10], [20], [-37], [36], [50], [-18], [-28], [39], [-18], [-36], [-7], [14], [48], [36], [-21], [-13], [-16], [65], [36], [36], [-31], [24], [-18], [44], [-58], [39], [68], [72], [-21], [-7], [50], [-27], [-42], [-30], [-55], [-49], [39], [-16], [0], [35], [42], [-24], [2], [-12], [-16], [-61], [-40], [30], [-28], [-54], [-25], [60], [12], [9], [24], [8], [50], [62], [12], [-27], [-22], [12], [41], [-30], [-22], [45], [18], [-13], [66], [0], [-25], [-46], [24], [20], [2], [-30], [17], [44], [18], [-37], [-6], [33], [32], [-42], [-25], [-16], [-28], [11], [14], [74], [-6], [-19], [2], [84], [-66], [32], [-18], [-58], [20], [-45], [2], [-42], [65], [30], [-45], [-72], [24], [39], [8], [-39], [-7], [-36], [-15], [-22], [-34], [-72], [47], [-42], [77], [-88], [-12], [41], [-51], [8], [30], [-69], [-3], [-10], [12], [-79], [39], [-45], [62], [-15], [-52], [68], [63], [20], [-24], [-79], [65], [-46], [-72], [-6], [-67], [30], [-61], [-60], [47], [-30], [47], [-6], [5], [56], [2], [54], [9], [-76], [-12], [60], [-7], [26], [-75], [-24], [47], [44], [-54], [26], [-7], [-30], [9], [-3], [-18], [-24], [-16], [-51], [36], [33], [98], [-4], [48], [-46], [-79], [51], [44], [18], [69], [-7], [68], [-33], [-33], [-34], [-15], [44], [-42], [54], [-70], [-30], [11], [12], [-64], [86], [-28], [78], [-10], [-60], [-84], [-34], [-27], [50], [-31], [30], [68], [3], [-49], [-48], [14], [-72], [9], [56], [-16], [-78], [-70], [45], [-28], [-42], [12], [-13], [92], [30], [-9], [38], [36], [-18], [-90], [-34], [36], [-21], [2], [30], [-6], [6], [11], [0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1620_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1620_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_1620_2_a_d(:prec:=1)
    chi := MakeCharacter_1620_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_1620_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_1620_2_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1620_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![4, 1]>,<11,R![-3, 1]>,<17,R![0, 1]>],Snew);
    return Vf;
end function;